Skip to content

Commit e7a4afb

Browse files
committed
Add support for refreshing views in memory connector
1 parent 013c8a5 commit e7a4afb

File tree

7 files changed

+79
-0
lines changed

7 files changed

+79
-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
@@ -182,6 +182,7 @@ protected boolean hasBehavior(TestingConnectorBehavior connectorBehavior)
182182
SUPPORTS_DROP_FIELD,
183183
SUPPORTS_LIMIT_PUSHDOWN,
184184
SUPPORTS_PREDICATE_PUSHDOWN,
185+
SUPPORTS_REFRESH_VIEW,
185186
SUPPORTS_RENAME_FIELD,
186187
SUPPORTS_RENAME_SCHEMA,
187188
SUPPORTS_SET_COLUMN_TYPE,

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
@@ -257,6 +257,7 @@ protected boolean hasBehavior(TestingConnectorBehavior connectorBehavior)
257257
SUPPORTS_DROP_FIELD,
258258
SUPPORTS_MERGE,
259259
SUPPORTS_NOT_NULL_CONSTRAINT,
260+
SUPPORTS_REFRESH_VIEW,
260261
SUPPORTS_RENAME_FIELD,
261262
SUPPORTS_SET_COLUMN_TYPE,
262263
SUPPORTS_TOPN_PUSHDOWN,

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
@@ -241,6 +241,7 @@ protected boolean hasBehavior(TestingConnectorBehavior connectorBehavior)
241241
SUPPORTS_REPORTING_WRITTEN_BYTES -> true;
242242
case SUPPORTS_ADD_COLUMN_NOT_NULL_CONSTRAINT,
243243
SUPPORTS_DEFAULT_COLUMN_VALUE,
244+
SUPPORTS_REFRESH_VIEW,
244245
SUPPORTS_RENAME_MATERIALIZED_VIEW_ACROSS_SCHEMAS,
245246
SUPPORTS_TOPN_PUSHDOWN -> false;
246247
default -> super.hasBehavior(connectorBehavior);

plugin/trino-lakehouse/src/test/java/io/trino/plugin/lakehouse/TestLakehouseConnectorTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ protected boolean hasBehavior(TestingConnectorBehavior connectorBehavior)
9090
SUPPORTS_REPORTING_WRITTEN_BYTES -> true;
9191
case SUPPORTS_ADD_COLUMN_NOT_NULL_CONSTRAINT,
9292
SUPPORTS_DEFAULT_COLUMN_VALUE,
93+
SUPPORTS_REFRESH_VIEW,
9394
SUPPORTS_RENAME_MATERIALIZED_VIEW_ACROSS_SCHEMAS,
9495
SUPPORTS_TOPN_PUSHDOWN -> false;
9596
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: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@
140140
import static io.trino.testing.TestingConnectorBehavior.SUPPORTS_MULTI_STATEMENT_WRITES;
141141
import static io.trino.testing.TestingConnectorBehavior.SUPPORTS_NEGATIVE_DATE;
142142
import static io.trino.testing.TestingConnectorBehavior.SUPPORTS_NOT_NULL_CONSTRAINT;
143+
import static io.trino.testing.TestingConnectorBehavior.SUPPORTS_REFRESH_VIEW;
143144
import static io.trino.testing.TestingConnectorBehavior.SUPPORTS_RENAME_COLUMN;
144145
import static io.trino.testing.TestingConnectorBehavior.SUPPORTS_RENAME_FIELD;
145146
import static io.trino.testing.TestingConnectorBehavior.SUPPORTS_RENAME_MATERIALIZED_VIEW;
@@ -989,6 +990,67 @@ public void testView()
989990
.doesNotContain(testView);
990991
}
991992

993+
@Test
994+
public void testRefreshView()
995+
{
996+
if (!hasBehavior(SUPPORTS_REFRESH_VIEW)) {
997+
if (hasBehavior(SUPPORTS_CREATE_VIEW)) {
998+
try (TestView testView = new TestView(getQueryRunner()::execute, "test_view", " SELECT * FROM nation")) {
999+
assertQueryFails("ALTER VIEW %s REFRESH".formatted(testView.getName()), "This connector does not support refreshing view definition");
1000+
}
1001+
}
1002+
return;
1003+
}
1004+
1005+
if (!hasBehavior(SUPPORTS_CREATE_TABLE) && !hasBehavior(SUPPORTS_ADD_COLUMN)) {
1006+
throw new AssertionError("Cannot test ALTER VIEW REFRESH without CREATE TABLE, the test needs to be implemented in a connector-specific way");
1007+
}
1008+
1009+
try (TestTable table = newTrinoTable("test_table", "(id BIGINT, column_to_dropped BIGINT, column_to_be_renamed BIGINT, column_with_comment BIGINT)", ImmutableList.of("1, 2, 3, 4"));
1010+
TestView view = new TestView(getQueryRunner()::execute, "test_view", " SELECT * FROM %s".formatted(table.getName()))) {
1011+
assertQuery("SELECT * FROM " + view.getName(), "VALUES (1, 2, 3, 4)");
1012+
1013+
assertUpdate("ALTER TABLE %s ADD COLUMN new_column BIGINT".formatted(table.getName()));
1014+
assertQueryFails(
1015+
"SELECT * FROM " + view.getName(),
1016+
".*is stale or in invalid state: stored view column count \\(4\\) does not match column count derived from the view query analysis \\(5\\)");
1017+
1018+
assertUpdate("ALTER VIEW %s REFRESH".formatted(view.getName()));
1019+
assertQuery("SELECT * FROM " + view.getName(), "VALUES (1, 2, 3, 4, null)");
1020+
1021+
if (hasBehavior(SUPPORTS_RENAME_COLUMN)) {
1022+
assertUpdate("ALTER TABLE %s RENAME COLUMN column_to_be_renamed TO renamed_column".formatted(table.getName()));
1023+
assertQueryFails(
1024+
"SELECT * FROM %s".formatted(view.getName()),
1025+
".*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 \\[column_to_be_renamed] of type bigint stored in view definition");
1026+
assertUpdate("ALTER VIEW %s REFRESH".formatted(view.getName()));
1027+
assertQuery("SELECT * FROM " + view.getName(), "VALUES (1, 2, 3, 4, null)");
1028+
}
1029+
1030+
if (hasBehavior(SUPPORTS_COMMENT_ON_COLUMN)) {
1031+
assertUpdate("COMMENT ON COLUMN %s.column_with_comment IS 'test comment'".formatted(view.getName()));
1032+
assertThat(getColumnComment(view.getName(), "column_with_comment")).isEqualTo("test comment");
1033+
1034+
// Add another column
1035+
assertUpdate("ALTER TABLE %s ADD COLUMN new_column_2 BIGINT".formatted(table.getName()));
1036+
assertUpdate("ALTER VIEW %s REFRESH".formatted(view.getName()));
1037+
assertThat(getColumnComment(view.getName(), "column_with_comment")).isEqualTo("test comment");
1038+
1039+
assertUpdate("ALTER TABLE %s RENAME COLUMN column_with_comment TO renamed_new_column".formatted(table.getName()));
1040+
}
1041+
1042+
if (hasBehavior(SUPPORTS_DROP_COLUMN)) {
1043+
assertUpdate("ALTER TABLE %s DROP COLUMN column_to_dropped".formatted(table.getName()));
1044+
assertQueryFails(
1045+
"SELECT * FROM " + view.getName(),
1046+
".*is stale or in invalid state: stored view column count \\(5\\) does not match column count derived from the view query analysis \\(4\\)");
1047+
1048+
assertUpdate("ALTER VIEW %s REFRESH".formatted(view.getName()));
1049+
assertQuery("SELECT * FROM " + view.getName(), "VALUES (1, 3, 4, null)");
1050+
}
1051+
}
1052+
}
1053+
9921054
@Test
9931055
public void testCreateViewSchemaNotFound()
9941056
{

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)