Skip to content

Commit

Permalink
fix: extract from time/date
Browse files Browse the repository at this point in the history
  • Loading branch information
tobymao committed Jul 17, 2024
1 parent dcc783a commit b42b7ac
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
13 changes: 12 additions & 1 deletion sqlglot/optimizer/annotate_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ class TypeAnnotator(metaclass=_TypeAnnotator):
exp.Ceil,
exp.DatetimeDiff,
exp.DateDiff,
exp.Extract,
exp.TimestampDiff,
exp.TimeDiff,
exp.DateToDi,
Expand Down Expand Up @@ -268,6 +267,7 @@ class TypeAnnotator(metaclass=_TypeAnnotator):
exp.Div: lambda self, e: self._annotate_div(e),
exp.Dot: lambda self, e: self._annotate_dot(e),
exp.Explode: lambda self, e: self._annotate_explode(e),
exp.Extract: lambda self, e: self._annotate_extract(e),
exp.Filter: lambda self, e: self._annotate_by_args(e, "this"),
exp.GenerateDateArray: lambda self, e: self._annotate_with_type(
e, exp.DataType.build("ARRAY<DATE>")
Expand Down Expand Up @@ -680,3 +680,14 @@ def _annotate_to_map(self, expression: exp.ToMap) -> exp.ToMap:

self._set_type(expression, map_type)
return expression

def _annotate_extract(self, expression: exp.Extract) -> exp.Extract:
self._annotate_args(expression)
part = expression.name
if part == "TIME":
self._set_type(expression, exp.DataType.Type.TIME)
elif part == "DATE":
self._set_type(expression, exp.DataType.Type.DATE)
else:
self._set_type(expression, exp.DataType.Type.INT)
return expression
12 changes: 12 additions & 0 deletions tests/fixtures/optimizer/annotate_types.sql
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,15 @@ STRUCT<a INT, b DOUBLE>;
# dialect: presto
ROW(1, 2.5, 'foo');
STRUCT<INT, DOUBLE, VARCHAR>;

# dialect: bigquery
EXTRACT(date from x);
DATE;

# dialect: bigquery
EXTRACT(time from x);
TIME;

# dialect: bigquery
EXTRACT(day from x);
INT;

0 comments on commit b42b7ac

Please sign in to comment.