Skip to content

Commit 6ba75b7

Browse files
authored
Add geography support to snowflake-sqlalchemy (#271)
1 parent 5ac9385 commit 6ba75b7

File tree

6 files changed

+77
-1
lines changed

6 files changed

+77
-1
lines changed

__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
DEC,
4444
DOUBLE,
4545
FIXED,
46+
GEOGRAPHY,
4647
NUMBER,
4748
OBJECT,
4849
STRING,
@@ -87,6 +88,7 @@
8788
'DEC',
8889
'DOUBLE',
8990
'FIXED',
91+
'GEOGRAPHY',
9092
'OBJECT',
9193
'NUMBER',
9294
'STRING',

base.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,8 @@ def visit_TIMESTAMP_LTZ(self, type_, **kw):
505505
def visit_TIMESTAMP(self, type_, **kw):
506506
return "TIMESTAMP"
507507

508+
def visit_GEOGRAPHY(self, type_, **kw):
509+
return "GEOGRAPHY"
508510

509511
construct_arguments = [
510512
(Table, {

custom_types.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,6 @@ class TIMESTAMP_LTZ(SnowflakeType):
4747

4848
class TIMESTAMP_NTZ(SnowflakeType):
4949
__visit_name__ = 'TIMESTAMP_NTZ'
50+
51+
class GEOGRAPHY(SnowflakeType):
52+
__visit_name__ = 'GEOGRAPHY'

snowdialect.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
SnowflakeIdentifierPreparer,
4545
SnowflakeTypeCompiler,
4646
)
47-
from .custom_types import ARRAY, OBJECT, TIMESTAMP_LTZ, TIMESTAMP_NTZ, TIMESTAMP_TZ, VARIANT
47+
from .custom_types import ARRAY, OBJECT, TIMESTAMP_LTZ, TIMESTAMP_NTZ, TIMESTAMP_TZ, VARIANT, GEOGRAPHY
4848

4949
colspecs = {}
5050

@@ -82,6 +82,7 @@
8282
'VARIANT': VARIANT,
8383
'OBJECT': OBJECT,
8484
'ARRAY': ARRAY,
85+
'GEOGRAPHY': GEOGRAPHY,
8586
}
8687

8788

test/test_custom_types.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ def test_string_conversions():
1010
'TIMESTAMP_TZ',
1111
'TIMESTAMP_LTZ',
1212
'TIMESTAMP_NTZ',
13+
'GEOGRAPHY',
1314
]
1415
sf_types = [
1516
'TEXT',

test/test_geography.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
from parameters import CONNECTION_PARAMETERS
5+
from snowflake.sqlalchemy import GEOGRAPHY
6+
from sqlalchemy import Column, Integer, MetaData, Table
7+
from sqlalchemy.sql import select
8+
from json import loads
9+
10+
def test_create_table_geography_datatypes(engine_testaccount):
11+
"""
12+
Create table including geography data types
13+
"""
14+
15+
metadata = MetaData()
16+
table_name = "test_geography0"
17+
test_geography = Table(
18+
table_name,
19+
metadata,
20+
Column('id', Integer, primary_key=True),
21+
Column('geo', GEOGRAPHY),
22+
)
23+
metadata.create_all(engine_testaccount)
24+
try:
25+
assert test_geography is not None
26+
finally:
27+
test_geography.drop(engine_testaccount)
28+
29+
def test_inspect_geography_datatypes(engine_testaccount):
30+
"""
31+
Create table including geography data types
32+
"""
33+
metadata = MetaData()
34+
table_name = "test_geography0"
35+
test_geography = Table(
36+
table_name,
37+
metadata,
38+
Column('id', Integer, primary_key=True),
39+
Column('geo1', GEOGRAPHY),
40+
Column('geo2', GEOGRAPHY))
41+
metadata.create_all(engine_testaccount)
42+
43+
try:
44+
test_point = 'POINT(-122.35 37.55)'
45+
test_point1 = '{"coordinates": [-122.35,37.55],"type": "Point"}'
46+
47+
ins = test_geography.insert().values(
48+
id=1,
49+
geo1=test_point,
50+
geo2=test_point1
51+
)
52+
53+
results = engine_testaccount.execute(ins)
54+
results.close()
55+
56+
# select
57+
conn = engine_testaccount.connect()
58+
s = select([test_geography])
59+
results = conn.execute(s)
60+
rows = results.fetchone()
61+
results.close()
62+
assert rows[0] == 1
63+
assert rows[1] == rows[2]
64+
assert loads(rows[2]) == loads(test_point1)
65+
finally:
66+
test_geography.drop(engine_testaccount)
67+

0 commit comments

Comments
 (0)