Skip to content

Commit 980fd2c

Browse files
msgpack: support datetime extended type
Tarantool supports datetime type since version 2.10.0 [1]. This patch introduced the support of Tarantool datetime type in msgpack decoders and encoders. The Tarantool datetime type is mapped to the pandas.Timestamp type. pandas.Timestamp was chosen because it could be used to store both nanoseconds and timezone information. In-build Python datetime.datetime supports microseconds at most, numpy.datetime64 do not support timezones. If you want to use numpy.datetime64 or datetime.datetime in your logic, you can use pandas converters in your code: - pandas.to_datetime(): numpy.datetime64 -> pandas.Timestamp - pandas.to_datetime(): datetime.datetime -> pandas.Timestamp - pandas.Timestamp.to_datetime64(): pandas.Timestamp -> numpy.datetime64 - pandas.Timestamp.to_pydatetime(): pandas.Timestamp -> datetime.datetime This patch does not yet introduce the support of timezones in datetime. 1. tarantool/tarantool#5941 Part of #204
1 parent 989af6c commit 980fd2c

File tree

7 files changed

+210
-5
lines changed

7 files changed

+210
-5
lines changed

Diff for: CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99
### Added
1010
- Decimal type support (#203).
1111
- UUID type support (#202).
12+
- Datetime type support (#204).
1213

1314
### Changed
1415
- Bump msgpack requirement to 1.0.4 (PR #223).

Diff for: requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
msgpack>=1.0.4
2+
pandas

Diff for: tarantool/msgpack_ext/datetime.py

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import time
2+
import math
3+
import pandas
4+
5+
# https://www.tarantool.io/ru/doc/latest/dev_guide/internals/msgpack_extensions/#the-datetime-type
6+
#
7+
# The datetime MessagePack representation looks like this:
8+
# +---------+----------------+==========+-----------------+
9+
# | MP_EXT | MP_DATETIME | seconds | nsec; tzoffset; |
10+
# | = d7/d8 | = 4 | | tzindex; |
11+
# +---------+----------------+==========+-----------------+
12+
# MessagePack data contains:
13+
#
14+
# * Seconds (8 bytes) as an unencoded 64-bit signed integer stored in the
15+
# little-endian order.
16+
# * The optional fields (8 bytes), if any of them have a non-zero value.
17+
# The fields include nsec (4 bytes), tzoffset (2 bytes), and
18+
# tzindex (2 bytes) packed in the little-endian order.
19+
#
20+
# seconds is seconds since Epoch, where the epoch is the point where the time
21+
# starts, and is platform dependent. For Unix, the epoch is January 1,
22+
# 1970, 00:00:00 (UTC). Tarantool uses a double type, see a structure
23+
# definition in src/lib/core/datetime.h and reasons in
24+
# https://github.com/tarantool/tarantool/wiki/Datetime-internals#intervals-in-c
25+
#
26+
# nsec is nanoseconds, fractional part of seconds. Tarantool uses int32_t, see
27+
# a definition in src/lib/core/datetime.h.
28+
#
29+
# tzoffset is timezone offset in minutes from UTC. Tarantool uses a int16_t type,
30+
# see a structure definition in src/lib/core/datetime.h.
31+
#
32+
# tzindex is Olson timezone id. Tarantool uses a int16_t type, see a structure
33+
# definition in src/lib/core/datetime.h. If both tzoffset and tzindex are
34+
# specified, tzindex has the preference and the tzoffset value is ignored.
35+
36+
EXT_ID = 4
37+
38+
SECONDS_SIZE_BYTES = 8
39+
NSEC_SIZE_BYTES = 4
40+
TZOFFSET_SIZE_BYTES = 2
41+
TZINDEX_SIZE_BYTES = 2
42+
43+
BYTEORDER = 'little'
44+
45+
NSEC_IN_SEC = 1000000000
46+
assert isinstance(NSEC_IN_SEC, int)
47+
48+
def get_int_as_bytes(data, size):
49+
return data.to_bytes(size, byteorder=BYTEORDER, signed=True)
50+
51+
def encode(obj):
52+
seconds = obj.value // NSEC_IN_SEC
53+
nsec = obj.value % NSEC_IN_SEC
54+
tzoffset = 0
55+
tzindex = 0
56+
57+
bytes_buffer = get_int_as_bytes(seconds, SECONDS_SIZE_BYTES)
58+
59+
if (nsec != 0) or (tzoffset != 0) or (tzindex != 0):
60+
bytes_buffer = bytes_buffer + get_int_as_bytes(nsec, NSEC_SIZE_BYTES)
61+
bytes_buffer = bytes_buffer + get_int_as_bytes(tzoffset, TZOFFSET_SIZE_BYTES)
62+
bytes_buffer = bytes_buffer + get_int_as_bytes(tzindex, TZINDEX_SIZE_BYTES)
63+
64+
return bytes_buffer
65+
66+
def get_bytes_as_int(data, cursor, size):
67+
part = data[cursor:cursor + size]
68+
return int.from_bytes(part, BYTEORDER, signed=True), cursor + size
69+
70+
def decode(data):
71+
cursor = 0
72+
seconds, cursor = get_bytes_as_int(data, cursor, SECONDS_SIZE_BYTES)
73+
74+
if len(data) > SECONDS_SIZE_BYTES:
75+
nsec, cursor = get_bytes_as_int(data, cursor, NSEC_SIZE_BYTES)
76+
tzoffset, cursor = get_bytes_as_int(data, cursor, TZOFFSET_SIZE_BYTES)
77+
tzindex, cursor = get_bytes_as_int(data, cursor, TZINDEX_SIZE_BYTES)
78+
else:
79+
nsec = 0
80+
tzoffset = 0
81+
tzindex = 0
82+
83+
if (tzoffset != 0) or (tzindex != 0):
84+
raise NotImplementedError
85+
86+
total_nsec = seconds * NSEC_IN_SEC + nsec
87+
88+
return pandas.to_datetime(total_nsec, unit='ns')

Diff for: tarantool/msgpack_ext/packer.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1+
import pandas
12
from decimal import Decimal
23
from uuid import UUID
34
from msgpack import ExtType
45

56
import tarantool.msgpack_ext.decimal as ext_decimal
67
import tarantool.msgpack_ext.uuid as ext_uuid
8+
import tarantool.msgpack_ext.datetime as ext_datetime
79

810
encoders = [
9-
{'type': Decimal, 'ext': ext_decimal},
10-
{'type': UUID, 'ext': ext_uuid },
11+
{'type': Decimal, 'ext': ext_decimal },
12+
{'type': UUID, 'ext': ext_uuid },
13+
{'type': pandas.Timestamp, 'ext': ext_datetime},
1114
]
1215

1316
def default(obj):

Diff for: tarantool/msgpack_ext/unpacker.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import tarantool.msgpack_ext.decimal as ext_decimal
22
import tarantool.msgpack_ext.uuid as ext_uuid
3+
import tarantool.msgpack_ext.datetime as ext_datetime
34

45
decoders = {
5-
ext_decimal.EXT_ID: ext_decimal.decode,
6-
ext_uuid.EXT_ID : ext_uuid.decode ,
6+
ext_decimal.EXT_ID : ext_decimal.decode ,
7+
ext_uuid.EXT_ID : ext_uuid.decode ,
8+
ext_datetime.EXT_ID: ext_datetime.decode,
79
}
810

911
def ext_hook(code, data):

Diff for: test/suites/lib/skip.py

+11
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,14 @@ def skip_or_run_UUID_test(func):
154154

155155
return skip_or_run_test_tarantool(func, '2.4.1',
156156
'does not support UUID type')
157+
158+
def skip_or_run_datetime_test(func):
159+
"""Decorator to skip or run datetime-related tests depending on
160+
the tarantool version.
161+
162+
Tarantool supports datetime type only since 2.10.0 version.
163+
See https://github.com/tarantool/tarantool/issues/5941
164+
"""
165+
166+
return skip_or_run_test_pcall_require(func, 'datetime',
167+
'does not support datetime type')

Diff for: test/suites/test_msgpack_ext.py

+100-1
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@
99
import msgpack
1010
import warnings
1111
import tarantool
12+
import pandas
1213

1314
from tarantool.msgpack_ext.packer import default as packer_default
1415
from tarantool.msgpack_ext.unpacker import ext_hook as unpacker_ext_hook
1516

1617
from .lib.tarantool_server import TarantoolServer
17-
from .lib.skip import skip_or_run_decimal_test, skip_or_run_UUID_test
18+
from .lib.skip import (
19+
skip_or_run_datetime_test, skip_or_run_decimal_test,
20+
skip_or_run_UUID_test,)
1821
from tarantool.error import MsgpackError, MsgpackWarning
1922

2023
class TestSuite_MsgpackExt(unittest.TestCase):
@@ -30,6 +33,7 @@ def setUpClass(self):
3033
self.adm(r"""
3134
_, decimal = pcall(require, 'decimal')
3235
_, uuid = pcall(require, 'uuid')
36+
_, datetime = pcall(require, 'datetime')
3337
3438
box.schema.space.create('test')
3539
box.space['test']:create_index('primary', {
@@ -505,6 +509,101 @@ def test_UUID_tarantool_encode(self):
505509

506510
self.assertSequenceEqual(self.con.eval(lua_eval), [True])
507511

512+
513+
datetime_cases = {
514+
'date': {
515+
'python': pandas.Timestamp(year=2022, month=8, day=31),
516+
'msgpack': (b'\x80\xa4\x0e\x63\x00\x00\x00\x00'),
517+
'tarantool': r"datetime.new({year=2022, month=8, day=31})",
518+
},
519+
'date_unix_start': {
520+
'python': pandas.Timestamp(year=1970, month=1, day=1),
521+
'msgpack': (b'\x00\x00\x00\x00\x00\x00\x00\x00'),
522+
'tarantool': r"datetime.new({year=1970, month=1, day=1})",
523+
},
524+
'date_before_1970': {
525+
'python': pandas.Timestamp(year=1900, month=1, day=1),
526+
'msgpack': (b'\x80\x81\x55\x7c\xff\xff\xff\xff'),
527+
'tarantool': r"datetime.new({year=1900, month=1, day=1})",
528+
},
529+
'datetime_with_minutes': {
530+
'python': pandas.Timestamp(year=2022, month=8, day=31, hour=18, minute=7),
531+
'msgpack': (b'\x44\xa3\x0f\x63\x00\x00\x00\x00'),
532+
'tarantool': r"datetime.new({year=2022, month=8, day=31, hour=18, min=7})",
533+
},
534+
'datetime_with_seconds': {
535+
'python': pandas.Timestamp(year=2022, month=8, day=31, hour=18, minute=7, second=54),
536+
'msgpack': (b'\x7a\xa3\x0f\x63\x00\x00\x00\x00'),
537+
'tarantool': r"datetime.new({year=2022, month=8, day=31, hour=18, min=7, sec=54})",
538+
},
539+
'datetime_with_microseconds': {
540+
'python': pandas.Timestamp(year=2022, month=8, day=31, hour=18, minute=7, second=54,
541+
microsecond=308543),
542+
'msgpack': (b'\x7a\xa3\x0f\x63\x00\x00\x00\x00\x18\xfe\x63\x12\x00\x00\x00\x00'),
543+
'tarantool': r"datetime.new({year=2022, month=8, day=31, hour=18, min=7, sec=54, " +
544+
r"nsec=308543000})",
545+
},
546+
'datetime_with_nanoseconds': {
547+
'python': pandas.Timestamp(year=2022, month=8, day=31, hour=18, minute=7, second=54,
548+
microsecond=308543, nanosecond=321),
549+
'msgpack': (b'\x7a\xa3\x0f\x63\x00\x00\x00\x00\x59\xff\x63\x12\x00\x00\x00\x00'),
550+
'tarantool': r"datetime.new({year=2022, month=8, day=31, hour=18, min=7, sec=54, " +
551+
r"nsec=308543321})",
552+
},
553+
}
554+
555+
def test_datetime_msgpack_decode(self):
556+
for name in self.datetime_cases.keys():
557+
with self.subTest(msg=name):
558+
datetime_case = self.datetime_cases[name]
559+
560+
self.assertEqual(unpacker_ext_hook(4, datetime_case['msgpack']),
561+
datetime_case['python'])
562+
563+
@skip_or_run_datetime_test
564+
def test_datetime_tarantool_decode(self):
565+
for name in self.datetime_cases.keys():
566+
with self.subTest(msg=name):
567+
datetime_case = self.datetime_cases[name]
568+
569+
self.adm(f"box.space['test']:replace{{'{name}', {datetime_case['tarantool']}}}")
570+
571+
self.assertSequenceEqual(self.con.select('test', name),
572+
[[name, datetime_case['python']]])
573+
574+
def test_datetime_msgpack_encode(self):
575+
for name in self.datetime_cases.keys():
576+
with self.subTest(msg=name):
577+
datetime_case = self.datetime_cases[name]
578+
579+
self.assertEqual(packer_default(datetime_case['python']),
580+
msgpack.ExtType(code=4, data=datetime_case['msgpack']))
581+
582+
@skip_or_run_datetime_test
583+
def test_datetime_tarantool_encode(self):
584+
for name in self.datetime_cases.keys():
585+
with self.subTest(msg=name):
586+
datetime_case = self.datetime_cases[name]
587+
588+
self.con.insert('test', [name, datetime_case['python']])
589+
590+
lua_eval = f"""
591+
local dt = {datetime_case['tarantool']}
592+
593+
local tuple = box.space['test']:get('{name}')
594+
assert(tuple ~= nil)
595+
596+
if tuple[2] == dt then
597+
return true
598+
else
599+
return nil, ('%s is not equal to expected %s'):format(
600+
tostring(tuple[2]), tostring(dt))
601+
end
602+
"""
603+
604+
self.assertSequenceEqual(self.adm(lua_eval), [True])
605+
606+
508607
@classmethod
509608
def tearDownClass(self):
510609
self.con.close()

0 commit comments

Comments
 (0)