Skip to content

Commit 11f79e6

Browse files
committed
cqltypes: Serialize None values in collections as NULLs
Fixes #201 When using parepared statements, None values in collections were serialized as empty values (values with length == 0). This is unexpected and inconsistent - None values are serialized as NULLs (vlaues with length == -1) in other cases: - Statement arguments, both for simple and prepared statements - Collection elements in simple statement This commit fixes this weird behavior - now None values should be serialized as NULLs in all cases.
1 parent 18ea6d4 commit 11f79e6

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

cassandra/cqltypes.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -832,9 +832,12 @@ def serialize_safe(cls, items, protocol_version):
832832
buf.write(pack(len(items)))
833833
inner_proto = max(3, protocol_version)
834834
for item in items:
835-
itembytes = subtype.to_binary(item, inner_proto)
836-
buf.write(pack(len(itembytes)))
837-
buf.write(itembytes)
835+
if item is None:
836+
buf.write(int32_pack(-1))
837+
else:
838+
itembytes = subtype.to_binary(item, inner_proto)
839+
buf.write(pack(len(itembytes)))
840+
buf.write(itembytes)
838841
return buf.getvalue()
839842

840843

@@ -904,10 +907,16 @@ def serialize_safe(cls, themap, protocol_version):
904907
for key, val in items:
905908
keybytes = key_type.to_binary(key, inner_proto)
906909
valbytes = value_type.to_binary(val, inner_proto)
907-
buf.write(pack(len(keybytes)))
908-
buf.write(keybytes)
909-
buf.write(pack(len(valbytes)))
910-
buf.write(valbytes)
910+
if keybytes is not None:
911+
buf.write(pack(len(keybytes)))
912+
buf.write(keybytes)
913+
else:
914+
buf.write(int32_pack(-1))
915+
if valbytes is not None:
916+
buf.write(pack(len(valbytes)))
917+
buf.write(valbytes)
918+
else:
919+
buf.write(int32_pack(-1))
911920
return buf.getvalue()
912921

913922

0 commit comments

Comments
 (0)