|
13 | 13 | */ |
14 | 14 | package com.facebook.presto.sql.analyzer; |
15 | 15 |
|
| 16 | +import com.facebook.presto.Session; |
16 | 17 | import com.facebook.presto.spi.WarningCollector; |
17 | 18 | import com.facebook.presto.spi.analyzer.AccessControlReferences; |
18 | 19 | import com.facebook.presto.sql.tree.Statement; |
|
24 | 25 | import java.util.Optional; |
25 | 26 | import java.util.stream.Collectors; |
26 | 27 |
|
| 28 | +import static com.facebook.presto.SystemSessionProperties.ALWAYS_ANALYZE_CREATE_TABLE_QUERY_ENABLED; |
| 29 | +import static com.facebook.presto.SystemSessionProperties.CHECK_ACCESS_CONTROL_ON_UTILIZED_COLUMNS_ONLY; |
| 30 | +import static com.facebook.presto.SystemSessionProperties.CHECK_ACCESS_CONTROL_WITH_SUBFIELDS; |
| 31 | +import static com.facebook.presto.testing.TestingSession.testSessionBuilder; |
27 | 32 | import static com.facebook.presto.transaction.TransactionBuilder.transaction; |
28 | 33 | import static com.facebook.presto.util.AnalyzerUtil.checkAccessPermissions; |
29 | 34 | import static org.testng.Assert.assertEquals; |
@@ -62,6 +67,26 @@ public void testCreateTableAsSelectWithViews() |
62 | 67 | ), ImmutableMap.of()); |
63 | 68 | } |
64 | 69 |
|
| 70 | + public void testCreateTableAsSelectIfNotExistsWithViews() |
| 71 | + { |
| 72 | + // t1 already exists, so this hits the IF NOT EXISTS no-op path. |
| 73 | + // With always_analyze_create_table_query_enabled, view definitions should still be populated. |
| 74 | + Session sessionWithProperty = testSessionBuilder() |
| 75 | + .setCatalog(TPCH_CATALOG) |
| 76 | + .setSchema("s1") |
| 77 | + .setSystemProperty(CHECK_ACCESS_CONTROL_ON_UTILIZED_COLUMNS_ONLY, "true") |
| 78 | + .setSystemProperty(CHECK_ACCESS_CONTROL_WITH_SUBFIELDS, "true") |
| 79 | + .setSystemProperty(ALWAYS_ANALYZE_CREATE_TABLE_QUERY_ENABLED, "true") |
| 80 | + .build(); |
| 81 | + |
| 82 | + @Language("SQL") String query = "CREATE TABLE IF NOT EXISTS t1 AS SELECT view_definer1.a, view_definer1.c, view_invoker2.y FROM view_definer1 left join view_invoker2 on view_invoker2.y = view_definer1.c"; |
| 83 | + |
| 84 | + assertViewDefinitions(sessionWithProperty, query, ImmutableMap.of( |
| 85 | + "tpch.s1.view_invoker2", "select x, y, z from t13", |
| 86 | + "tpch.s1.view_definer1", "select a,b,c from t1" |
| 87 | + ), ImmutableMap.of()); |
| 88 | + } |
| 89 | + |
65 | 90 | public void testExplainWithViews() |
66 | 91 | { |
67 | 92 | @Language("SQL") String query = "EXPLAIN SELECT view_definer1.a, view_definer1.c, view_invoker2.y FROM view_definer1 left join view_invoker2 on view_invoker2.y = view_definer1.c"; |
@@ -193,12 +218,17 @@ public void testExplainTypeValidateExplainAnalyzeWithViews() |
193 | 218 | } |
194 | 219 |
|
195 | 220 | private void assertViewDefinitions(@Language("SQL") String query, Map<String, String> expectedViewDefinitions, Map<String, String> expectedMaterializedViewDefinitions) |
| 221 | + { |
| 222 | + assertViewDefinitions(CLIENT_SESSION, query, expectedViewDefinitions, expectedMaterializedViewDefinitions); |
| 223 | + } |
| 224 | + |
| 225 | + private void assertViewDefinitions(Session clientSession, @Language("SQL") String query, Map<String, String> expectedViewDefinitions, Map<String, String> expectedMaterializedViewDefinitions) |
196 | 226 | { |
197 | 227 | transaction(transactionManager, accessControl) |
198 | 228 | .singleStatement() |
199 | 229 | .readUncommitted() |
200 | 230 | .readOnly() |
201 | | - .execute(CLIENT_SESSION, session -> { |
| 231 | + .execute(clientSession, session -> { |
202 | 232 | Analyzer analyzer = createAnalyzer(session, metadata, WarningCollector.NOOP, Optional.of(createTestingQueryExplainer(session, accessControl, metadata)), query); |
203 | 233 | Statement statement = SQL_PARSER.createStatement(query); |
204 | 234 | Analysis analysis = analyzer.analyzeSemantic(statement, false); |
|
0 commit comments