Skip to content

Commit 12bb183

Browse files
committed
Add support for byte and smallint types
1 parent 6f6ef9d commit 12bb183

5 files changed

Lines changed: 45 additions & 4 deletions

File tree

cassandra/cqltypes.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
import warnings
4545

4646

47-
from cassandra.marshal import (int8_pack, int8_unpack,
47+
from cassandra.marshal import (int8_pack, int8_unpack, int16_pack, int16_unpack,
4848
uint16_pack, uint16_unpack, uint32_pack, uint32_unpack,
4949
int32_pack, int32_unpack, int64_pack, int64_unpack,
5050
float_pack, float_unpack, double_pack, double_unpack,
@@ -424,6 +424,17 @@ def deserialize(byts, protocol_version):
424424
def serialize(truth, protocol_version):
425425
return int8_pack(truth)
426426

427+
class TinyIntType(_CassandraType):
428+
typename = 'tinyint'
429+
430+
@staticmethod
431+
def deserialize(byts, protocol_version):
432+
return int8_unpack(byts)
433+
434+
@staticmethod
435+
def serialize(byts, protocol_version):
436+
return int8_pack(byts)
437+
427438

428439
if six.PY2:
429440
class AsciiType(_CassandraType):
@@ -650,6 +661,18 @@ def deserialize(byts, protocol_version):
650661
return util.Date(days)
651662

652663

664+
class SmallIntType(_CassandraType):
665+
typename = 'smallint'
666+
667+
@staticmethod
668+
def deserialize(byts, protocol_version):
669+
return int16_unpack(byts)
670+
671+
@staticmethod
672+
def serialize(byts, protocol_version):
673+
return int16_pack(byts)
674+
675+
653676
class TimeType(_CassandraType):
654677
typename = 'time'
655678

cassandra/protocol.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
LongType, MapType, SetType, TimeUUIDType,
3535
UTF8Type, UUIDType, UserType,
3636
TupleType, lookup_casstype, SimpleDateType,
37-
TimeType)
37+
TimeType, TinyIntType, SmallIntType)
3838
from cassandra.policies import WriteType
3939

4040
log = logging.getLogger(__name__)
@@ -533,6 +533,8 @@ class ResultMessage(_MessageType):
533533
0x0010: InetAddressType,
534534
0x0011: SimpleDateType,
535535
0x0012: TimeType,
536+
0x0013: SmallIntType,
537+
0x0014: TinyIntType,
536538
0x0020: ListType,
537539
0x0021: MapType,
538540
0x0022: SetType,

tests/integration/datatype_utils.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ def update_datatypes():
6060
if _cass_version >= (3, 0, 0):
6161
PRIMITIVE_DATATYPES.append('date')
6262
PRIMITIVE_DATATYPES.append('time')
63+
PRIMITIVE_DATATYPES.append('tinyint')
64+
PRIMITIVE_DATATYPES.append('smallint')
6365

6466

6567
def get_sample_data():
@@ -117,6 +119,12 @@ def get_sample_data():
117119
elif datatype == 'time':
118120
sample_data[datatype] = time(16, 47, 25, 7)
119121

122+
elif datatype == 'tinyint':
123+
sample_data[datatype] = 123
124+
125+
elif datatype == 'smallint':
126+
sample_data[datatype] = 32523
127+
120128
else:
121129
raise Exception("Missing handling of {0}".format(datatype))
122130

tests/unit/test_marshalling.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,11 @@
8181
(b'\x00\x01\x00\x10\xafYC\xa3\xea<\x11\xe1\xabc\xc4,\x03"y\xf0', 'ListType(TimeUUIDType)', [UUID(bytes=b'\xafYC\xa3\xea<\x11\xe1\xabc\xc4,\x03"y\xf0')]),
8282
(b'\x80\x00\x00\x01', 'SimpleDateType', Date(1)),
8383
(b'\x7f\xff\xff\xff', 'SimpleDateType', Date('1969-12-31')),
84-
(b'\x00\x00\x00\x00\x00\x00\x00\x01', 'TimeType', Time(1))
84+
(b'\x00\x00\x00\x00\x00\x00\x00\x01', 'TimeType', Time(1)),
85+
(b'\x7f', 'TinyIntType', 127),
86+
(b'\xff\xff\xff\x80', 'TinyIntType', -128),
87+
(b'\xff\xff\x80\x00', 'SmallIntType', 32767),
88+
(b'\xff\xff\x80\x00', 'SmallIntType', -32768)
8589
)
8690

8791
ordered_map_value = OrderedMapSerializedKey(UTF8Type, 2)

tests/unit/test_types.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
from cassandra.cqltypes import (BooleanType, lookup_casstype_simple, lookup_casstype,
2727
LongType, DecimalType, SetType, cql_typename,
2828
CassandraType, UTF8Type, parse_casstype_args,
29-
SimpleDateType, TimeType,
29+
SimpleDateType, TimeType, TinyIntType, SmallIntType,
3030
EmptyValue, _CassandraType, DateType, int64_pack)
3131
from cassandra.encoder import cql_quote
3232
from cassandra.protocol import (write_string, read_longstring, write_stringmap,
@@ -55,6 +55,8 @@ def test_lookup_casstype_simple(self):
5555
self.assertEqual(lookup_casstype_simple('UTF8Type'), cassandra.cqltypes.UTF8Type)
5656
self.assertEqual(lookup_casstype_simple('DateType'), cassandra.cqltypes.DateType)
5757
self.assertEqual(lookup_casstype_simple('SimpleDateType'), cassandra.cqltypes.SimpleDateType)
58+
self.assertEqual(lookup_casstype_simple('TinyIntType'), cassandra.cqltypes.TinyIntType)
59+
self.assertEqual(lookup_casstype_simple('SmallIntType'), cassandra.cqltypes.SmallIntType)
5860
self.assertEqual(lookup_casstype_simple('TimeUUIDType'), cassandra.cqltypes.TimeUUIDType)
5961
self.assertEqual(lookup_casstype_simple('TimeType'), cassandra.cqltypes.TimeType)
6062
self.assertEqual(lookup_casstype_simple('UUIDType'), cassandra.cqltypes.UUIDType)
@@ -87,6 +89,8 @@ def test_lookup_casstype(self):
8789
self.assertEqual(lookup_casstype('UTF8Type'), cassandra.cqltypes.UTF8Type)
8890
self.assertEqual(lookup_casstype('DateType'), cassandra.cqltypes.DateType)
8991
self.assertEqual(lookup_casstype('TimeType'), cassandra.cqltypes.TimeType)
92+
self.assertEqual(lookup_casstype('TinyIntType'), cassandra.cqltypes.TinyIntType)
93+
self.assertEqual(lookup_casstype('SmallIntType'), cassandra.cqltypes.SmallIntType)
9094
self.assertEqual(lookup_casstype('TimeUUIDType'), cassandra.cqltypes.TimeUUIDType)
9195
self.assertEqual(lookup_casstype('UUIDType'), cassandra.cqltypes.UUIDType)
9296
self.assertEqual(lookup_casstype('IntegerType'), cassandra.cqltypes.IntegerType)

0 commit comments

Comments
 (0)