Skip to content

Commit 66e7b51

Browse files
committed
Transform dates from crate to python datetime
In execute(), transform the columns with type timestamp and timestamp without time zone to python datetime, this will correctly display dates in apache superset
1 parent f9bd08c commit 66e7b51

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/crate/client/cursor.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from .exceptions import ProgrammingError
2323
from distutils.version import StrictVersion
2424
import warnings
25+
from datetime import datetime
2526

2627
BULK_INSERT_MIN_VERSION = StrictVersion("0.42.0")
2728

@@ -53,8 +54,35 @@ def execute(self, sql, parameters=None, bulk_parameters=None):
5354
self._result = self.connection.client.sql(sql, parameters,
5455
bulk_parameters)
5556
if "rows" in self._result:
57+
if "col_types" in self._result:
58+
col_types = self._result["col_types"]
59+
tmp_data = self._result["rows"]
60+
61+
rows_to_convert = self._get_rows_to_convert_to_date(col_types)
62+
tmp_data = self._convert_dates_to_datetime(tmp_data, rows_to_convert)
63+
64+
self._result["rows"] = tmp_data
65+
5666
self.rows = iter(self._result["rows"])
5767

68+
@staticmethod
69+
def _get_rows_to_convert_to_date(col_types):
70+
return [True if col_type == 11 or col_type == 15 else False for col_type in col_types]
71+
72+
@staticmethod
73+
def _date_to_datetime(row, rows_to_convert):
74+
return list(
75+
map(lambda x, y:
76+
datetime.fromtimestamp(float(str(x)[0:10])) if y else x,
77+
row,
78+
rows_to_convert))
79+
80+
def _convert_dates_to_datetime(self, rows, rows_to_convert):
81+
return list(
82+
map(lambda x:
83+
self._date_to_datetime(x, rows_to_convert),
84+
rows))
85+
5886
def executemany(self, sql, seq_of_parameters):
5987
"""
6088
Prepare a database operation (query or command) and then execute it

0 commit comments

Comments
 (0)