Skip to content

Commit e180b23

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.
1 parent 6615f51 commit e180b23

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

base.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -419,10 +419,10 @@ def visit_BLOB(self, type_, **kw):
419419
return "BINARY"
420420

421421
def visit_datetime(self, type_, **kw):
422-
return "datetime"
422+
return self.visit_TIMESTAMP(type_, **kw)
423423

424424
def visit_DATETIME(self, type_, **kw):
425-
return "DATETIME"
425+
return self.visit_TIMESTAMP(type_, **kw)
426426

427427
def visit_TIMESTAMP_NTZ(self, type_, **kw):
428428
return "TIMESTAMP_NTZ"
@@ -434,8 +434,14 @@ def visit_TIMESTAMP_LTZ(self, type_, **kw):
434434
return "TIMESTAMP_LTZ"
435435

436436
def visit_TIMESTAMP(self, type_, **kw):
437-
return "TIMESTAMP"
438-
437+
is_local = kw.get('is_local', False)
438+
timezone = kw.get('timezone', type_.timezone)
439+
return "TIMESTAMP %s%s" % (
440+
(timezone and "WITH" or "WITHOUT") + (
441+
is_local and " LOCAL" or "") + " TIME ZONE",
442+
"(%d)" % type_.precision if getattr(type_, 'precision',
443+
None) is not None else ""
444+
)
439445

440446
construct_arguments = [
441447
(Table, {

test/test_timestamp.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import pytz
1010
from parameters import CONNECTION_PARAMETERS
1111
from snowflake.sqlalchemy import TIMESTAMP_LTZ, TIMESTAMP_NTZ, TIMESTAMP_TZ
12-
from sqlalchemy import Column, Integer, MetaData, Table
12+
from sqlalchemy import Column, Integer, MetaData, Table, DateTime
1313
from sqlalchemy.sql import select
1414

1515
PST_TZ = "America/Los_Angeles"
@@ -33,7 +33,10 @@ def test_create_table_timestamp_datatypes(engine_testaccount):
3333
Column('id', Integer, primary_key=True),
3434
Column('tsntz', TIMESTAMP_NTZ),
3535
Column('tsltz', TIMESTAMP_LTZ),
36-
Column('tstz', TIMESTAMP_TZ))
36+
Column('tstz', TIMESTAMP_TZ),
37+
Column('dttz', DateTime(timezone=True)),
38+
Column('dtntz', DateTime(timezone=False))
39+
)
3740
metadata.create_all(engine_testaccount)
3841
try:
3942
assert test_timestamp is not None
@@ -53,7 +56,10 @@ def test_inspect_timestamp_datatypes(engine_testaccount):
5356
Column('id', Integer, primary_key=True),
5457
Column('tsntz', TIMESTAMP_NTZ),
5558
Column('tsltz', TIMESTAMP_LTZ),
56-
Column('tstz', TIMESTAMP_TZ))
59+
Column('tstz', TIMESTAMP_TZ),
60+
Column('dttz', DateTime(timezone=True)),
61+
Column('dtntz', DateTime(timezone=False)),
62+
)
5763
metadata.create_all(engine_testaccount)
5864
try:
5965
current_utctime = datetime.utcnow()
@@ -70,6 +76,8 @@ def test_inspect_timestamp_datatypes(engine_testaccount):
7076
tsntz=current_utctime,
7177
tsltz=current_localtime,
7278
tstz=current_localtime_with_other_tz,
79+
dttz=current_localtime_with_other_tz,
80+
dtntz=current_utctime,
7381
)
7482
results = engine_testaccount.execute(ins)
7583
results.close()
@@ -84,5 +92,7 @@ def test_inspect_timestamp_datatypes(engine_testaccount):
8492
assert rows[1] == current_utctime
8593
assert rows[2] == current_localtime
8694
assert rows[3] == current_localtime_with_other_tz
95+
assert rows[4] == current_localtime_with_other_tz
96+
assert rows[5] == current_utctime
8797
finally:
8898
test_timestamp.drop(engine_testaccount)

0 commit comments

Comments
 (0)