@@ -43,8 +43,8 @@ class TableAlterColumn(PydanticModel):
4343 quoted : bool = False
4444
4545 @classmethod
46- def primitive (self , name : str , quoted : bool = False ) -> TableAlterColumn :
47- return self (
46+ def primitive (cls , name : str , quoted : bool = False ) -> TableAlterColumn :
47+ return cls (
4848 name = name ,
4949 is_struct = False ,
5050 is_array_of_struct = False ,
@@ -53,8 +53,8 @@ def primitive(self, name: str, quoted: bool = False) -> TableAlterColumn:
5353 )
5454
5555 @classmethod
56- def struct (self , name : str , quoted : bool = False ) -> TableAlterColumn :
57- return self (
56+ def struct (cls , name : str , quoted : bool = False ) -> TableAlterColumn :
57+ return cls (
5858 name = name ,
5959 is_struct = True ,
6060 is_array_of_struct = False ,
@@ -63,8 +63,8 @@ def struct(self, name: str, quoted: bool = False) -> TableAlterColumn:
6363 )
6464
6565 @classmethod
66- def array_of_struct (self , name : str , quoted : bool = False ) -> TableAlterColumn :
67- return self (
66+ def array_of_struct (cls , name : str , quoted : bool = False ) -> TableAlterColumn :
67+ return cls (
6868 name = name ,
6969 is_struct = False ,
7070 is_array_of_struct = True ,
@@ -73,8 +73,8 @@ def array_of_struct(self, name: str, quoted: bool = False) -> TableAlterColumn:
7373 )
7474
7575 @classmethod
76- def array_of_primitive (self , name : str , quoted : bool = False ) -> TableAlterColumn :
77- return self (
76+ def array_of_primitive (cls , name : str , quoted : bool = False ) -> TableAlterColumn :
77+ return cls (
7878 name = name ,
7979 is_struct = False ,
8080 is_array_of_struct = False ,
@@ -83,20 +83,22 @@ def array_of_primitive(self, name: str, quoted: bool = False) -> TableAlterColum
8383 )
8484
8585 @classmethod
86- def from_struct_kwarg (self , struct : exp .ColumnDef ) -> TableAlterColumn :
86+ def from_struct_kwarg (cls , struct : exp .ColumnDef ) -> TableAlterColumn :
8787 name = struct .alias_or_name
8888 quoted = struct .this .quoted
8989 kwarg_type = struct .args ["kind" ]
9090
9191 if kwarg_type .is_type (exp .DataType .Type .STRUCT ):
92- return self .struct (name , quoted = quoted )
92+ return cls .struct (name , quoted = quoted )
9393 elif kwarg_type .is_type (exp .DataType .Type .ARRAY ):
94- if kwarg_type .expressions [0 ].is_type (exp .DataType .Type .STRUCT ):
95- return self .array_of_struct (name , quoted = quoted )
94+ if kwarg_type .expressions and kwarg_type .expressions [0 ].is_type (
95+ exp .DataType .Type .STRUCT
96+ ):
97+ return cls .array_of_struct (name , quoted = quoted )
9698 else :
97- return self .array_of_primitive (name , quoted = quoted )
99+ return cls .array_of_primitive (name , quoted = quoted )
98100 else :
99- return self .primitive (name , quoted = quoted )
101+ return cls .primitive (name , quoted = quoted )
100102
101103 @property
102104 def is_array (self ) -> bool :
@@ -121,22 +123,22 @@ class TableAlterColumnPosition(PydanticModel):
121123 after : t .Optional [exp .Identifier ] = None
122124
123125 @classmethod
124- def first (self ) -> TableAlterColumnPosition :
125- return self (is_first = True , is_last = False , after = None )
126+ def first (cls ) -> TableAlterColumnPosition :
127+ return cls (is_first = True , is_last = False , after = None )
126128
127129 @classmethod
128130 def last (
129- self , after : t .Optional [t .Union [str , exp .Identifier ]] = None
131+ cls , after : t .Optional [t .Union [str , exp .Identifier ]] = None
130132 ) -> TableAlterColumnPosition :
131- return self (is_first = False , is_last = True , after = exp .to_identifier (after ) if after else None )
133+ return cls (is_first = False , is_last = True , after = exp .to_identifier (after ) if after else None )
132134
133135 @classmethod
134- def middle (self , after : t .Union [str , exp .Identifier ]) -> TableAlterColumnPosition :
135- return self (is_first = False , is_last = False , after = exp .to_identifier (after ))
136+ def middle (cls , after : t .Union [str , exp .Identifier ]) -> TableAlterColumnPosition :
137+ return cls (is_first = False , is_last = False , after = exp .to_identifier (after ))
136138
137139 @classmethod
138140 def create (
139- self ,
141+ cls ,
140142 pos : int ,
141143 current_kwargs : t .List [exp .ColumnDef ],
142144 replacing_col : bool = False ,
@@ -147,7 +149,7 @@ def create(
147149 if not is_first :
148150 prior_kwarg = current_kwargs [pos - 1 ]
149151 after , _ = _get_name_and_type (prior_kwarg )
150- return self (is_first = is_first , is_last = is_last , after = after )
152+ return cls (is_first = is_first , is_last = is_last , after = after )
151153
152154 @property
153155 def column_position_node (self ) -> t .Optional [exp .ColumnPosition ]:
@@ -170,13 +172,13 @@ class TableAlterOperation(PydanticModel):
170172
171173 @classmethod
172174 def add (
173- self ,
175+ cls ,
174176 columns : t .Union [TableAlterColumn , t .List [TableAlterColumn ]],
175177 column_type : t .Union [str , exp .DataType ],
176178 expected_table_struct : t .Union [str , exp .DataType ],
177179 position : t .Optional [TableAlterColumnPosition ] = None ,
178180 ) -> TableAlterOperation :
179- return self (
181+ return cls (
180182 op = TableAlterOperationType .ADD ,
181183 columns = ensure_list (columns ),
182184 column_type = exp .DataType .build (column_type ),
@@ -186,13 +188,13 @@ def add(
186188
187189 @classmethod
188190 def drop (
189- self ,
191+ cls ,
190192 columns : t .Union [TableAlterColumn , t .List [TableAlterColumn ]],
191193 expected_table_struct : t .Union [str , exp .DataType ],
192194 column_type : t .Optional [t .Union [str , exp .DataType ]] = None ,
193195 ) -> TableAlterOperation :
194196 column_type = exp .DataType .build (column_type ) if column_type else exp .DataType .build ("INT" )
195- return self (
197+ return cls (
196198 op = TableAlterOperationType .DROP ,
197199 columns = ensure_list (columns ),
198200 column_type = column_type ,
@@ -201,14 +203,14 @@ def drop(
201203
202204 @classmethod
203205 def alter_type (
204- self ,
206+ cls ,
205207 columns : t .Union [TableAlterColumn , t .List [TableAlterColumn ]],
206208 column_type : t .Union [str , exp .DataType ],
207209 current_type : t .Union [str , exp .DataType ],
208210 expected_table_struct : t .Union [str , exp .DataType ],
209211 position : t .Optional [TableAlterColumnPosition ] = None ,
210212 ) -> TableAlterOperation :
211- return self (
213+ return cls (
212214 op = TableAlterOperationType .ALTER_TYPE ,
213215 columns = ensure_list (columns ),
214216 column_type = exp .DataType .build (column_type ),
@@ -456,6 +458,9 @@ def _alter_operation(
456458 root_struct ,
457459 )
458460 if new_type .this == current_type .this == exp .DataType .Type .ARRAY :
461+ # Some engines (i.e. Snowflake) don't support defining types on arrays
462+ if not new_type .expressions or not current_type .expressions :
463+ return []
459464 new_array_type = new_type .expressions [0 ]
460465 current_array_type = current_type .expressions [0 ]
461466 if new_array_type .this == current_array_type .this == exp .DataType .Type .STRUCT :
0 commit comments