Support empty string values in NumpyProtocolHandler

This commit is contained in:
Mark Florisson
2016-04-27 12:06:45 +01:00
parent 0f14135a09
commit 6d1af98a64
3 changed files with 19 additions and 3 deletions

View File

@@ -44,6 +44,8 @@ cdef class Deserializer:
cdef class DesBytesType(Deserializer):
cdef deserialize(self, Buffer *buf, int protocol_version):
if buf.size == 0:
return ""
return to_bytes(buf)
# this is to facilitate cqlsh integration, which requires bytearrays for BytesType
@@ -51,6 +53,8 @@ cdef class DesBytesType(Deserializer):
# deserializers.DesBytesType = deserializers.DesBytesTypeByteArray
cdef class DesBytesTypeByteArray(Deserializer):
cdef deserialize(self, Buffer *buf, int protocol_version):
if buf.size == 0:
return bytearray()
return bytearray(buf.ptr[:buf.size])
# TODO: Use libmpdec: http://www.bytereef.org/mpdecimal/index.html
@@ -84,6 +88,8 @@ cdef class DesByteType(Deserializer):
cdef class DesAsciiType(Deserializer):
cdef deserialize(self, Buffer *buf, int protocol_version):
if buf.size == 0:
return ""
if PY2:
return to_bytes(buf)
return to_bytes(buf).decode('ascii')
@@ -169,6 +175,8 @@ cdef class DesTimeType(Deserializer):
cdef class DesUTF8Type(Deserializer):
cdef deserialize(self, Buffer *buf, int protocol_version):
if buf.size == 0:
return ""
cdef val = to_bytes(buf)
return val.decode('utf8')

View File

@@ -52,12 +52,14 @@ ctypedef struct ArrDesc:
Py_uintptr_t buf_ptr
int stride # should be large enough as we allocate contiguous arrays
int is_object
int empty_binary_ok
arrDescDtype = np.dtype(
[ ('buf_ptr', np.uintp)
, ('stride', np.dtype('i'))
, ('is_object', np.dtype('i'))
])
, ('empty_binary_ok', np.dtype('i'))
], align=True)
_cqltype_to_numpy = {
cqltypes.LongType: np.dtype('>i8'),
@@ -117,6 +119,7 @@ def make_arrays(ParseDesc desc, array_size):
array_descs[i]['buf_ptr'] = arr.ctypes.data
array_descs[i]['stride'] = arr.strides[0]
array_descs[i]['is_object'] = coltype not in _cqltype_to_numpy
array_descs[i]['empty_binary_ok'] = coltype.empty_binary_ok
arrays.append(arr)
return array_descs, arrays
@@ -145,8 +148,9 @@ cdef inline int unpack_row(
get_buf(reader, &buf)
arr = arrays[i]
if buf.size == 0:
if buf.size < 0 or (buf.size == 0 and not arr.empty_binary_ok):
raise ValueError("Cannot handle NULL value")
if arr.is_object:
deserializer = desc.deserializers[i]
val = from_binary(deserializer, &buf, desc.protocol_version)

View File

@@ -45,7 +45,11 @@ def get_all_primitive_params(key):
"""
params = [key]
for datatype in PRIMITIVE_DATATYPES:
params.append(get_sample(datatype))
# Also test for empty strings
if key == 1 and datatype == 'ascii':
params.append('')
else:
params.append(get_sample(datatype))
return params