Skip to content

Commit 713e95f

Browse files
eakmanrqclaude
andauthored
Fix(snowflake): parse GET_DDL #unknown_policy in ROW ACCESS POLICY [CLAUDE] (#7491)
* Fix(snowflake): parse GET_DDL #unknown_policy in ROW ACCESS POLICY Snowflake's GET_DDL outputs `#unknown_policy` (without ON clause) when the user lacks privileges to see the actual policy name. Handle the `#` token prefix in the parser and ensure it is never quoted in output. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * Fix: ruff-format and mypy type errors Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * Fix: use X | None instead of t.Optional[X] per ruff UP007 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * Fix: use Var instead of Identifier for #unknown_policy to avoid quoting Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * Fix: simplify #unknown_policy parsing to use _parse_var directly Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 3835725 commit 713e95f

3 files changed

Lines changed: 26 additions & 10 deletions

File tree

sqlglot/generators/snowflake.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -863,7 +863,7 @@ def show_sql(self, expression: exp.Show) -> str:
863863
def rowaccessproperty_sql(self, expression: exp.RowAccessProperty) -> str:
864864
if not expression.this:
865865
return "ROW ACCESS"
866-
on = f" ON ({self.expressions(expression, flat=True)})"
866+
on = f" ON ({self.expressions(expression, flat=True)})" if expression.expressions else ""
867867
return f"WITH ROW ACCESS POLICY {self.sql(expression, 'this')}{on}"
868868

869869
def describe_sql(self, expression: exp.Describe) -> str:

sqlglot/parsers/snowflake.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -978,15 +978,21 @@ def _parse_with_property(self) -> exp.Expr | None | list[exp.Expr]:
978978
return super()._parse_with_property()
979979

980980
def _parse_row_access_policy(self) -> exp.RowAccessProperty:
981-
policy = self._parse_column()
982-
if not self._match(TokenType.ON):
983-
self.raise_error("Expected ON after ROW ACCESS POLICY name")
984-
return self.expression(
985-
exp.RowAccessProperty(
986-
this=policy.to_dot() if isinstance(policy, exp.Column) else policy,
987-
expressions=self._parse_wrapped_csv(self._parse_id_var),
988-
)
989-
)
981+
# GET_DDL outputs #unknown_policy when the user lacks privileges to see the policy name
982+
if self._match(TokenType.HASH):
983+
policy: exp.Expr | None = self._parse_var(any_token=True)
984+
if policy:
985+
policy = exp.Var(this=f"#{policy.name}")
986+
expressions = None
987+
else:
988+
policy = self._parse_column()
989+
if isinstance(policy, exp.Column):
990+
policy = policy.to_dot()
991+
if not self._match(TokenType.ON):
992+
self.raise_error("Expected ON after ROW ACCESS POLICY name")
993+
expressions = self._parse_wrapped_csv(self._parse_id_var)
994+
995+
return self.expression(exp.RowAccessProperty(this=policy, expressions=expressions))
990996

991997
def _parse_create(self) -> exp.Create | exp.Command:
992998
expression = super()._parse_create()

tests/dialects/test_snowflake.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5973,6 +5973,16 @@ def test_create_view_row_access_policy(self):
59735973
"CREATE VIEW v WITH ROW ACCESS POLICY p AS SELECT 1",
59745974
dialect="snowflake",
59755975
)
5976+
self.validate_identity(
5977+
"CREATE VIEW v WITH ROW ACCESS POLICY #unknown_policy AS SELECT 1",
5978+
)
5979+
self.assertEqual(
5980+
parse_one(
5981+
"CREATE VIEW v WITH ROW ACCESS POLICY #unknown_policy AS SELECT 1",
5982+
dialect="snowflake",
5983+
).sql(dialect="snowflake", identify=True),
5984+
'CREATE VIEW "v" WITH ROW ACCESS POLICY #unknown_policy AS SELECT 1',
5985+
)
59765986

59775987
def test_semantic_view(self):
59785988
for dimensions, metrics, where, facts in [

0 commit comments

Comments
 (0)