summaryrefslogtreecommitdiffstats
path: root/sqlglot/dialects/bigquery.py
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-02-16 05:45:49 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-02-16 05:45:49 +0000
commit87bb420e43bf31021b090be98143a32042255a46 (patch)
treef68baf119ccffb6ebc64ba066af1491ddcb0f121 /sqlglot/dialects/bigquery.py
parentAdding upstream version 21.0.2. (diff)
downloadsqlglot-87bb420e43bf31021b090be98143a32042255a46.tar.xz
sqlglot-87bb420e43bf31021b090be98143a32042255a46.zip
Adding upstream version 21.1.1.upstream/21.1.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'sqlglot/dialects/bigquery.py')
-rw-r--r--sqlglot/dialects/bigquery.py50
1 files changed, 30 insertions, 20 deletions
diff --git a/sqlglot/dialects/bigquery.py b/sqlglot/dialects/bigquery.py
index 9068235..c0191b2 100644
--- a/sqlglot/dialects/bigquery.py
+++ b/sqlglot/dialects/bigquery.py
@@ -39,24 +39,31 @@ def _derived_table_values_to_unnest(self: BigQuery.Generator, expression: exp.Va
alias = expression.args.get("alias")
- structs = [
- exp.Struct(
+ return self.unnest_sql(
+ exp.Unnest(
expressions=[
- exp.alias_(value, column_name)
- for value, column_name in zip(
- t.expressions,
- (
- alias.columns
- if alias and alias.columns
- else (f"_c{i}" for i in range(len(t.expressions)))
+ exp.array(
+ *(
+ exp.Struct(
+ expressions=[
+ exp.alias_(value, column_name)
+ for value, column_name in zip(
+ t.expressions,
+ (
+ alias.columns
+ if alias and alias.columns
+ else (f"_c{i}" for i in range(len(t.expressions)))
+ ),
+ )
+ ]
+ )
+ for t in expression.find_all(exp.Tuple)
),
+ copy=False,
)
]
)
- for t in expression.find_all(exp.Tuple)
- ]
-
- return self.unnest_sql(exp.Unnest(expressions=[exp.Array(expressions=structs)]))
+ )
def _returnsproperty_sql(self: BigQuery.Generator, expression: exp.ReturnsProperty) -> str:
@@ -161,12 +168,18 @@ def _pushdown_cte_column_names(expression: exp.Expression) -> exp.Expression:
return expression
-def _parse_timestamp(args: t.List) -> exp.StrToTime:
+def _parse_parse_timestamp(args: t.List) -> exp.StrToTime:
this = format_time_lambda(exp.StrToTime, "bigquery")([seq_get(args, 1), seq_get(args, 0)])
this.set("zone", seq_get(args, 2))
return this
+def _parse_timestamp(args: t.List) -> exp.Timestamp:
+ timestamp = exp.Timestamp.from_arg_list(args)
+ timestamp.set("with_tz", True)
+ return timestamp
+
+
def _parse_date(args: t.List) -> exp.Date | exp.DateFromParts:
expr_type = exp.DateFromParts if len(args) == 3 else exp.Date
return expr_type.from_arg_list(args)
@@ -318,6 +331,7 @@ class BigQuery(Dialect):
"TIMESTAMP": TokenType.TIMESTAMPTZ,
}
KEYWORDS.pop("DIV")
+ KEYWORDS.pop("VALUES")
class Parser(parser.Parser):
PREFIXED_PIVOT_COLUMNS = True
@@ -348,7 +362,7 @@ class BigQuery(Dialect):
"PARSE_DATE": lambda args: format_time_lambda(exp.StrToDate, "bigquery")(
[seq_get(args, 1), seq_get(args, 0)]
),
- "PARSE_TIMESTAMP": _parse_timestamp,
+ "PARSE_TIMESTAMP": _parse_parse_timestamp,
"REGEXP_CONTAINS": exp.RegexpLike.from_arg_list,
"REGEXP_EXTRACT": lambda args: exp.RegexpExtract(
this=seq_get(args, 0),
@@ -367,6 +381,7 @@ class BigQuery(Dialect):
"TIME": _parse_time,
"TIME_ADD": parse_date_delta_with_interval(exp.TimeAdd),
"TIME_SUB": parse_date_delta_with_interval(exp.TimeSub),
+ "TIMESTAMP": _parse_timestamp,
"TIMESTAMP_ADD": parse_date_delta_with_interval(exp.TimestampAdd),
"TIMESTAMP_SUB": parse_date_delta_with_interval(exp.TimestampSub),
"TIMESTAMP_MICROS": lambda args: exp.UnixToTime(
@@ -395,11 +410,6 @@ class BigQuery(Dialect):
TokenType.TABLE,
}
- ID_VAR_TOKENS = {
- *parser.Parser.ID_VAR_TOKENS,
- TokenType.VALUES,
- }
-
PROPERTY_PARSERS = {
**parser.Parser.PROPERTY_PARSERS,
"NOT DETERMINISTIC": lambda self: self.expression(