sqlglot-odps 0.1.0__tar.gz → 0.2.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- sqlglot_odps-0.2.0/CHANGELOG.md +14 -0
- {sqlglot_odps-0.1.0 → sqlglot_odps-0.2.0}/PKG-INFO +6 -5
- {sqlglot_odps-0.1.0 → sqlglot_odps-0.2.0}/README.md +5 -4
- {sqlglot_odps-0.1.0 → sqlglot_odps-0.2.0}/pyproject.toml +1 -1
- {sqlglot_odps-0.1.0 → sqlglot_odps-0.2.0}/src/sqlglot_odps/dialect.py +23 -0
- sqlglot_odps-0.2.0/src/sqlglot_odps/expressions.py +92 -0
- {sqlglot_odps-0.1.0 → sqlglot_odps-0.2.0}/src/sqlglot_odps/generator.py +253 -12
- {sqlglot_odps-0.1.0 → sqlglot_odps-0.2.0}/src/sqlglot_odps/parser.py +675 -11
- {sqlglot_odps-0.1.0 → sqlglot_odps-0.2.0}/src/sqlglot_odps/statements.py +18 -0
- {sqlglot_odps-0.1.0 → sqlglot_odps-0.2.0}/tests/test_maxcompute.py +19 -12
- {sqlglot_odps-0.1.0 → sqlglot_odps-0.2.0}/tests/test_statement_kind.py +53 -0
- {sqlglot_odps-0.1.0 → sqlglot_odps-0.2.0}/tests/test_statement_syntax.py +194 -1
- {sqlglot_odps-0.1.0 → sqlglot_odps-0.2.0}/uv.lock +1 -1
- sqlglot_odps-0.1.0/CHANGELOG.md +0 -7
- {sqlglot_odps-0.1.0 → sqlglot_odps-0.2.0}/.github/workflows/release.yml +0 -0
- {sqlglot_odps-0.1.0 → sqlglot_odps-0.2.0}/.gitignore +0 -0
- {sqlglot_odps-0.1.0 → sqlglot_odps-0.2.0}/.python-version +0 -0
- {sqlglot_odps-0.1.0 → sqlglot_odps-0.2.0}/LICENSE +0 -0
- {sqlglot_odps-0.1.0 → sqlglot_odps-0.2.0}/src/sqlglot_odps/__init__.py +0 -0
- {sqlglot_odps-0.1.0 → sqlglot_odps-0.2.0}/src/sqlglot_odps/py.typed +0 -0
- {sqlglot_odps-0.1.0 → sqlglot_odps-0.2.0}/tests/__init__.py +0 -0
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 0.2.0
|
|
4
|
+
|
|
5
|
+
- Preserve generated columns, bounded string types, object tables, and managed Iceberg table definitions.
|
|
6
|
+
- Parse Iceberg partition changes, table options, snapshot references, and nested column changes in `ALTER TABLE`.
|
|
7
|
+
- Preserve typed script variables, `IF` branches, `BEGIN ... END` blocks, and SQL function bodies.
|
|
8
|
+
- Preserve optimizer hints, colon-qualified function names, and expressions in `CLUSTER BY`.
|
|
9
|
+
|
|
10
|
+
## 0.1.0
|
|
11
|
+
|
|
12
|
+
- Register the `odps` SQLGlot dialect for MaxCompute.
|
|
13
|
+
- Parse and generate supported MaxCompute queries, DDL, and DML, including partition writes and shared-source multi-inserts.
|
|
14
|
+
- Classify standalone statements and submitted SQL scripts. Keep ordered `SET` configuration in the script AST.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: sqlglot-odps
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.2.0
|
|
4
4
|
Summary: MaxCompute dialect plugin for SQLGlot
|
|
5
5
|
Project-URL: Homepage, https://github.com/dingxin-tech/sqlglot-odps
|
|
6
6
|
Project-URL: Repository, https://github.com/dingxin-tech/sqlglot-odps
|
|
@@ -104,7 +104,7 @@ print(statement_kind(ast).value) # DQL
|
|
|
104
104
|
| `DDL` | `CREATE` including CTAS, `ALTER`, `DROP`, `TRUNCATE TABLE` |
|
|
105
105
|
| `DML` | `INSERT`, `UPDATE`, `DELETE`, `MERGE`, data loading and transfer |
|
|
106
106
|
| `DQL` | Queries and set operations, supported `SHOW` variants, `DESCRIBE`, `EXPLAIN`, `READ` |
|
|
107
|
-
| `OTHER` | `SET`, `USE`, parsed `GRANT` and `REVOKE`, `ANALYZE`,
|
|
107
|
+
| `OTHER` | `SET`, `USE`, parsed `GRANT` and `REVOKE`, `ANALYZE`, script variable declarations and assignments, `IF` and `BEGIN ... END` |
|
|
108
108
|
| `MIXED` | A script with multiple different operation kinds |
|
|
109
109
|
| `UNKNOWN` | Opaque or unrecognized statements, and empty scripts |
|
|
110
110
|
|
|
@@ -112,9 +112,9 @@ Empty statements and comments are skipped. SQLGlot parse and tokenizer errors pr
|
|
|
112
112
|
|
|
113
113
|
### Statement syntax
|
|
114
114
|
|
|
115
|
-
|
|
115
|
+
The dialect preserves generated columns, bounded string types in schemas, object tables, and managed Iceberg table definitions, including partition transformations, connections, and table options. It parses partition-targeted `UPDATE` and `DELETE`, shared-source multi-insert, and `ALTER TABLE` actions for partitions, columns, options, and Iceberg snapshot references.
|
|
116
116
|
|
|
117
|
-
|
|
117
|
+
Scripts support typed `@` variables, `IF` branches, and `BEGIN ... END` blocks. Queries preserve optimizer hints, colon-qualified function names, `CLUSTER BY` expressions, `QUALIFY`, time travel, and `ZORDER BY` when rendered with `write="odps"`. SQLGlot may return `UNKNOWN` or raise a parse error for unsupported statements. Server-side validation and execution require MaxCompute.
|
|
118
118
|
|
|
119
119
|
## What's supported
|
|
120
120
|
|
|
@@ -154,7 +154,8 @@ Explicit transforms on top of Hive:
|
|
|
154
154
|
| `VAR_SAMP(x)` | `VAR_SAMP(x)` | Hive emits `VARIANCE` |
|
|
155
155
|
| `INSTR(str, sub)` | `INSTR(str, sub)` | Hive emits `LOCATE(sub, str)` |
|
|
156
156
|
| `SUBSTR(str, pos, len)` | `SUBSTR(...)` | Hive emits `SUBSTRING` |
|
|
157
|
-
| Type: `VARCHAR
|
|
157
|
+
| Type: `VARCHAR(n)` / `CHAR(n)` | `VARCHAR(n)` / `CHAR(n)` | In schema definitions |
|
|
158
|
+
| Type: `TEXT` | `STRING` | |
|
|
158
159
|
| Type: `DATETIME` | `DATETIME` | |
|
|
159
160
|
|
|
160
161
|
### DDL
|
|
@@ -84,7 +84,7 @@ print(statement_kind(ast).value) # DQL
|
|
|
84
84
|
| `DDL` | `CREATE` including CTAS, `ALTER`, `DROP`, `TRUNCATE TABLE` |
|
|
85
85
|
| `DML` | `INSERT`, `UPDATE`, `DELETE`, `MERGE`, data loading and transfer |
|
|
86
86
|
| `DQL` | Queries and set operations, supported `SHOW` variants, `DESCRIBE`, `EXPLAIN`, `READ` |
|
|
87
|
-
| `OTHER` | `SET`, `USE`, parsed `GRANT` and `REVOKE`, `ANALYZE`,
|
|
87
|
+
| `OTHER` | `SET`, `USE`, parsed `GRANT` and `REVOKE`, `ANALYZE`, script variable declarations and assignments, `IF` and `BEGIN ... END` |
|
|
88
88
|
| `MIXED` | A script with multiple different operation kinds |
|
|
89
89
|
| `UNKNOWN` | Opaque or unrecognized statements, and empty scripts |
|
|
90
90
|
|
|
@@ -92,9 +92,9 @@ Empty statements and comments are skipped. SQLGlot parse and tokenizer errors pr
|
|
|
92
92
|
|
|
93
93
|
### Statement syntax
|
|
94
94
|
|
|
95
|
-
|
|
95
|
+
The dialect preserves generated columns, bounded string types in schemas, object tables, and managed Iceberg table definitions, including partition transformations, connections, and table options. It parses partition-targeted `UPDATE` and `DELETE`, shared-source multi-insert, and `ALTER TABLE` actions for partitions, columns, options, and Iceberg snapshot references.
|
|
96
96
|
|
|
97
|
-
|
|
97
|
+
Scripts support typed `@` variables, `IF` branches, and `BEGIN ... END` blocks. Queries preserve optimizer hints, colon-qualified function names, `CLUSTER BY` expressions, `QUALIFY`, time travel, and `ZORDER BY` when rendered with `write="odps"`. SQLGlot may return `UNKNOWN` or raise a parse error for unsupported statements. Server-side validation and execution require MaxCompute.
|
|
98
98
|
|
|
99
99
|
## What's supported
|
|
100
100
|
|
|
@@ -134,7 +134,8 @@ Explicit transforms on top of Hive:
|
|
|
134
134
|
| `VAR_SAMP(x)` | `VAR_SAMP(x)` | Hive emits `VARIANCE` |
|
|
135
135
|
| `INSTR(str, sub)` | `INSTR(str, sub)` | Hive emits `LOCATE(sub, str)` |
|
|
136
136
|
| `SUBSTR(str, pos, len)` | `SUBSTR(...)` | Hive emits `SUBSTRING` |
|
|
137
|
-
| Type: `VARCHAR
|
|
137
|
+
| Type: `VARCHAR(n)` / `CHAR(n)` | `VARCHAR(n)` / `CHAR(n)` | In schema definitions |
|
|
138
|
+
| Type: `TEXT` | `STRING` | |
|
|
138
139
|
| Type: `DATETIME` | `DATETIME` | |
|
|
139
140
|
|
|
140
141
|
### DDL
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
|
+
import re
|
|
4
|
+
|
|
3
5
|
from sqlglot.dialects.hive import Hive
|
|
4
6
|
from sqlglot.tokens import TokenType
|
|
5
7
|
|
|
@@ -7,6 +9,22 @@ from sqlglot_odps.generator import OdpsGenerator
|
|
|
7
9
|
from sqlglot_odps.parser import OdpsParser
|
|
8
10
|
|
|
9
11
|
|
|
12
|
+
_SQL_QUOTES_OR_COMMENTS = re.compile(
|
|
13
|
+
r"'(?:\\[\s\S]|''|[^'\\])*'|\"(?:\\[\s\S]|\"\"|[^\"\\])*\"|`(?:``|[^`])*`"
|
|
14
|
+
r"|--[^\r\n]*"
|
|
15
|
+
r"|/(?P<slash_space>[ \t\r]*)\*(?P<star_space>[ \t\r]*)\+(?P<hint>.*?)\*(?P<end_space>[ \t\r]*)/"
|
|
16
|
+
r"|/\*.*?\*/",
|
|
17
|
+
re.DOTALL,
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def _normalize_hint(match):
|
|
22
|
+
if match["hint"] is None:
|
|
23
|
+
return match[0]
|
|
24
|
+
opening_space = match["slash_space"] + match["star_space"]
|
|
25
|
+
return f"/*+{opening_space}{match['hint']}{match['end_space']}*/"
|
|
26
|
+
|
|
27
|
+
|
|
10
28
|
class Odps(Hive):
|
|
11
29
|
ALIAS_POST_VERSION = False
|
|
12
30
|
|
|
@@ -38,5 +56,10 @@ class Odps(Hive):
|
|
|
38
56
|
"VERSION BETWEEN": TokenType.FOR,
|
|
39
57
|
}
|
|
40
58
|
|
|
59
|
+
def tokenize(self, sql):
|
|
60
|
+
# Keep token offsets aligned with SQLGlot's original SQL string.
|
|
61
|
+
normalized = _SQL_QUOTES_OR_COMMENTS.sub(_normalize_hint, sql)
|
|
62
|
+
return super().tokenize(normalized)
|
|
63
|
+
|
|
41
64
|
Parser = OdpsParser
|
|
42
65
|
Generator = OdpsGenerator
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
from sqlglot import exp
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class OdpsCreate(exp.Create):
|
|
5
|
+
arg_types = {
|
|
6
|
+
**exp.Create.arg_types,
|
|
7
|
+
"native_definition": False,
|
|
8
|
+
"secure": False,
|
|
9
|
+
"return_variable": False,
|
|
10
|
+
"model_version": False,
|
|
11
|
+
"model_input": False,
|
|
12
|
+
"schedule": False,
|
|
13
|
+
"task_properties": False,
|
|
14
|
+
"task_comment": False,
|
|
15
|
+
"task_condition": False,
|
|
16
|
+
"index_properties": False,
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class DescribeModel(exp.Describe):
|
|
21
|
+
arg_types = {**exp.Describe.arg_types, "version": False}
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class IcebergConnection(exp.Property):
|
|
25
|
+
arg_types = {"this": True}
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class TableOptions(exp.Property):
|
|
29
|
+
arg_types = {"expressions": True, "kind": False}
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class ExternalTableResource(exp.Property):
|
|
33
|
+
arg_types = {"this": True}
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class TableShards(exp.Property):
|
|
37
|
+
arg_types = {"this": True, "lifecycle": False}
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
class ViewResources(exp.Property):
|
|
41
|
+
arg_types = {"expressions": True}
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
class DisableViewRewrite(exp.Property):
|
|
45
|
+
arg_types = {}
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
class PartitionProperties(exp.Property):
|
|
49
|
+
arg_types = {"expressions": True}
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
class PartitionFields(exp.Expression):
|
|
53
|
+
arg_types = {"kind": True, "expressions": True, "replacement": False}
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
class ChangeColumns(exp.Expression):
|
|
57
|
+
arg_types = {"expressions": True}
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
class DropOptions(exp.Expression):
|
|
61
|
+
arg_types = {"expressions": True}
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
class CompoundStatement(exp.Expression):
|
|
65
|
+
arg_types = {"expressions": False}
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
class VariableDeclaration(exp.Expression):
|
|
69
|
+
arg_types = {
|
|
70
|
+
"this": True,
|
|
71
|
+
"kind": False,
|
|
72
|
+
"expression": False,
|
|
73
|
+
"default": False,
|
|
74
|
+
"comment": False,
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
class RebuildIndex(exp.Expression):
|
|
79
|
+
arg_types = {"this": False, "kind": False, "auto": False}
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
class SnapshotReference(exp.Expression):
|
|
83
|
+
arg_types = {
|
|
84
|
+
"this": True,
|
|
85
|
+
"kind": True,
|
|
86
|
+
"action": True,
|
|
87
|
+
"exists": False,
|
|
88
|
+
"version": False,
|
|
89
|
+
"retain_days": False,
|
|
90
|
+
"min_snapshots": False,
|
|
91
|
+
"snapshot_days": False,
|
|
92
|
+
}
|
|
@@ -14,6 +14,25 @@ from sqlglot.transforms import (
|
|
|
14
14
|
unnest_to_explode,
|
|
15
15
|
)
|
|
16
16
|
|
|
17
|
+
from sqlglot_odps.expressions import (
|
|
18
|
+
ChangeColumns,
|
|
19
|
+
CompoundStatement,
|
|
20
|
+
DescribeModel,
|
|
21
|
+
DisableViewRewrite,
|
|
22
|
+
DropOptions,
|
|
23
|
+
ExternalTableResource,
|
|
24
|
+
IcebergConnection,
|
|
25
|
+
OdpsCreate,
|
|
26
|
+
PartitionFields,
|
|
27
|
+
PartitionProperties,
|
|
28
|
+
RebuildIndex,
|
|
29
|
+
SnapshotReference,
|
|
30
|
+
TableOptions,
|
|
31
|
+
TableShards,
|
|
32
|
+
VariableDeclaration,
|
|
33
|
+
ViewResources,
|
|
34
|
+
)
|
|
35
|
+
|
|
17
36
|
_AUTO_PARTITION_TYPES = (
|
|
18
37
|
exp.DateTrunc,
|
|
19
38
|
exp.TimestampTrunc,
|
|
@@ -25,22 +44,48 @@ def _move_schema_columns_to_partitioned_by(expression: exp.Expr) -> exp.Expr:
|
|
|
25
44
|
"""Like the Hive transform, but skip AUTO PARTITIONED BY (where this is a DateTrunc/Alias)."""
|
|
26
45
|
assert isinstance(expression, exp.Create)
|
|
27
46
|
prop = expression.find(exp.PartitionedByProperty)
|
|
28
|
-
if prop and prop.args.get("auto"):
|
|
47
|
+
if prop and (prop.args.get("auto") or prop.args.get("field_transforms")):
|
|
29
48
|
return expression
|
|
30
49
|
return move_schema_columns_to_partitioned_by(expression)
|
|
31
50
|
|
|
32
51
|
|
|
33
52
|
class OdpsGenerator(HiveGenerator):
|
|
53
|
+
QUERY_HINTS = True
|
|
34
54
|
TYPE_MAPPING = {
|
|
35
55
|
**HiveGenerator.TYPE_MAPPING,
|
|
36
56
|
exp.DType.DATETIME: "DATETIME",
|
|
37
|
-
exp.DType.VARCHAR: "STRING",
|
|
38
|
-
exp.DType.CHAR: "STRING",
|
|
39
57
|
exp.DType.TEXT: "STRING",
|
|
40
58
|
}
|
|
41
59
|
|
|
60
|
+
PROPERTIES_LOCATION = {
|
|
61
|
+
**HiveGenerator.PROPERTIES_LOCATION,
|
|
62
|
+
IcebergConnection: exp.Properties.Location.POST_SCHEMA,
|
|
63
|
+
ExternalTableResource: exp.Properties.Location.POST_SCHEMA,
|
|
64
|
+
TableOptions: exp.Properties.Location.POST_SCHEMA,
|
|
65
|
+
TableShards: exp.Properties.Location.POST_SCHEMA,
|
|
66
|
+
ViewResources: exp.Properties.Location.POST_SCHEMA,
|
|
67
|
+
DisableViewRewrite: exp.Properties.Location.POST_SCHEMA,
|
|
68
|
+
exp.BuildProperty: exp.Properties.Location.POST_SCHEMA,
|
|
69
|
+
}
|
|
70
|
+
|
|
42
71
|
TRANSFORMS = {
|
|
43
72
|
**HiveGenerator.TRANSFORMS,
|
|
73
|
+
ChangeColumns: lambda self, e: self.changecolumns_sql(e),
|
|
74
|
+
CompoundStatement: lambda self, e: self.compoundstatement_sql(e),
|
|
75
|
+
DescribeModel: lambda self, e: self.describe_sql(e),
|
|
76
|
+
DisableViewRewrite: lambda *_: "DISABLE REWRITE",
|
|
77
|
+
DropOptions: lambda self, e: self.dropoptions_sql(e),
|
|
78
|
+
ExternalTableResource: lambda self, e: self.externaltableresource_sql(e),
|
|
79
|
+
IcebergConnection: lambda self, e: self.icebergconnection_sql(e),
|
|
80
|
+
OdpsCreate: lambda self, e: self.create_sql(e),
|
|
81
|
+
PartitionFields: lambda self, e: self.partitionfields_sql(e),
|
|
82
|
+
PartitionProperties: lambda self, e: self.partitionproperties_sql(e),
|
|
83
|
+
RebuildIndex: lambda self, e: self.rebuildindex_sql(e),
|
|
84
|
+
SnapshotReference: lambda self, e: self.snapshotreference_sql(e),
|
|
85
|
+
TableOptions: lambda self, e: self.tableoptions_sql(e),
|
|
86
|
+
TableShards: lambda self, e: self.tableshards_sql(e),
|
|
87
|
+
VariableDeclaration: lambda self, e: self.variabledeclaration_sql(e),
|
|
88
|
+
ViewResources: lambda self, e: self.viewresources_sql(e),
|
|
44
89
|
exp.Select: preprocess(
|
|
45
90
|
[
|
|
46
91
|
eliminate_distinct_on,
|
|
@@ -191,9 +236,41 @@ class OdpsGenerator(HiveGenerator):
|
|
|
191
236
|
return f"PARTITIONED BY {self.sql(inner)}"
|
|
192
237
|
|
|
193
238
|
def block_sql(self, expression: exp.Block) -> str:
|
|
194
|
-
|
|
239
|
+
return self.sep().join(
|
|
240
|
+
self._terminated_sql(statement) for statement in expression.expressions
|
|
241
|
+
)
|
|
242
|
+
|
|
243
|
+
def _terminated_sql(self, statement: exp.Expr) -> str:
|
|
244
|
+
sql = self.sql(statement).rstrip()
|
|
195
245
|
return sql if not sql or sql.endswith(";") else f"{sql};"
|
|
196
246
|
|
|
247
|
+
def compoundstatement_sql(self, expression: CompoundStatement) -> str:
|
|
248
|
+
body = self.block_sql(expression)
|
|
249
|
+
return f"BEGIN{self.sep()}{body}{self.sep()}END" if body else "BEGIN END"
|
|
250
|
+
|
|
251
|
+
def ifblock_sql(self, expression: exp.IfBlock) -> str:
|
|
252
|
+
def branch_sql(branch):
|
|
253
|
+
if isinstance(branch, (CompoundStatement, exp.IfBlock)):
|
|
254
|
+
return self.sql(branch)
|
|
255
|
+
return self._terminated_sql(branch)
|
|
256
|
+
|
|
257
|
+
sql = f"IF {self.sql(expression, 'this')} {branch_sql(expression.args['true'])}"
|
|
258
|
+
if false := expression.args.get("false"):
|
|
259
|
+
sql += f" ELSE {branch_sql(false)}"
|
|
260
|
+
return sql
|
|
261
|
+
|
|
262
|
+
def variabledeclaration_sql(self, expression: VariableDeclaration) -> str:
|
|
263
|
+
sql = self.sql(expression, "this")
|
|
264
|
+
if kind := self.sql(expression, "kind"):
|
|
265
|
+
sql += f" {kind}"
|
|
266
|
+
if value := self.sql(expression, "expression"):
|
|
267
|
+
sql += f" := {value}"
|
|
268
|
+
if default := self.sql(expression, "default"):
|
|
269
|
+
sql += f" = {default}"
|
|
270
|
+
if comment := self.sql(expression, "comment"):
|
|
271
|
+
sql += f" COMMENT {comment}"
|
|
272
|
+
return sql
|
|
273
|
+
|
|
197
274
|
def select_sql(self, expression: exp.Select) -> str:
|
|
198
275
|
if not expression.args.get("read"):
|
|
199
276
|
return super().select_sql(expression)
|
|
@@ -225,6 +302,24 @@ class OdpsGenerator(HiveGenerator):
|
|
|
225
302
|
return super().propertyeq_sql(expression)
|
|
226
303
|
|
|
227
304
|
def create_sql(self, expression: exp.Create) -> str:
|
|
305
|
+
if expression.args.get("native_definition"):
|
|
306
|
+
return self._definition_sql(expression)
|
|
307
|
+
if expression.kind == "MODEL" and expression.args.get("model_version"):
|
|
308
|
+
exists = " IF NOT EXISTS" if expression.args.get("exists") else ""
|
|
309
|
+
name = self.sql(expression, "this")
|
|
310
|
+
version = self.sql(expression, "model_version")
|
|
311
|
+
sql = f"CREATE MODEL{exists} {name} WITH VERSION {version}"
|
|
312
|
+
if schema := self.sql(expression, "model_input"):
|
|
313
|
+
sql += f" INPUT {schema}"
|
|
314
|
+
if props := expression.args.get("properties"):
|
|
315
|
+
sql += " " + " ".join(
|
|
316
|
+
self._model_property_sql(prop) for prop in props.expressions
|
|
317
|
+
)
|
|
318
|
+
return sql
|
|
319
|
+
if expression.kind == "TASK":
|
|
320
|
+
return self._create_task_sql(expression)
|
|
321
|
+
if expression.kind in {"BLOOMFILTER INDEX", "BITMAP INDEX"}:
|
|
322
|
+
return self._create_typed_index_sql(expression)
|
|
228
323
|
sql = super().create_sql(expression)
|
|
229
324
|
if resources := expression.args.get("resources"):
|
|
230
325
|
resources_sql = ", ".join(
|
|
@@ -234,6 +329,138 @@ class OdpsGenerator(HiveGenerator):
|
|
|
234
329
|
return f"{sql} USING {resources_sql}"
|
|
235
330
|
return sql
|
|
236
331
|
|
|
332
|
+
def _definition_sql(self, expression):
|
|
333
|
+
replace = " OR REPLACE" if expression.args.get("replace") else ""
|
|
334
|
+
secure = " SECURE" if expression.args.get("secure") else ""
|
|
335
|
+
kind = "SQL FUNCTION" if expression.kind == "FUNCTION" else "VIEW"
|
|
336
|
+
exists = " IF NOT EXISTS" if expression.args.get("exists") else ""
|
|
337
|
+
sql = f"CREATE{replace}{secure} {kind}{exists} {self.sql(expression, 'this')}"
|
|
338
|
+
if returns := self.sql(expression, "return_variable"):
|
|
339
|
+
sql += f" RETURNS {returns}"
|
|
340
|
+
if properties := self.sql(expression, "properties"):
|
|
341
|
+
sql += f" {properties}"
|
|
342
|
+
return f"{sql} AS {self.sql(expression, 'expression')}"
|
|
343
|
+
|
|
344
|
+
def _model_property_sql(self, prop):
|
|
345
|
+
if type(prop) is exp.Property and isinstance(prop.this, exp.Var):
|
|
346
|
+
return f"{prop.name} {self.sql(prop, 'value')}"
|
|
347
|
+
return self.sql(prop)
|
|
348
|
+
|
|
349
|
+
def _create_task_sql(self, expression):
|
|
350
|
+
replace = " OR REPLACE" if expression.args.get("replace") else ""
|
|
351
|
+
exists = " IF NOT EXISTS" if expression.args.get("exists") else ""
|
|
352
|
+
name = self.sql(expression, "this")
|
|
353
|
+
schedule = self.sql(expression, "schedule")
|
|
354
|
+
sql = f"CREATE{replace} TASK{exists} {name} SCHEDULE = {schedule}"
|
|
355
|
+
if properties := self.expressions(expression, key="task_properties", flat=True):
|
|
356
|
+
sql += f" TASKPROPERTIES ({properties})"
|
|
357
|
+
if comment := self.sql(expression, "task_comment"):
|
|
358
|
+
sql += f" COMMENT {comment}"
|
|
359
|
+
if condition := self.sql(expression, "task_condition"):
|
|
360
|
+
sql += f" WHEN {condition}"
|
|
361
|
+
return f"{sql} AS {self.sql(expression, 'expression')}"
|
|
362
|
+
|
|
363
|
+
def _create_typed_index_sql(self, expression):
|
|
364
|
+
index = expression.this
|
|
365
|
+
name = self.sql(index, "this")
|
|
366
|
+
table = self.sql(index, "table")
|
|
367
|
+
columns = self.expressions(index.args["params"], key="columns", flat=True)
|
|
368
|
+
sql = (
|
|
369
|
+
f"CREATE {expression.kind} {name} ON TABLE {table} FOR COLUMNS ({columns})"
|
|
370
|
+
)
|
|
371
|
+
if properties := self.expressions(
|
|
372
|
+
expression, key="index_properties", flat=True
|
|
373
|
+
):
|
|
374
|
+
sql += f" IDXPROPERTIES ({properties})"
|
|
375
|
+
if properties := self.sql(expression, "properties"):
|
|
376
|
+
sql += f" {properties}"
|
|
377
|
+
return sql
|
|
378
|
+
|
|
379
|
+
def icebergconnection_sql(self, expression: IcebergConnection) -> str:
|
|
380
|
+
return f"WITH CONNECTION {self.sql(expression, 'this')}"
|
|
381
|
+
|
|
382
|
+
def tableoptions_sql(self, expression: TableOptions) -> str:
|
|
383
|
+
kind = expression.args.get("kind") or "OPTIONS"
|
|
384
|
+
return f"{kind} ({self.expressions(expression, flat=True)})"
|
|
385
|
+
|
|
386
|
+
def dropoptions_sql(self, expression: DropOptions) -> str:
|
|
387
|
+
return f"DROP OPTIONS ({self.expressions(expression, flat=True)})"
|
|
388
|
+
|
|
389
|
+
def externaltableresource_sql(self, expression: ExternalTableResource) -> str:
|
|
390
|
+
return f"USING {self.sql(expression, 'this')}"
|
|
391
|
+
|
|
392
|
+
def viewresources_sql(self, expression: ViewResources) -> str:
|
|
393
|
+
return f"RESOURCES {self.expressions(expression, flat=True)}"
|
|
394
|
+
|
|
395
|
+
def tableshards_sql(self, expression: TableShards) -> str:
|
|
396
|
+
sql = f"INTO {self.sql(expression, 'this')} SHARDS"
|
|
397
|
+
if lifecycle := self.sql(expression, "lifecycle"):
|
|
398
|
+
sql += f" HUBLIFECYCLE {lifecycle}"
|
|
399
|
+
return sql
|
|
400
|
+
|
|
401
|
+
def partitionproperties_sql(self, expression: PartitionProperties) -> str:
|
|
402
|
+
return f"PARTITIONPROPERTIES ({self.expressions(expression, flat=True)})"
|
|
403
|
+
|
|
404
|
+
def partitionfields_sql(self, expression: PartitionFields) -> str:
|
|
405
|
+
fields = self.expressions(expression, flat=True)
|
|
406
|
+
sql = f"{expression.args['kind']} PARTITION FIELDS {fields}"
|
|
407
|
+
if replacement := expression.args.get("replacement"):
|
|
408
|
+
sql += f" WITH {self.expressions(replacement, flat=True)}"
|
|
409
|
+
return sql
|
|
410
|
+
|
|
411
|
+
def changecolumns_sql(self, expression: ChangeColumns) -> str:
|
|
412
|
+
columns = ", ".join(
|
|
413
|
+
self._changed_column_sql(column) for column in expression.expressions
|
|
414
|
+
)
|
|
415
|
+
return f"CHANGE COLUMNS ({columns})"
|
|
416
|
+
|
|
417
|
+
def _changed_column_sql(self, expression: exp.AlterColumn) -> str:
|
|
418
|
+
old = self.sql(expression, "this")
|
|
419
|
+
new = self.sql(expression, "rename_to")
|
|
420
|
+
dtype = self.sql(expression, "dtype")
|
|
421
|
+
comment = self.sql(expression, "comment")
|
|
422
|
+
sql = f"{old} {new} {dtype}"
|
|
423
|
+
return f"{sql} COMMENT {comment}" if comment else sql
|
|
424
|
+
|
|
425
|
+
def rebuildindex_sql(self, expression: RebuildIndex) -> str:
|
|
426
|
+
kind = self.sql(expression, "kind")
|
|
427
|
+
kind = f" {kind}" if kind else ""
|
|
428
|
+
name = self.sql(expression, "this")
|
|
429
|
+
name = f" {name}" if name else ""
|
|
430
|
+
auto = " AUTO" if expression.args.get("auto") else ""
|
|
431
|
+
return f"REBUILD{kind} INDEX{name}{auto}"
|
|
432
|
+
|
|
433
|
+
def snapshotreference_sql(self, expression: SnapshotReference) -> str:
|
|
434
|
+
action = expression.args["action"]
|
|
435
|
+
kind = expression.args["kind"]
|
|
436
|
+
exists = ""
|
|
437
|
+
if expression.args.get("exists"):
|
|
438
|
+
exists = " IF EXISTS" if action == "DROP" else " IF NOT EXISTS"
|
|
439
|
+
sql = f"{action} {kind}{exists} {self.sql(expression, 'this')}"
|
|
440
|
+
if version := self.sql(expression, "version"):
|
|
441
|
+
sql += f" AS OF VERSION {version}"
|
|
442
|
+
if retain_days := self.sql(expression, "retain_days"):
|
|
443
|
+
sql += f" RETAIN {retain_days} DAYS"
|
|
444
|
+
min_snapshots = self.sql(expression, "min_snapshots")
|
|
445
|
+
snapshot_days = self.sql(expression, "snapshot_days")
|
|
446
|
+
if min_snapshots or snapshot_days:
|
|
447
|
+
sql += " WITH SNAPSHOT RETENTION"
|
|
448
|
+
if min_snapshots:
|
|
449
|
+
sql += f" {min_snapshots} SNAPSHOTS"
|
|
450
|
+
if snapshot_days:
|
|
451
|
+
sql += f" {snapshot_days} DAYS"
|
|
452
|
+
return sql
|
|
453
|
+
|
|
454
|
+
def computedcolumnconstraint_sql(
|
|
455
|
+
self, expression: exp.ComputedColumnConstraint
|
|
456
|
+
) -> str:
|
|
457
|
+
if expression.args.get("persisted"):
|
|
458
|
+
this = self.sql(expression, "this")
|
|
459
|
+
async_sql = " ASYNC" if expression.args.get("async") else ""
|
|
460
|
+
not_null = " NOT NULL" if expression.args.get("not_null") else ""
|
|
461
|
+
return f"GENERATED ALWAYS AS ({this}) STORED{async_sql}{not_null}"
|
|
462
|
+
return super().computedcolumnconstraint_sql(expression)
|
|
463
|
+
|
|
237
464
|
def version_sql(self, expression: exp.Version) -> str:
|
|
238
465
|
if expression.args.get("kind") == "BETWEEN":
|
|
239
466
|
start, end = expression.expression.expressions
|
|
@@ -266,6 +493,12 @@ class OdpsGenerator(HiveGenerator):
|
|
|
266
493
|
def describe_sql(self, expression: exp.Describe) -> str:
|
|
267
494
|
if expression.args.get("kind") == "EXPLAIN":
|
|
268
495
|
return f"EXPLAIN {self.sql(expression, 'this')}"
|
|
496
|
+
if expression.args.get("kind") == "MODEL":
|
|
497
|
+
extended = " EXTENDED" if expression.args.get("style") else ""
|
|
498
|
+
sql = f"DESCRIBE MODEL{extended} {self.sql(expression, 'this')}"
|
|
499
|
+
if version := self.sql(expression, "version"):
|
|
500
|
+
sql += f" WITH VERSION {version}"
|
|
501
|
+
return sql
|
|
269
502
|
if expression.args.get("kind"):
|
|
270
503
|
kind = self.sql(expression, "kind").upper()
|
|
271
504
|
return (
|
|
@@ -280,6 +513,22 @@ class OdpsGenerator(HiveGenerator):
|
|
|
280
513
|
return f"@{self.sql(expression, 'this')}"
|
|
281
514
|
return super().parameter_sql(expression)
|
|
282
515
|
|
|
516
|
+
def anonymous_sql(self, expression: exp.Anonymous) -> str:
|
|
517
|
+
if qualifiers := expression.args.get("qualifiers"):
|
|
518
|
+
name = ":".join(self.sql(part) for part in qualifiers)
|
|
519
|
+
function = self.func(
|
|
520
|
+
self.sql(expression, "this"), *expression.expressions, normalize=False
|
|
521
|
+
)
|
|
522
|
+
return f"{name}:{function}"
|
|
523
|
+
sql = super().anonymous_sql(expression)
|
|
524
|
+
return f"::{sql}" if expression.args.get("builtin") else sql
|
|
525
|
+
|
|
526
|
+
def drop_sql(self, expression: exp.Drop) -> str:
|
|
527
|
+
if expression.kind == "MODEL" and expression.this is not None:
|
|
528
|
+
exists = " IF EXISTS" if expression.args.get("exists") else ""
|
|
529
|
+
return f"DROP MODEL{exists} {self.sql(expression, 'this')}"
|
|
530
|
+
return super().drop_sql(expression)
|
|
531
|
+
|
|
283
532
|
def struct_sql(self, expression: exp.Struct) -> str:
|
|
284
533
|
values = [
|
|
285
534
|
f"{self.sql(value, 'expression')} AS {self.sql(value, 'this')}"
|
|
@@ -352,14 +601,6 @@ class OdpsGenerator(HiveGenerator):
|
|
|
352
601
|
buckets = f" INTO {buckets} BUCKETS" if buckets else ""
|
|
353
602
|
return f"{kind}{auto}{columns}{sorted_by}{buckets}"
|
|
354
603
|
|
|
355
|
-
def datatype_sql(self, expression: exp.DataType) -> str:
|
|
356
|
-
# VARCHAR and CHAR map to STRING in MaxCompute, with no length parameters
|
|
357
|
-
if expression.this in (exp.DType.VARCHAR, exp.DType.CHAR):
|
|
358
|
-
return self.TYPE_MAPPING.get(
|
|
359
|
-
expression.this, super().datatype_sql(expression)
|
|
360
|
-
)
|
|
361
|
-
return super().datatype_sql(expression)
|
|
362
|
-
|
|
363
604
|
def properties_sql(self, expression: exp.Properties) -> str:
|
|
364
605
|
# Var-keyed exp.Property instances (e.g. LIFECYCLE 30) render as bare
|
|
365
606
|
# KEY value after the schema. String-keyed ones stay in TBLPROPERTIES.
|