Adding upstream version 25.26.0.
Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
parent
7af32ea9ec
commit
dfac4c492f
117 changed files with 49296 additions and 47316 deletions
sqlglot
|
@ -404,9 +404,9 @@ class Expression(metaclass=_Expression):
|
|||
def iter_expressions(self, reverse: bool = False) -> t.Iterator[Expression]:
|
||||
"""Yields the key and expression for all arguments, exploding list args."""
|
||||
# remove tuple when python 3.7 is deprecated
|
||||
for vs in reversed(tuple(self.args.values())) if reverse else self.args.values():
|
||||
for vs in reversed(tuple(self.args.values())) if reverse else self.args.values(): # type: ignore
|
||||
if type(vs) is list:
|
||||
for v in reversed(vs) if reverse else vs:
|
||||
for v in reversed(vs) if reverse else vs: # type: ignore
|
||||
if hasattr(v, "parent"):
|
||||
yield v
|
||||
else:
|
||||
|
@ -1247,7 +1247,7 @@ class Query(Expression):
|
|||
)
|
||||
|
||||
def union(
|
||||
self, expression: ExpOrStr, distinct: bool = True, dialect: DialectType = None, **opts
|
||||
self, *expressions: ExpOrStr, distinct: bool = True, dialect: DialectType = None, **opts
|
||||
) -> Union:
|
||||
"""
|
||||
Builds a UNION expression.
|
||||
|
@ -1258,8 +1258,8 @@ class Query(Expression):
|
|||
'SELECT * FROM foo UNION SELECT * FROM bla'
|
||||
|
||||
Args:
|
||||
expression: the SQL code string.
|
||||
If an `Expression` instance is passed, it will be used as-is.
|
||||
expressions: the SQL code strings.
|
||||
If `Expression` instances are passed, they will be used as-is.
|
||||
distinct: set the DISTINCT flag if and only if this is true.
|
||||
dialect: the dialect used to parse the input expression.
|
||||
opts: other options to use to parse the input expressions.
|
||||
|
@ -1267,10 +1267,10 @@ class Query(Expression):
|
|||
Returns:
|
||||
The new Union expression.
|
||||
"""
|
||||
return union(left=self, right=expression, distinct=distinct, dialect=dialect, **opts)
|
||||
return union(self, *expressions, distinct=distinct, dialect=dialect, **opts)
|
||||
|
||||
def intersect(
|
||||
self, expression: ExpOrStr, distinct: bool = True, dialect: DialectType = None, **opts
|
||||
self, *expressions: ExpOrStr, distinct: bool = True, dialect: DialectType = None, **opts
|
||||
) -> Intersect:
|
||||
"""
|
||||
Builds an INTERSECT expression.
|
||||
|
@ -1281,8 +1281,8 @@ class Query(Expression):
|
|||
'SELECT * FROM foo INTERSECT SELECT * FROM bla'
|
||||
|
||||
Args:
|
||||
expression: the SQL code string.
|
||||
If an `Expression` instance is passed, it will be used as-is.
|
||||
expressions: the SQL code strings.
|
||||
If `Expression` instances are passed, they will be used as-is.
|
||||
distinct: set the DISTINCT flag if and only if this is true.
|
||||
dialect: the dialect used to parse the input expression.
|
||||
opts: other options to use to parse the input expressions.
|
||||
|
@ -1290,10 +1290,10 @@ class Query(Expression):
|
|||
Returns:
|
||||
The new Intersect expression.
|
||||
"""
|
||||
return intersect(left=self, right=expression, distinct=distinct, dialect=dialect, **opts)
|
||||
return intersect(self, *expressions, distinct=distinct, dialect=dialect, **opts)
|
||||
|
||||
def except_(
|
||||
self, expression: ExpOrStr, distinct: bool = True, dialect: DialectType = None, **opts
|
||||
self, *expressions: ExpOrStr, distinct: bool = True, dialect: DialectType = None, **opts
|
||||
) -> Except:
|
||||
"""
|
||||
Builds an EXCEPT expression.
|
||||
|
@ -1304,8 +1304,8 @@ class Query(Expression):
|
|||
'SELECT * FROM foo EXCEPT SELECT * FROM bla'
|
||||
|
||||
Args:
|
||||
expression: the SQL code string.
|
||||
If an `Expression` instance is passed, it will be used as-is.
|
||||
expressions: the SQL code strings.
|
||||
If `Expression` instance are passed, they will be used as-is.
|
||||
distinct: set the DISTINCT flag if and only if this is true.
|
||||
dialect: the dialect used to parse the input expression.
|
||||
opts: other options to use to parse the input expressions.
|
||||
|
@ -1313,7 +1313,7 @@ class Query(Expression):
|
|||
Returns:
|
||||
The new Except expression.
|
||||
"""
|
||||
return except_(left=self, right=expression, distinct=distinct, dialect=dialect, **opts)
|
||||
return except_(self, *expressions, distinct=distinct, dialect=dialect, **opts)
|
||||
|
||||
|
||||
class UDTF(DerivedTable):
|
||||
|
@ -1697,7 +1697,7 @@ class RenameColumn(Expression):
|
|||
arg_types = {"this": True, "to": True, "exists": False}
|
||||
|
||||
|
||||
class RenameTable(Expression):
|
||||
class AlterRename(Expression):
|
||||
pass
|
||||
|
||||
|
||||
|
@ -2400,6 +2400,7 @@ class Join(Expression):
|
|||
"global": False,
|
||||
"hint": False,
|
||||
"match_condition": False, # Snowflake
|
||||
"expressions": False,
|
||||
}
|
||||
|
||||
@property
|
||||
|
@ -2995,6 +2996,10 @@ class WithSystemVersioningProperty(Property):
|
|||
}
|
||||
|
||||
|
||||
class WithProcedureOptions(Property):
|
||||
arg_types = {"expressions": True}
|
||||
|
||||
|
||||
class Properties(Expression):
|
||||
arg_types = {"expressions": True}
|
||||
|
||||
|
@ -3213,10 +3218,18 @@ class Table(Expression):
|
|||
|
||||
def to_column(self, copy: bool = True) -> Alias | Column | Dot:
|
||||
parts = self.parts
|
||||
col = column(*reversed(parts[0:4]), fields=parts[4:], copy=copy) # type: ignore
|
||||
last_part = parts[-1]
|
||||
|
||||
if isinstance(last_part, Identifier):
|
||||
col = column(*reversed(parts[0:4]), fields=parts[4:], copy=copy) # type: ignore
|
||||
else:
|
||||
# This branch will be reached if a function or array is wrapped in a `Table`
|
||||
col = last_part
|
||||
|
||||
alias = self.args.get("alias")
|
||||
if alias:
|
||||
col = alias_(col, alias.this, copy=copy)
|
||||
|
||||
return col
|
||||
|
||||
|
||||
|
@ -3278,7 +3291,7 @@ class Intersect(SetOperation):
|
|||
pass
|
||||
|
||||
|
||||
class Update(Expression):
|
||||
class Update(DML):
|
||||
arg_types = {
|
||||
"with": False,
|
||||
"this": False,
|
||||
|
@ -3526,6 +3539,7 @@ class Select(Query):
|
|||
"distinct": False,
|
||||
"into": False,
|
||||
"from": False,
|
||||
"operation_modifiers": False,
|
||||
**QUERY_MODIFIERS,
|
||||
}
|
||||
|
||||
|
@ -5184,6 +5198,14 @@ class ToNumber(Func):
|
|||
}
|
||||
|
||||
|
||||
# https://docs.snowflake.com/en/sql-reference/functions/to_double
|
||||
class ToDouble(Func):
|
||||
arg_types = {
|
||||
"this": True,
|
||||
"format": False,
|
||||
}
|
||||
|
||||
|
||||
class Columns(Func):
|
||||
arg_types = {"this": True, "unpack": False}
|
||||
|
||||
|
@ -5641,7 +5663,7 @@ class Exp(Func):
|
|||
|
||||
|
||||
# https://docs.snowflake.com/en/sql-reference/functions/flatten
|
||||
class Explode(Func):
|
||||
class Explode(Func, UDTF):
|
||||
arg_types = {"this": True, "expressions": False}
|
||||
is_var_len_args = True
|
||||
|
||||
|
@ -6248,6 +6270,11 @@ class Split(Func):
|
|||
arg_types = {"this": True, "expression": True, "limit": False}
|
||||
|
||||
|
||||
# https://spark.apache.org/docs/latest/api/python/reference/pyspark.sql/api/pyspark.sql.functions.split_part.html
|
||||
class SplitPart(Func):
|
||||
arg_types = {"this": True, "delimiter": True, "part_index": True}
|
||||
|
||||
|
||||
# Start may be omitted in the case of postgres
|
||||
# https://www.postgresql.org/docs/9.1/functions-string.html @ Table 9-6
|
||||
class Substring(Func):
|
||||
|
@ -6857,26 +6884,37 @@ def _wrap(expression: E, kind: t.Type[Expression]) -> E | Paren:
|
|||
return Paren(this=expression) if isinstance(expression, kind) else expression
|
||||
|
||||
|
||||
def _apply_set_operation(
|
||||
*expressions: ExpOrStr,
|
||||
set_operation: t.Type[S],
|
||||
distinct: bool = True,
|
||||
dialect: DialectType = None,
|
||||
copy: bool = True,
|
||||
**opts,
|
||||
) -> S:
|
||||
return reduce(
|
||||
lambda x, y: set_operation(this=x, expression=y, distinct=distinct),
|
||||
(maybe_parse(e, dialect=dialect, copy=copy, **opts) for e in expressions),
|
||||
)
|
||||
|
||||
|
||||
def union(
|
||||
left: ExpOrStr,
|
||||
right: ExpOrStr,
|
||||
*expressions: ExpOrStr,
|
||||
distinct: bool = True,
|
||||
dialect: DialectType = None,
|
||||
copy: bool = True,
|
||||
**opts,
|
||||
) -> Union:
|
||||
"""
|
||||
Initializes a syntax tree from one UNION expression.
|
||||
Initializes a syntax tree for the `UNION` operation.
|
||||
|
||||
Example:
|
||||
>>> union("SELECT * FROM foo", "SELECT * FROM bla").sql()
|
||||
'SELECT * FROM foo UNION SELECT * FROM bla'
|
||||
|
||||
Args:
|
||||
left: the SQL code string corresponding to the left-hand side.
|
||||
If an `Expression` instance is passed, it will be used as-is.
|
||||
right: the SQL code string corresponding to the right-hand side.
|
||||
If an `Expression` instance is passed, it will be used as-is.
|
||||
expressions: the SQL code strings, corresponding to the `UNION`'s operands.
|
||||
If `Expression` instances are passed, they will be used as-is.
|
||||
distinct: set the DISTINCT flag if and only if this is true.
|
||||
dialect: the dialect used to parse the input expression.
|
||||
copy: whether to copy the expression.
|
||||
|
@ -6885,32 +6923,29 @@ def union(
|
|||
Returns:
|
||||
The new Union instance.
|
||||
"""
|
||||
left = maybe_parse(sql_or_expression=left, dialect=dialect, copy=copy, **opts)
|
||||
right = maybe_parse(sql_or_expression=right, dialect=dialect, copy=copy, **opts)
|
||||
|
||||
return Union(this=left, expression=right, distinct=distinct)
|
||||
assert len(expressions) >= 2, "At least two expressions are required by `union`."
|
||||
return _apply_set_operation(
|
||||
*expressions, set_operation=Union, distinct=distinct, dialect=dialect, copy=copy, **opts
|
||||
)
|
||||
|
||||
|
||||
def intersect(
|
||||
left: ExpOrStr,
|
||||
right: ExpOrStr,
|
||||
*expressions: ExpOrStr,
|
||||
distinct: bool = True,
|
||||
dialect: DialectType = None,
|
||||
copy: bool = True,
|
||||
**opts,
|
||||
) -> Intersect:
|
||||
"""
|
||||
Initializes a syntax tree from one INTERSECT expression.
|
||||
Initializes a syntax tree for the `INTERSECT` operation.
|
||||
|
||||
Example:
|
||||
>>> intersect("SELECT * FROM foo", "SELECT * FROM bla").sql()
|
||||
'SELECT * FROM foo INTERSECT SELECT * FROM bla'
|
||||
|
||||
Args:
|
||||
left: the SQL code string corresponding to the left-hand side.
|
||||
If an `Expression` instance is passed, it will be used as-is.
|
||||
right: the SQL code string corresponding to the right-hand side.
|
||||
If an `Expression` instance is passed, it will be used as-is.
|
||||
expressions: the SQL code strings, corresponding to the `INTERSECT`'s operands.
|
||||
If `Expression` instances are passed, they will be used as-is.
|
||||
distinct: set the DISTINCT flag if and only if this is true.
|
||||
dialect: the dialect used to parse the input expression.
|
||||
copy: whether to copy the expression.
|
||||
|
@ -6919,32 +6954,29 @@ def intersect(
|
|||
Returns:
|
||||
The new Intersect instance.
|
||||
"""
|
||||
left = maybe_parse(sql_or_expression=left, dialect=dialect, copy=copy, **opts)
|
||||
right = maybe_parse(sql_or_expression=right, dialect=dialect, copy=copy, **opts)
|
||||
|
||||
return Intersect(this=left, expression=right, distinct=distinct)
|
||||
assert len(expressions) >= 2, "At least two expressions are required by `intersect`."
|
||||
return _apply_set_operation(
|
||||
*expressions, set_operation=Intersect, distinct=distinct, dialect=dialect, copy=copy, **opts
|
||||
)
|
||||
|
||||
|
||||
def except_(
|
||||
left: ExpOrStr,
|
||||
right: ExpOrStr,
|
||||
*expressions: ExpOrStr,
|
||||
distinct: bool = True,
|
||||
dialect: DialectType = None,
|
||||
copy: bool = True,
|
||||
**opts,
|
||||
) -> Except:
|
||||
"""
|
||||
Initializes a syntax tree from one EXCEPT expression.
|
||||
Initializes a syntax tree for the `EXCEPT` operation.
|
||||
|
||||
Example:
|
||||
>>> except_("SELECT * FROM foo", "SELECT * FROM bla").sql()
|
||||
'SELECT * FROM foo EXCEPT SELECT * FROM bla'
|
||||
|
||||
Args:
|
||||
left: the SQL code string corresponding to the left-hand side.
|
||||
If an `Expression` instance is passed, it will be used as-is.
|
||||
right: the SQL code string corresponding to the right-hand side.
|
||||
If an `Expression` instance is passed, it will be used as-is.
|
||||
expressions: the SQL code strings, corresponding to the `EXCEPT`'s operands.
|
||||
If `Expression` instances are passed, they will be used as-is.
|
||||
distinct: set the DISTINCT flag if and only if this is true.
|
||||
dialect: the dialect used to parse the input expression.
|
||||
copy: whether to copy the expression.
|
||||
|
@ -6953,10 +6985,10 @@ def except_(
|
|||
Returns:
|
||||
The new Except instance.
|
||||
"""
|
||||
left = maybe_parse(sql_or_expression=left, dialect=dialect, copy=copy, **opts)
|
||||
right = maybe_parse(sql_or_expression=right, dialect=dialect, copy=copy, **opts)
|
||||
|
||||
return Except(this=left, expression=right, distinct=distinct)
|
||||
assert len(expressions) >= 2, "At least two expressions are required by `except_`."
|
||||
return _apply_set_operation(
|
||||
*expressions, set_operation=Except, distinct=distinct, dialect=dialect, copy=copy, **opts
|
||||
)
|
||||
|
||||
|
||||
def select(*expressions: ExpOrStr, dialect: DialectType = None, **opts) -> Select:
|
||||
|
@ -7410,15 +7442,9 @@ def to_interval(interval: str | Literal) -> Interval:
|
|||
|
||||
interval = interval.this
|
||||
|
||||
interval_parts = INTERVAL_STRING_RE.match(interval) # type: ignore
|
||||
|
||||
if not interval_parts:
|
||||
raise ValueError("Invalid interval string.")
|
||||
|
||||
return Interval(
|
||||
this=Literal.string(interval_parts.group(1)),
|
||||
unit=Var(this=interval_parts.group(2).upper()),
|
||||
)
|
||||
interval = maybe_parse(f"INTERVAL {interval}")
|
||||
assert isinstance(interval, Interval)
|
||||
return interval
|
||||
|
||||
|
||||
def to_table(
|
||||
|
@ -7795,7 +7821,7 @@ def rename_table(
|
|||
this=old_table,
|
||||
kind="TABLE",
|
||||
actions=[
|
||||
RenameTable(this=new_table),
|
||||
AlterRename(this=new_table),
|
||||
],
|
||||
)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue