Repository navigation
Expand file tree
/
Copy pathmaterialize.py
More file actions
56 lines (44 loc) · 2.01 KB
/
Copy pathmaterialize.py
File metadata and controls
56 lines (44 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from __future__ import annotations
from sqlglot import exp
from sqlglot.helper import seq_get
from sqlglot.generators.postgres import PostgresGenerator
from sqlglot.transforms import (
remove_unique_constraints,
ctas_with_tmp_tables_to_create_tmp_view,
preprocess,
)
class MaterializeGenerator(PostgresGenerator):
SUPPORTS_CREATE_TABLE_LIKE = False
SUPPORTS_BETWEEN_FLAGS = False
TRANSFORMS = {
**{k: v for k, v in PostgresGenerator.TRANSFORMS.items() if k != exp.ToMap},
exp.AutoIncrementColumnConstraint: lambda self, e: "",
exp.Create: preprocess(
[
remove_unique_constraints,
ctas_with_tmp_tables_to_create_tmp_view,
]
),
exp.GeneratedAsIdentityColumnConstraint: lambda self, e: "",
exp.OnConflict: lambda self, e: "",
exp.PrimaryKeyColumnConstraint: lambda self, e: "",
}
def propertyeq_sql(self, expression: exp.PropertyEQ) -> str:
return self.binary(expression, "=>")
def datatype_sql(self, expression: exp.DataType) -> str:
if expression.is_type(exp.DType.LIST):
if expression.expressions:
return f"{self.expressions(expression, flat=True)} LIST"
return "LIST"
if expression.is_type(exp.DType.MAP) and len(expression.expressions) == 2:
key, value = expression.expressions
return f"MAP[{self.sql(key)} => {self.sql(value)}]"
return super().datatype_sql(expression)
def list_sql(self, expression: exp.List) -> str:
if isinstance(seq_get(expression.expressions, 0), exp.Select):
return self.func("LIST", seq_get(expression.expressions, 0))
return f"{self.normalize_func('LIST')}[{self.expressions(expression, flat=True)}]"
def tomap_sql(self, expression: exp.ToMap) -> str:
if isinstance(expression.this, exp.Select):
return self.func("MAP", expression.this)
return f"{self.normalize_func('MAP')}[{self.expressions(expression.this)}]"