@@ -32,6 +32,18 @@ def _generate_as_hive(expression: exp.Expression) -> bool:
3232 return False
3333
3434
35+ def _is_iceberg_table (properties : exp .Properties ) -> bool :
36+ table_type_property = next (
37+ (
38+ p
39+ for p in properties .expressions
40+ if isinstance (p , exp .Property ) and p .name == "table_type"
41+ ),
42+ None ,
43+ )
44+ return bool (table_type_property and table_type_property .text ("value" ).lower () == "iceberg" )
45+
46+
3547def _location_property_sql (self : Athena .Generator , e : exp .LocationProperty ):
3648 # If table_type='iceberg', the LocationProperty is called 'location'
3749 # Otherwise, it's called 'external_location'
@@ -40,20 +52,25 @@ def _location_property_sql(self: Athena.Generator, e: exp.LocationProperty):
4052 prop_name = "external_location"
4153
4254 if isinstance (e .parent , exp .Properties ):
43- table_type_property = next (
44- (
45- p
46- for p in e .parent .expressions
47- if isinstance (p , exp .Property ) and p .name == "table_type"
48- ),
49- None ,
50- )
51- if table_type_property and table_type_property .text ("value" ).lower () == "iceberg" :
55+ if _is_iceberg_table (e .parent ):
5256 prop_name = "location"
5357
5458 return f"{ prop_name } ={ self .sql (e , 'this' )} "
5559
5660
61+ def _partitioned_by_property_sql (self : Athena .Generator , e : exp .PartitionedByProperty ):
62+ # If table_type='iceberg' then the table property for partitioning is called 'partitioning'
63+ # If table_type='hive' it's called 'partitioned_by'
64+ # ref: https://docs.aws.amazon.com/athena/latest/ug/create-table-as.html#ctas-table-properties
65+
66+ prop_name = "partitioned_by"
67+ if isinstance (e .parent , exp .Properties ):
68+ if _is_iceberg_table (e .parent ):
69+ prop_name = "partitioning"
70+
71+ return f"{ prop_name } ={ self .sql (e , 'this' )} "
72+
73+
5774class Athena (Trino ):
5875 """
5976 Over the years, it looks like AWS has taken various execution engines, bolted on AWS-specific modifications and then
@@ -132,7 +149,7 @@ class Generator(Trino.Generator):
132149 TRANSFORMS = {
133150 ** Trino .Generator .TRANSFORMS ,
134151 exp .FileFormatProperty : lambda self , e : f"format={ self .sql (e , 'this' )} " ,
135- exp .PartitionedByProperty : lambda self , e : f"partitioned_by= { self . sql ( e , 'this' ) } " ,
152+ exp .PartitionedByProperty : _partitioned_by_property_sql ,
136153 exp .LocationProperty : _location_property_sql ,
137154 }
138155
0 commit comments