1
0
Fork 0

Merging upstream version 25.24.5.

Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
Daniel Baumann 2025-02-13 21:55:40 +01:00
parent f2b92bd29a
commit 1763c7a4ef
Signed by: daniel
GPG key ID: FBB4F0E80A80222F
80 changed files with 61531 additions and 59444 deletions

View file

@ -114,6 +114,8 @@ class Generator(metaclass=_Generator):
**JSON_PATH_PART_TRANSFORMS,
exp.AllowedValuesProperty: lambda self,
e: f"ALLOWED_VALUES {self.expressions(e, flat=True)}",
exp.ArrayContainsAll: lambda self, e: self.binary(e, "@>"),
exp.ArrayOverlaps: lambda self, e: self.binary(e, "&&"),
exp.AutoRefreshProperty: lambda self, e: f"AUTO REFRESH {self.sql(e, 'this')}",
exp.BackupProperty: lambda self, e: f"BACKUP {self.sql(e, 'this')}",
exp.CaseSpecificColumnConstraint: lambda _,
@ -203,7 +205,8 @@ class Generator(metaclass=_Generator):
}
# Whether null ordering is supported in order by
# True: Full Support, None: No support, False: No support in window specifications
# True: Full Support, None: No support, False: No support for certain cases
# such as window specifications, aggregate functions etc
NULL_ORDERING_SUPPORTED: t.Optional[bool] = True
# Whether ignore nulls is inside the agg or outside.
@ -671,9 +674,7 @@ class Generator(metaclass=_Generator):
self._escaped_quote_end: str = (
self.dialect.tokenizer_class.STRING_ESCAPES[0] + self.dialect.QUOTE_END
)
self._escaped_identifier_end: str = (
self.dialect.tokenizer_class.IDENTIFIER_ESCAPES[0] + self.dialect.IDENTIFIER_END
)
self._escaped_identifier_end = self.dialect.IDENTIFIER_END * 2
self._next_name = name_sequence("_t")
@ -716,6 +717,16 @@ class Generator(metaclass=_Generator):
def preprocess(self, expression: exp.Expression) -> exp.Expression:
"""Apply generic preprocessing transformations to a given expression."""
expression = self._move_ctes_to_top_level(expression)
if self.ENSURE_BOOLS:
from sqlglot.transforms import ensure_bools
expression = ensure_bools(expression)
return expression
def _move_ctes_to_top_level(self, expression: E) -> E:
if (
not expression.parent
and type(expression) in self.EXPRESSIONS_WITHOUT_NESTED_CTES
@ -724,12 +735,6 @@ class Generator(metaclass=_Generator):
from sqlglot.transforms import move_ctes_to_top_level
expression = move_ctes_to_top_level(expression)
if self.ENSURE_BOOLS:
from sqlglot.transforms import ensure_bools
expression = ensure_bools(expression)
return expression
def unsupported(self, message: str) -> None:
@ -1191,7 +1196,11 @@ class Generator(metaclass=_Generator):
return f"WITH {recursive}{sql}"
def cte_sql(self, expression: exp.CTE) -> str:
alias = self.sql(expression, "alias")
alias = expression.args.get("alias")
if alias:
alias.add_comments(expression.pop_comments())
alias_sql = self.sql(expression, "alias")
materialized = expression.args.get("materialized")
if materialized is False:
@ -1199,7 +1208,7 @@ class Generator(metaclass=_Generator):
elif materialized:
materialized = "MATERIALIZED "
return f"{alias} AS {materialized or ''}{self.wrap(expression)}"
return f"{alias_sql} AS {materialized or ''}{self.wrap(expression)}"
def tablealias_sql(self, expression: exp.TableAlias) -> str:
alias = self.sql(expression, "this")
@ -1376,7 +1385,9 @@ class Generator(metaclass=_Generator):
order = expression.args.get("order")
if limit or order:
select = exp.subquery(expression, "_l_0", copy=False).select("*", copy=False)
select = self._move_ctes_to_top_level(
exp.subquery(expression, "_l_0", copy=False).select("*", copy=False)
)
if limit:
select = select.limit(limit.pop(), copy=False)
@ -2037,6 +2048,7 @@ class Generator(metaclass=_Generator):
def var_sql(self, expression: exp.Var) -> str:
return self.sql(expression, "this")
@unsupported_args("expressions")
def into_sql(self, expression: exp.Into) -> str:
temporary = " TEMPORARY" if expression.args.get("temporary") else ""
unlogged = " UNLOGGED" if expression.args.get("unlogged") else ""
@ -2345,6 +2357,18 @@ class Generator(metaclass=_Generator):
f"'{nulls_sort_change.strip()}' translation not supported in window functions"
)
nulls_sort_change = ""
elif (
self.NULL_ORDERING_SUPPORTED is False
and (isinstance(expression.find_ancestor(exp.AggFunc, exp.Select), exp.AggFunc))
and (
(asc and nulls_sort_change == " NULLS LAST")
or (desc and nulls_sort_change == " NULLS FIRST")
)
):
self.unsupported(
f"'{nulls_sort_change.strip()}' translation not supported for aggregate functions with {sort_order} sort order"
)
nulls_sort_change = ""
elif self.NULL_ORDERING_SUPPORTED is None:
if expression.this.is_int:
self.unsupported(
@ -2497,6 +2521,11 @@ class Generator(metaclass=_Generator):
self.sql(expression, "from", comment=False),
)
# If both the CTE and SELECT clauses have comments, generate the latter earlier
if expression.args.get("with"):
sql = self.maybe_comment(sql, expression)
expression.pop_comments()
sql = self.prepend_ctes(expression, sql)
if not self.SUPPORTS_SELECT_INTO and into:
@ -2644,11 +2673,13 @@ class Generator(metaclass=_Generator):
high = self.sql(expression, "high")
return f"{this} BETWEEN {low} AND {high}"
def bracket_offset_expressions(self, expression: exp.Bracket) -> t.List[exp.Expression]:
def bracket_offset_expressions(
self, expression: exp.Bracket, index_offset: t.Optional[int] = None
) -> t.List[exp.Expression]:
return apply_index_offset(
expression.this,
expression.expressions,
self.dialect.INDEX_OFFSET - expression.args.get("offset", 0),
(index_offset or self.dialect.INDEX_OFFSET) - expression.args.get("offset", 0),
)
def bracket_sql(self, expression: exp.Bracket) -> str: