Merging upstream version 20.11.0.
Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
parent
1bce3d0317
commit
e71ccc03da
141 changed files with 66644 additions and 54334 deletions
|
@ -23,7 +23,6 @@ from copy import deepcopy
|
|||
from enum import auto
|
||||
from functools import reduce
|
||||
|
||||
from sqlglot._typing import E
|
||||
from sqlglot.errors import ErrorLevel, ParseError
|
||||
from sqlglot.helper import (
|
||||
AutoName,
|
||||
|
@ -36,8 +35,7 @@ from sqlglot.helper import (
|
|||
from sqlglot.tokens import Token
|
||||
|
||||
if t.TYPE_CHECKING:
|
||||
from typing_extensions import Literal as Lit
|
||||
|
||||
from sqlglot._typing import E, Lit
|
||||
from sqlglot.dialects.dialect import DialectType
|
||||
|
||||
|
||||
|
@ -389,7 +387,7 @@ class Expression(metaclass=_Expression):
|
|||
ancestor = self.parent
|
||||
while ancestor and not isinstance(ancestor, expression_types):
|
||||
ancestor = ancestor.parent
|
||||
return t.cast(E, ancestor)
|
||||
return ancestor # type: ignore
|
||||
|
||||
@property
|
||||
def parent_select(self) -> t.Optional[Select]:
|
||||
|
@ -555,12 +553,10 @@ class Expression(metaclass=_Expression):
|
|||
return new_node
|
||||
|
||||
@t.overload
|
||||
def replace(self, expression: E) -> E:
|
||||
...
|
||||
def replace(self, expression: E) -> E: ...
|
||||
|
||||
@t.overload
|
||||
def replace(self, expression: None) -> None:
|
||||
...
|
||||
def replace(self, expression: None) -> None: ...
|
||||
|
||||
def replace(self, expression):
|
||||
"""
|
||||
|
@ -781,13 +777,16 @@ class Expression(metaclass=_Expression):
|
|||
this=maybe_copy(self, copy),
|
||||
expressions=[convert(e, copy=copy) for e in expressions],
|
||||
query=maybe_parse(query, copy=copy, **opts) if query else None,
|
||||
unnest=Unnest(
|
||||
expressions=[
|
||||
maybe_parse(t.cast(ExpOrStr, e), copy=copy, **opts) for e in ensure_list(unnest)
|
||||
]
|
||||
)
|
||||
if unnest
|
||||
else None,
|
||||
unnest=(
|
||||
Unnest(
|
||||
expressions=[
|
||||
maybe_parse(t.cast(ExpOrStr, e), copy=copy, **opts)
|
||||
for e in ensure_list(unnest)
|
||||
]
|
||||
)
|
||||
if unnest
|
||||
else None
|
||||
),
|
||||
)
|
||||
|
||||
def between(self, low: t.Any, high: t.Any, copy: bool = True, **opts) -> Between:
|
||||
|
@ -926,7 +925,7 @@ class DerivedTable(Expression):
|
|||
class Unionable(Expression):
|
||||
def union(
|
||||
self, expression: ExpOrStr, distinct: bool = True, dialect: DialectType = None, **opts
|
||||
) -> Unionable:
|
||||
) -> Union:
|
||||
"""
|
||||
Builds a UNION expression.
|
||||
|
||||
|
@ -1134,9 +1133,12 @@ class SetItem(Expression):
|
|||
class Show(Expression):
|
||||
arg_types = {
|
||||
"this": True,
|
||||
"terse": False,
|
||||
"target": False,
|
||||
"offset": False,
|
||||
"starts_with": False,
|
||||
"limit": False,
|
||||
"from": False,
|
||||
"like": False,
|
||||
"where": False,
|
||||
"db": False,
|
||||
|
@ -1274,9 +1276,14 @@ class AlterColumn(Expression):
|
|||
"using": False,
|
||||
"default": False,
|
||||
"drop": False,
|
||||
"comment": False,
|
||||
}
|
||||
|
||||
|
||||
class RenameColumn(Expression):
|
||||
arg_types = {"this": True, "to": True, "exists": False}
|
||||
|
||||
|
||||
class RenameTable(Expression):
|
||||
pass
|
||||
|
||||
|
@ -1402,7 +1409,7 @@ class GeneratedAsIdentityColumnConstraint(ColumnConstraintKind):
|
|||
|
||||
|
||||
class GeneratedAsRowColumnConstraint(ColumnConstraintKind):
|
||||
arg_types = {"start": True, "hidden": False}
|
||||
arg_types = {"start": False, "hidden": False}
|
||||
|
||||
|
||||
# https://dev.mysql.com/doc/refman/8.0/en/create-table.html
|
||||
|
@ -1667,6 +1674,7 @@ class Index(Expression):
|
|||
"unique": False,
|
||||
"primary": False,
|
||||
"amp": False, # teradata
|
||||
"include": False,
|
||||
"partition_by": False, # teradata
|
||||
"where": False, # postgres partial indexes
|
||||
}
|
||||
|
@ -2016,7 +2024,13 @@ class AutoRefreshProperty(Property):
|
|||
|
||||
|
||||
class BlockCompressionProperty(Property):
|
||||
arg_types = {"autotemp": False, "always": False, "default": True, "manual": True, "never": True}
|
||||
arg_types = {
|
||||
"autotemp": False,
|
||||
"always": False,
|
||||
"default": False,
|
||||
"manual": False,
|
||||
"never": False,
|
||||
}
|
||||
|
||||
|
||||
class CharacterSetProperty(Property):
|
||||
|
@ -2089,6 +2103,10 @@ class FreespaceProperty(Property):
|
|||
arg_types = {"this": True, "percent": False}
|
||||
|
||||
|
||||
class InheritsProperty(Property):
|
||||
arg_types = {"expressions": True}
|
||||
|
||||
|
||||
class InputModelProperty(Property):
|
||||
arg_types = {"this": True}
|
||||
|
||||
|
@ -2099,11 +2117,11 @@ class OutputModelProperty(Property):
|
|||
|
||||
class IsolatedLoadingProperty(Property):
|
||||
arg_types = {
|
||||
"no": True,
|
||||
"concurrent": True,
|
||||
"for_all": True,
|
||||
"for_insert": True,
|
||||
"for_none": True,
|
||||
"no": False,
|
||||
"concurrent": False,
|
||||
"for_all": False,
|
||||
"for_insert": False,
|
||||
"for_none": False,
|
||||
}
|
||||
|
||||
|
||||
|
@ -2264,6 +2282,10 @@ class SetProperty(Property):
|
|||
arg_types = {"multi": True}
|
||||
|
||||
|
||||
class SetConfigProperty(Property):
|
||||
arg_types = {"this": True}
|
||||
|
||||
|
||||
class SettingsProperty(Property):
|
||||
arg_types = {"expressions": True}
|
||||
|
||||
|
@ -2407,13 +2429,16 @@ class Tuple(Expression):
|
|||
this=maybe_copy(self, copy),
|
||||
expressions=[convert(e, copy=copy) for e in expressions],
|
||||
query=maybe_parse(query, copy=copy, **opts) if query else None,
|
||||
unnest=Unnest(
|
||||
expressions=[
|
||||
maybe_parse(t.cast(ExpOrStr, e), copy=copy, **opts) for e in ensure_list(unnest)
|
||||
]
|
||||
)
|
||||
if unnest
|
||||
else None,
|
||||
unnest=(
|
||||
Unnest(
|
||||
expressions=[
|
||||
maybe_parse(t.cast(ExpOrStr, e), copy=copy, **opts)
|
||||
for e in ensure_list(unnest)
|
||||
]
|
||||
)
|
||||
if unnest
|
||||
else None
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
|
@ -3631,6 +3656,8 @@ class DataType(Expression):
|
|||
|
||||
class Type(AutoName):
|
||||
ARRAY = auto()
|
||||
AGGREGATEFUNCTION = auto()
|
||||
SIMPLEAGGREGATEFUNCTION = auto()
|
||||
BIGDECIMAL = auto()
|
||||
BIGINT = auto()
|
||||
BIGSERIAL = auto()
|
||||
|
@ -4162,6 +4189,10 @@ class AtTimeZone(Expression):
|
|||
arg_types = {"this": True, "zone": True}
|
||||
|
||||
|
||||
class FromTimeZone(Expression):
|
||||
arg_types = {"this": True, "zone": True}
|
||||
|
||||
|
||||
class Between(Predicate):
|
||||
arg_types = {"this": True, "low": True, "high": True}
|
||||
|
||||
|
@ -5456,8 +5487,7 @@ def maybe_parse(
|
|||
prefix: t.Optional[str] = None,
|
||||
copy: bool = False,
|
||||
**opts,
|
||||
) -> E:
|
||||
...
|
||||
) -> E: ...
|
||||
|
||||
|
||||
@t.overload
|
||||
|
@ -5469,8 +5499,7 @@ def maybe_parse(
|
|||
prefix: t.Optional[str] = None,
|
||||
copy: bool = False,
|
||||
**opts,
|
||||
) -> E:
|
||||
...
|
||||
) -> E: ...
|
||||
|
||||
|
||||
def maybe_parse(
|
||||
|
@ -5522,13 +5551,11 @@ def maybe_parse(
|
|||
|
||||
|
||||
@t.overload
|
||||
def maybe_copy(instance: None, copy: bool = True) -> None:
|
||||
...
|
||||
def maybe_copy(instance: None, copy: bool = True) -> None: ...
|
||||
|
||||
|
||||
@t.overload
|
||||
def maybe_copy(instance: E, copy: bool = True) -> E:
|
||||
...
|
||||
def maybe_copy(instance: E, copy: bool = True) -> E: ...
|
||||
|
||||
|
||||
def maybe_copy(instance, copy=True):
|
||||
|
@ -6151,15 +6178,13 @@ SAFE_IDENTIFIER_RE = re.compile(r"^[_a-zA-Z][\w]*$")
|
|||
|
||||
|
||||
@t.overload
|
||||
def to_identifier(name: None, quoted: t.Optional[bool] = None, copy: bool = True) -> None:
|
||||
...
|
||||
def to_identifier(name: None, quoted: t.Optional[bool] = None, copy: bool = True) -> None: ...
|
||||
|
||||
|
||||
@t.overload
|
||||
def to_identifier(
|
||||
name: str | Identifier, quoted: t.Optional[bool] = None, copy: bool = True
|
||||
) -> Identifier:
|
||||
...
|
||||
) -> Identifier: ...
|
||||
|
||||
|
||||
def to_identifier(name, quoted=None, copy=True):
|
||||
|
@ -6231,13 +6256,11 @@ def to_interval(interval: str | Literal) -> Interval:
|
|||
|
||||
|
||||
@t.overload
|
||||
def to_table(sql_path: str | Table, **kwargs) -> Table:
|
||||
...
|
||||
def to_table(sql_path: str | Table, **kwargs) -> Table: ...
|
||||
|
||||
|
||||
@t.overload
|
||||
def to_table(sql_path: None, **kwargs) -> None:
|
||||
...
|
||||
def to_table(sql_path: None, **kwargs) -> None: ...
|
||||
|
||||
|
||||
def to_table(
|
||||
|
@ -6562,6 +6585,34 @@ def rename_table(old_name: str | Table, new_name: str | Table) -> AlterTable:
|
|||
)
|
||||
|
||||
|
||||
def rename_column(
|
||||
table_name: str | Table,
|
||||
old_column_name: str | Column,
|
||||
new_column_name: str | Column,
|
||||
exists: t.Optional[bool] = None,
|
||||
) -> AlterTable:
|
||||
"""Build ALTER TABLE... RENAME COLUMN... expression
|
||||
|
||||
Args:
|
||||
table_name: Name of the table
|
||||
old_column: The old name of the column
|
||||
new_column: The new name of the column
|
||||
exists: Whether or not to add the `IF EXISTS` clause
|
||||
|
||||
Returns:
|
||||
Alter table expression
|
||||
"""
|
||||
table = to_table(table_name)
|
||||
old_column = to_column(old_column_name)
|
||||
new_column = to_column(new_column_name)
|
||||
return AlterTable(
|
||||
this=table,
|
||||
actions=[
|
||||
RenameColumn(this=old_column, to=new_column, exists=exists),
|
||||
],
|
||||
)
|
||||
|
||||
|
||||
def convert(value: t.Any, copy: bool = False) -> Expression:
|
||||
"""Convert a python value into an expression object.
|
||||
|
||||
|
@ -6581,7 +6632,7 @@ def convert(value: t.Any, copy: bool = False) -> Expression:
|
|||
if isinstance(value, bool):
|
||||
return Boolean(this=value)
|
||||
if value is None or (isinstance(value, float) and math.isnan(value)):
|
||||
return NULL
|
||||
return null()
|
||||
if isinstance(value, numbers.Number):
|
||||
return Literal.number(value)
|
||||
if isinstance(value, datetime.datetime):
|
||||
|
@ -6674,9 +6725,11 @@ def table_name(table: Table | str, dialect: DialectType = None, identify: bool =
|
|||
raise ValueError(f"Cannot parse {table}")
|
||||
|
||||
return ".".join(
|
||||
part.sql(dialect=dialect, identify=True, copy=False)
|
||||
if identify or not SAFE_IDENTIFIER_RE.match(part.name)
|
||||
else part.name
|
||||
(
|
||||
part.sql(dialect=dialect, identify=True, copy=False)
|
||||
if identify or not SAFE_IDENTIFIER_RE.match(part.name)
|
||||
else part.name
|
||||
)
|
||||
for part in table.parts
|
||||
)
|
||||
|
||||
|
@ -6942,9 +6995,3 @@ def null() -> Null:
|
|||
Returns a Null expression.
|
||||
"""
|
||||
return Null()
|
||||
|
||||
|
||||
# TODO: deprecate this
|
||||
TRUE = Boolean(this=True)
|
||||
FALSE = Boolean(this=False)
|
||||
NULL = Null()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue