Skip to content

Commit 2d66d8e

Browse files
committed
Add parser support for refreshing view
1 parent c2eaffd commit 2d66d8e

File tree

7 files changed

+122
-0
lines changed

7 files changed

+122
-0
lines changed

core/trino-grammar/src/main/antlr4/io/trino/grammar/sql/SqlBase.g4

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ statement
114114
SET PROPERTIES propertyAssignments #setMaterializedViewProperties
115115
| DROP VIEW (IF EXISTS)? qualifiedName #dropView
116116
| ALTER VIEW from=qualifiedName RENAME TO to=qualifiedName #renameView
117+
| ALTER VIEW viewName=qualifiedName REFRESH #refreshView
117118
| CALL qualifiedName '(' (callArgument (',' callArgument)*)? ')' #call
118119
| CREATE (OR REPLACE)? functionSpecification #createFunction
119120
| DROP FUNCTION (IF EXISTS)? functionDeclaration #dropFunction

core/trino-parser/src/main/java/io/trino/sql/SqlFormatter.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@
119119
import io.trino.sql.tree.QueryPeriod;
120120
import io.trino.sql.tree.QuerySpecification;
121121
import io.trino.sql.tree.RefreshMaterializedView;
122+
import io.trino.sql.tree.RefreshView;
122123
import io.trino.sql.tree.Relation;
123124
import io.trino.sql.tree.RenameColumn;
124125
import io.trino.sql.tree.RenameMaterializedView;
@@ -1273,6 +1274,15 @@ protected Void visitRefreshMaterializedView(RefreshMaterializedView node, Intege
12731274
return null;
12741275
}
12751276

1277+
@Override
1278+
protected Void visitRefreshView(RefreshView node, Integer indent)
1279+
{
1280+
builder.append("ALTER VIEW ");
1281+
builder.append(formatName(node.getName()));
1282+
builder.append(" REFRESH");
1283+
return null;
1284+
}
1285+
12761286
@Override
12771287
protected Void visitDropMaterializedView(DropMaterializedView node, Integer indent)
12781288
{

core/trino-parser/src/main/java/io/trino/sql/parser/AstBuilder.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@
219219
import io.trino.sql.tree.QuerySpecification;
220220
import io.trino.sql.tree.RangeQuantifier;
221221
import io.trino.sql.tree.RefreshMaterializedView;
222+
import io.trino.sql.tree.RefreshView;
222223
import io.trino.sql.tree.Relation;
223224
import io.trino.sql.tree.RenameColumn;
224225
import io.trino.sql.tree.RenameMaterializedView;
@@ -628,6 +629,14 @@ public Node visitRefreshMaterializedView(SqlBaseParser.RefreshMaterializedViewCo
628629
new Table(getQualifiedName(context.qualifiedName())));
629630
}
630631

632+
@Override
633+
public Node visitRefreshView(SqlBaseParser.RefreshViewContext context)
634+
{
635+
return new RefreshView(
636+
getLocation(context),
637+
getQualifiedName(context.qualifiedName()));
638+
}
639+
631640
@Override
632641
public Node visitDropMaterializedView(SqlBaseParser.DropMaterializedViewContext context)
633642
{

core/trino-parser/src/main/java/io/trino/sql/tree/AstVisitor.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,11 @@ protected R visitRefreshMaterializedView(RefreshMaterializedView node, C context
732732
return visitStatement(node, context);
733733
}
734734

735+
protected R visitRefreshView(RefreshView node, C context)
736+
{
737+
return visitStatement(node, context);
738+
}
739+
735740
protected R visitCall(Call node, C context)
736741
{
737742
return visitStatement(node, context);
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
package io.trino.sql.tree;
15+
16+
import com.google.common.collect.ImmutableList;
17+
18+
import java.util.List;
19+
import java.util.Objects;
20+
21+
import static com.google.common.base.MoreObjects.toStringHelper;
22+
import static java.util.Objects.requireNonNull;
23+
24+
public final class RefreshView
25+
extends Statement
26+
{
27+
private final QualifiedName name;
28+
29+
public RefreshView(NodeLocation location, QualifiedName name)
30+
{
31+
super(location);
32+
this.name = requireNonNull(name, "name is null");
33+
}
34+
35+
public QualifiedName getName()
36+
{
37+
return name;
38+
}
39+
40+
@Override
41+
public <R, C> R accept(AstVisitor<R, C> visitor, C context)
42+
{
43+
return visitor.visitRefreshView(this, context);
44+
}
45+
46+
@Override
47+
public List<Node> getChildren()
48+
{
49+
return ImmutableList.of();
50+
}
51+
52+
@Override
53+
public int hashCode()
54+
{
55+
return Objects.hash(name);
56+
}
57+
58+
@Override
59+
public boolean equals(Object obj)
60+
{
61+
if (this == obj) {
62+
return true;
63+
}
64+
if ((obj == null) || (getClass() != obj.getClass())) {
65+
return false;
66+
}
67+
RefreshView o = (RefreshView) obj;
68+
return Objects.equals(name, o.name);
69+
}
70+
71+
@Override
72+
public String toString()
73+
{
74+
return toStringHelper(this)
75+
.add("name", name)
76+
.toString();
77+
}
78+
}

core/trino-parser/src/test/java/io/trino/sql/TestSqlFormatter.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import io.trino.sql.tree.Property;
3333
import io.trino.sql.tree.QualifiedName;
3434
import io.trino.sql.tree.Query;
35+
import io.trino.sql.tree.RefreshView;
3536
import io.trino.sql.tree.ShowCatalogs;
3637
import io.trino.sql.tree.ShowColumns;
3738
import io.trino.sql.tree.ShowFunctions;
@@ -554,6 +555,14 @@ public void testCommentOnColumn()
554555
.isEqualTo("COMMENT ON COLUMN test.a IS '攻殻機動隊'");
555556
}
556557

558+
@Test
559+
public void testRefreshView()
560+
{
561+
assertThat(formatSql(
562+
new RefreshView(new NodeLocation(1, 1), QualifiedName.of("catalog", "schema", "view"))))
563+
.isEqualTo("ALTER VIEW catalog.schema.view REFRESH");
564+
}
565+
557566
@Test
558567
public void testExecuteImmediate()
559568
{

core/trino-parser/src/test/java/io/trino/sql/parser/TestSqlParser.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@
161161
import io.trino.sql.tree.QuerySpecification;
162162
import io.trino.sql.tree.RangeQuantifier;
163163
import io.trino.sql.tree.RefreshMaterializedView;
164+
import io.trino.sql.tree.RefreshView;
164165
import io.trino.sql.tree.Relation;
165166
import io.trino.sql.tree.RenameColumn;
166167
import io.trino.sql.tree.RenameMaterializedView;
@@ -3533,6 +3534,15 @@ public void testRenameView()
35333534
QualifiedName.of(ImmutableList.of(new Identifier(location(1, 24), "b", false)))));
35343535
}
35353536

3537+
@Test
3538+
public void testRefreshView()
3539+
{
3540+
assertThat(statement("ALTER VIEW a REFRESH"))
3541+
.isEqualTo(new RefreshView(
3542+
location(1, 1),
3543+
QualifiedName.of(ImmutableList.of(new Identifier(location(1, 12), "a", false)))));
3544+
}
3545+
35363546
@Test
35373547
public void testAlterViewSetAuthorization()
35383548
{

0 commit comments

Comments
 (0)