Skip to content

Commit 60c99de

Browse files
committed
Add support for refreshing views in memory connector
1 parent 2b9e794 commit 60c99de

File tree

6 files changed

+57
-0
lines changed

6 files changed

+57
-0
lines changed

plugin/trino-delta-lake/src/test/java/io/trino/plugin/deltalake/TestDeltaLakeConnectorTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ protected boolean hasBehavior(TestingConnectorBehavior connectorBehavior)
184184
SUPPORTS_RENAME_FIELD,
185185
SUPPORTS_RENAME_SCHEMA,
186186
SUPPORTS_SET_COLUMN_TYPE,
187+
SUPPORTS_REFRESH_VIEW,
187188
SUPPORTS_TOPN_PUSHDOWN -> false;
188189
default -> super.hasBehavior(connectorBehavior);
189190
};

plugin/trino-hive/src/test/java/io/trino/plugin/hive/BaseHiveConnectorTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ protected boolean hasBehavior(TestingConnectorBehavior connectorBehavior)
252252
case SUPPORTS_ADD_COLUMN_WITH_POSITION,
253253
SUPPORTS_ADD_FIELD,
254254
SUPPORTS_CREATE_MATERIALIZED_VIEW,
255+
SUPPORTS_REFRESH_VIEW,
255256
SUPPORTS_DROP_FIELD,
256257
SUPPORTS_MERGE,
257258
SUPPORTS_NOT_NULL_CONSTRAINT,

plugin/trino-iceberg/src/test/java/io/trino/plugin/iceberg/BaseIcebergConnectorTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ protected boolean hasBehavior(TestingConnectorBehavior connectorBehavior)
238238
case SUPPORTS_CREATE_OR_REPLACE_TABLE,
239239
SUPPORTS_REPORTING_WRITTEN_BYTES -> true;
240240
case SUPPORTS_ADD_COLUMN_NOT_NULL_CONSTRAINT,
241+
SUPPORTS_REFRESH_VIEW,
241242
SUPPORTS_RENAME_MATERIALIZED_VIEW_ACROSS_SCHEMAS,
242243
SUPPORTS_TOPN_PUSHDOWN -> false;
243244
default -> super.hasBehavior(connectorBehavior);

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
@@ -559,6 +559,18 @@ public synchronized void renameView(ConnectorSession session, SchemaTableName vi
559559
views.put(newViewName, views.remove(viewName));
560560
}
561561

562+
@Override
563+
public synchronized void refreshView(ConnectorSession session, SchemaTableName viewName, ConnectorViewDefinition viewDefinition)
564+
{
565+
checkSchemaExists(viewName.getSchemaName());
566+
567+
if (!tableIds.containsKey(viewName)) {
568+
throw new TrinoException(NOT_FOUND, "View not found: " + viewName);
569+
}
570+
571+
views.replace(viewName, viewDefinition);
572+
}
573+
562574
@Override
563575
public synchronized void dropView(ConnectorSession session, SchemaTableName viewName)
564576
{

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 support refreshing view definition");
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)