|
140 | 140 | import static io.trino.testing.TestingConnectorBehavior.SUPPORTS_MULTI_STATEMENT_WRITES;
|
141 | 141 | import static io.trino.testing.TestingConnectorBehavior.SUPPORTS_NEGATIVE_DATE;
|
142 | 142 | import static io.trino.testing.TestingConnectorBehavior.SUPPORTS_NOT_NULL_CONSTRAINT;
|
| 143 | +import static io.trino.testing.TestingConnectorBehavior.SUPPORTS_REFRESH_VIEW; |
143 | 144 | import static io.trino.testing.TestingConnectorBehavior.SUPPORTS_RENAME_COLUMN;
|
144 | 145 | import static io.trino.testing.TestingConnectorBehavior.SUPPORTS_RENAME_FIELD;
|
145 | 146 | import static io.trino.testing.TestingConnectorBehavior.SUPPORTS_RENAME_MATERIALIZED_VIEW;
|
@@ -989,6 +990,67 @@ public void testView()
|
989 | 990 | .doesNotContain(testView);
|
990 | 991 | }
|
991 | 992 |
|
| 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 | + |
992 | 1054 | @Test
|
993 | 1055 | public void testCreateViewSchemaNotFound()
|
994 | 1056 | {
|
|
0 commit comments