Skip to content

Commit

Permalink
feat(bigquery): Support INT64(...) (#4391)
Browse files Browse the repository at this point in the history
  • Loading branch information
VaggelisD authored Nov 14, 2024
1 parent bb46ee3 commit 37c4809
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 0 deletions.
1 change: 1 addition & 0 deletions sqlglot/dialects/bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,7 @@ class Generator(generator.Generator):
exp.If: if_sql(false_value="NULL"),
exp.ILike: no_ilike_sql,
exp.IntDiv: rename_func("DIV"),
exp.Int64: rename_func("INT64"),
exp.JSONFormat: rename_func("TO_JSON_STRING"),
exp.Levenshtein: _levenshtein_sql,
exp.Max: max_or_greatest,
Expand Down
5 changes: 5 additions & 0 deletions sqlglot/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5828,6 +5828,11 @@ class IsNan(Func):
_sql_names = ["IS_NAN", "ISNAN"]


# https://cloud.google.com/bigquery/docs/reference/standard-sql/json_functions#int64_for_json
class Int64(Func):
pass


class IsInf(Func):
_sql_names = ["IS_INF", "ISINF"]

Expand Down
1 change: 1 addition & 0 deletions sqlglot/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ class Generator(metaclass=_Generator):
exp.InputModelProperty: lambda self, e: f"INPUT{self.sql(e, 'this')}",
exp.Intersect: lambda self, e: self.set_operations(e),
exp.IntervalSpan: lambda self, e: f"{self.sql(e, 'this')} TO {self.sql(e, 'expression')}",
exp.Int64: lambda self, e: self.sql(exp.cast(e.this, exp.DataType.Type.BIGINT)),
exp.LanguageProperty: lambda self, e: self.naked_property(e),
exp.LocationProperty: lambda self, e: self.naked_property(e),
exp.LogProperty: lambda _, e: f"{'NO ' if e.args.get('no') else ''}LOG",
Expand Down
8 changes: 8 additions & 0 deletions tests/dialects/test_bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -1606,6 +1606,14 @@ def test_bigquery(self):
"snowflake": "SELECT ts + INTERVAL '1 year, 2 month, 5 minute, 3 day'",
},
)
self.validate_all(
"""SELECT INT64(JSON_QUERY(JSON '{"key": 2000}', '$.key'))""",
write={
"bigquery": """SELECT INT64(JSON_QUERY(PARSE_JSON('{"key": 2000}'), '$.key'))""",
"duckdb": """SELECT CAST(JSON('{"key": 2000}') -> '$.key' AS BIGINT)""",
"snowflake": """SELECT CAST(GET_PATH(PARSE_JSON('{"key": 2000}'), 'key') AS BIGINT)""",
},
)

def test_errors(self):
with self.assertRaises(TokenError):
Expand Down

0 comments on commit 37c4809

Please sign in to comment.