Skip to content

Commit 755e1e4

Browse files
committed
Add parser support for refreshing view
1 parent 195ea23 commit 755e1e4

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
@@ -122,6 +122,7 @@
122122
import io.trino.sql.tree.QueryPeriod;
123123
import io.trino.sql.tree.QuerySpecification;
124124
import io.trino.sql.tree.RefreshMaterializedView;
125+
import io.trino.sql.tree.RefreshView;
125126
import io.trino.sql.tree.Relation;
126127
import io.trino.sql.tree.RenameColumn;
127128
import io.trino.sql.tree.RenameMaterializedView;
@@ -1277,6 +1278,15 @@ protected Void visitRefreshMaterializedView(RefreshMaterializedView node, Intege
12771278
return null;
12781279
}
12791280

1281+
@Override
1282+
protected Void visitRefreshView(RefreshView node, Integer indent)
1283+
{
1284+
builder.append("ALTER VIEW ");
1285+
builder.append(formatName(node.getName()));
1286+
builder.append(" REFRESH");
1287+
return null;
1288+
}
1289+
12801290
@Override
12811291
protected Void visitDropMaterializedView(DropMaterializedView node, Integer indent)
12821292
{

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
@@ -223,6 +223,7 @@
223223
import io.trino.sql.tree.QuerySpecification;
224224
import io.trino.sql.tree.RangeQuantifier;
225225
import io.trino.sql.tree.RefreshMaterializedView;
226+
import io.trino.sql.tree.RefreshView;
226227
import io.trino.sql.tree.Relation;
227228
import io.trino.sql.tree.RenameColumn;
228229
import io.trino.sql.tree.RenameMaterializedView;
@@ -638,6 +639,14 @@ public Node visitRefreshMaterializedView(SqlBaseParser.RefreshMaterializedViewCo
638639
new Table(getQualifiedName(context.qualifiedName())));
639640
}
640641

642+
@Override
643+
public Node visitRefreshView(SqlBaseParser.RefreshViewContext context)
644+
{
645+
return new RefreshView(
646+
getLocation(context),
647+
getQualifiedName(context.qualifiedName()));
648+
}
649+
641650
@Override
642651
public Node visitDropMaterializedView(SqlBaseParser.DropMaterializedViewContext context)
643652
{

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
@@ -35,6 +35,7 @@
3535
import io.trino.sql.tree.Property;
3636
import io.trino.sql.tree.QualifiedName;
3737
import io.trino.sql.tree.Query;
38+
import io.trino.sql.tree.RefreshView;
3839
import io.trino.sql.tree.ShowBranches;
3940
import io.trino.sql.tree.ShowCatalogs;
4041
import io.trino.sql.tree.ShowColumns;
@@ -560,6 +561,14 @@ public void testCommentOnColumn()
560561
.isEqualTo("COMMENT ON COLUMN test.a IS '攻殻機動隊'");
561562
}
562563

564+
@Test
565+
public void testRefreshView()
566+
{
567+
assertThat(formatSql(
568+
new RefreshView(new NodeLocation(1, 1), QualifiedName.of("catalog", "schema", "view"))))
569+
.isEqualTo("ALTER VIEW catalog.schema.view REFRESH");
570+
}
571+
563572
@Test
564573
public void testExecuteImmediate()
565574
{

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
@@ -166,6 +166,7 @@
166166
import io.trino.sql.tree.QuerySpecification;
167167
import io.trino.sql.tree.RangeQuantifier;
168168
import io.trino.sql.tree.RefreshMaterializedView;
169+
import io.trino.sql.tree.RefreshView;
169170
import io.trino.sql.tree.Relation;
170171
import io.trino.sql.tree.RenameColumn;
171172
import io.trino.sql.tree.RenameMaterializedView;
@@ -3700,6 +3701,15 @@ public void testRenameView()
37003701
QualifiedName.of(ImmutableList.of(new Identifier(location(1, 24), "b", false)))));
37013702
}
37023703

3704+
@Test
3705+
public void testRefreshView()
3706+
{
3707+
assertThat(statement("ALTER VIEW a REFRESH"))
3708+
.isEqualTo(new RefreshView(
3709+
location(1, 1),
3710+
QualifiedName.of(ImmutableList.of(new Identifier(location(1, 12), "a", false)))));
3711+
}
3712+
37033713
@Test
37043714
public void testAlterViewSetAuthorization()
37053715
{

0 commit comments

Comments
 (0)