Skip to content

Commit d8cbdfd

Browse files
committed
WIP
1 parent eb76733 commit d8cbdfd

File tree

6 files changed

+69
-37
lines changed

6 files changed

+69
-37
lines changed

preql/core/pql_functions.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1053,7 +1053,7 @@ def f(datetime):
10531053

10541054
_datetime_methods = 'hour', 'minute', 'day', 'month', 'year', 'day_of_week', 'week_of_year'
10551055

1056-
T.datetime.proto_attrs.update(create_internal_properties({
1056+
T.timestamp.proto_attrs.update(create_internal_properties({
10571057
n: _make_datetime_method(n) for n in _datetime_methods
10581058
}))
10591059

preql/core/pql_types.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,10 @@ def __setattr__(self, name, args):
256256
T.decimal = [T.number]
257257

258258
# TODO datetime vs timestamp !
259+
T.timestamp = [T.primitive] # struct?
259260
T.datetime = [T.primitive] # struct?
260261
T.date = [T.primitive] # struct?
262+
T.time = [T.primitive] # struct?
261263

262264
T.container = [T.object]
263265

@@ -331,18 +333,18 @@ def _get_subtypes():
331333
#-------------
332334

333335

334-
_t = {
336+
_python_type_to_sql_type = {
335337
bool: T.bool,
336338
int: T.int,
337339
float: T.float,
338340
str: T.string,
339-
datetime: T.datetime,
341+
datetime: T.timestamp,
340342
Decimal: T.decimal,
341-
arrow.Arrow: T.datetime,
343+
arrow.Arrow: T.timestamp, # datetime?
342344
}
343345
def from_python(t):
344346
# TODO throw proper exception if this fails
345-
return _t[t]
347+
return _python_type_to_sql_type[t]
346348

347349

348350
def common_type(t1, t2):

preql/core/sql.py

+43-29
Original file line numberDiff line numberDiff line change
@@ -904,6 +904,10 @@ def _repr(_t: T.datetime, x):
904904
# TODO Better to pass the object instead of a string?
905905
return repr(str(x))
906906

907+
def _repr(_t: T.timestamp, x):
908+
# TODO Better to pass the object instead of a string?
909+
return repr(str(x))
910+
907911
@dp_type
908912
def _repr(_t: T.union[T.string, T.text], x):
909913
quoted_quote = r"\'" if get_db_target() == bigquery else "''"
@@ -930,37 +934,47 @@ def _compile_type(target, type_: T.t_relation):
930934
#return 'INTEGER' # Foreign-key is integer
931935
return _compile_type(target, type_.elems['item'])
932936

937+
938+
class Types_PqlToSql:
939+
bool = "BOOLEAN"
940+
time = "TIME"
941+
date = "DATE"
942+
datetime = "DATETIME"
943+
timestamp = "TIMESTAMP"
944+
945+
int = "INTEGER"
946+
string = "VARCHAR(4000)"
947+
float = "FLOAT"
948+
text = "TEXT"
949+
950+
class P2S_BigQuery(Types_PqlToSql):
951+
int = "INT64"
952+
string = "STRING"
953+
float = "FLOAT64"
954+
text = "STRING"
955+
956+
957+
class P2S_MySql(Types_PqlToSql):
958+
int = "SIGNED"
959+
960+
class P2S_Sqlite(Types_PqlToSql):
961+
datetime = "TEXT"
962+
timestamp = "TEXT"
963+
964+
class P2S_Postgres(Types_PqlToSql):
965+
datetime = "timestamp with time zone"
966+
967+
_pql_to_sql_by_target = {
968+
bigquery: P2S_BigQuery,
969+
mysql: P2S_MySql,
970+
sqlite: P2S_Sqlite,
971+
postgres: P2S_Postgres,
972+
}
973+
974+
933975
@dp_type
934976
def _compile_type(target, type_: T.primitive):
935-
if target == bigquery:
936-
d = {
937-
'int': "INT64",
938-
'string': "STRING",
939-
'float': "FLOAT64",
940-
'bool': "BOOLEAN",
941-
'text': "STRING",
942-
'datetime': "DATETIME",
943-
'date': "DATE",
944-
}
945-
elif target == mysql:
946-
d = {
947-
'int': "SIGNED",
948-
'string': "VARCHAR(4000)",
949-
'float': "FLOAT",
950-
'bool': "BOOLEAN",
951-
'text': "TEXT",
952-
'datetime': "TIMESTAMP",
953-
}
954-
else:
955-
d = {
956-
'int': "INTEGER",
957-
'string': "VARCHAR(4000)",
958-
'float': "FLOAT",
959-
'bool': "BOOLEAN",
960-
'text': "TEXT",
961-
'datetime': "TIMESTAMP",
962-
}
963-
s = d[type_.typename]
977+
s = getattr(_pql_to_sql_by_target[target], type_.typename)
964978
if not type_.maybe_null():
965979
s += " NOT NULL"
966980
return s

preql/core/sql_import_result.py

+16-1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ def _restructure_result(_t: T.datetime, i):
8989
s = next(i)
9090
return _from_datetime(s)
9191

92+
@dp_type
93+
def _restructure_result(_t: T.timestamp, i):
94+
s = next(i)
95+
return _from_datetime(s)
96+
9297

9398

9499
@dp_inst
@@ -115,6 +120,14 @@ def sql_result_to_python(res: T.datetime):
115120
s = item
116121
return _from_datetime(s)
117122

123+
@dp_inst
124+
def sql_result_to_python(res: T.timestamp):
125+
# XXX doesn't belong here?
126+
row ,= res.value
127+
item ,= row
128+
s = item
129+
return _from_datetime(s)
130+
118131
def _from_sql_primitive(p):
119132
if isinstance(p, decimal.Decimal):
120133
# TODO Needs different handling when we expect a decimal
@@ -170,9 +183,11 @@ def type_from_sql(type, nullable):
170183
'float': T.float,
171184
'double precision': T.float, # double on 32-bit?
172185
'boolean': T.bool,
173-
'timestamp': T.datetime,
186+
'timestamp': T.timestamp,
174187
'timestamp without time zone': T.datetime,
188+
'datetime': T.datetime,
175189
'date': T.date,
190+
'time': T.time,
176191
'text': T.text,
177192
}
178193
try:

preql/docstring/type_docs.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@
5959
T.bool: "A boolean, which can be either `true` or `false`",
6060
# T.decimal: "A decimal number",
6161

62-
T.datetime: "A datetime type",
62+
T.datetime: "A datetime type (date+time combined)",
63+
T.timestamp: "A timestamp type (unix epoch)",
6364

6465
T.container: """The base type of containers.
6566

tests/test_basic.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1380,7 +1380,7 @@ def test_import_table(self):
13801380
c: string
13811381
d: float
13821382
e: bool
1383-
f: datetime
1383+
f: timestamp
13841384
g: text
13851385
}
13861386
""")

0 commit comments

Comments
 (0)