Skip to content

Commit

Permalink
chore: cleanup teradata to simplify first
Browse files Browse the repository at this point in the history
  • Loading branch information
tobymao committed Apr 11, 2024
1 parent b28cd89 commit b523bb6
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions sqlglot/dialects/teradata.py
Expand Up @@ -19,20 +19,19 @@ def _date_add_sql(
def func(self: Teradata.Generator, expression: exp.DateAdd | exp.DateSub) -> str:
this = self.sql(expression, "this")
unit = expression.args.get("unit")
value = self._simplify_unless_literal(expression.expression)

if expression.expression.is_negative:
if not isinstance(value, exp.Literal):
self.unsupported("Cannot add non literal")

if value.is_negative:
kind_to_op = {"+": "-", "-": "+"}
simplify_expr = expression.expression.this
value = exp.Literal.string(value.name[1:])
else:
kind_to_op = {"+": "+", "-": "-"}
simplify_expr = expression.expression

expression = self._simplify_unless_literal(simplify_expr)
if not isinstance(expression, exp.Literal):
self.unsupported("Cannot add non literal")
value.set("is_string", True)

expression.set("is_string", True)
return f"{this} {kind_to_op[kind]} {self.sql(exp.Interval(this=expression, unit=unit))}"
return f"{this} {kind_to_op[kind]} {self.sql(exp.Interval(this=value, unit=unit))}"

return func

Expand Down

0 comments on commit b523bb6

Please sign in to comment.