Skip to content

Commit

Permalink
Feat(postgres): add COMMENT ON MATERIALIZED VIEW (#3293)
Browse files Browse the repository at this point in the history
* Add COMMENT ON MATERIALIZED VIEW

* Fix materialized generation

* Fix style

---------

Co-authored-by: Jo <46752250+georgesittas@users.noreply.github.com>
  • Loading branch information
l-vincent-l and georgesittas committed Apr 9, 2024
1 parent 215a755 commit 942856d
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
8 changes: 7 additions & 1 deletion sqlglot/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1536,7 +1536,13 @@ class SwapTable(Expression):


class Comment(Expression):
arg_types = {"this": True, "kind": True, "expression": True, "exists": False}
arg_types = {
"this": True,
"kind": True,
"expression": True,
"exists": False,
"materialized": False,
}


class Comprehension(Expression):
Expand Down
3 changes: 2 additions & 1 deletion sqlglot/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2877,9 +2877,10 @@ def command_sql(self, expression: exp.Command) -> str:
def comment_sql(self, expression: exp.Comment) -> str:
this = self.sql(expression, "this")
kind = expression.args["kind"]
materialized = " MATERIALIZED" if expression.args.get("materialized") else ""
exists_sql = " IF EXISTS " if expression.args.get("exists") else " "
expression_sql = self.sql(expression, "expression")
return f"COMMENT{exists_sql}ON {kind} {this} IS {expression_sql}"
return f"COMMENT{exists_sql}ON{materialized} {kind} {this} IS {expression_sql}"

def mergetreettlaction_sql(self, expression: exp.MergeTreeTTLAction) -> str:
this = self.sql(expression, "this")
Expand Down
8 changes: 7 additions & 1 deletion sqlglot/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1388,6 +1388,7 @@ def _parse_comment(self, allow_exists: bool = True) -> exp.Expression:

self._match(TokenType.ON)

materialized = self._match_text_seq("MATERIALIZED")
kind = self._match_set(self.CREATABLES) and self._prev
if not kind:
return self._parse_as_command(start)
Expand All @@ -1404,7 +1405,12 @@ def _parse_comment(self, allow_exists: bool = True) -> exp.Expression:
self._match(TokenType.IS)

return self.expression(
exp.Comment, this=this, kind=kind.text, expression=self._parse_string(), exists=exists
exp.Comment,
this=this,
kind=kind.text,
expression=self._parse_string(),
exists=exists,
materialized=materialized,
)

def _parse_to_table(
Expand Down
1 change: 1 addition & 0 deletions tests/dialects/test_postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def test_postgres(self):
self.validate_identity("STRING_AGG(DISTINCT x, ',' ORDER BY y DESC)")
self.validate_identity("SELECT CASE WHEN SUBSTRING('abcdefg') IN ('ab') THEN 1 ELSE 0 END")
self.validate_identity("COMMENT ON TABLE mytable IS 'this'")
self.validate_identity("COMMENT ON MATERIALIZED VIEW my_view IS 'this'")
self.validate_identity("SELECT e'\\xDEADBEEF'")
self.validate_identity("SELECT CAST(e'\\176' AS BYTEA)")
self.validate_identity("SELECT * FROM x WHERE SUBSTRING('Thomas' FROM '...$') IN ('mas')")
Expand Down

0 comments on commit 942856d

Please sign in to comment.