summaryrefslogtreecommitdiffstats
path: root/sqlglot/expressions.py
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--sqlglot/expressions.py81
1 files changed, 64 insertions, 17 deletions
diff --git a/sqlglot/expressions.py b/sqlglot/expressions.py
index 8e9575e..1e4aad6 100644
--- a/sqlglot/expressions.py
+++ b/sqlglot/expressions.py
@@ -52,6 +52,9 @@ class _Expression(type):
return klass
+SQLGLOT_META = "sqlglot.meta"
+
+
class Expression(metaclass=_Expression):
"""
The base class for all expressions in a syntax tree. Each Expression encapsulates any necessary
@@ -266,7 +269,14 @@ class Expression(metaclass=_Expression):
if self.comments is None:
self.comments = []
if comments:
- self.comments.extend(comments)
+ for comment in comments:
+ _, *meta = comment.split(SQLGLOT_META)
+ if meta:
+ for kv in "".join(meta).split(","):
+ k, *v = kv.split("=")
+ value = v[0].strip() if v else True
+ self.meta[k.strip()] = value
+ self.comments.append(comment)
def append(self, arg_key: str, value: t.Any) -> None:
"""
@@ -1036,11 +1046,14 @@ class Create(DDL):
"indexes": False,
"no_schema_binding": False,
"begin": False,
+ "end": False,
"clone": False,
}
# https://docs.snowflake.com/en/sql-reference/sql/create-clone
+# https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#create_table_clone_statement
+# https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#create_table_copy
class Clone(Expression):
arg_types = {
"this": True,
@@ -1048,6 +1061,7 @@ class Clone(Expression):
"kind": False,
"shallow": False,
"expression": False,
+ "copy": False,
}
@@ -1610,6 +1624,11 @@ class Identifier(Expression):
return self.name
+# https://www.postgresql.org/docs/current/indexes-opclass.html
+class Opclass(Expression):
+ arg_types = {"this": True, "expression": True}
+
+
class Index(Expression):
arg_types = {
"this": False,
@@ -2156,6 +2175,10 @@ class QueryTransform(Expression):
}
+class SampleProperty(Property):
+ arg_types = {"this": True}
+
+
class SchemaCommentProperty(Property):
arg_types = {"this": True}
@@ -2440,6 +2463,8 @@ class Table(Expression):
"hints": False,
"system_time": False,
"version": False,
+ "format": False,
+ "pattern": False,
}
@property
@@ -2465,17 +2490,17 @@ class Table(Expression):
return []
@property
- def parts(self) -> t.List[Identifier]:
+ def parts(self) -> t.List[Expression]:
"""Return the parts of a table in order catalog, db, table."""
- parts: t.List[Identifier] = []
+ parts: t.List[Expression] = []
for arg in ("catalog", "db", "this"):
part = self.args.get(arg)
- if isinstance(part, Identifier):
- parts.append(part)
- elif isinstance(part, Dot):
+ if isinstance(part, Dot):
parts.extend(part.flatten())
+ elif isinstance(part, Expression):
+ parts.append(part)
return parts
@@ -2910,6 +2935,7 @@ class Select(Subqueryable):
prefix="OFFSET",
dialect=dialect,
copy=copy,
+ into_arg="expression",
**opts,
)
@@ -3572,6 +3598,7 @@ class DataType(Expression):
UINT128 = auto()
UINT256 = auto()
UMEDIUMINT = auto()
+ UDECIMAL = auto()
UNIQUEIDENTIFIER = auto()
UNKNOWN = auto() # Sentinel value, useful for type annotation
USERDEFINED = "USER-DEFINED"
@@ -3693,13 +3720,13 @@ class DataType(Expression):
# https://www.postgresql.org/docs/15/datatype-pseudo.html
-class PseudoType(Expression):
- pass
+class PseudoType(DataType):
+ arg_types = {"this": True}
# https://www.postgresql.org/docs/15/datatype-oid.html
-class ObjectIdentifier(Expression):
- pass
+class ObjectIdentifier(DataType):
+ arg_types = {"this": True}
# WHERE x <OP> EXISTS|ALL|ANY|SOME(SELECT ...)
@@ -4027,10 +4054,20 @@ class TimeUnit(Expression):
return self.args.get("unit")
+class IntervalOp(TimeUnit):
+ arg_types = {"unit": True, "expression": True}
+
+ def interval(self):
+ return Interval(
+ this=self.expression.copy(),
+ unit=self.unit.copy(),
+ )
+
+
# https://www.oracletutorial.com/oracle-basics/oracle-interval/
# https://trino.io/docs/current/language/types.html#interval-day-to-second
# https://docs.databricks.com/en/sql/language-manual/data-types/interval-type.html
-class IntervalSpan(Expression):
+class IntervalSpan(DataType):
arg_types = {"this": True, "expression": True}
@@ -4269,7 +4306,7 @@ class CastToStrType(Func):
arg_types = {"this": True, "to": True}
-class Collate(Binary):
+class Collate(Binary, Func):
pass
@@ -4284,6 +4321,12 @@ class Coalesce(Func):
_sql_names = ["COALESCE", "IFNULL", "NVL"]
+class Chr(Func):
+ arg_types = {"this": True, "charset": False, "expressions": False}
+ is_var_len_args = True
+ _sql_names = ["CHR", "CHAR"]
+
+
class Concat(Func):
arg_types = {"expressions": True}
is_var_len_args = True
@@ -4326,11 +4369,11 @@ class CurrentUser(Func):
arg_types = {"this": False}
-class DateAdd(Func, TimeUnit):
+class DateAdd(Func, IntervalOp):
arg_types = {"this": True, "expression": True, "unit": False}
-class DateSub(Func, TimeUnit):
+class DateSub(Func, IntervalOp):
arg_types = {"this": True, "expression": True, "unit": False}
@@ -4347,11 +4390,11 @@ class DateTrunc(Func):
return self.args["unit"]
-class DatetimeAdd(Func, TimeUnit):
+class DatetimeAdd(Func, IntervalOp):
arg_types = {"this": True, "expression": True, "unit": False}
-class DatetimeSub(Func, TimeUnit):
+class DatetimeSub(Func, IntervalOp):
arg_types = {"this": True, "expression": True, "unit": False}
@@ -4375,6 +4418,10 @@ class DayOfYear(Func):
_sql_names = ["DAY_OF_YEAR", "DAYOFYEAR"]
+class ToDays(Func):
+ pass
+
+
class WeekOfYear(Func):
_sql_names = ["WEEK_OF_YEAR", "WEEKOFYEAR"]
@@ -6160,7 +6207,7 @@ def table_name(table: Table | str, dialect: DialectType = None) -> str:
The table name.
"""
- table = maybe_parse(table, into=Table)
+ table = maybe_parse(table, into=Table, dialect=dialect)
if not table:
raise ValueError(f"Cannot parse {table}")