sqlglotc 30.32.0__tar.gz → 30.34.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.32.0/sqlglotc.egg-info → sqlglotc-30.34.0}/PKG-INFO +2 -2
  2. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/anonymize.py +12 -1
  3. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/expressions/array.py +8 -0
  4. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/expressions/constraints.py +2 -2
  5. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/expressions/core.py +29 -7
  6. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/expressions/datatypes.py +1 -0
  7. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/expressions/dml.py +2 -0
  8. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/expressions/math.py +1 -0
  9. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/expressions/query.py +4 -0
  10. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/expressions/string.py +9 -3
  11. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/expressions/temporal.py +3 -2
  12. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/generator.py +55 -12
  13. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/generators/bigquery.py +38 -4
  14. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/generators/clickhouse.py +28 -3
  15. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/generators/databricks.py +3 -0
  16. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/generators/duckdb.py +126 -48
  17. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/generators/exasol.py +1 -0
  18. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/generators/hive.py +1 -2
  19. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/generators/materialize.py +9 -0
  20. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/generators/postgres.py +75 -3
  21. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/generators/presto.py +37 -1
  22. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/generators/redshift.py +7 -0
  23. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/generators/snowflake.py +10 -2
  24. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/generators/spark.py +14 -1
  25. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/generators/spark2.py +28 -8
  26. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/generators/sqlite.py +158 -33
  27. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/generators/teradata.py +8 -0
  28. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/generators/tsql.py +10 -3
  29. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/helper.py +18 -0
  30. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/optimizer/annotate_types.py +19 -14
  31. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/optimizer/qualify_columns.py +74 -19
  32. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/optimizer/scope.py +4 -0
  33. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/optimizer/simplify.py +91 -45
  34. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/parser.py +107 -48
  35. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/parsers/clickhouse.py +7 -2
  36. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/parsers/databricks.py +30 -0
  37. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/parsers/drill.py +0 -2
  38. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/parsers/duckdb.py +14 -7
  39. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/parsers/hive.py +3 -1
  40. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/parsers/materialize.py +2 -0
  41. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/parsers/mysql.py +1 -0
  42. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/parsers/postgres.py +9 -7
  43. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/parsers/presto.py +32 -0
  44. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/parsers/redshift.py +10 -1
  45. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/parsers/snowflake.py +29 -4
  46. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/parsers/sqlite.py +133 -12
  47. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/parsers/teradata.py +211 -57
  48. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/parsers/tsql.py +3 -3
  49. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/tokenizer_core.py +118 -9
  50. {sqlglotc-30.32.0 → sqlglotc-30.34.0/sqlglotc.egg-info}/PKG-INFO +2 -2
  51. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglotc.egg-info/requires.txt +1 -1
  52. sqlglotc-30.34.0/sqlglotc.egg-info/scm_version.json +8 -0
  53. sqlglotc-30.32.0/sqlglotc.egg-info/scm_version.json +0 -8
  54. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/MANIFEST.in +0 -0
  55. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/pyproject.toml +0 -0
  56. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/setup.cfg +0 -0
  57. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/setup.py +0 -0
  58. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/errors.py +0 -0
  59. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/executor/table.py +0 -0
  60. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/expressions/aggregate.py +0 -0
  61. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/expressions/builders.py +0 -0
  62. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/expressions/ddl.py +0 -0
  63. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/expressions/functions.py +0 -0
  64. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/expressions/json.py +0 -0
  65. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/expressions/properties.py +0 -0
  66. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/generators/athena.py +0 -0
  67. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/generators/dax.py +0 -0
  68. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/generators/doris.py +0 -0
  69. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/generators/dremio.py +0 -0
  70. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/generators/drill.py +0 -0
  71. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/generators/druid.py +0 -0
  72. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/generators/dune.py +0 -0
  73. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/generators/fabric.py +0 -0
  74. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/generators/mysql.py +0 -0
  75. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/generators/oracle.py +0 -0
  76. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/generators/prql.py +0 -0
  77. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/generators/python.py +0 -0
  78. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/generators/risingwave.py +0 -0
  79. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/generators/singlestore.py +0 -0
  80. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/generators/solr.py +0 -0
  81. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/generators/starrocks.py +0 -0
  82. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/generators/tableau.py +0 -0
  83. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/generators/trino.py +0 -0
  84. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/lineage.py +0 -0
  85. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/optimizer/canonicalize_internal_names.py +0 -0
  86. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/optimizer/isolate_table_selects.py +0 -0
  87. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/optimizer/normalize_identifiers.py +0 -0
  88. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/optimizer/qualify.py +0 -0
  89. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/optimizer/qualify_tables.py +0 -0
  90. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/optimizer/resolver.py +0 -0
  91. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/parsers/athena.py +0 -0
  92. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/parsers/base.py +0 -0
  93. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/parsers/bigquery.py +0 -0
  94. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/parsers/dax.py +0 -0
  95. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/parsers/doris.py +0 -0
  96. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/parsers/dremio.py +0 -0
  97. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/parsers/druid.py +0 -0
  98. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/parsers/dune.py +0 -0
  99. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/parsers/exasol.py +0 -0
  100. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/parsers/fabric.py +0 -0
  101. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/parsers/oracle.py +0 -0
  102. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/parsers/prql.py +0 -0
  103. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/parsers/risingwave.py +0 -0
  104. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/parsers/singlestore.py +0 -0
  105. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/parsers/solr.py +0 -0
  106. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/parsers/spark.py +0 -0
  107. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/parsers/spark2.py +0 -0
  108. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/parsers/starrocks.py +0 -0
  109. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/parsers/tableau.py +0 -0
  110. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/parsers/trino.py +0 -0
  111. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/serde.py +0 -0
  112. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/time.py +0 -0
  113. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglot/trie.py +0 -0
  114. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglotc.egg-info/SOURCES.txt +0 -0
  115. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglotc.egg-info/dependency_links.txt +0 -0
  116. {sqlglotc-30.32.0 → sqlglotc-30.34.0}/sqlglotc.egg-info/scm_file_list.json +0 -0
  117. {sqlglotc-30.32.0 → sqlglotc-30.34.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.32.0
3
+ Version: 30.34.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.32.0
10
+ Requires-Dist: sqlglot==30.34.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,11 @@ 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}
@@ -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(
@@ -1900,6 +1906,8 @@ class Boolean(Expression, Condition):
1900
1906
 
1901
1907
 
1902
1908
  class Dot(Expression, Binary):
1909
+ arg_types = {"this": True, "expression": True, "subcolumn_depth": False}
1910
+
1903
1911
  @property
1904
1912
  def is_star(self) -> bool:
1905
1913
  return self.expression.is_star
@@ -2336,6 +2344,20 @@ class FormatPhrase(Expression):
2336
2344
  return self.this.output_name
2337
2345
 
2338
2346
 
2347
+ class TeradataConversion(Expression):
2348
+ """A postfix type conversion with Teradata-specific conversion or formatting semantics."""
2349
+
2350
+ arg_types = {"this": True, "to": True, "format": False}
2351
+
2352
+ @property
2353
+ def name(self) -> str:
2354
+ return self.this.name
2355
+
2356
+ @property
2357
+ def output_name(self) -> str:
2358
+ return self.this.output_name
2359
+
2360
+
2339
2361
  class TeradataTitle(Expression):
2340
2362
  """A Teradata TITLE display heading, which does not rename the expression."""
2341
2363
 
@@ -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()
@@ -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
 
@@ -186,6 +186,7 @@ class Round(Expression, Func):
186
186
  "decimals": False,
187
187
  "truncate": False,
188
188
  "casts_non_integer_decimals": False,
189
+ "negative_decimals_are_zero": False,
189
190
  }
190
191
 
191
192
 
@@ -1975,6 +1975,10 @@ class Where(Expression):
1975
1975
  pass
1976
1976
 
1977
1977
 
1978
+ class Optimize(Expression):
1979
+ arg_types = {"this": True, "full": False, "where": False, "zorder": False}
1980
+
1981
+
1978
1982
  class Analyze(Expression):
1979
1983
  arg_types = {
1980
1984
  "kind": False,
@@ -45,13 +45,19 @@ 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}
51
55
 
52
56
 
53
57
  class Format(Expression, Func):
54
- arg_types = {"this": True, "expressions": False}
58
+ """Formats values using printf syntax, or brace syntax when is_fmt is set."""
59
+
60
+ arg_types = {"this": True, "expressions": False, "is_fmt": False}
55
61
  is_var_len_args = True
56
62
 
57
63
 
@@ -68,7 +74,7 @@ class Left(Expression, Func):
68
74
 
69
75
 
70
76
  class Length(Expression, Func):
71
- arg_types = {"this": True, "binary": False, "encoding": False}
77
+ arg_types = {"this": True, "binary": False, "encoding": False, "null_terminated": False}
72
78
  _sql_names = ["LENGTH", "LEN", "CHAR_LENGTH", "CHARACTER_LENGTH"]
73
79
 
74
80
 
@@ -253,7 +259,7 @@ class Trim(Expression, Func):
253
259
 
254
260
 
255
261
  class Unicode(Expression, Func):
256
- arg_types = {"this": True, "empty_is_zero": False}
262
+ arg_types = {"this": True, "empty_is_zero": False, "empty_is_null": False}
257
263
 
258
264
 
259
265
  class Upper(Expression, Func):
@@ -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):
@@ -504,7 +504,8 @@ class TimeStrToUnix(Expression, Func):
504
504
 
505
505
 
506
506
  class TimeToStr(Expression, Func):
507
- arg_types = {"this": True, "format": True, "culture": False, "zone": False}
507
+ # joda: parsed from Presto/Trino's FORMAT_DATETIME, which DATE_FORMAT can't always express
508
+ arg_types = {"this": True, "format": True, "culture": False, "zone": False, "joda": False}
508
509
 
509
510
 
510
511
  class TimeToTimeStr(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,8 @@ 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
+
503
508
  # Whether UNLOGGED tables can be created
504
509
  SUPPORTS_UNLOGGED_TABLES = False
505
510
 
@@ -1353,16 +1358,20 @@ class Generator:
1353
1358
  return f"PERIOD FOR SYSTEM_TIME ({self.sql(expression, 'this')}, {self.sql(expression, 'expression')})"
1354
1359
 
1355
1360
  def notnullcolumnconstraint_sql(self, expression: exp.NotNullColumnConstraint) -> str:
1356
- return f"{'' if expression.args.get('allow_null') else 'NOT '}NULL"
1361
+ on_conflict = self.sql(expression, "on_conflict")
1362
+ on_conflict = f" {on_conflict}" if on_conflict else ""
1363
+ return f"{'' if expression.args.get('allow_null') else 'NOT '}NULL{on_conflict}"
1357
1364
 
1358
1365
  def primarykeycolumnconstraint_sql(self, expression: exp.PrimaryKeyColumnConstraint) -> str:
1366
+ on_conflict = self.sql(expression, "on_conflict")
1367
+ on_conflict = f" {on_conflict}" if on_conflict else ""
1359
1368
  desc = expression.args.get("desc")
1360
1369
  if desc is not None:
1361
- return f"PRIMARY KEY{' DESC' if desc else ' ASC'}"
1370
+ return f"PRIMARY KEY{' DESC' if desc else ' ASC'}{on_conflict}"
1362
1371
  options = self.expressions(expression, key="options", flat=True, sep=" ")
1363
1372
  options = f" {options}" if options else ""
1364
1373
  include = self.sql(expression, "include")
1365
- return f"PRIMARY KEY{include}{options}"
1374
+ return f"PRIMARY KEY{on_conflict}{include}{options}"
1366
1375
 
1367
1376
  def uniquecolumnconstraint_sql(self, expression: exp.UniqueColumnConstraint) -> str:
1368
1377
  this = self.sql(expression, "this")
@@ -2063,13 +2072,7 @@ class Generator:
2063
2072
  and node.args.get("limit")
2064
2073
  ):
2065
2074
  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
- ):
2075
+ if not self.SET_OP_PARENTHESIZED_OPERANDS and isinstance(node, exp.Subquery):
2073
2076
  node = exp.select("*").from_(node, copy=False)
2074
2077
  sqls.append(open_parens + self.indent(self.sql(node), level=depth, pad=0))
2075
2078
  open_parens = ""
@@ -2874,6 +2877,13 @@ class Generator:
2874
2877
 
2875
2878
  def update_sql(self, expression: exp.Update) -> str:
2876
2879
  hint = self.sql(expression, "hint")
2880
+ alternative = self.sql(expression, "alternative")
2881
+ if alternative:
2882
+ if self.dialect.SUPPORTS_UPDATE_OR or self.dialect == "":
2883
+ alternative = f" OR {alternative}"
2884
+ else:
2885
+ self.unsupported(f"UPDATE OR {alternative} is not supported")
2886
+ alternative = ""
2877
2887
  this = self.sql(expression, "this")
2878
2888
  join_sql, from_sql = self._update_from_joins_sql(expression)
2879
2889
  set_sql = self.expressions(expression, flat=True)
@@ -2887,7 +2897,7 @@ class Generator:
2887
2897
  expression_sql = f"{returning}{from_sql}{where_sql}"
2888
2898
  options = self.expressions(expression, key="options")
2889
2899
  options = f" OPTION({options})" if options else ""
2890
- sql = f"UPDATE{hint} {this}{join_sql} SET {set_sql}{expression_sql}{order}{limit}{options}"
2900
+ sql = f"UPDATE{hint}{alternative} {this}{join_sql} SET {set_sql}{expression_sql}{order}{limit}{options}"
2891
2901
  return self.prepend_ctes(expression, sql)
2892
2902
 
2893
2903
  def values_sql(self, expression: exp.Values, values_as_table: bool = True) -> str:
@@ -4116,6 +4126,18 @@ class Generator:
4116
4126
 
4117
4127
  return escaped
4118
4128
 
4129
+ def format_sql(self, expression: exp.Format) -> str:
4130
+ if (
4131
+ expression.args.get("is_fmt")
4132
+ and expression.this.is_string
4133
+ and not expression.expressions
4134
+ and not any(c in expression.name for c in "{}")
4135
+ ):
4136
+ return self.sql(expression.this)
4137
+
4138
+ self._warn_unsupported_arg(expression, "is_fmt")
4139
+ return self.function_fallback_sql(expression)
4140
+
4119
4141
  def formatjson_sql(self, expression: exp.FormatJson) -> str:
4120
4142
  return f"{self.sql(expression, 'this')} FORMAT JSON"
4121
4143
 
@@ -4126,6 +4148,10 @@ class Generator:
4126
4148
  fmt = self.sql(expression, "format")
4127
4149
  return f"{this} (FORMAT {fmt})"
4128
4150
 
4151
+ def teradataconversion_sql(self, expression: exp.TeradataConversion) -> str:
4152
+ self.unsupported("Teradata postfix conversion semantics are not supported")
4153
+ return self.sql(exp.cast(expression.this, expression.args["to"]))
4154
+
4129
4155
  def teradatatitle_sql(self, expression: exp.TeradataTitle) -> str:
4130
4156
  self.unsupported("Teradata TITLE display headings are not supported")
4131
4157
  return self.sql(expression, "this")
@@ -4474,6 +4500,15 @@ class Generator:
4474
4500
  def command_sql(self, expression: exp.Command) -> str:
4475
4501
  return f"{self.sql(expression, 'this')} {expression.text('expression').strip()}"
4476
4502
 
4503
+ def optimize_sql(self, expression: exp.Optimize) -> str:
4504
+ if not self.SUPPORTS_OPTIMIZE:
4505
+ self.unsupported("OPTIMIZE is only supported in Databricks")
4506
+ full = " FULL" if expression.args.get("full") else ""
4507
+ where = self.sql(expression, "where")
4508
+ columns = self.expressions(expression, key="zorder", flat=True)
4509
+ zorder = self.seg(f"ZORDER BY ({columns})") if columns else ""
4510
+ return f"OPTIMIZE {self.sql(expression, 'this')}{full}{where}{zorder}"
4511
+
4477
4512
  def comment_sql(self, expression: exp.Comment) -> str:
4478
4513
  this = self.sql(expression, "this")
4479
4514
  kind = expression.args["kind"]
@@ -5219,6 +5254,14 @@ class Generator:
5219
5254
  return self.expressions(expression, sep=" ", indent=False)
5220
5255
 
5221
5256
  def merge_sql(self, expression: exp.Merge) -> str:
5257
+ schema_evolution = ""
5258
+ if expression.args.get("schema_evolution"):
5259
+ min_version = self.MERGE_SCHEMA_EVOLUTION_MIN_VERSION
5260
+ if min_version and self.dialect.version >= min_version:
5261
+ schema_evolution = " WITH SCHEMA EVOLUTION"
5262
+ else:
5263
+ self.unsupported("MERGE WITH SCHEMA EVOLUTION is not supported")
5264
+
5222
5265
  table = expression.this
5223
5266
  table_alias = ""
5224
5267
 
@@ -5246,7 +5289,7 @@ class Generator:
5246
5289
 
5247
5290
  return self.prepend_ctes(
5248
5291
  expression,
5249
- f"MERGE INTO {this}{table_alias}{sep}{using}{sep}{on}{sep}{whens}",
5292
+ f"MERGE{schema_evolution} INTO {this}{table_alias}{sep}{using}{sep}{on}{sep}{whens}",
5250
5293
  )
5251
5294
 
5252
5295
  @unsupported_args("format")
@@ -223,6 +223,10 @@ def _array_agg_fold_sql(
223
223
 
224
224
  order = array_sort_order(sort) if sort else None
225
225
 
226
+ condition = None
227
+ if isinstance(agg, exp.Filter):
228
+ condition, agg = agg.expression.this, agg.this
229
+
226
230
  # An ARRAY_AGG with its own ORDER BY or LIMIT can't take another
227
231
  if (
228
232
  not isinstance(agg, exp.ArrayAgg)
@@ -237,14 +241,35 @@ def _array_agg_fold_sql(
237
241
  value = agg.this
238
242
  if isinstance(value, exp.Distinct):
239
243
  distinct, value = True, value.expressions[0]
244
+ kept_nulls: exp.Expr | None = None
245
+ if condition is not None:
246
+ if not ignore_nulls and not agg.args.get("nulls_excluded"):
247
+ # A kept NULL contributes [NULL], a kept value contributes [], and a rejected row
248
+ # contributes no array. ARRAY_CONCAT_AGG returns NULL when no rows qualify.
249
+ nulls = exp.If(
250
+ this=value.is_(exp.null()),
251
+ true=exp.Array(expressions=[value.copy()], value_constructor=True),
252
+ false=exp.array(),
253
+ )
254
+ kept_nulls = exp.ArrayConcatAgg(
255
+ this=exp.If(this=condition.copy(), true=nulls, false=exp.null())
256
+ )
257
+ if distinct:
258
+ kept_nulls = exp.func("ARRAY_SLICE", kept_nulls, 0, 0)
259
+
260
+ # With DISTINCT, BigQuery only orders by the aggregated value, so the keys take the IF too
261
+ value = exp.If(this=condition, true=value, false=exp.null())
262
+ agg.set("nulls_excluded", None)
240
263
 
241
264
  folded: exp.Expr = exp.Distinct(expressions=[value]) if distinct else value
265
+ nulls_first = False
242
266
  if order is not None:
243
- asc, nulls_first = order
267
+ asc, placement = order
268
+ nulls_first = asc if placement is None else placement
244
269
  keys = [exp.Ordered(this=value.copy(), desc=not asc or None, nulls_first=asc)]
245
270
 
246
271
  # Aggregate ORDER BY only puts NULLs first when ascending, so other placements need a key
247
- if not ignore_nulls and nulls_first is not None and nulls_first != asc:
272
+ if not ignore_nulls and condition is None and nulls_first != asc:
248
273
  if distinct:
249
274
  self.unsupported("ARRAY_AGG(DISTINCT) only puts NULLs first when ascending")
250
275
  else:
@@ -257,13 +282,19 @@ def _array_agg_fold_sql(
257
282
  )
258
283
  folded = exp.Order(this=folded, expressions=keys)
259
284
  agg.set("this", folded)
285
+ if ignore_nulls or condition is not None:
286
+ agg = exp.func("COALESCE", exp.IgnoreNulls(this=agg), exp.array())
260
287
  if ignore_nulls:
261
288
  # All-NULL input yields [], but an empty input still yields NULL.
289
+ rows = exp.CountIf(this=condition.copy()) if condition else exp.Count(this=exp.Star())
262
290
  agg = exp.If(
263
- this=exp.Count(this=exp.Star()).eq(0),
291
+ this=rows.eq(0),
264
292
  true=exp.null(),
265
- false=exp.func("COALESCE", exp.IgnoreNulls(this=agg), exp.array()),
293
+ false=agg,
266
294
  )
295
+ elif kept_nulls is not None:
296
+ arrays = (kept_nulls, agg) if nulls_first else (agg, kept_nulls)
297
+ return self.func("ARRAY_CONCAT", *arrays)
267
298
  return self.sql(agg)
268
299
 
269
300
 
@@ -426,6 +457,9 @@ class BigQueryGenerator(generator.Generator):
426
457
  exp.Cast: transforms.preprocess(
427
458
  [_widen_decimal, transforms.remove_precision_parameterized_types]
428
459
  ),
460
+ exp.TryCast: transforms.preprocess(
461
+ [_widen_decimal, transforms.remove_precision_parameterized_types]
462
+ ),
429
463
  exp.CollateProperty: lambda self, e: (
430
464
  f"DEFAULT COLLATE {self.sql(e, 'this')}"
431
465
  if e.args.get("default")
@@ -303,7 +303,6 @@ class ClickHouseGenerator(generator.Generator):
303
303
  exp.ArrayContains: rename_func("has"),
304
304
  exp.ArrayFilter: lambda self, e: self.func("arrayFilter", e.expression, e.this),
305
305
  exp.Transform: lambda self, e: self.func("arrayMap", e.expression, e.this),
306
- exp.ArrayRemove: remove_from_array_using_filter,
307
306
  exp.ArrayReverse: rename_func("arrayReverse"),
308
307
  exp.ArraySort: _array_sort_sql,
309
308
  exp.ArraySlice: rename_func("arraySlice"),
@@ -385,9 +384,9 @@ class ClickHouseGenerator(generator.Generator):
385
384
  exp.Xor: lambda self, e: self.func("xor", e.this, e.expression),
386
385
  exp.MD5Digest: rename_func("MD5"),
387
386
  exp.MD5: lambda self, e: self.func("LOWER", self.func("HEX", self.func("MD5", e.this))),
388
- exp.SHA: rename_func("SHA1"),
387
+ exp.SHA: lambda self, e: self.func("LOWER", self.func("HEX", self.func("SHA1", e.this))),
389
388
  exp.SHA1Digest: rename_func("SHA1"),
390
- exp.SHA2: sha256_sql,
389
+ exp.SHA2: lambda self, e: self.func("LOWER", self.func("HEX", sha256_sql(self, e))),
391
390
  exp.SHA2Digest: sha2_digest_sql,
392
391
  exp.Split: lambda self, e: self.func(
393
392
  "splitByString", e.args.get("expression"), e.this, e.args.get("limit")
@@ -450,6 +449,32 @@ class ClickHouseGenerator(generator.Generator):
450
449
  exp.DType.MULTIPOLYGON,
451
450
  }
452
451
 
452
+ def arrayremove_sql(self, expression: exp.ArrayRemove) -> str:
453
+ return remove_from_array_using_filter(
454
+ self,
455
+ expression,
456
+ keep=lambda element, target: exp.ArrayContains(
457
+ this=exp.array(target), expression=element
458
+ ).not_(),
459
+ supports_nullable_arrays=False,
460
+ )
461
+
462
+ def dot_sql(self, expression: exp.Dot) -> str:
463
+ if depth := expression.args.get("subcolumn_depth"):
464
+ root: exp.Expr = expression
465
+ fields: list[str] = []
466
+ while isinstance(root, exp.Dot) and len(fields) < depth:
467
+ fields.append(root.expression.name)
468
+ root = root.this
469
+
470
+ # expr.a.b performs two lookups, while a column path resolves the entire subcolumn.
471
+ if len(fields) == depth and not isinstance(root, exp.Column):
472
+ return self.func(
473
+ "getSubcolumn", root, exp.Literal.string(".".join(reversed(fields)))
474
+ )
475
+
476
+ return super().dot_sql(expression)
477
+
453
478
  @unsupported_args("json_scope")
454
479
  def contains_sql(self, expression: exp.Contains) -> str:
455
480
  # ClickHouse has no CONTAINS; keep the parentheses only where the parent needs them
@@ -30,6 +30,9 @@ class DatabricksGenerator(SparkGenerator):
30
30
  QUOTE_JSON_PATH = False
31
31
  PARSE_JSON_NAME: str | None = "PARSE_JSON"
32
32
  SUPPORTS_TEMPORAL_PRECISION = True
33
+ SUPPORTS_OPTIMIZE = True
34
+ MERGE_SCHEMA_EVOLUTION_MIN_VERSION = (15, 2)
35
+ ORDERED_PERCENTILE_MIN_VERSIONS = {exp.PercentileCont: (10, 4), exp.PercentileDisc: (11, 3)}
33
36
 
34
37
  # Known limitation: with no partition keys to order by, CUME_DIST() OVER () becomes
35
38
  # CUME_DIST() OVER (ORDER BY NULL), which Databricks still rejects with