diff --git a/sqlglot/dialects/bigquery.py b/sqlglot/dialects/bigquery.py index 75811f7ee1..06145416a2 100644 --- a/sqlglot/dialects/bigquery.py +++ b/sqlglot/dialects/bigquery.py @@ -139,6 +139,7 @@ class Tokenizer(tokens.Tokenizer): "GEOGRAPHY": TokenType.GEOGRAPHY, "FLOAT64": TokenType.DOUBLE, "INT64": TokenType.BIGINT, + "BYTES": TokenType.BINARY, "NOT DETERMINISTIC": TokenType.VOLATILE, "UNKNOWN": TokenType.NULL, } @@ -261,6 +262,7 @@ class Generator(generator.Generator): **generator.Generator.TYPE_MAPPING, # type: ignore exp.DataType.Type.BIGDECIMAL: "BIGNUMERIC", exp.DataType.Type.BIGINT: "INT64", + exp.DataType.Type.BINARY: "BYTES", exp.DataType.Type.BOOLEAN: "BOOL", exp.DataType.Type.CHAR: "STRING", exp.DataType.Type.DECIMAL: "NUMERIC", @@ -274,6 +276,7 @@ class Generator(generator.Generator): exp.DataType.Type.TIMESTAMP: "DATETIME", exp.DataType.Type.TIMESTAMPTZ: "TIMESTAMP", exp.DataType.Type.TINYINT: "INT64", + exp.DataType.Type.VARBINARY: "BYTES", exp.DataType.Type.VARCHAR: "STRING", exp.DataType.Type.VARIANT: "ANY TYPE", } diff --git a/tests/dialects/test_bigquery.py b/tests/dialects/test_bigquery.py index 703b7dc0a1..7ac7346308 100644 --- a/tests/dialects/test_bigquery.py +++ b/tests/dialects/test_bigquery.py @@ -97,6 +97,16 @@ def test_bigquery(self): "spark": "CAST(a AS LONG)", }, ) + self.validate_all( + "CAST(a AS BYTES)", + write={ + "bigquery": "CAST(a AS BYTES)", + "duckdb": "CAST(a AS BLOB)", + "presto": "CAST(a AS VARBINARY)", + "hive": "CAST(a AS BINARY)", + "spark": "CAST(a AS BINARY)", + }, + ) self.validate_all( "CAST(a AS NUMERIC)", write={ diff --git a/tests/dialects/test_dialect.py b/tests/dialects/test_dialect.py index bcbbfd66b1..f12273b95b 100644 --- a/tests/dialects/test_dialect.py +++ b/tests/dialects/test_dialect.py @@ -95,7 +95,7 @@ def test_cast(self): self.validate_all( "CAST(a AS BINARY(4))", write={ - "bigquery": "CAST(a AS BINARY)", + "bigquery": "CAST(a AS BYTES)", "clickhouse": "CAST(a AS BINARY(4))", "drill": "CAST(a AS VARBINARY(4))", "duckdb": "CAST(a AS BLOB(4))", @@ -114,7 +114,7 @@ def test_cast(self): self.validate_all( "CAST(a AS VARBINARY(4))", write={ - "bigquery": "CAST(a AS VARBINARY)", + "bigquery": "CAST(a AS BYTES)", "clickhouse": "CAST(a AS VARBINARY(4))", "duckdb": "CAST(a AS BLOB(4))", "mysql": "CAST(a AS VARBINARY(4))",