44from  sqlalchemy .engine  import  reflection 
55from  sqlalchemy .ext .compiler  import  compiles 
66from  sqlalchemy .sql .expression  import  BindParameter , Executable , ClauseElement 
7- from  sqlalchemy .types  import  VARCHAR , NullType 
7+ from  sqlalchemy .types  import  VARCHAR , NullType ,  BigInteger ,  Integer 
88
99
1010class  RedShiftDDLCompiler (PGDDLCompiler ):
1111    ''' Handles Redshift specific create table syntax. 
12-      
12+ 
1313    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 
1717    example, to specify a distkey and style you apply the following :: 
18-      
19-         table = Table(metadata,   
18+ 
19+         table = Table(metadata, 
2020                      Column('id', Integer, primary_key=True), 
2121                      Column('name', String), 
2222                      redshift_diststyle="KEY", 
2323                      redshift_distkey="id" 
2424                      redshift_sortkey=["id", "name"] 
2525                      ) 
26-                        
26+ 
2727    A single sortkey can be applied without a wrapping list :: 
28-      
29-         table = Table(metadata,   
28+ 
29+         table = Table(metadata, 
3030                      Column('id', Integer, primary_key=True), 
3131                      Column('name', String), 
3232                      redshift_sortkey="id" 
3333                      ) 
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 
3636    dictionary. For example, we can specify the encode for a column :: 
37-      
38-         table = Table(metadata,   
37+ 
38+         table = Table(metadata, 
3939                      Column('id', Integer, primary_key=True), 
4040                      Column('name', String, info={"encode":"lzo"}) 
4141                      ) 
42-                        
42+ 
4343    We can also specify the distkey and sortkey options :: 
44-      
45-         table = Table(metadata,   
44+ 
45+         table = Table(metadata, 
4646                      Column('id', Integer, primary_key=True), 
47-                       Column('name', String,   
47+                       Column('name', String, 
4848                             info={"distkey":True, "sortkey":True}) 
4949                      ) 
50-                        
50+ 
5151    ''' 
5252
5353    def  post_create_table (self , table ):
@@ -75,17 +75,16 @@ def post_create_table(self, table):
7575        return  text 
7676
7777    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. 
8078        colspec  =  self .preparer .format_column (column )
79+ 
8180        colspec  +=  " "  +  self .dialect .type_compiler .process (column .type )
82-  
83-         colspec  +=  self ._fetch_redshift_column_attributes (column )
84-  
81+ 
8582        default  =  self .get_column_default_string (column )
8683        if  default  is  not None :
8784            colspec  +=  " DEFAULT "  +  default 
88-  
85+ 
86+         colspec  +=  self ._fetch_redshift_column_attributes (column )
87+ 
8988        if  not  column .nullable :
9089            colspec  +=  " NOT NULL" 
9190        return  colspec 
@@ -95,6 +94,10 @@ def _fetch_redshift_column_attributes(self, column):
9594        if  not  hasattr (column , 'info' ):
9695            return  text 
9796        info  =  column .info 
97+         identity  =  info .get ('identity' , None )
98+         if  identity :
99+             text  +=  " IDENTITY({0},{1})" .format (identity [0 ], identity [1 ])
100+ 
98101        encode  =  info .get ('encode' , None )
99102        if  encode :
100103            text  +=  " ENCODE "  +  encode 
@@ -111,7 +114,7 @@ def _fetch_redshift_column_attributes(self, column):
111114class  RedshiftDialect (PGDialect_psycopg2 ):
112115    name  =  'redshift' 
113116    ddl_compiler  =  RedShiftDDLCompiler 
114-      
117+ 
115118    construct_arguments  =  [
116119                            (schema .Index , {
117120                                "using" : False ,
@@ -125,7 +128,7 @@ class RedshiftDialect(PGDialect_psycopg2):
125128                                'sortkey' : None 
126129                            }),
127130                           ]
128-      
131+ 
129132    @reflection .cache  
130133    def  get_pk_constraint (self , connection , table_name , schema = None , ** kw ):
131134        """ 
0 commit comments