summaryrefslogtreecommitdiffstats
path: root/sqlglot/dialects/dialect.py
diff options
context:
space:
mode:
Diffstat (limited to 'sqlglot/dialects/dialect.py')
-rw-r--r--sqlglot/dialects/dialect.py28
1 files changed, 24 insertions, 4 deletions
diff --git a/sqlglot/dialects/dialect.py b/sqlglot/dialects/dialect.py
index 8c84639..05e81ce 100644
--- a/sqlglot/dialects/dialect.py
+++ b/sqlglot/dialects/dialect.py
@@ -5,6 +5,7 @@ from enum import Enum
from sqlglot import exp
from sqlglot._typing import E
+from sqlglot.errors import ParseError
from sqlglot.generator import Generator
from sqlglot.helper import flatten, seq_get
from sqlglot.parser import Parser
@@ -168,6 +169,10 @@ class Dialect(metaclass=_Dialect):
# special syntax cast(x as date format 'yyyy') defaults to time_mapping
FORMAT_MAPPING: t.Dict[str, str] = {}
+ # Columns that are auto-generated by the engine corresponding to this dialect
+ # Such columns may be excluded from SELECT * queries, for example
+ PSEUDOCOLUMNS: t.Set[str] = set()
+
# Autofilled
tokenizer_class = Tokenizer
parser_class = Parser
@@ -497,6 +502,10 @@ def parse_date_delta_with_interval(
return None
interval = args[1]
+
+ if not isinstance(interval, exp.Interval):
+ raise ParseError(f"INTERVAL expression expected but got '{interval}'")
+
expression = interval.this
if expression and expression.is_string:
expression = exp.Literal.number(expression.this)
@@ -555,11 +564,11 @@ def right_to_substring_sql(self: Generator, expression: exp.Left) -> str:
def timestrtotime_sql(self: Generator, expression: exp.TimeStrToTime) -> str:
- return f"CAST({self.sql(expression, 'this')} AS TIMESTAMP)"
+ return self.sql(exp.cast(expression.this, "timestamp"))
def datestrtodate_sql(self: Generator, expression: exp.DateStrToDate) -> str:
- return f"CAST({self.sql(expression, 'this')} AS DATE)"
+ return self.sql(exp.cast(expression.this, "date"))
def min_or_least(self: Generator, expression: exp.Min) -> str:
@@ -608,8 +617,9 @@ def ts_or_ds_to_date_sql(dialect: str) -> t.Callable:
_dialect = Dialect.get_or_raise(dialect)
time_format = self.format_time(expression)
if time_format and time_format not in (_dialect.TIME_FORMAT, _dialect.DATE_FORMAT):
- return f"CAST({str_to_time_sql(self, expression)} AS DATE)"
- return f"CAST({self.sql(expression, 'this')} AS DATE)"
+ return self.sql(exp.cast(str_to_time_sql(self, expression), "date"))
+
+ return self.sql(exp.cast(self.sql(expression, "this"), "date"))
return _ts_or_ds_to_date_sql
@@ -664,5 +674,15 @@ def pivot_column_names(aggregations: t.List[exp.Expression], dialect: DialectTyp
return names
+def simplify_literal(expression: E, copy: bool = True) -> E:
+ if not isinstance(expression.expression, exp.Literal):
+ from sqlglot.optimizer.simplify import simplify
+
+ expression = exp.maybe_copy(expression, copy)
+ simplify(expression.expression)
+
+ return expression
+
+
def binary_from_function(expr_type: t.Type[B]) -> t.Callable[[t.List], B]:
return lambda args: expr_type(this=seq_get(args, 0), expression=seq_get(args, 1))