cqlsh COPY FROM fails for null values with non-prepared statements
patch by Stefania Alborghetti and Robert Stupp; reviewed by Robert Stupp for CASSANDRA-11631
diff --git a/CHANGES.txt b/CHANGES.txt
index ff26fde..5885a9a 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -1,4 +1,5 @@
2.1.15
+ * cqlsh COPY FROM fails for null values with non-prepared statements (CASSANDRA-11631)
* Make cython optional in pylib/setup.py (CASSANDRA-11630)
* Change order of directory searching for cassandra.in.sh to favor local one (CASSANDRA-11628)
* cqlsh COPY FROM fails with []{} chars in UDT/tuple fields/values (CASSANDRA-11633)
diff --git a/pylib/cqlshlib/copyutil.py b/pylib/cqlshlib/copyutil.py
index e12b72f..d68812c 100644
--- a/pylib/cqlshlib/copyutil.py
+++ b/pylib/cqlshlib/copyutil.py
@@ -1634,6 +1634,7 @@
self.thousands_sep = parent.thousands_sep
self.boolean_styles = parent.boolean_styles
self.time_format = parent.time_format
+ self.debug = parent.debug
self.table_meta = table_meta
self.primary_key_indexes = [self.columns.index(col.name) for col in self.table_meta.primary_key]
@@ -1682,7 +1683,16 @@
return CqlRuleSet.dequote_value(v)
def convert(t, v):
- return converters.get(t.typename, convert_unknown)(unprotect(v), ct=t)
+ v = unprotect(v)
+ if v == self.nullval:
+ return self.get_null_val()
+ return converters.get(t.typename, convert_unknown)(v, ct=t)
+
+ def convert_mandatory(t, v):
+ v = unprotect(v)
+ if v == self.nullval:
+ raise ParseError('Empty values are not allowed')
+ return converters.get(t.typename, convert_unknown)(v, ct=t)
def convert_blob(v, **_):
try:
@@ -1788,13 +1798,13 @@
return Time(v)
def convert_tuple(val, ct=cql_type):
- return tuple(convert(t, v) for t, v in zip(ct.subtypes, split(val)))
+ return tuple(convert_mandatory(t, v) for t, v in zip(ct.subtypes, split(val)))
def convert_list(val, ct=cql_type):
- return list(convert(ct.subtypes[0], v) for v in split(val))
+ return list(convert_mandatory(ct.subtypes[0], v) for v in split(val))
def convert_set(val, ct=cql_type):
- return frozenset(convert(ct.subtypes[0], v) for v in split(val))
+ return frozenset(convert_mandatory(ct.subtypes[0], v) for v in split(val))
def convert_map(val, ct=cql_type):
"""
@@ -1806,7 +1816,7 @@
class ImmutableDict(frozenset):
iteritems = frozenset.__iter__
- return ImmutableDict(frozenset((convert(ct.subtypes[0], v[0]), convert(ct.subtypes[1], v[1]))
+ return ImmutableDict(frozenset((convert_mandatory(ct.subtypes[0], v[0]), convert(ct.subtypes[1], v[1]))
for v in [split('{%s}' % vv, sep=':') for vv in split(val)]))
def convert_user_type(val, ct=cql_type):
@@ -1862,6 +1872,9 @@
return converters.get(cql_type.typename, convert_unknown)
+ def get_null_val(self):
+ return None if self.use_prepared_statements else "NULL"
+
def convert_row(self, row):
"""
Convert the row into a list of parsed values if using prepared statements, else simply apply the
@@ -1877,10 +1890,15 @@
if row[i] == self.nullval:
raise ParseError(self.get_null_primary_key_message(i))
- try:
- return [conv(val) if val != self.nullval else None for conv, val in zip(converters, row)]
- except Exception, e:
- raise ParseError(str(e))
+ def convert(c, v):
+ try:
+ return c(v) if v != self.nullval else self.get_null_val()
+ except Exception, e:
+ if self.debug:
+ traceback.print_exc()
+ raise ParseError("Failed to parse %s : %s" % (val, str(e)))
+
+ return [convert(conv, val) for conv, val in zip(converters, row)]
def get_null_primary_key_message(self, idx):
message = "Cannot insert null value for primary key column '%s'." % (self.columns[idx],)