From 05ccdc6411b9d3388a74ba06d1ce153d33ffe1c3 Mon Sep 17 00:00:00 2001 From: Stefania Alborghetti Date: Thu, 19 Nov 2015 09:02:03 +0800 Subject: [PATCH] Improve COPY command to work with Counter columns patch by jasonstack; reviewed by Stefania for CASSANDRA-9043 --- CHANGES.txt | 1 + bin/cqlsh | 27 +++++++++++++++++++++------ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 9e2869ee2b..86e5cb28b7 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,4 +1,5 @@ 2.1.12 + * (cqlsh) Support counters in COPY commands (CASSANDRA-9043) * Try next replica if not possible to connect to primary replica on ColumnFamilyRecordReader (CASSANDRA-2388) * Limit window size in DTCS (CASSANDRA-10280) diff --git a/bin/cqlsh b/bin/cqlsh index 5459d675f3..56cd218f9e 100755 --- a/bin/cqlsh +++ b/bin/cqlsh @@ -1696,10 +1696,16 @@ class Shell(cmd.Cmd): pk_cols = [col.name for col in table_meta.primary_key] cqltypes = [table_meta.columns[name].typestring for name in columns] pk_indexes = [columns.index(col.name) for col in table_meta.primary_key] - query = 'INSERT INTO %s.%s (%s) VALUES (%%s)' % ( - protect_name(ks), - protect_name(cf), - ', '.join(protect_names(columns))) + is_counter_table = ("counter" in cqltypes) + if is_counter_table: + query = 'Update %s.%s SET %%s WHERE %%s' % ( + protect_name(ks), + protect_name(cf)) + else: + query = 'INSERT INTO %s.%s (%s) VALUES (%%s)' % ( + protect_name(ks), + protect_name(cf), + ', '.join(protect_names(columns))) # we need to handle some types specially should_escape = [t in ('ascii', 'text', 'timestamp', 'date', 'time', 'inet') for t in cqltypes] @@ -1764,8 +1770,17 @@ class Shell(cmd.Cmd): return else: row[i] = 'null' - - full_query = query % (','.join(row),) + if is_counter_table: + where_clause = [] + set_clause = [] + for i, value in enumerate(row): + if i in pk_indexes: + where_clause.append("%s=%s" % (columns[i], value)) + else: + set_clause.append("%s=%s+%s" % (columns[i], columns[i], value)) + full_query = query % (','.join(set_clause), ' AND '.join(where_clause)) + else: + full_query = query % (','.join(row),) query_message = QueryMessage( full_query, self.consistency_level, serial_consistency_level=None, fetch_size=None, paging_state=None, timestamp=insert_timestamp)