Skip to content

Commit

Permalink
Transform dates from crate to python datetime
Browse files Browse the repository at this point in the history
In execute(), transform the columns with type timestamp and timestamp without time zone to python datetime, this will correctly display dates in apache superset
  • Loading branch information
Aymaru committed Feb 22, 2021
1 parent f9bd08c commit 66e7b51
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/crate/client/cursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from .exceptions import ProgrammingError
from distutils.version import StrictVersion
import warnings
from datetime import datetime

BULK_INSERT_MIN_VERSION = StrictVersion("0.42.0")

Expand Down Expand Up @@ -53,8 +54,35 @@ def execute(self, sql, parameters=None, bulk_parameters=None):
self._result = self.connection.client.sql(sql, parameters,
bulk_parameters)
if "rows" in self._result:
if "col_types" in self._result:
col_types = self._result["col_types"]
tmp_data = self._result["rows"]

rows_to_convert = self._get_rows_to_convert_to_date(col_types)
tmp_data = self._convert_dates_to_datetime(tmp_data, rows_to_convert)

self._result["rows"] = tmp_data

self.rows = iter(self._result["rows"])

@staticmethod
def _get_rows_to_convert_to_date(col_types):
return [True if col_type == 11 or col_type == 15 else False for col_type in col_types]

@staticmethod
def _date_to_datetime(row, rows_to_convert):
return list(
map(lambda x, y:
datetime.fromtimestamp(float(str(x)[0:10])) if y else x,
row,
rows_to_convert))

def _convert_dates_to_datetime(self, rows, rows_to_convert):
return list(
map(lambda x:
self._date_to_datetime(x, rows_to_convert),
rows))

def executemany(self, sql, seq_of_parameters):
"""
Prepare a database operation (query or command) and then execute it
Expand Down

0 comments on commit 66e7b51

Please sign in to comment.