Skip to content

Commit 74a3881

Browse files
committed
Add support for refreshing views in memory connector
1 parent 8476eea commit 74a3881

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed

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: 36 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,41 @@ public void testView()
988989
.doesNotContain(testView);
989990
}
990991

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

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)