Skip to content

Commit 01890eb

Browse files
authored
Feat(trino): support refresh materialized view statement closes #6387 (#6388)
1 parent 2473f7e commit 01890eb

File tree

5 files changed

+23
-6
lines changed

5 files changed

+23
-6
lines changed

sqlglot/dialects/trino.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ class Trino(Presto):
1616
SUPPORTS_USER_DEFINED_TYPES = False
1717
LOG_BASE_FIRST = True
1818

19+
class Tokenizer(Presto.Tokenizer):
20+
KEYWORDS = {
21+
**Presto.Tokenizer.KEYWORDS,
22+
"REFRESH": TokenType.REFRESH,
23+
}
24+
1925
class Parser(Presto.Parser):
2026
FUNCTION_PARSERS = {
2127
**Presto.Parser.FUNCTION_PARSERS,

sqlglot/expressions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1484,7 +1484,7 @@ class Uncache(Expression):
14841484

14851485

14861486
class Refresh(Expression):
1487-
pass
1487+
arg_types = {"this": True, "kind": True}
14881488

14891489

14901490
class DDL(Expression):

sqlglot/generator.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4375,8 +4375,8 @@ def forin_sql(self, expression: exp.ForIn) -> str:
43754375

43764376
def refresh_sql(self, expression: exp.Refresh) -> str:
43774377
this = self.sql(expression, "this")
4378-
table = "" if isinstance(expression.this, exp.Literal) else "TABLE "
4379-
return f"REFRESH {table}{this}"
4378+
kind = "" if isinstance(expression.this, exp.Literal) else f"{expression.text('kind')} "
4379+
return f"REFRESH {kind}{this}"
43804380

43814381
def toarray_sql(self, expression: exp.ToArray) -> str:
43824382
arg = expression.this

sqlglot/parser.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7554,9 +7554,19 @@ def _parse_commit_or_rollback(self) -> exp.Commit | exp.Rollback:
75547554

75557555
return self.expression(exp.Commit, chain=chain)
75567556

7557-
def _parse_refresh(self) -> exp.Refresh:
7558-
self._match(TokenType.TABLE)
7559-
return self.expression(exp.Refresh, this=self._parse_string() or self._parse_table())
7557+
def _parse_refresh(self) -> exp.Refresh | exp.Command:
7558+
if self._match(TokenType.TABLE):
7559+
kind = "TABLE"
7560+
elif self._match_text_seq("MATERIALIZED", "VIEW"):
7561+
kind = "MATERIALIZED VIEW"
7562+
else:
7563+
kind = ""
7564+
7565+
this = self._parse_string() or self._parse_table()
7566+
if not kind and not isinstance(this, exp.Literal):
7567+
return self._parse_as_command(self._prev)
7568+
7569+
return self.expression(exp.Refresh, this=this, kind=kind)
75607570

75617571
def _parse_column_def_with_exists(self):
75627572
start = self._index

tests/dialects/test_trino.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ class TestTrino(Validator):
55
dialect = "trino"
66

77
def test_trino(self):
8+
self.validate_identity("REFRESH MATERIALIZED VIEW mynamespace.test_view")
89
self.validate_identity("JSON_QUERY(m.properties, 'lax $.area' OMIT QUOTES NULL ON ERROR)")
910
self.validate_identity("JSON_EXTRACT(content, json_path)")
1011
self.validate_identity("JSON_QUERY(content, 'lax $.HY.*')")

0 commit comments

Comments
 (0)