Skip to content

Commit

Permalink
cqltypes: Serialize None values in collections as NULLs
Browse files Browse the repository at this point in the history
Fixes scylladb#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.
  • Loading branch information
Lorak-mmk committed Jul 13, 2023
1 parent 18ea6d4 commit 11f79e6
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions cassandra/cqltypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -832,9 +832,12 @@ def serialize_safe(cls, items, protocol_version):
buf.write(pack(len(items)))
inner_proto = max(3, protocol_version)
for item in items:
itembytes = subtype.to_binary(item, inner_proto)
buf.write(pack(len(itembytes)))
buf.write(itembytes)
if item is None:
buf.write(int32_pack(-1))
else:
itembytes = subtype.to_binary(item, inner_proto)
buf.write(pack(len(itembytes)))
buf.write(itembytes)
return buf.getvalue()


Expand Down Expand Up @@ -904,10 +907,16 @@ def serialize_safe(cls, themap, protocol_version):
for key, val in items:
keybytes = key_type.to_binary(key, inner_proto)
valbytes = value_type.to_binary(val, inner_proto)
buf.write(pack(len(keybytes)))
buf.write(keybytes)
buf.write(pack(len(valbytes)))
buf.write(valbytes)
if keybytes is not None:
buf.write(pack(len(keybytes)))
buf.write(keybytes)
else:
buf.write(int32_pack(-1))
if valbytes is not None:
buf.write(pack(len(valbytes)))
buf.write(valbytes)
else:
buf.write(int32_pack(-1))
return buf.getvalue()


Expand Down

0 comments on commit 11f79e6

Please sign in to comment.