Skip to content

Commit 8379d8d

Browse files
committed
Add support for refreshing views in memory connector
1 parent 5a4c1f2 commit 8379d8d

File tree

5 files changed

+64
-1
lines changed

5 files changed

+64
-1
lines changed

core/trino-main/src/main/java/io/trino/execution/RefreshViewTask.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public ListenableFuture<Void> execute(
104104

105105
Session viewSession = stateMachine.getSession();
106106
if (!viewDefinition.isRunAsInvoker()) {
107-
checkArgument(viewDefinition.getRunAsIdentity().isEmpty(), "View owner detail is missing");
107+
checkArgument(viewDefinition.getRunAsIdentity().isPresent(), "View owner detail is missing");
108108
viewSession = viewSession.createViewSession(viewDefinition.getCatalog(), viewDefinition.getSchema(), viewDefinition.getRunAsIdentity().get(), viewDefinition.getPath());
109109
}
110110

core/trino-main/src/main/java/io/trino/tracing/TracingConnectorMetadata.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,15 @@ public void renameView(ConnectorSession session, SchemaTableName source, SchemaT
789789
}
790790
}
791791

792+
@Override
793+
public void refreshView(ConnectorSession session, SchemaTableName viewName, ConnectorViewDefinition definition)
794+
{
795+
Span span = startSpan("refreshView", viewName);
796+
try (var _ = scopedSpan(span)) {
797+
delegate.refreshView(session, viewName, definition);
798+
}
799+
}
800+
792801
@Override
793802
public void setViewAuthorization(ConnectorSession session, SchemaTableName viewName, TrinoPrincipal principal)
794803
{

plugin/trino-memory/src/main/java/io/trino/plugin/memory/MemoryMetadata.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,18 @@ public synchronized void renameView(ConnectorSession session, SchemaTableName vi
555555
views.put(newViewName, views.remove(viewName));
556556
}
557557

558+
@Override
559+
public synchronized void refreshView(ConnectorSession session, SchemaTableName viewName, ConnectorViewDefinition viewDefinition)
560+
{
561+
checkSchemaExists(viewName.getSchemaName());
562+
563+
if (!tableIds.containsKey(viewName)) {
564+
throw new TrinoException(NOT_FOUND, "View not found: " + viewName);
565+
}
566+
567+
views.replace(viewName, viewDefinition);
568+
}
569+
558570
@Override
559571
public synchronized void dropView(ConnectorSession session, SchemaTableName viewName)
560572
{

testing/trino-testing/src/main/java/io/trino/testing/BaseConnectorTest.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@
139139
import static io.trino.testing.TestingConnectorBehavior.SUPPORTS_MULTI_STATEMENT_WRITES;
140140
import static io.trino.testing.TestingConnectorBehavior.SUPPORTS_NEGATIVE_DATE;
141141
import static io.trino.testing.TestingConnectorBehavior.SUPPORTS_NOT_NULL_CONSTRAINT;
142+
import static io.trino.testing.TestingConnectorBehavior.SUPPORTS_REFRESH_VIEW;
142143
import static io.trino.testing.TestingConnectorBehavior.SUPPORTS_RENAME_COLUMN;
143144
import static io.trino.testing.TestingConnectorBehavior.SUPPORTS_RENAME_FIELD;
144145
import static io.trino.testing.TestingConnectorBehavior.SUPPORTS_RENAME_MATERIALIZED_VIEW;
@@ -988,6 +989,46 @@ public void testView()
988989
.doesNotContain(testView);
989990
}
990991

992+
@Test
993+
public void testRefreshView()
994+
{
995+
if (!hasBehavior(SUPPORTS_REFRESH_VIEW)) {
996+
if (hasBehavior(SUPPORTS_CREATE_VIEW)) {
997+
try (TestView testView = new TestView(getQueryRunner()::execute, "test_view", " SELECT * FROM nation")) {
998+
assertQueryFails("ALTER VIEW %s REFRESH".formatted(testView.getName()), "This connector does not refreshing views");
999+
}
1000+
}
1001+
else {
1002+
assertQueryFails("CREATE VIEW sample_view AS SELECT * FROM nation", "This connector does not support creating views");
1003+
}
1004+
return;
1005+
}
1006+
1007+
if (!hasBehavior(SUPPORTS_CREATE_TABLE) && !hasBehavior(SUPPORTS_ADD_COLUMN)) {
1008+
throw new AssertionError("Cannot test ALTER VIEW REFRESH without CREATE TABLE, the test needs to be implemented in a connector-specific way");
1009+
}
1010+
1011+
try (TestTable table = new TestTable(getQueryRunner()::execute, "test_table", "(id BIGINT, column_to_dropped BIGINT)", ImmutableList.of("1, 2"));
1012+
TestView view = new TestView(getQueryRunner()::execute, "test_view", " SELECT * FROM %s".formatted(table.getName()))) {
1013+
assertQuery("SELECT * FROM %s".formatted(view.getName()), "VALUES (1, 2)");
1014+
1015+
assertUpdate("ALTER TABLE %s ADD COLUMN new_column BIGINT".formatted(table.getName()));
1016+
assertQueryFails(
1017+
"SELECT * FROM %s".formatted(view.getName()),
1018+
".*is stale or in invalid state: stored view column count \\(2\\) does not match column count derived from the view query analysis \\(3\\)");
1019+
1020+
assertUpdate("ALTER VIEW %s REFRESH".formatted(view.getName()));
1021+
assertQuery("SELECT * FROM %s".formatted(view.getName()), "VALUES (1, 2, null)");
1022+
1023+
assertUpdate("ALTER TABLE %s RENAME COLUMN new_column TO renamed_column".formatted(table.getName()));
1024+
assertQueryFails(
1025+
"SELECT * FROM %s".formatted(view.getName()),
1026+
".*is stale or in invalid state: column \\[renamed_column] of type bigint projected from query view at position 2 has a different name from column \\[new_column] of type bigint stored in view definition");
1027+
assertUpdate("ALTER VIEW %s REFRESH".formatted(view.getName()));
1028+
assertQuery("SELECT * FROM %s".formatted(view.getName()), "VALUES (1, 2, null)");
1029+
}
1030+
}
1031+
9911032
@Test
9921033
public void testCreateViewSchemaNotFound()
9931034
{

testing/trino-testing/src/main/java/io/trino/testing/TestingConnectorBehavior.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ public enum TestingConnectorBehavior
104104
SUPPORTS_CREATE_VIEW,
105105
SUPPORTS_COMMENT_ON_VIEW(and(SUPPORTS_CREATE_VIEW, SUPPORTS_COMMENT_ON_TABLE)),
106106
SUPPORTS_COMMENT_ON_VIEW_COLUMN(SUPPORTS_COMMENT_ON_VIEW),
107+
SUPPORTS_REFRESH_VIEW(SUPPORTS_CREATE_VIEW),
107108

108109
SUPPORTS_CREATE_MATERIALIZED_VIEW,
109110
SUPPORTS_CREATE_MATERIALIZED_VIEW_GRACE_PERIOD(SUPPORTS_CREATE_MATERIALIZED_VIEW),

0 commit comments

Comments
 (0)