From 9778c168d57fcd886d1653df10e75df59722ce1b Mon Sep 17 00:00:00 2001 From: tobymao Date: Wed, 3 May 2023 14:56:48 -0700 Subject: [PATCH] Fix: create table options for bigquery closes #1531 --- sqlglot/dialects/bigquery.py | 9 +++++++++ sqlglot/generator.py | 2 +- tests/dialects/test_bigquery.py | 3 +++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/sqlglot/dialects/bigquery.py b/sqlglot/dialects/bigquery.py index 06145416a2..dcd1326034 100644 --- a/sqlglot/dialects/bigquery.py +++ b/sqlglot/dialects/bigquery.py @@ -207,6 +207,12 @@ class Parser(parser.Parser): "NOT DETERMINISTIC": lambda self: self.expression( exp.StabilityProperty, this=exp.Literal.string("VOLATILE") ), + "OPTIONS": lambda self: self._parse_with_property(), + } + + CONSTRAINT_PARSERS = { + **parser.Parser.CONSTRAINT_PARSERS, # type: ignore + "OPTIONS": lambda self: exp.Properties(expressions=self._parse_with_property()), } class Generator(generator.Generator): @@ -315,3 +321,6 @@ def intersect_op(self, expression: exp.Intersect) -> str: if not expression.args.get("distinct", False): self.unsupported("INTERSECT without DISTINCT is not supported in BigQuery") return f"INTERSECT{' DISTINCT' if expression.args.get('distinct') else ' ALL'}" + + def with_properties(self, properties: exp.Properties) -> str: + return self.properties(properties, prefix=self.seg("OPTIONS")) diff --git a/sqlglot/generator.py b/sqlglot/generator.py index bd12d54ac9..1db57e00f3 100644 --- a/sqlglot/generator.py +++ b/sqlglot/generator.py @@ -524,7 +524,7 @@ def columndef_sql(self, expression: exp.ColumnDef) -> str: def columnconstraint_sql(self, expression: exp.ColumnConstraint) -> str: this = self.sql(expression, "this") - kind_sql = self.sql(expression, "kind") + kind_sql = self.sql(expression, "kind").strip() return f"CONSTRAINT {this} {kind_sql}" if this else kind_sql def autoincrementcolumnconstraint_sql(self, _) -> str: diff --git a/tests/dialects/test_bigquery.py b/tests/dialects/test_bigquery.py index 7ac7346308..bb639e42b6 100644 --- a/tests/dialects/test_bigquery.py +++ b/tests/dialects/test_bigquery.py @@ -6,6 +6,9 @@ class TestBigQuery(Validator): dialect = "bigquery" def test_bigquery(self): + self.validate_identity( + """CREATE TABLE x (a STRING OPTIONS (description='x')) OPTIONS (table_expiration_days=1)""" + ) self.validate_identity("SELECT AS STRUCT 1 AS a, 2 AS b") self.validate_identity("SELECT AS VALUE STRUCT(1 AS a, 2 AS b)") self.validate_identity("SELECT STRUCT>(['2023-01-17'])")