Skip to content

Commit

Permalink
feat: support ALTER TABLE .. SET COMMENT
Browse files Browse the repository at this point in the history
  • Loading branch information
tekumara committed Jul 1, 2023
1 parent 8f30d1a commit 6470bae
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion fakesnow/fakes.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def execute(

sql = transformed.sql(dialect="duckdb")

if cmd != "COMMENT TABLE":
if cmd not in ("COMMENT TABLE", "ALTERTABLE"):
try:
self._last_sql = sql
self._last_params = params
Expand Down
13 changes: 13 additions & 0 deletions fakesnow/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,19 @@ def extract_comment(expression: exp.Expression) -> exp.Expression:
new = expression.copy()
new.args["table_comment"] = cexp.this
return new
elif (
isinstance(expression, exp.AlterTable)
and (sexp := expression.find(exp.Set))
and not sexp.args["tag"]
and (eq := sexp.find(exp.EQ))
and (id := eq.find(exp.Identifier))
and isinstance(id.this, str)
and id.this.upper() == "COMMENT"
and (lit := eq.find(exp.Literal))
):
new = expression.copy()
new.args["table_comment"] = lit.this
return new

return expression

Expand Down
2 changes: 2 additions & 0 deletions tests/test_fakes.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,8 @@ def read_comment() -> str:
assert read_comment() == "pepperoni"
cur.execute("COMMENT IF EXISTS ON TABLE schema1.ingredients IS 'mushrooms'")
assert read_comment() == "mushrooms"
cur.execute("ALTER TABLE ingredients SET comment = 'pineapple'")
assert read_comment() == "pineapple"


def test_tags_noop(cur: snowflake.connector.cursor.SnowflakeCursor):
Expand Down
4 changes: 4 additions & 0 deletions tests/test_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ def test_extract_comment() -> None:
assert e.sql() == "COMMENT ON TABLE table1 IS 'comment1'"
assert e.args["table_comment"] == "comment1"

e = sqlglot.parse_one("ALTER TABLE table1 SET COMMENT = 'comment1'", read="snowflake").transform(extract_comment)
assert e.sql() == "ALTER TABLE table1 SET COMMENT = 'comment1'"
assert e.args["table_comment"] == "comment1"


def test_float_to_double() -> None:
assert (
Expand Down

0 comments on commit 6470bae

Please sign in to comment.