sqlglotc 30.29.0__tar.gz → 30.30.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.29.0/sqlglotc.egg-info → sqlglotc-30.30.0}/PKG-INFO +2 -2
  2. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/expressions/aggregate.py +2 -2
  3. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/expressions/properties.py +8 -0
  4. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/expressions/query.py +21 -9
  5. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/expressions/string.py +3 -1
  6. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/expressions/temporal.py +1 -1
  7. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/generator.py +23 -1
  8. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/generators/databricks.py +21 -0
  9. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/generators/duckdb.py +44 -17
  10. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/generators/snowflake.py +7 -8
  11. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/generators/spark2.py +4 -0
  12. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/generators/sqlite.py +1 -1
  13. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/optimizer/annotate_types.py +11 -5
  14. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/optimizer/qualify_columns.py +49 -15
  15. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/optimizer/scope.py +21 -1
  16. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/parser.py +39 -5
  17. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/parsers/base.py +2 -0
  18. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/parsers/bigquery.py +5 -1
  19. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/parsers/duckdb.py +12 -0
  20. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/parsers/hive.py +5 -7
  21. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/parsers/mysql.py +2 -0
  22. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/parsers/postgres.py +2 -0
  23. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/parsers/presto.py +2 -0
  24. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/parsers/snowflake.py +13 -1
  25. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/parsers/spark2.py +13 -0
  26. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/parsers/starrocks.py +3 -0
  27. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/tokenizer_core.py +16 -2
  28. {sqlglotc-30.29.0 → sqlglotc-30.30.0/sqlglotc.egg-info}/PKG-INFO +2 -2
  29. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglotc.egg-info/requires.txt +1 -1
  30. sqlglotc-30.30.0/sqlglotc.egg-info/scm_version.json +8 -0
  31. sqlglotc-30.29.0/sqlglotc.egg-info/scm_version.json +0 -8
  32. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/MANIFEST.in +0 -0
  33. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/pyproject.toml +0 -0
  34. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/setup.cfg +0 -0
  35. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/setup.py +0 -0
  36. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/anonymize.py +0 -0
  37. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/errors.py +0 -0
  38. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/executor/table.py +0 -0
  39. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/expressions/array.py +0 -0
  40. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/expressions/builders.py +0 -0
  41. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/expressions/constraints.py +0 -0
  42. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/expressions/core.py +0 -0
  43. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/expressions/datatypes.py +0 -0
  44. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/expressions/ddl.py +0 -0
  45. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/expressions/dml.py +0 -0
  46. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/expressions/functions.py +0 -0
  47. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/expressions/json.py +0 -0
  48. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/expressions/math.py +0 -0
  49. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/generators/athena.py +0 -0
  50. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/generators/bigquery.py +0 -0
  51. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/generators/clickhouse.py +0 -0
  52. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/generators/dax.py +0 -0
  53. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/generators/doris.py +0 -0
  54. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/generators/dremio.py +0 -0
  55. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/generators/drill.py +0 -0
  56. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/generators/druid.py +0 -0
  57. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/generators/dune.py +0 -0
  58. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/generators/exasol.py +0 -0
  59. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/generators/fabric.py +0 -0
  60. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/generators/hive.py +0 -0
  61. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/generators/materialize.py +0 -0
  62. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/generators/mysql.py +0 -0
  63. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/generators/oracle.py +0 -0
  64. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/generators/postgres.py +0 -0
  65. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/generators/presto.py +0 -0
  66. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/generators/prql.py +0 -0
  67. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/generators/python.py +0 -0
  68. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/generators/redshift.py +0 -0
  69. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/generators/risingwave.py +0 -0
  70. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/generators/singlestore.py +0 -0
  71. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/generators/solr.py +0 -0
  72. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/generators/spark.py +0 -0
  73. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/generators/starrocks.py +0 -0
  74. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/generators/tableau.py +0 -0
  75. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/generators/teradata.py +0 -0
  76. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/generators/trino.py +0 -0
  77. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/generators/tsql.py +0 -0
  78. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/helper.py +0 -0
  79. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/lineage.py +0 -0
  80. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/optimizer/canonicalize_internal_names.py +0 -0
  81. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/optimizer/isolate_table_selects.py +0 -0
  82. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/optimizer/normalize_identifiers.py +0 -0
  83. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/optimizer/qualify.py +0 -0
  84. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/optimizer/qualify_tables.py +0 -0
  85. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/optimizer/resolver.py +0 -0
  86. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/optimizer/simplify.py +0 -0
  87. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/parsers/athena.py +0 -0
  88. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/parsers/clickhouse.py +0 -0
  89. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/parsers/databricks.py +0 -0
  90. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/parsers/dax.py +0 -0
  91. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/parsers/doris.py +0 -0
  92. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/parsers/dremio.py +0 -0
  93. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/parsers/drill.py +0 -0
  94. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/parsers/druid.py +0 -0
  95. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/parsers/dune.py +0 -0
  96. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/parsers/exasol.py +0 -0
  97. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/parsers/fabric.py +0 -0
  98. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/parsers/materialize.py +0 -0
  99. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/parsers/oracle.py +0 -0
  100. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/parsers/prql.py +0 -0
  101. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/parsers/redshift.py +0 -0
  102. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/parsers/risingwave.py +0 -0
  103. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/parsers/singlestore.py +0 -0
  104. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/parsers/solr.py +0 -0
  105. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/parsers/spark.py +0 -0
  106. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/parsers/sqlite.py +0 -0
  107. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/parsers/tableau.py +0 -0
  108. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/parsers/teradata.py +0 -0
  109. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/parsers/trino.py +0 -0
  110. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/parsers/tsql.py +0 -0
  111. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/serde.py +0 -0
  112. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/time.py +0 -0
  113. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglot/trie.py +0 -0
  114. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglotc.egg-info/SOURCES.txt +0 -0
  115. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglotc.egg-info/dependency_links.txt +0 -0
  116. {sqlglotc-30.29.0 → sqlglotc-30.30.0}/sqlglotc.egg-info/scm_file_list.json +0 -0
  117. {sqlglotc-30.29.0 → sqlglotc-30.30.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.29.0
3
+ Version: 30.30.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.29.0
10
+ Requires-Dist: sqlglot==30.30.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"
@@ -180,7 +180,7 @@ class LogicalOr(Expression, AggFunc):
180
180
 
181
181
 
182
182
  class Max(Expression, AggFunc):
183
- arg_types = {"this": True, "expressions": False}
183
+ arg_types = {"this": True, "expressions": False, "count": False}
184
184
  is_var_len_args = True
185
185
 
186
186
 
@@ -189,7 +189,7 @@ class Median(Expression, AggFunc):
189
189
 
190
190
 
191
191
  class Min(Expression, AggFunc):
192
- arg_types = {"this": True, "expressions": False}
192
+ arg_types = {"this": True, "expressions": False, "count": False}
193
193
  is_var_len_args = True
194
194
 
195
195
 
@@ -210,6 +210,10 @@ class IcebergProperty(Property):
210
210
  arg_types = {}
211
211
 
212
212
 
213
+ class IgnoreGrantsProperty(Property):
214
+ arg_types = {}
215
+
216
+
213
217
  class InheritsProperty(Property):
214
218
  arg_types = {"expressions": True}
215
219
 
@@ -334,6 +338,10 @@ class OnCommitProperty(Property):
334
338
  arg_types = {"delete": False}
335
339
 
336
340
 
341
+ class OptionsProperty(Property):
342
+ arg_types = {"expressions": True}
343
+
344
+
337
345
  class PartitionedByProperty(Property):
338
346
  arg_types = {"this": True}
339
347
 
@@ -1111,15 +1111,27 @@ class SetOperation(Expression, Query):
1111
1111
 
1112
1112
  @property
1113
1113
  def named_selects(self) -> list[str]:
1114
- expr: Expr = self
1115
- while isinstance(expr, SetOperation):
1116
- if expr.args.get("by_name"):
1117
- left = t.cast(Selectable, expr.this.unnest()).named_selects
1118
- right = t.cast(Selectable, expr.expression.unnest()).named_selects
1119
- return list(dict.fromkeys(left + right))
1120
-
1121
- expr = expr.this.unnest()
1122
- return _named_selects(expr)
1114
+ names = []
1115
+ stack: list[Expr] = [self]
1116
+ by_name = False
1117
+
1118
+ while stack:
1119
+ expr = stack.pop()
1120
+ if not isinstance(expr, SetOperation):
1121
+ names.extend(t.cast(Selectable, expr).named_selects)
1122
+ continue
1123
+
1124
+ while isinstance(expr, SetOperation):
1125
+ if expr.args.get("by_name"):
1126
+ by_name = True
1127
+ stack.append(expr.expression.unnest())
1128
+ stack.append(expr.this.unnest())
1129
+ break
1130
+ expr = expr.this.unnest()
1131
+ else:
1132
+ names.extend(_named_selects(expr))
1133
+
1134
+ return list(dict.fromkeys(names)) if by_name else names
1123
1135
 
1124
1136
  @property
1125
1137
  def is_star(self) -> bool:
@@ -435,7 +435,9 @@ class RegexpExtract(Expression, Func):
435
435
  "occurrence": False,
436
436
  "parameters": False,
437
437
  "group": False,
438
- "null_if_pos_overflow": False, # for transpilation target behavior
438
+ "null_if_pos_overflow": False,
439
+ "null_if_no_match": False,
440
+ "empty_if_group_missing": False,
439
441
  }
440
442
 
441
443
 
@@ -192,7 +192,7 @@ class TimestampTrunc(Expression, Func, TimeUnit):
192
192
 
193
193
 
194
194
  class TimeSlice(Expression, Func, TimeUnit):
195
- arg_types = {"this": True, "expression": True, "unit": True, "kind": False}
195
+ arg_types = {"this": True, "expression": True, "unit": True, "kind": False, "origin": False}
196
196
 
197
197
 
198
198
  class TimeTrunc(Expression, Func, TimeUnit):
@@ -202,6 +202,7 @@ class Generator:
202
202
  exp.HeapProperty: lambda *_: "HEAP",
203
203
  exp.HybridProperty: lambda *_: "HYBRID",
204
204
  exp.IcebergProperty: lambda *_: "ICEBERG",
205
+ exp.IgnoreGrantsProperty: lambda *_: "IGNORE GRANTS",
205
206
  exp.InheritsProperty: lambda self, e: f"INHERITS ({self.expressions(e, flat=True)})",
206
207
  exp.InlineLengthColumnConstraint: lambda self, e: f"INLINE LENGTH {self.sql(e, 'this')}",
207
208
  exp.InputModelProperty: lambda self, e: f"INPUT{self.sql(e, 'this')}",
@@ -350,7 +351,7 @@ class Generator:
350
351
  # Whether limit and fetch are supported (possible values: "ALL", "LIMIT", "FETCH")
351
352
  LIMIT_FETCH = "ALL"
352
353
 
353
- # Whether limit and fetch allows expresions or just limits
354
+ # Whether limit and fetch allows expressions or just limits
354
355
  LIMIT_ONLY_LITERALS = False
355
356
 
356
357
  # Whether a table is allowed to be renamed with a db
@@ -763,6 +764,7 @@ class Generator:
763
764
  exp.HybridProperty: exp.Properties.Location.POST_CREATE,
764
765
  exp.InheritsProperty: exp.Properties.Location.POST_SCHEMA,
765
766
  exp.IcebergProperty: exp.Properties.Location.POST_CREATE,
767
+ exp.IgnoreGrantsProperty: exp.Properties.Location.POST_SCHEMA,
766
768
  exp.IncludeProperty: exp.Properties.Location.POST_SCHEMA,
767
769
  exp.InputModelProperty: exp.Properties.Location.POST_SCHEMA,
768
770
  exp.InteractiveProperty: exp.Properties.Location.UNSUPPORTED,
@@ -782,6 +784,7 @@ class Generator:
782
784
  exp.NoPrimaryIndexProperty: exp.Properties.Location.POST_EXPRESSION,
783
785
  exp.OnProperty: exp.Properties.Location.POST_SCHEMA,
784
786
  exp.OnCommitProperty: exp.Properties.Location.POST_EXPRESSION,
787
+ exp.OptionsProperty: exp.Properties.Location.UNSUPPORTED,
785
788
  exp.Order: exp.Properties.Location.POST_SCHEMA,
786
789
  exp.OutputModelProperty: exp.Properties.Location.POST_SCHEMA,
787
790
  exp.PartitionedByProperty: exp.Properties.Location.POST_WITH,
@@ -6053,6 +6056,14 @@ class Generator:
6053
6056
 
6054
6057
  return array_agg_sql
6055
6058
 
6059
+ @unsupported_args("count")
6060
+ def min_sql(self, expression: exp.Min) -> str:
6061
+ return self.function_fallback_sql(expression)
6062
+
6063
+ @unsupported_args("count")
6064
+ def max_sql(self, expression: exp.Max) -> str:
6065
+ return self.function_fallback_sql(expression)
6066
+
6056
6067
  def arrayagg_sql(self, expression: exp.ArrayAgg) -> str:
6057
6068
  array_agg = self.function_fallback_sql(expression)
6058
6069
  column_expr = expression.this
@@ -6061,6 +6072,17 @@ class Generator:
6061
6072
 
6062
6073
  return self._add_arrayagg_null_filter(array_agg, expression, column_expr)
6063
6074
 
6075
+ def timeslice_sql(self, expression: exp.TimeSlice) -> str:
6076
+ # Only the SQLGlot dialect serializes the internal origin for round-tripping.
6077
+ origin = expression.args.get("origin") if self.dialect == "" else None
6078
+ kind = expression.args.get("kind")
6079
+ if origin is not None and kind is None:
6080
+ kind = exp.Literal.string("START")
6081
+
6082
+ return self.func(
6083
+ "TIME_SLICE", expression.this, expression.expression, expression.unit, kind, origin
6084
+ )
6085
+
6064
6086
  def slice_sql(self, expression: exp.Slice) -> str:
6065
6087
  step = self.sql(expression, "step")
6066
6088
  # An omitted end is written as `-` before a step, since `::` is the cast operator
@@ -10,6 +10,16 @@ from sqlglot.dialects.dialect import (
10
10
  from sqlglot.generators.spark import SparkGenerator
11
11
 
12
12
 
13
+ TIMESTAMP_TYPES = {
14
+ exp.DType.DATETIME,
15
+ exp.DType.TIME,
16
+ exp.DType.TIMESTAMP,
17
+ exp.DType.TIMESTAMPNTZ,
18
+ exp.DType.TIMESTAMPLTZ,
19
+ exp.DType.TIMESTAMPTZ,
20
+ }
21
+
22
+
13
23
  class DatabricksGenerator(SparkGenerator):
14
24
  TABLESAMPLE_SEED_KEYWORD = "REPEATABLE"
15
25
  COPY_PARAMS_ARE_WRAPPED = False
@@ -70,6 +80,17 @@ class DatabricksGenerator(SparkGenerator):
70
80
  exp.DType.NULL: "VOID",
71
81
  }
72
82
 
83
+ def datatype_sql(self, expression: exp.DataType) -> str:
84
+ # Databricks only supports a timestamp precision of 6 (microseconds) or 7 to 9
85
+ # (nanoseconds), so drop any other precision it would reject, e.g. TIMESTAMP(0)
86
+ if expression.this in TIMESTAMP_TYPES and expression.expressions:
87
+ param = expression.expressions[0]
88
+ param = param.this if isinstance(param, exp.DataTypeParam) else param
89
+ if param.is_int and not 6 <= param.to_py() <= 9:
90
+ expression.set("expressions", None)
91
+
92
+ return super().datatype_sql(expression)
93
+
73
94
  def create_sql(self, expression: exp.Create) -> str:
74
95
  body = expression.expression
75
96
  if (
@@ -24,6 +24,7 @@ from sqlglot.dialects.dialect import (
24
24
  getbit_sql,
25
25
  groupconcat_sql,
26
26
  inline_array_unless_query,
27
+ map_date_part,
27
28
  months_between_sql,
28
29
  no_datetime_sql,
29
30
  no_comment_column_constraint_sql,
@@ -1770,6 +1771,8 @@ class DuckDBGenerator(generator.Generator):
1770
1771
  exp.MakeInterval: lambda self, e: no_make_interval_sql(self, e, sep=" "),
1771
1772
  exp.Initcap: _initcap_sql,
1772
1773
  exp.MD5Digest: lambda self, e: self.func("UNHEX", self.func("MD5", e.this)),
1774
+ exp.Max: rename_func("MAX"),
1775
+ exp.Min: rename_func("MIN"),
1773
1776
  exp.SHA: lambda self, e: _sha_sql(self, e, "SHA1"),
1774
1777
  exp.SHA1Digest: lambda self, e: _sha_sql(self, e, "SHA1", is_binary=True),
1775
1778
  exp.SHA2: lambda self, e: _sha_sql(self, e, "SHA256"),
@@ -2423,32 +2426,37 @@ class DuckDBGenerator(generator.Generator):
2423
2426
  Transform Snowflake's TIME_SLICE to DuckDB's time_bucket.
2424
2427
 
2425
2428
  Snowflake: TIME_SLICE(date_expr, slice_length, 'UNIT' [, 'START'|'END'])
2426
- DuckDB: time_bucket(INTERVAL 'slice_length' UNIT, date_expr)
2429
+ DuckDB: time_bucket(INTERVAL 'slice_length' UNIT, date_expr, origin)
2427
2430
 
2428
- For 'END' kind, add the interval to get the end of the slice.
2429
- For DATE type with 'END', cast result back to DATE to preserve type.
2431
+ Snowflake aligns slices to the 1970-01-01 epoch, so an explicit origin is
2432
+ required because DuckDB's default origin differs. For 'END' kind, add the
2433
+ interval to get the next slice's start; for DATE input, cast back to DATE.
2430
2434
  """
2431
2435
  date_expr = expression.this
2432
2436
  slice_length = expression.expression
2433
- unit = expression.unit
2437
+ # Normalize unit aliases (e.g. W/WK/WOY -> WEEK) so the interval and origin agree
2438
+ unit = map_date_part(expression.unit, self.dialect)
2434
2439
  kind = expression.text("kind").upper()
2435
2440
 
2436
- # Create INTERVAL expression: INTERVAL 'N' UNIT
2437
2441
  interval_expr = exp.Interval(this=slice_length, unit=unit)
2438
2442
 
2439
- # Create base time_bucket expression
2440
- time_bucket_expr = exp.func("time_bucket", interval_expr, date_expr)
2443
+ origin = expression.args.get("origin")
2444
+ if origin is None:
2445
+ # Snowflake aligns to 1970-01-01; WEEK to the Monday of the epoch week (WEEK_START=0)
2446
+ origin = exp.Literal.string(
2447
+ "1969-12-29" if unit and unit.name.upper() == "WEEK" else "1970-01-01"
2448
+ )
2449
+ # A DATE origin keeps a DATE input a DATE; DuckDB widens it for TIMESTAMP inputs
2450
+ time_bucket_expr = exp.func(
2451
+ "time_bucket", interval_expr, date_expr, exp.cast(origin, exp.DType.DATE)
2452
+ )
2441
2453
 
2442
- # Check if we need the end of the slice (default is start)
2443
- if not kind == "END":
2444
- # For 'START', return time_bucket directly
2454
+ if kind != "END":
2445
2455
  return self.sql(time_bucket_expr)
2446
2456
 
2447
- # For 'END', add the interval to get end of slice
2448
2457
  add_expr = exp.Add(this=time_bucket_expr, expression=interval_expr.copy())
2449
2458
 
2450
- # If input is DATE type, cast result back to DATE to preserve type
2451
- # DuckDB converts DATE to TIMESTAMP when adding intervals
2459
+ # DuckDB widens DATE to TIMESTAMP when adding intervals, so cast back
2452
2460
  if date_expr.is_type(exp.DType.DATE):
2453
2461
  return self.sql(exp.cast(add_expr, exp.DType.DATE))
2454
2462
 
@@ -4330,6 +4338,7 @@ class DuckDBGenerator(generator.Generator):
4330
4338
  position = expression.args.get("position")
4331
4339
  occurrence = expression.args.get("occurrence")
4332
4340
  null_if_pos_overflow = expression.args.get("null_if_pos_overflow")
4341
+ null_if_no_match = expression.args.get("null_if_no_match")
4333
4342
 
4334
4343
  # Handle Snowflake's 'e' flag: it enables capture group extraction
4335
4344
  # In DuckDB, this is controlled by the group parameter directly
@@ -4358,7 +4367,7 @@ class DuckDBGenerator(generator.Generator):
4358
4367
  is_extract_all = isinstance(expression, exp.RegexpExtractAll)
4359
4368
  non_single_occurrence = occurrence and (not occurrence.is_int or occurrence.to_py() > 1)
4360
4369
 
4361
- if is_extract_all or non_single_occurrence:
4370
+ if is_extract_all or non_single_occurrence or null_if_no_match:
4362
4371
  name = "REGEXP_EXTRACT_ALL"
4363
4372
  else:
4364
4373
  name = "REGEXP_EXTRACT"
@@ -4370,9 +4379,27 @@ class DuckDBGenerator(generator.Generator):
4370
4379
  # Array slicing for REGEXP_EXTRACT_ALL with occurrence
4371
4380
  if is_extract_all and non_single_occurrence:
4372
4381
  result = exp.Bracket(this=result, expressions=[exp.Slice(this=occurrence)])
4373
- # ARRAY_EXTRACT for REGEXP_EXTRACT with occurrence > 1
4374
- elif non_single_occurrence:
4375
- result = exp.Anonymous(this="ARRAY_EXTRACT", expressions=[result, occurrence])
4382
+ elif not is_extract_all and (non_single_occurrence or null_if_no_match):
4383
+ # Non-participating capture group yields [NULL] in DuckDB; Snowflake returns '' on a match
4384
+ if (
4385
+ expression.args.get("empty_if_group_missing")
4386
+ and group
4387
+ and not (group.is_int and group.to_py() == 0)
4388
+ ):
4389
+ result = exp.Transform(
4390
+ this=result,
4391
+ expression=exp.Lambda(
4392
+ this=exp.Coalesce(
4393
+ this=exp.column("x"), expressions=[exp.Literal.string("")]
4394
+ ),
4395
+ expressions=[exp.to_identifier("x")],
4396
+ ),
4397
+ )
4398
+ return self.func(
4399
+ "ARRAY_EXTRACT",
4400
+ result,
4401
+ occurrence if non_single_occurrence else exp.Literal.number(1),
4402
+ )
4376
4403
 
4377
4404
  return self.sql(result)
4378
4405
 
@@ -1013,23 +1013,22 @@ class SnowflakeGenerator(generator.Generator):
1013
1013
 
1014
1014
  def createable_sql(self, expression: exp.Create, locations: defaultdict) -> str:
1015
1015
  is_materialized = expression.find(exp.MaterializedProperty)
1016
- copy_grants_property = expression.find(exp.CopyGrantsProperty)
1016
+ grants_property = expression.find(exp.CopyGrantsProperty, exp.IgnoreGrantsProperty)
1017
1017
 
1018
- if expression.kind == "VIEW" and is_materialized and copy_grants_property:
1019
- # For materialized views, COPY GRANTS is located *before* the columns list
1020
- # This is in contrast to normal views where COPY GRANTS is located *after* the columns list
1021
- # We default CopyGrantsProperty to POST_SCHEMA which means we need to output it POST_NAME if a materialized view is detected
1018
+ if expression.kind == "VIEW" and is_materialized and grants_property:
1019
+ # Materialized views require COPY/IGNORE GRANTS before the column list,
1020
+ # whereas normal views use the default POST_SCHEMA location.
1022
1021
  # ref: https://docs.snowflake.com/en/sql-reference/sql/create-materialized-view#syntax
1023
1022
  # ref: https://docs.snowflake.com/en/sql-reference/sql/create-view#syntax
1024
1023
  post_schema_properties = locations[exp.Properties.Location.POST_SCHEMA]
1025
- post_schema_properties.pop(post_schema_properties.index(copy_grants_property))
1024
+ post_schema_properties.pop(post_schema_properties.index(grants_property))
1026
1025
 
1027
1026
  this_name = self.sql(expression.this, "this")
1028
- copy_grants = self.sql(copy_grants_property)
1027
+ grants = self.sql(grants_property)
1029
1028
  this_schema = self.schema_columns_sql(expression.this)
1030
1029
  this_schema = f"{self.sep()}{this_schema}" if this_schema else ""
1031
1030
 
1032
- return f"{this_name}{self.sep()}{copy_grants}{this_schema}"
1031
+ return f"{this_name}{self.sep()}{grants}{this_schema}"
1033
1032
 
1034
1033
  return super().createable_sql(expression, locations)
1035
1034
 
@@ -142,6 +142,7 @@ class Spark2Generator(HiveGenerator):
142
142
  exp.AutoIncrementProperty: exp.Properties.Location.UNSUPPORTED,
143
143
  exp.CharacterSetProperty: exp.Properties.Location.UNSUPPORTED,
144
144
  exp.CollateProperty: exp.Properties.Location.UNSUPPORTED,
145
+ exp.OptionsProperty: exp.Properties.Location.POST_SCHEMA,
145
146
  }
146
147
 
147
148
  TS_OR_DS_EXPRESSIONS: t.ClassVar = (
@@ -272,6 +273,9 @@ class Spark2Generator(HiveGenerator):
272
273
 
273
274
  return f"USING {expression.name.upper()}"
274
275
 
276
+ def optionsproperty_sql(self, expression: exp.OptionsProperty) -> str:
277
+ return f"OPTIONS ({self.expressions(expression, flat=True)})"
278
+
275
279
  def altercolumn_sql(self, expression: exp.AlterColumn) -> str:
276
280
  if expression.args.get("exists"):
277
281
  self.unsupported("ALTER COLUMN IF EXISTS is not supported by this dialect")
@@ -355,7 +355,7 @@ class SQLiteGenerator(generator.Generator):
355
355
  sql = f"{sql} * 86400000000.0"
356
356
  elif unit == "NANOSECOND":
357
357
  sql = f"{sql} * 86400000000000.0"
358
- else:
358
+ elif unit != "DAY":
359
359
  self.unsupported(f"DATEDIFF unsupported for '{unit}'.")
360
360
 
361
361
  return f"CAST({sql} AS INTEGER)"
@@ -379,13 +379,12 @@ class TypeAnnotator:
379
379
  struct_type = exp_type if exp_type and exp_type.is_type(exp.DType.STRUCT) else None
380
380
 
381
381
  if struct_type:
382
- return {
382
+ result = {
383
383
  col_def.name: t.cast(t.Union[exp.DataType, exp.DType], col_def.kind)
384
384
  for col_def in struct_type.expressions
385
385
  if isinstance(col_def, exp.ColumnDef) and col_def.kind
386
386
  }
387
-
388
- if isinstance(expression, exp.Values):
387
+ elif isinstance(expression, exp.Values):
389
388
  # Like a set operation's, each VALUES column's type is coerced across all rows
390
389
  column_types: list[exp.DataType | exp.DType] = [exp.DType.NULL] * len(values)
391
390
  for row in expression.expressions:
@@ -394,9 +393,16 @@ class TypeAnnotator:
394
393
  value.type or exp.DType.UNKNOWN, column_types[i]
395
394
  )
396
395
 
397
- return dict(zip(alias_column_names, column_types))
396
+ result = dict(zip(alias_column_names, column_types))
397
+ else:
398
+ result = {alias: column.type for alias, column in zip(alias_column_names, values)}
399
+
400
+ if isinstance(expression, exp.Unnest) and isinstance(
401
+ offset := expression.args.get("offset"), exp.Identifier
402
+ ):
403
+ result[offset.name] = exp.DType.BIGINT
398
404
 
399
- return {alias: column.type for alias, column in zip(alias_column_names, values)}
405
+ return result
400
406
 
401
407
  if isinstance(expression, exp.SetOperation):
402
408
  return dict(self._get_setop_column_types(expression))
@@ -26,6 +26,9 @@ if t.TYPE_CHECKING:
26
26
  from collections.abc import Iterable
27
27
 
28
28
 
29
+ GROUPING_CONTAINERS = (exp.Group, exp.GroupingSets, exp.Cube, exp.Rollup, exp.Tuple, exp.Paren)
30
+
31
+
29
32
  def qualify_columns(
30
33
  expression: E,
31
34
  schema: dict[str, object] | Schema,
@@ -106,20 +109,22 @@ def qualify_columns(
106
109
  # Refresh classification caches: a column just qualified in place may have been cached as external
107
110
  scope.clear_column_cache()
108
111
 
112
+ if is_select and expand_stars:
113
+ _expand_stars(
114
+ scope,
115
+ resolver,
116
+ using_column_tables,
117
+ using_columns,
118
+ pseudocolumns,
119
+ annotator,
120
+ unannotated_scopes,
121
+ )
122
+
123
+ # HAVING precedence can depend on positional GROUP BY targets within a star.
109
124
  if expand_alias_refs:
110
125
  _expand_alias_refs(scope, resolver, dialect)
111
126
 
112
127
  if is_select:
113
- if expand_stars:
114
- _expand_stars(
115
- scope,
116
- resolver,
117
- using_column_tables,
118
- using_columns,
119
- pseudocolumns,
120
- annotator,
121
- unannotated_scopes,
122
- )
123
128
  qualify_outputs(scope, dialect=dialect)
124
129
 
125
130
  _expand_group_by(scope, dialect)
@@ -371,6 +376,7 @@ def _expand_alias_refs(
371
376
  return
372
377
 
373
378
  alias_to_expression: dict[str, tuple[exp.Expr, int]] = {}
379
+ grouped_columns: set[tuple[str, str]] = set()
374
380
  projections = {s.alias_or_name for s in expression.selects}
375
381
  replaced = False
376
382
 
@@ -423,7 +429,14 @@ def _expand_alias_refs(
423
429
  node.parts[0].name in projections
424
430
  for node in alias_expr.find_all(exp.Column)
425
431
  )
426
- if table and (not alias_expr or skip_replace):
432
+ prefer_column = dialect.PREFER_COLUMNS_TO_ALIASES_IN_HAVING_AND_QUALIFY
433
+ if table and is_having and dialect.HAVING_ALIASES_SHADOW_UNGROUPED_COLUMNS:
434
+ prefer_column = (
435
+ isinstance(column.find_ancestor(exp.AggFunc, exp.Having), exp.AggFunc)
436
+ or (table.name, column.name) in grouped_columns
437
+ )
438
+
439
+ if table and (not alias_expr or skip_replace or prefer_column):
427
440
  column.set("table", table)
428
441
  elif not column.table and alias_expr and not skip_replace:
429
442
  # For queries like SELECT x + 1 AS y FROM read_parquet('f') WHERE y > 1, we can't
@@ -487,6 +500,21 @@ def _expand_alias_refs(
487
500
 
488
501
  replace_columns(expression.args.get("where"))
489
502
  replace_columns(expression.args.get("group"), literal_index=True)
503
+
504
+ if (
505
+ dialect.HAVING_ALIASES_SHADOW_UNGROUPED_COLUMNS
506
+ and expression.args.get("having")
507
+ and (group := expression.args.get("group"))
508
+ ):
509
+ # Only standalone grouping columns shadow HAVING aliases, not columns inside expressions.
510
+ for grouping in group.walk(prune=lambda node: not isinstance(node, GROUPING_CONTAINERS)):
511
+ if isinstance(grouping, exp.Literal) and grouping.is_int:
512
+ index = int(grouping.to_py()) - 1
513
+ if 0 <= index < len(expression.selects):
514
+ grouping = expression.selects[index].unalias().unnest()
515
+ if isinstance(grouping, exp.Column):
516
+ grouped_columns.add((grouping.table, grouping.name))
517
+
490
518
  replace_columns(expression.args.get("having"), resolve_table=True)
491
519
  replace_columns(expression.args.get("qualify"), resolve_table=True)
492
520
 
@@ -1032,7 +1060,6 @@ def _expand_stars(
1032
1060
  rename_columns: dict[int, dict[str, str]] = {}
1033
1061
  ilike_pattern: str | None = None
1034
1062
 
1035
- coalesced_columns = set()
1036
1063
  dialect = resolver.dialect
1037
1064
 
1038
1065
  if dialect.SUPPORTS_STRUCT_STAR_EXPANSION:
@@ -1057,7 +1084,10 @@ def _expand_stars(
1057
1084
 
1058
1085
  for expression in scope_expression.selects:
1059
1086
  tables: list[str] = []
1060
- if isinstance(expression, exp.Star):
1087
+ is_unqualified_star = isinstance(expression, exp.Star)
1088
+ coalesced_columns = set()
1089
+
1090
+ if is_unqualified_star:
1061
1091
  # Only a string literal ILIKE pattern can filter the expansion at optimization time
1062
1092
  ilike = expression.args.get("ilike")
1063
1093
  if ilike and not ilike.is_string:
@@ -1104,7 +1134,7 @@ def _expand_stars(
1104
1134
  start = len(new_selections)
1105
1135
  # The selections of the USING columns, which go first, in order
1106
1136
  using_selections: dict[str, exp.Expr | None] = (
1107
- dict.fromkeys(using_columns) if isinstance(expression, exp.Star) else {}
1137
+ dict.fromkeys(using_columns) if is_unqualified_star else {}
1108
1138
  )
1109
1139
 
1110
1140
  for table in tables:
@@ -1206,7 +1236,11 @@ def _expand_stars(
1206
1236
  continue
1207
1237
  if ilike_pattern and not re.fullmatch(ilike_pattern, name, re.IGNORECASE):
1208
1238
  continue
1209
- if name in using_column_tables and table in using_column_tables[name]:
1239
+ if (
1240
+ is_unqualified_star
1241
+ and name in using_column_tables
1242
+ and table in using_column_tables[name]
1243
+ ):
1210
1244
  coalesced_columns.add(name)
1211
1245
  # TODO (mypyc): use a separate variable to avoid reusing `tables` (list) with dict type
1212
1246
  using_tables = using_column_tables[name]
@@ -808,14 +808,34 @@ def _traverse_set_operation(scope: Scope) -> Iterator[Scope]:
808
808
  # Access the args directly instead of the left/right properties, because set operation
809
809
  # operands aren't guaranteed to be Query nodes, e.g. in VALUES (1) UNION ALL SELECT 1
810
810
  expression_stack: list[exp.Expr] = [set_op.expression, set_op.this]
811
+ by_name_col_map: dict[str, str] | None = None
811
812
 
812
813
  while expression_stack:
813
814
  expression = expression_stack.pop()
814
815
  set_op_scope = set_op_scope_stack[-1]
815
816
 
817
+ outer = set_op_scope.outer_columns
818
+ set_op_expr = set_op_scope.expression
819
+ # all arms need their outer aliases reordered for BY NAME
820
+ if (
821
+ outer
822
+ and prev_scope is not None
823
+ and isinstance(set_op_expr, exp.SetOperation)
824
+ and set_op_expr.args.get("by_name")
825
+ ):
826
+ if by_name_col_map is None:
827
+ names = set_op.named_selects
828
+ by_name_col_map = {} if "*" in names else dict(zip(names, scope.outer_columns))
829
+
830
+ if by_name_col_map:
831
+ arm_node = expression.unnest()
832
+ outer = [
833
+ by_name_col_map.get(name, "") for name in getattr(arm_node, "named_selects", [])
834
+ ]
835
+
816
836
  new_scope = set_op_scope.branch(
817
837
  expression,
818
- outer_columns=set_op_scope.outer_columns,
838
+ outer_columns=outer,
819
839
  scope_type=ScopeType.SET_OPERATION,
820
840
  )
821
841