4
4
from sqlalchemy .engine import reflection
5
5
from sqlalchemy .ext .compiler import compiles
6
6
from sqlalchemy .sql .expression import BindParameter , Executable , ClauseElement
7
- from sqlalchemy .types import VARCHAR , NullType
7
+ from sqlalchemy .types import VARCHAR , NullType , BigInteger , Integer
8
8
9
9
10
10
class RedShiftDDLCompiler (PGDDLCompiler ):
11
11
''' Handles Redshift specific create table syntax.
12
-
12
+
13
13
Users can specify the DISTSTYLE, DISTKEY, SORTKEY and ENCODE properties per
14
- table and per column.
15
-
16
- Table level properties can be set using the dialect specific syntax. For
14
+ table and per column.
15
+
16
+ Table level properties can be set using the dialect specific syntax. For
17
17
example, to specify a distkey and style you apply the following ::
18
-
19
- table = Table(metadata,
18
+
19
+ table = Table(metadata,
20
20
Column('id', Integer, primary_key=True),
21
21
Column('name', String),
22
22
redshift_diststyle="KEY",
23
23
redshift_distkey="id"
24
24
redshift_sortkey=["id", "name"]
25
25
)
26
-
26
+
27
27
A single sortkey can be applied without a wrapping list ::
28
-
29
- table = Table(metadata,
28
+
29
+ table = Table(metadata,
30
30
Column('id', Integer, primary_key=True),
31
31
Column('name', String),
32
32
redshift_sortkey="id"
33
33
)
34
-
35
- Column level special syntax can also be applied using the column info
34
+
35
+ Column level special syntax can also be applied using the column info
36
36
dictionary. For example, we can specify the encode for a column ::
37
-
38
- table = Table(metadata,
37
+
38
+ table = Table(metadata,
39
39
Column('id', Integer, primary_key=True),
40
40
Column('name', String, info={"encode":"lzo"})
41
41
)
42
-
42
+
43
43
We can also specify the distkey and sortkey options ::
44
-
45
- table = Table(metadata,
44
+
45
+ table = Table(metadata,
46
46
Column('id', Integer, primary_key=True),
47
- Column('name', String,
47
+ Column('name', String,
48
48
info={"distkey":True, "sortkey":True})
49
49
)
50
-
50
+
51
51
'''
52
52
53
53
def post_create_table (self , table ):
@@ -75,17 +75,16 @@ def post_create_table(self, table):
75
75
return text
76
76
77
77
def get_column_specification (self , column , ** kwargs ):
78
- # aron - Apr 21, 2014: Redshift doesn't support serial types. So I
79
- # removed support for them here.
80
78
colspec = self .preparer .format_column (column )
79
+
81
80
colspec += " " + self .dialect .type_compiler .process (column .type )
82
-
83
- colspec += self ._fetch_redshift_column_attributes (column )
84
-
81
+
85
82
default = self .get_column_default_string (column )
86
83
if default is not None :
87
84
colspec += " DEFAULT " + default
88
-
85
+
86
+ colspec += self ._fetch_redshift_column_attributes (column )
87
+
89
88
if not column .nullable :
90
89
colspec += " NOT NULL"
91
90
return colspec
@@ -95,6 +94,10 @@ def _fetch_redshift_column_attributes(self, column):
95
94
if not hasattr (column , 'info' ):
96
95
return text
97
96
info = column .info
97
+ identity = info .get ('identity' , None )
98
+ if identity :
99
+ text += " IDENTITY({0},{1})" .format (identity [0 ], identity [1 ])
100
+
98
101
encode = info .get ('encode' , None )
99
102
if encode :
100
103
text += " ENCODE " + encode
@@ -111,7 +114,7 @@ def _fetch_redshift_column_attributes(self, column):
111
114
class RedshiftDialect (PGDialect_psycopg2 ):
112
115
name = 'redshift'
113
116
ddl_compiler = RedShiftDDLCompiler
114
-
117
+
115
118
construct_arguments = [
116
119
(schema .Index , {
117
120
"using" : False ,
@@ -125,7 +128,7 @@ class RedshiftDialect(PGDialect_psycopg2):
125
128
'sortkey' : None
126
129
}),
127
130
]
128
-
131
+
129
132
@reflection .cache
130
133
def get_pk_constraint (self , connection , table_name , schema = None , ** kw ):
131
134
"""
0 commit comments