Skip to content

Commit cea4464

Browse files
committed
SNOW-227101: fix to datetime columns being created without timezone (…
…TIMESTAMP_NTZ). Commit 28e8031 introduced a bug where all sqlalchemy.DateTime(timezone=True) columns were created as DATETIME in Snowflake which is an alias to TIMESTAMP_NTZ. This commit reverts datetime logic back so if the timezone is defined in datetime column it will create TIMESTAMP_TZ and TIMESTAMP_NTZ otherwise. (cherry picked from commit e180b23)
1 parent de68d86 commit cea4464

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

Diff for: src/snowflake/sqlalchemy/base.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -634,10 +634,10 @@ def visit_BLOB(self, type_, **kw):
634634
return "BINARY"
635635

636636
def visit_datetime(self, type_, **kw):
637-
return "datetime"
637+
return self.visit_TIMESTAMP(type_, **kw)
638638

639639
def visit_DATETIME(self, type_, **kw):
640-
return "DATETIME"
640+
return self.visit_TIMESTAMP(type_, **kw)
641641

642642
def visit_TIMESTAMP_NTZ(self, type_, **kw):
643643
return "TIMESTAMP_NTZ"
@@ -649,7 +649,14 @@ def visit_TIMESTAMP_LTZ(self, type_, **kw):
649649
return "TIMESTAMP_LTZ"
650650

651651
def visit_TIMESTAMP(self, type_, **kw):
652-
return "TIMESTAMP"
652+
is_local = kw.get('is_local', False)
653+
timezone = kw.get('timezone', type_.timezone)
654+
return "TIMESTAMP %s%s" % (
655+
(timezone and "WITH" or "WITHOUT") + (
656+
is_local and " LOCAL" or "") + " TIME ZONE",
657+
"(%d)" % type_.precision if getattr(type_, 'precision',
658+
None) is not None else ""
659+
)
653660

654661
def visit_GEOGRAPHY(self, type_, **kw):
655662
return "GEOGRAPHY"

Diff for: tests/test_timestamp.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from datetime import datetime
66

77
import pytz
8-
from sqlalchemy import Column, Integer, MetaData, Table
8+
from sqlalchemy import Column, Integer, MetaData, Table, DateTime
99
from sqlalchemy.sql import select
1010

1111
from snowflake.sqlalchemy import TIMESTAMP_LTZ, TIMESTAMP_NTZ, TIMESTAMP_TZ
@@ -27,6 +27,8 @@ def test_create_table_timestamp_datatypes(engine_testaccount):
2727
Column("tsntz", TIMESTAMP_NTZ),
2828
Column("tsltz", TIMESTAMP_LTZ),
2929
Column("tstz", TIMESTAMP_TZ),
30+
Column("dttz", DateTime(timezone=True)),
31+
Column("dtntz", DateTime(timezone=False)),
3032
)
3133
metadata.create_all(engine_testaccount)
3234
try:
@@ -48,6 +50,8 @@ def test_inspect_timestamp_datatypes(engine_testaccount):
4850
Column("tsntz", TIMESTAMP_NTZ),
4951
Column("tsltz", TIMESTAMP_LTZ),
5052
Column("tstz", TIMESTAMP_TZ),
53+
Column("dttz", DateTime(timezone=True)),
54+
Column("dtntz", DateTime(timezone=False)),
5155
)
5256
metadata.create_all(engine_testaccount)
5357
try:
@@ -79,5 +83,7 @@ def test_inspect_timestamp_datatypes(engine_testaccount):
7983
assert rows[1] == current_utctime
8084
assert rows[2] == current_localtime
8185
assert rows[3] == current_localtime_with_other_tz
86+
assert rows[4] == current_localtime_with_other_tz
87+
assert rows[5] == current_utctime
8288
finally:
8389
test_timestamp.drop(engine_testaccount)

0 commit comments

Comments
 (0)