sqlglotc 30.33.0__tar.gz → 30.35.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.
Files changed (117) hide show
  1. {sqlglotc-30.33.0/sqlglotc.egg-info → sqlglotc-30.35.0}/PKG-INFO +2 -2
  2. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/anonymize.py +12 -1
  3. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/expressions/array.py +13 -0
  4. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/expressions/constraints.py +2 -2
  5. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/expressions/core.py +33 -7
  6. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/expressions/datatypes.py +1 -0
  7. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/expressions/ddl.py +4 -0
  8. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/expressions/dml.py +2 -0
  9. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/expressions/query.py +5 -1
  10. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/expressions/string.py +5 -1
  11. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/expressions/temporal.py +1 -1
  12. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/generator.py +73 -24
  13. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/generators/athena.py +1 -0
  14. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/generators/bigquery.py +142 -6
  15. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/generators/clickhouse.py +26 -1
  16. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/generators/databricks.py +3 -3
  17. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/generators/duckdb.py +52 -24
  18. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/generators/exasol.py +1 -0
  19. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/generators/hive.py +1 -2
  20. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/generators/materialize.py +5 -0
  21. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/generators/postgres.py +68 -3
  22. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/generators/presto.py +24 -0
  23. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/generators/redshift.py +4 -0
  24. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/generators/snowflake.py +9 -3
  25. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/generators/spark.py +2 -0
  26. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/generators/spark2.py +10 -3
  27. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/generators/sqlite.py +155 -38
  28. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/generators/teradata.py +8 -0
  29. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/generators/trino.py +1 -0
  30. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/generators/tsql.py +1 -0
  31. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/helper.py +18 -0
  32. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/optimizer/annotate_types.py +34 -19
  33. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/optimizer/qualify_columns.py +126 -4
  34. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/optimizer/scope.py +17 -2
  35. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/optimizer/simplify.py +91 -45
  36. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/parser.py +149 -57
  37. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/parsers/athena.py +1 -0
  38. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/parsers/bigquery.py +22 -14
  39. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/parsers/databricks.py +17 -10
  40. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/parsers/drill.py +0 -2
  41. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/parsers/duckdb.py +12 -7
  42. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/parsers/hive.py +3 -1
  43. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/parsers/materialize.py +2 -0
  44. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/parsers/mysql.py +1 -0
  45. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/parsers/postgres.py +9 -7
  46. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/parsers/redshift.py +10 -1
  47. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/parsers/snowflake.py +27 -2
  48. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/parsers/sqlite.py +124 -12
  49. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/parsers/teradata.py +211 -57
  50. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/parsers/trino.py +28 -0
  51. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/tokenizer_core.py +117 -9
  52. {sqlglotc-30.33.0 → sqlglotc-30.35.0/sqlglotc.egg-info}/PKG-INFO +2 -2
  53. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglotc.egg-info/requires.txt +1 -1
  54. sqlglotc-30.35.0/sqlglotc.egg-info/scm_version.json +8 -0
  55. sqlglotc-30.33.0/sqlglotc.egg-info/scm_version.json +0 -8
  56. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/MANIFEST.in +0 -0
  57. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/pyproject.toml +0 -0
  58. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/setup.cfg +0 -0
  59. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/setup.py +0 -0
  60. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/errors.py +0 -0
  61. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/executor/table.py +0 -0
  62. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/expressions/aggregate.py +0 -0
  63. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/expressions/builders.py +0 -0
  64. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/expressions/functions.py +0 -0
  65. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/expressions/json.py +0 -0
  66. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/expressions/math.py +0 -0
  67. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/expressions/properties.py +0 -0
  68. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/generators/dax.py +0 -0
  69. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/generators/doris.py +0 -0
  70. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/generators/dremio.py +0 -0
  71. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/generators/drill.py +0 -0
  72. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/generators/druid.py +0 -0
  73. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/generators/dune.py +0 -0
  74. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/generators/fabric.py +0 -0
  75. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/generators/mysql.py +0 -0
  76. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/generators/oracle.py +0 -0
  77. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/generators/prql.py +0 -0
  78. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/generators/python.py +0 -0
  79. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/generators/risingwave.py +0 -0
  80. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/generators/singlestore.py +0 -0
  81. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/generators/solr.py +0 -0
  82. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/generators/starrocks.py +0 -0
  83. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/generators/tableau.py +0 -0
  84. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/lineage.py +0 -0
  85. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/optimizer/canonicalize_internal_names.py +0 -0
  86. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/optimizer/isolate_table_selects.py +0 -0
  87. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/optimizer/normalize_identifiers.py +0 -0
  88. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/optimizer/qualify.py +0 -0
  89. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/optimizer/qualify_tables.py +0 -0
  90. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/optimizer/resolver.py +0 -0
  91. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/parsers/base.py +0 -0
  92. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/parsers/clickhouse.py +0 -0
  93. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/parsers/dax.py +0 -0
  94. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/parsers/doris.py +0 -0
  95. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/parsers/dremio.py +0 -0
  96. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/parsers/druid.py +0 -0
  97. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/parsers/dune.py +0 -0
  98. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/parsers/exasol.py +0 -0
  99. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/parsers/fabric.py +0 -0
  100. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/parsers/oracle.py +0 -0
  101. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/parsers/presto.py +0 -0
  102. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/parsers/prql.py +0 -0
  103. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/parsers/risingwave.py +0 -0
  104. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/parsers/singlestore.py +0 -0
  105. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/parsers/solr.py +0 -0
  106. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/parsers/spark.py +0 -0
  107. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/parsers/spark2.py +0 -0
  108. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/parsers/starrocks.py +0 -0
  109. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/parsers/tableau.py +0 -0
  110. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/parsers/tsql.py +0 -0
  111. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/serde.py +0 -0
  112. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/time.py +0 -0
  113. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglot/trie.py +0 -0
  114. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglotc.egg-info/SOURCES.txt +0 -0
  115. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglotc.egg-info/dependency_links.txt +0 -0
  116. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglotc.egg-info/scm_file_list.json +0 -0
  117. {sqlglotc-30.33.0 → sqlglotc-30.35.0}/sqlglotc.egg-info/top_level.txt +0 -0
@@ -1,13 +1,13 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: sqlglotc
3
- Version: 30.33.0
3
+ Version: 30.35.0
4
4
  Summary: mypyc-compiled extensions for sqlglot
5
5
  Author-email: Toby Mao <toby.mao@gmail.com>
6
6
  License-Expression: MIT
7
7
  Project-URL: Homepage, https://sqlglot.com/
8
8
  Project-URL: Repository, https://github.com/tobymao/sqlglot
9
9
  Requires-Python: >=3.10
10
- Requires-Dist: sqlglot==30.33.0
10
+ Requires-Dist: sqlglot==30.35.0
11
11
  Provides-Extra: dev
12
12
  Requires-Dist: setuptools>=61.0; extra == "dev"
13
13
  Requires-Dist: setuptools_scm>=10.2; extra == "dev"
@@ -21,6 +21,7 @@ ANONYMIZED_TYPES = {
21
21
  TokenType.RAW_STRING,
22
22
  TokenType.STRING,
23
23
  TokenType.UNICODE_STRING,
24
+ TokenType.UNICODE_IDENTIFIER,
24
25
  TokenType.VAR,
25
26
  }
26
27
  QUOTED_TYPES = ANONYMIZED_TYPES - {TokenType.NUMBER, TokenType.VAR}
@@ -152,8 +153,9 @@ def render(sql: str, tokens: list[Token], dialect: DialectType = None) -> str:
152
153
 
153
154
  result = []
154
155
  prev = 0
156
+ uescape_alias = ""
155
157
 
156
- for token in tokens:
158
+ for i, token in enumerate(tokens):
157
159
  result.append(_redact(sql[prev : token.start], comments, nested))
158
160
  prev = token.end + 1
159
161
  span = sql[token.start : prev]
@@ -162,6 +164,13 @@ def render(sql: str, tokens: list[Token], dialect: DialectType = None) -> str:
162
164
  result.append(token.text)
163
165
  elif token.token_type in ANONYMIZED_TYPES:
164
166
  result.append(_fit(span, token.text, quotes, token.token_type))
167
+ elif token.token_type == TokenType.UESCAPE:
168
+ if token.text.upper() == "UESCAPE":
169
+ uescape_alias = tokens[i - 1].text
170
+ elif token.text and token.text in uescape_alias:
171
+ # Keep the alias literal when it contains the clause's escape character.
172
+ span = "'!'".ljust(len(span))
173
+ result.append(span)
165
174
  else:
166
175
  result.append(span)
167
176
 
@@ -251,6 +260,8 @@ def _fit(span: str, alias: str, quotes: list[tuple[str, str]], token_type: Token
251
260
  break
252
261
 
253
262
  width = len(span) - start - end
263
+ if token_type == TokenType.IDENTIFIER and end and len(alias) < width:
264
+ return (span[:start] + alias + span[-end:]).ljust(len(span))
254
265
  pad = "0" if token_type == TokenType.NUMBER else "a"
255
266
 
256
267
  return span[:start] + alias[:width].rjust(width, pad) + span[len(span) - end :]
@@ -397,3 +397,16 @@ class StDistance(Expression, Func):
397
397
  class StPoint(Expression, Func):
398
398
  arg_types = {"this": True, "expression": True, "z": False, "m": False}
399
399
  _sql_names = ["ST_POINT", "ST_MAKEPOINT"]
400
+
401
+
402
+ class StX(Expression, Func):
403
+ arg_types = {"this": True, "expression": False}
404
+
405
+
406
+ class StY(Expression, Func):
407
+ arg_types = {"this": True, "expression": False}
408
+
409
+
410
+ class StGeogPoint(Expression, Func):
411
+ arg_types = {"this": True, "expression": True}
412
+ _sql_names = ["ST_GEOGPOINT"]
@@ -153,7 +153,7 @@ class MaskingPolicyColumnConstraint(Expression, ColumnConstraintKind):
153
153
 
154
154
 
155
155
  class NotNullColumnConstraint(Expression, ColumnConstraintKind):
156
- arg_types = {"allow_null": False}
156
+ arg_types = {"allow_null": False, "on_conflict": False}
157
157
 
158
158
 
159
159
  class OnUpdateColumnConstraint(Expression, ColumnConstraintKind):
@@ -161,7 +161,7 @@ class OnUpdateColumnConstraint(Expression, ColumnConstraintKind):
161
161
 
162
162
 
163
163
  class PrimaryKeyColumnConstraint(Expression, ColumnConstraintKind):
164
- arg_types = {"desc": False, "options": False, "include": False}
164
+ arg_types = {"desc": False, "options": False, "include": False, "on_conflict": False}
165
165
 
166
166
 
167
167
  class TitleColumnConstraint(Expression, ColumnConstraintKind):
@@ -1405,14 +1405,20 @@ class Expression(Expr):
1405
1405
  return alias_(self, alias, quoted=quoted, dialect=dialect, copy=copy, table=table, **opts)
1406
1406
 
1407
1407
  def _binop(self, klass: Type[E], other: t.Any, reverse: bool = False) -> E:
1408
- this = self.copy()
1409
- other = convert(other, copy=True)
1410
- if not isinstance(this, klass) and not isinstance(other, klass):
1411
- this = _wrap(this, Binary)
1412
- other = _wrap(other, Binary)
1408
+ left: Expr = self.copy()
1409
+ right = convert(other, copy=True)
1413
1410
  if reverse:
1414
- return klass(this=other, expression=this)
1415
- return klass(this=this, expression=other)
1411
+ left, right = right, left
1412
+
1413
+ # NOT binds more tightly than AND and OR, but more loosely than comparisons and arithmetic.
1414
+ if not issubclass(klass, Connector):
1415
+ left = _wrap(left, Not)
1416
+ right = _wrap(right, Not)
1417
+
1418
+ # A repeated operator can chain on the left, but SQL may reject chained predicates.
1419
+ if not isinstance(left, klass) or isinstance(left, Predicate):
1420
+ left = _wrap(left, Binary)
1421
+ return klass(this=left, expression=_wrap(right, Binary))
1416
1422
 
1417
1423
  def __getitem__(self, other: ExpOrStr | tuple[ExpOrStr, ...]) -> Bracket:
1418
1424
  return Bracket(
@@ -1473,6 +1479,10 @@ class Expression(Expr):
1473
1479
  return between
1474
1480
 
1475
1481
  def is_(self, other: ExpOrStr) -> Is:
1482
+ if isinstance(other, Not) and isinstance(other.this, (Null, Boolean)):
1483
+ predicate = self._binop(Is, other.this)
1484
+ predicate.set("negate", True)
1485
+ return predicate
1476
1486
  return self._binop(Is, other)
1477
1487
 
1478
1488
  def like(self, other: ExpOrStr) -> Like:
@@ -1900,6 +1910,8 @@ class Boolean(Expression, Condition):
1900
1910
 
1901
1911
 
1902
1912
  class Dot(Expression, Binary):
1913
+ arg_types = {"this": True, "expression": True, "subcolumn_depth": False}
1914
+
1903
1915
  @property
1904
1916
  def is_star(self) -> bool:
1905
1917
  return self.expression.is_star
@@ -2336,6 +2348,20 @@ class FormatPhrase(Expression):
2336
2348
  return self.this.output_name
2337
2349
 
2338
2350
 
2351
+ class TeradataConversion(Expression):
2352
+ """A postfix type conversion with Teradata-specific conversion or formatting semantics."""
2353
+
2354
+ arg_types = {"this": True, "to": True, "format": False}
2355
+
2356
+ @property
2357
+ def name(self) -> str:
2358
+ return self.this.name
2359
+
2360
+ @property
2361
+ def output_name(self) -> str:
2362
+ return self.this.output_name
2363
+
2364
+
2339
2365
  class TeradataTitle(Expression):
2340
2366
  """A Teradata TITLE display heading, which does not rename the expression."""
2341
2367
 
@@ -74,6 +74,7 @@ class DType(AutoName):
74
74
  GEOGRAPHY = auto()
75
75
  GEOGRAPHYPOINT = auto()
76
76
  GEOMETRY = auto()
77
+ HASHTYPE = auto()
77
78
  POINT = auto()
78
79
  RING = auto()
79
80
  LINESTRING = auto()
@@ -453,3 +453,7 @@ class Execute(Expression):
453
453
 
454
454
  class ExecuteSql(Execute):
455
455
  pass
456
+
457
+
458
+ class ExecuteImmediate(Execute):
459
+ arg_types = {"this": True, "expressions": False}
@@ -305,6 +305,7 @@ class Update(Expression, DML):
305
305
  "limit": False,
306
306
  "options": False,
307
307
  "hint": False,
308
+ "alternative": False,
308
309
  }
309
310
 
310
311
  def table(
@@ -515,6 +516,7 @@ class Merge(Expression, DML):
515
516
  "whens": True,
516
517
  "with_": False,
517
518
  "returning": False,
519
+ "schema_evolution": False,
518
520
  }
519
521
 
520
522
 
@@ -850,6 +850,10 @@ class Lateral(Expression, UDTF):
850
850
  return columns
851
851
 
852
852
 
853
+ class TableArgument(Expression):
854
+ """A relation passed to a table function with the TABLE keyword."""
855
+
856
+
853
857
  class TableFromRows(Expression, UDTF):
854
858
  arg_types = {
855
859
  "this": True,
@@ -1976,7 +1980,7 @@ class Where(Expression):
1976
1980
 
1977
1981
 
1978
1982
  class Optimize(Expression):
1979
- pass
1983
+ arg_types = {"this": True, "full": False, "where": False, "zorder": False}
1980
1984
 
1981
1985
 
1982
1986
  class Analyze(Expression):
@@ -45,6 +45,10 @@ class Elt(Expression, Func):
45
45
  is_var_len_args = True
46
46
 
47
47
 
48
+ class FindInSet(Expression, Func):
49
+ arg_types = {"this": True, "expression": True}
50
+
51
+
48
52
  class EndsWith(Expression, Func):
49
53
  _sql_names = ["ENDS_WITH", "ENDSWITH"]
50
54
  arg_types = {"this": True, "expression": True}
@@ -70,7 +74,7 @@ class Left(Expression, Func):
70
74
 
71
75
 
72
76
  class Length(Expression, Func):
73
- arg_types = {"this": True, "binary": False, "encoding": False}
77
+ arg_types = {"this": True, "binary": False, "encoding": False, "null_terminated": False}
74
78
  _sql_names = ["LENGTH", "LEN", "CHAR_LENGTH", "CHARACTER_LENGTH"]
75
79
 
76
80
 
@@ -239,7 +239,7 @@ class Dayname(Expression, Func):
239
239
 
240
240
 
241
241
  class Extract(Expression, Func):
242
- arg_types = {"this": True, "expression": True}
242
+ arg_types = {"this": True, "expression": True, "date_part": False}
243
243
 
244
244
 
245
245
  class GetExtract(Expression, Func):
@@ -332,6 +332,9 @@ class Generator:
332
332
  # Whether MERGE ... WHEN MATCHED/NOT MATCHED THEN UPDATE/INSERT ... WHERE is supported
333
333
  SUPPORTS_MERGE_WHERE = False
334
334
 
335
+ # First version supporting MERGE WITH SCHEMA EVOLUTION
336
+ MERGE_SCHEMA_EVOLUTION_MIN_VERSION: t.ClassVar[tuple[int, ...] | None] = None
337
+
335
338
  # Whether MERGE uses ALL BY NAME instead of a star to update or insert all columns
336
339
  MERGE_ALL_BY_NAME = False
337
340
 
@@ -500,6 +503,9 @@ class Generator:
500
503
  # Whether the SELECT .. INTO syntax is used instead of CTAS
501
504
  SUPPORTS_SELECT_INTO = False
502
505
 
506
+ SUPPORTS_OPTIMIZE = False
507
+ SUPPORTS_EXECUTE_IMMEDIATE = True
508
+
503
509
  # Whether UNLOGGED tables can be created
504
510
  SUPPORTS_UNLOGGED_TABLES = False
505
511
 
@@ -618,6 +624,7 @@ class Generator:
618
624
  ALTER_SET_WRAPPED = False
619
625
 
620
626
  # Whether to normalize the date parts in EXTRACT(<date_part> FROM <expr>) into a common representation
627
+ # using the dialect's general aliases as a fallback for parts without an EXTRACT-specific mapping.
621
628
  # For instance, to extract the day of week in ISO semantics, one can use ISODOW, DAYOFWEEKISO etc depending on the dialect.
622
629
  # TODO: The normalization should be done by default once we've tested it across all dialects.
623
630
  NORMALIZE_EXTRACT_DATE_PARTS = False
@@ -1026,7 +1033,9 @@ class Generator:
1026
1033
  expression = self.preprocess(expression)
1027
1034
 
1028
1035
  self.unsupported_messages = []
1029
- sql = self.sql(expression).strip()
1036
+ sql = self.sql(expression)
1037
+ # A trailing newline in a raw command can terminate a line comment.
1038
+ sql = sql.lstrip() if isinstance(expression, exp.Command) else sql.strip()
1030
1039
 
1031
1040
  if self.pretty:
1032
1041
  sql = sql.replace(self.SENTINEL_LINE_BREAK, "\n")
@@ -1353,16 +1362,20 @@ class Generator:
1353
1362
  return f"PERIOD FOR SYSTEM_TIME ({self.sql(expression, 'this')}, {self.sql(expression, 'expression')})"
1354
1363
 
1355
1364
  def notnullcolumnconstraint_sql(self, expression: exp.NotNullColumnConstraint) -> str:
1356
- return f"{'' if expression.args.get('allow_null') else 'NOT '}NULL"
1365
+ on_conflict = self.sql(expression, "on_conflict")
1366
+ on_conflict = f" {on_conflict}" if on_conflict else ""
1367
+ return f"{'' if expression.args.get('allow_null') else 'NOT '}NULL{on_conflict}"
1357
1368
 
1358
1369
  def primarykeycolumnconstraint_sql(self, expression: exp.PrimaryKeyColumnConstraint) -> str:
1370
+ on_conflict = self.sql(expression, "on_conflict")
1371
+ on_conflict = f" {on_conflict}" if on_conflict else ""
1359
1372
  desc = expression.args.get("desc")
1360
1373
  if desc is not None:
1361
- return f"PRIMARY KEY{' DESC' if desc else ' ASC'}"
1374
+ return f"PRIMARY KEY{' DESC' if desc else ' ASC'}{on_conflict}"
1362
1375
  options = self.expressions(expression, key="options", flat=True, sep=" ")
1363
1376
  options = f" {options}" if options else ""
1364
1377
  include = self.sql(expression, "include")
1365
- return f"PRIMARY KEY{include}{options}"
1378
+ return f"PRIMARY KEY{on_conflict}{include}{options}"
1366
1379
 
1367
1380
  def uniquecolumnconstraint_sql(self, expression: exp.UniqueColumnConstraint) -> str:
1368
1381
  this = self.sql(expression, "this")
@@ -2063,13 +2076,7 @@ class Generator:
2063
2076
  and node.args.get("limit")
2064
2077
  ):
2065
2078
  node = node.subquery(copy=False)
2066
- if not self.SET_OP_PARENTHESIZED_OPERANDS:
2067
- node = exp.select("*").from_(node, copy=False)
2068
- elif (
2069
- not self.SET_OP_PARENTHESIZED_OPERANDS
2070
- and isinstance(node, exp.Subquery)
2071
- and isinstance(node.unnest(), exp.SetOperation)
2072
- ):
2079
+ if not self.SET_OP_PARENTHESIZED_OPERANDS and isinstance(node, exp.Subquery):
2073
2080
  node = exp.select("*").from_(node, copy=False)
2074
2081
  sqls.append(open_parens + self.indent(self.sql(node), level=depth, pad=0))
2075
2082
  open_parens = ""
@@ -2658,6 +2665,10 @@ class Generator:
2658
2665
 
2659
2666
  return f"{only}{table}{changes}{partition}{file_format}{pre_alias}{alias}{indexed}{hints}{pivots}{post_alias}{joins}{laterals}{ordinality}"
2660
2667
 
2668
+ def tableargument_sql(self, expression: exp.TableArgument) -> str:
2669
+ table = expression.this
2670
+ return f"TABLE {self.maybe_comment(self.table_parts(table), table)}"
2671
+
2661
2672
  def tablefromrows_sql(self, expression: exp.TableFromRows) -> str:
2662
2673
  table = self.func("TABLE", expression.this)
2663
2674
  alias = self.sql(expression, "alias")
@@ -2874,6 +2885,13 @@ class Generator:
2874
2885
 
2875
2886
  def update_sql(self, expression: exp.Update) -> str:
2876
2887
  hint = self.sql(expression, "hint")
2888
+ alternative = self.sql(expression, "alternative")
2889
+ if alternative:
2890
+ if self.dialect.SUPPORTS_UPDATE_OR or self.dialect == "":
2891
+ alternative = f" OR {alternative}"
2892
+ else:
2893
+ self.unsupported(f"UPDATE OR {alternative} is not supported")
2894
+ alternative = ""
2877
2895
  this = self.sql(expression, "this")
2878
2896
  join_sql, from_sql = self._update_from_joins_sql(expression)
2879
2897
  set_sql = self.expressions(expression, flat=True)
@@ -2887,7 +2905,7 @@ class Generator:
2887
2905
  expression_sql = f"{returning}{from_sql}{where_sql}"
2888
2906
  options = self.expressions(expression, key="options")
2889
2907
  options = f" OPTION({options})" if options else ""
2890
- sql = f"UPDATE{hint} {this}{join_sql} SET {set_sql}{expression_sql}{order}{limit}{options}"
2908
+ sql = f"UPDATE{hint}{alternative} {this}{join_sql} SET {set_sql}{expression_sql}{order}{limit}{options}"
2891
2909
  return self.prepend_ctes(expression, sql)
2892
2910
 
2893
2911
  def values_sql(self, expression: exp.Values, values_as_table: bool = True) -> str:
@@ -3937,14 +3955,18 @@ class Generator:
3937
3955
  order = f" OVER ({self.order_sql(order, flat=True)})" if order else ""
3938
3956
  return f"NEXT VALUE FOR {self.sql(expression, 'this')}{order}"
3939
3957
 
3940
- def extract_sql(self, expression: exp.Extract) -> str:
3941
- import sqlglot.dialects.dialect
3958
+ def extract_date_part(self, part: exp.Expr) -> exp.Expr:
3959
+ mapping = self.dialect.EXTRACT_DATE_PART_MAPPING
3960
+ if self.NORMALIZE_EXTRACT_DATE_PARTS and part.name.upper() not in mapping:
3961
+ mapping = self.dialect.DATE_PART_MAPPING
3962
+ if mapping:
3963
+ import sqlglot.dialects.dialect
3942
3964
 
3943
- this = (
3944
- sqlglot.dialects.dialect.map_date_part(expression.this, self.dialect)
3945
- if self.NORMALIZE_EXTRACT_DATE_PARTS
3946
- else expression.this
3947
- )
3965
+ return sqlglot.dialects.dialect.map_date_part(part, mapping=mapping)
3966
+ return part
3967
+
3968
+ def extract_sql(self, expression: exp.Extract) -> str:
3969
+ this = self.extract_date_part(expression.this)
3948
3970
  if self.EXTRACT_ALLOWS_QUOTES:
3949
3971
  this_sql = self.sql(this)
3950
3972
  elif isinstance(this, exp.WeekStart):
@@ -4138,6 +4160,10 @@ class Generator:
4138
4160
  fmt = self.sql(expression, "format")
4139
4161
  return f"{this} (FORMAT {fmt})"
4140
4162
 
4163
+ def teradataconversion_sql(self, expression: exp.TeradataConversion) -> str:
4164
+ self.unsupported("Teradata postfix conversion semantics are not supported")
4165
+ return self.sql(exp.cast(expression.this, expression.args["to"]))
4166
+
4141
4167
  def teradatatitle_sql(self, expression: exp.TeradataTitle) -> str:
4142
4168
  self.unsupported("Teradata TITLE display headings are not supported")
4143
4169
  return self.sql(expression, "this")
@@ -4484,11 +4510,17 @@ class Generator:
4484
4510
  return self.binary(expression, "COLLATE")
4485
4511
 
4486
4512
  def command_sql(self, expression: exp.Command) -> str:
4487
- return f"{self.sql(expression, 'this')} {expression.text('expression').strip()}"
4513
+ args = expression.text("expression").lstrip()
4514
+ return f"{self.sql(expression, 'this')} {args}".rstrip(" \t")
4488
4515
 
4489
4516
  def optimize_sql(self, expression: exp.Optimize) -> str:
4490
- self.unsupported("OPTIMIZE is only supported in Databricks")
4491
- return f"OPTIMIZE {self.sql(expression, 'this')}"
4517
+ if not self.SUPPORTS_OPTIMIZE:
4518
+ self.unsupported("OPTIMIZE is only supported in Databricks")
4519
+ full = " FULL" if expression.args.get("full") else ""
4520
+ where = self.sql(expression, "where")
4521
+ columns = self.expressions(expression, key="zorder", flat=True)
4522
+ zorder = self.seg(f"ZORDER BY ({columns})") if columns else ""
4523
+ return f"OPTIMIZE {self.sql(expression, 'this')}{full}{where}{zorder}"
4492
4524
 
4493
4525
  def comment_sql(self, expression: exp.Comment) -> str:
4494
4526
  this = self.sql(expression, "this")
@@ -5235,6 +5267,14 @@ class Generator:
5235
5267
  return self.expressions(expression, sep=" ", indent=False)
5236
5268
 
5237
5269
  def merge_sql(self, expression: exp.Merge) -> str:
5270
+ schema_evolution = ""
5271
+ if expression.args.get("schema_evolution"):
5272
+ min_version = self.MERGE_SCHEMA_EVOLUTION_MIN_VERSION
5273
+ if min_version and self.dialect.version >= min_version:
5274
+ schema_evolution = " WITH SCHEMA EVOLUTION"
5275
+ else:
5276
+ self.unsupported("MERGE WITH SCHEMA EVOLUTION is not supported")
5277
+
5238
5278
  table = expression.this
5239
5279
  table_alias = ""
5240
5280
 
@@ -5262,7 +5302,7 @@ class Generator:
5262
5302
 
5263
5303
  return self.prepend_ctes(
5264
5304
  expression,
5265
- f"MERGE INTO {this}{table_alias}{sep}{using}{sep}{on}{sep}{whens}",
5305
+ f"MERGE{schema_evolution} INTO {this}{table_alias}{sep}{using}{sep}{on}{sep}{whens}",
5266
5306
  )
5267
5307
 
5268
5308
  @unsupported_args("format")
@@ -5940,7 +5980,8 @@ class Generator:
5940
5980
 
5941
5981
  def gapfill_sql(self, expression: exp.GapFill) -> str:
5942
5982
  this = self.sql(expression, "this")
5943
- this = f"TABLE {this}"
5983
+ if isinstance(expression.this, exp.Table):
5984
+ this = f"TABLE {this}"
5944
5985
  return self.func("GAP_FILL", this, *[v for k, v in expression.args.items() if k != "this"])
5945
5986
 
5946
5987
  def scope_resolution(self, rhs: str, scope_name: str) -> str:
@@ -6857,6 +6898,14 @@ class Generator:
6857
6898
  self.unsupported("Unsupported Execute syntax")
6858
6899
  return ""
6859
6900
 
6901
+ def executeimmediate_sql(self, expression: exp.ExecuteImmediate) -> str:
6902
+ if not self.SUPPORTS_EXECUTE_IMMEDIATE:
6903
+ self.unsupported("EXECUTE IMMEDIATE is not supported")
6904
+ return ""
6905
+ using = self.expressions(expression, flat=True)
6906
+ using = f" USING {using}" if using else ""
6907
+ return f"EXECUTE IMMEDIATE {self.sql(expression, 'this')}{using}"
6908
+
6860
6909
  def altermodifysqlsecurity_sql(self, expression: exp.AlterModifySqlSecurity) -> str:
6861
6910
  props = self.expressions(expression, sep=" ")
6862
6911
  return f"MODIFY {props}"
@@ -114,6 +114,7 @@ class _HiveGenerator(HiveGenerator):
114
114
 
115
115
 
116
116
  class AthenaTrinoGenerator(TrinoGenerator):
117
+ SUPPORTS_EXECUTE_IMMEDIATE = False
117
118
  PROPERTIES_LOCATION = {
118
119
  **TrinoGenerator.PROPERTIES_LOCATION,
119
120
  exp.LocationProperty: exp.Properties.Location.POST_WITH,