summaryrefslogtreecommitdiffstats
path: root/sqlglot/expressions.py
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2023-02-27 10:46:36 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2023-02-27 10:46:36 +0000
commita8b22b4c5bdf9139a187c92b7b9f81bdeaa84888 (patch)
tree93b8523df3ce9e02e435f56e493bd9b724eb9c7c /sqlglot/expressions.py
parentReleasing debian version 11.2.0-1. (diff)
downloadsqlglot-a8b22b4c5bdf9139a187c92b7b9f81bdeaa84888.tar.xz
sqlglot-a8b22b4c5bdf9139a187c92b7b9f81bdeaa84888.zip
Merging upstream version 11.2.3.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'sqlglot/expressions.py')
-rw-r--r--sqlglot/expressions.py227
1 files changed, 143 insertions, 84 deletions
diff --git a/sqlglot/expressions.py b/sqlglot/expressions.py
index a29aeb4..59881d6 100644
--- a/sqlglot/expressions.py
+++ b/sqlglot/expressions.py
@@ -82,7 +82,7 @@ class Expression(metaclass=_Expression):
key = "expression"
arg_types = {"this": True}
- __slots__ = ("args", "parent", "arg_key", "comments", "_type")
+ __slots__ = ("args", "parent", "arg_key", "comments", "_type", "_meta")
def __init__(self, **args: t.Any):
self.args: t.Dict[str, t.Any] = args
@@ -90,6 +90,7 @@ class Expression(metaclass=_Expression):
self.arg_key: t.Optional[str] = None
self.comments: t.Optional[t.List[str]] = None
self._type: t.Optional[DataType] = None
+ self._meta: t.Optional[t.Dict[str, t.Any]] = None
for arg_key, value in self.args.items():
self._set_parent(arg_key, value)
@@ -219,10 +220,23 @@ class Expression(metaclass=_Expression):
dtype = DataType.build(dtype)
self._type = dtype # type: ignore
+ @property
+ def meta(self) -> t.Dict[str, t.Any]:
+ if self._meta is None:
+ self._meta = {}
+ return self._meta
+
def __deepcopy__(self, memo):
copy = self.__class__(**deepcopy(self.args))
- copy.comments = self.comments
- copy.type = self.type
+ if self.comments is not None:
+ copy.comments = deepcopy(self.comments)
+
+ if self._type is not None:
+ copy._type = self._type.copy()
+
+ if self._meta is not None:
+ copy._meta = deepcopy(self._meta)
+
return copy
def copy(self):
@@ -329,6 +343,15 @@ class Expression(metaclass=_Expression):
"""
return self.find_ancestor(Select)
+ def root(self) -> Expression:
+ """
+ Returns the root expression of this tree.
+ """
+ expression = self
+ while expression.parent:
+ expression = expression.parent
+ return expression
+
def walk(self, bfs=True, prune=None):
"""
Returns a generator object which visits all nodes in this tree.
@@ -767,21 +790,10 @@ class Create(Expression):
"this": True,
"kind": True,
"expression": False,
- "set": False,
- "multiset": False,
- "global_temporary": False,
- "volatile": False,
"exists": False,
"properties": False,
- "temporary": False,
- "transient": False,
- "external": False,
"replace": False,
"unique": False,
- "materialized": False,
- "data": False,
- "statistics": False,
- "no_primary_index": False,
"indexes": False,
"no_schema_binding": False,
"begin": False,
@@ -1336,47 +1348,47 @@ class Property(Expression):
arg_types = {"this": True, "value": True}
-class AlgorithmProperty(Property):
- arg_types = {"this": True}
+class AfterJournalProperty(Property):
+ arg_types = {"no": True, "dual": False, "local": False}
-class DefinerProperty(Property):
+class AlgorithmProperty(Property):
arg_types = {"this": True}
-class SqlSecurityProperty(Property):
- arg_types = {"definer": True}
+class AutoIncrementProperty(Property):
+ arg_types = {"this": True}
-class TableFormatProperty(Property):
- arg_types = {"this": True}
+class BlockCompressionProperty(Property):
+ arg_types = {"autotemp": False, "always": False, "default": True, "manual": True, "never": True}
-class PartitionedByProperty(Property):
- arg_types = {"this": True}
+class CharacterSetProperty(Property):
+ arg_types = {"this": True, "default": True}
-class FileFormatProperty(Property):
- arg_types = {"this": True}
+class ChecksumProperty(Property):
+ arg_types = {"on": False, "default": False}
-class DistKeyProperty(Property):
+class CollateProperty(Property):
arg_types = {"this": True}
-class SortKeyProperty(Property):
- arg_types = {"this": True, "compound": False}
+class DataBlocksizeProperty(Property):
+ arg_types = {"size": False, "units": False, "min": False, "default": False}
-class DistStyleProperty(Property):
+class DefinerProperty(Property):
arg_types = {"this": True}
-class LikeProperty(Property):
- arg_types = {"this": True, "expressions": False}
+class DistKeyProperty(Property):
+ arg_types = {"this": True}
-class LocationProperty(Property):
+class DistStyleProperty(Property):
arg_types = {"this": True}
@@ -1384,38 +1396,90 @@ class EngineProperty(Property):
arg_types = {"this": True}
-class AutoIncrementProperty(Property):
+class ExecuteAsProperty(Property):
arg_types = {"this": True}
-class CharacterSetProperty(Property):
- arg_types = {"this": True, "default": True}
+class ExternalProperty(Property):
+ arg_types = {"this": False}
-class CollateProperty(Property):
- arg_types = {"this": True}
+class FallbackProperty(Property):
+ arg_types = {"no": True, "protection": False}
-class SchemaCommentProperty(Property):
+class FileFormatProperty(Property):
arg_types = {"this": True}
-class ReturnsProperty(Property):
- arg_types = {"this": True, "is_table": False, "table": False}
+class FreespaceProperty(Property):
+ arg_types = {"this": True, "percent": False}
+
+
+class IsolatedLoadingProperty(Property):
+ arg_types = {
+ "no": True,
+ "concurrent": True,
+ "for_all": True,
+ "for_insert": True,
+ "for_none": True,
+ }
+
+
+class JournalProperty(Property):
+ arg_types = {"no": True, "dual": False, "before": False}
class LanguageProperty(Property):
arg_types = {"this": True}
-class ExecuteAsProperty(Property):
+class LikeProperty(Property):
+ arg_types = {"this": True, "expressions": False}
+
+
+class LocationProperty(Property):
arg_types = {"this": True}
-class VolatilityProperty(Property):
+class LockingProperty(Property):
+ arg_types = {
+ "this": False,
+ "kind": True,
+ "for_or_in": True,
+ "lock_type": True,
+ "override": False,
+ }
+
+
+class LogProperty(Property):
+ arg_types = {"no": True}
+
+
+class MaterializedProperty(Property):
+ arg_types = {"this": False}
+
+
+class MergeBlockRatioProperty(Property):
+ arg_types = {"this": False, "no": False, "default": False, "percent": False}
+
+
+class NoPrimaryIndexProperty(Property):
+ arg_types = {"this": False}
+
+
+class OnCommitProperty(Property):
+ arg_type = {"this": False}
+
+
+class PartitionedByProperty(Property):
arg_types = {"this": True}
+class ReturnsProperty(Property):
+ arg_types = {"this": True, "is_table": False, "table": False}
+
+
class RowFormatDelimitedProperty(Property):
# https://cwiki.apache.org/confluence/display/hive/languagemanual+dml
arg_types = {
@@ -1433,68 +1497,48 @@ class RowFormatSerdeProperty(Property):
arg_types = {"this": True}
-class SerdeProperties(Property):
- arg_types = {"expressions": True}
-
-
-class FallbackProperty(Property):
- arg_types = {"no": True, "protection": False}
-
-
-class WithJournalTableProperty(Property):
+class SchemaCommentProperty(Property):
arg_types = {"this": True}
-class LogProperty(Property):
- arg_types = {"no": True}
+class SerdeProperties(Property):
+ arg_types = {"expressions": True}
-class JournalProperty(Property):
- arg_types = {"no": True, "dual": False, "before": False}
+class SetProperty(Property):
+ arg_types = {"multi": True}
-class AfterJournalProperty(Property):
- arg_types = {"no": True, "dual": False, "local": False}
+class SortKeyProperty(Property):
+ arg_types = {"this": True, "compound": False}
-class ChecksumProperty(Property):
- arg_types = {"on": False, "default": False}
+class SqlSecurityProperty(Property):
+ arg_types = {"definer": True}
-class FreespaceProperty(Property):
- arg_types = {"this": True, "percent": False}
+class TableFormatProperty(Property):
+ arg_types = {"this": True}
-class MergeBlockRatioProperty(Property):
- arg_types = {"this": False, "no": False, "default": False, "percent": False}
+class TemporaryProperty(Property):
+ arg_types = {"global_": True}
-class DataBlocksizeProperty(Property):
- arg_types = {"size": False, "units": False, "min": False, "default": False}
+class TransientProperty(Property):
+ arg_types = {"this": False}
-class BlockCompressionProperty(Property):
- arg_types = {"autotemp": False, "always": False, "default": True, "manual": True, "never": True}
+class VolatilityProperty(Property):
+ arg_types = {"this": True}
-class IsolatedLoadingProperty(Property):
- arg_types = {
- "no": True,
- "concurrent": True,
- "for_all": True,
- "for_insert": True,
- "for_none": True,
- }
+class WithDataProperty(Property):
+ arg_types = {"no": True, "statistics": False}
-class LockingProperty(Property):
- arg_types = {
- "this": False,
- "kind": True,
- "for_or_in": True,
- "lock_type": True,
- "override": False,
- }
+class WithJournalTableProperty(Property):
+ arg_types = {"this": True}
class Properties(Expression):
@@ -1533,7 +1577,7 @@ class Properties(Expression):
# Form: alias selection
# create [POST_CREATE]
# table a [POST_NAME]
- # as [POST_ALIAS] (select * from b)
+ # as [POST_ALIAS] (select * from b) [POST_EXPRESSION]
# index (c) [POST_INDEX]
class Location(AutoName):
POST_CREATE = auto()
@@ -1541,6 +1585,7 @@ class Properties(Expression):
POST_SCHEMA = auto()
POST_WITH = auto()
POST_ALIAS = auto()
+ POST_EXPRESSION = auto()
POST_INDEX = auto()
UNSUPPORTED = auto()
@@ -1798,6 +1843,10 @@ class Union(Subqueryable):
return self.this.unnest().named_selects
@property
+ def is_star(self) -> bool:
+ return self.this.is_star or self.expression.is_star
+
+ @property
def selects(self):
return self.this.unnest().selects
@@ -2425,6 +2474,10 @@ class Select(Subqueryable):
return [e.output_name for e in self.expressions if e.alias_or_name]
@property
+ def is_star(self) -> bool:
+ return any(expression.is_star for expression in self.expressions)
+
+ @property
def selects(self) -> t.List[Expression]:
return self.expressions
@@ -2447,6 +2500,10 @@ class Subquery(DerivedTable, Unionable):
return expression
@property
+ def is_star(self) -> bool:
+ return self.this.is_star
+
+ @property
def output_name(self):
return self.alias
@@ -2478,6 +2535,7 @@ class Tag(Expression):
class Pivot(Expression):
arg_types = {
"this": False,
+ "alias": False,
"expressions": True,
"field": True,
"unpivot": True,
@@ -2603,6 +2661,7 @@ class DataType(Expression):
IMAGE = auto()
VARIANT = auto()
OBJECT = auto()
+ INET = auto()
NULL = auto()
UNKNOWN = auto() # Sentinel value, useful for type annotation