raminqaf commented on code in PR #28510:
URL: https://github.com/apache/flink/pull/28510#discussion_r3498774548


##########
flink-table/flink-sql-gateway/src/test/java/org/apache/flink/table/gateway/service/MaterializedTableStatementITCase.java:
##########
@@ -1858,6 +1858,83 @@ void 
testRefreshMaterializedTableWithInvalidParameterInContinuousMode() throws E
         dropMaterializedTable(getObjectIdentifier("my_materialized_table"));
     }
 
+    @Test
+    void testExplainCreateOrAlterMaterializedTableOnExistingTable() throws 
Exception {
+        createAndVerifyCreateMaterializedTableWithData(
+                "users_shops", List.of(), Map.of(), RefreshMode.FULL);
+        ObjectIdentifier userShopsIdentifier = 
getObjectIdentifier("users_shops");
+        ResolvedCatalogMaterializedTable oldTable = 
getTable(userShopsIdentifier);
+
+        // CREATE OR ALTER on an existing table behaves like an 
alter-with-query. Re-state the same
+        // definition so EXPLAIN plans the sink over the (unchanged) table.
+        String explainDDL =
+                "EXPLAIN CREATE OR ALTER MATERIALIZED TABLE users_shops"
+                        + " PARTITIONED BY (ds)"
+                        + " WITH ('format' = 'debezium-json')"
+                        + " FRESHNESS = INTERVAL '30' SECOND"
+                        + " REFRESH_MODE = FULL"
+                        + " AS SELECT"
+                        + "  user_id,"
+                        + "  shop_id,"
+                        + "  ds,"
+                        + "  COUNT(order_id) AS order_cnt"
+                        + " FROM ("
+                        + "    SELECT user_id, shop_id, order_created_at AS 
ds, order_id FROM my_source"
+                        + " ) AS tmp"
+                        + " GROUP BY (user_id, shop_id, ds)";
+
+        // EXPLAIN succeeds (the storage options carried over from the 
existing table avoid a
+        // "Missing required options" failure) and returns a single non-empty 
plan. The plan content
+        // itself is asserted by the planner tests in TableSinkTest.
+        OperationHandle explainHandle = executeStatement(explainDDL);
+        awaitOperationTermination(service, sessionHandle, explainHandle);
+        List<RowData> explainResults = fetchAllResults(service, sessionHandle, 
explainHandle);
+        assertThat(explainResults).hasSize(1);
+        assertThat(explainResults.get(0).getString(0).toString()).isNotBlank();
+
+        // EXPLAIN is side-effect-free: the existing materialized table is 
unchanged.
+        ResolvedCatalogMaterializedTable newTable = 
getTable(userShopsIdentifier);
+        
assertThat(newTable.getResolvedSchema()).isEqualTo(oldTable.getResolvedSchema());
+        
assertThat(newTable.getExpandedQuery()).isEqualTo(oldTable.getExpandedQuery());
+
+        dropMaterializedTable(userShopsIdentifier);
+    }
+
+    @Test
+    void testExplainAlterMaterializedTableAsQuery() throws Exception {
+        createAndVerifyCreateMaterializedTableWithData(
+                "users_shops", List.of(), Map.of(), RefreshMode.FULL);
+        ObjectIdentifier userShopsIdentifier = 
getObjectIdentifier("users_shops");
+        ResolvedCatalogMaterializedTable oldTable = 
getTable(userShopsIdentifier);
+
+        String explainDDL =
+                "EXPLAIN ALTER MATERIALIZED TABLE users_shops"
+                        + " AS SELECT"
+                        + "  user_id,"
+                        + "  shop_id,"
+                        + "  ds,"
+                        + "  COUNT(order_id) AS order_cnt"
+                        + " FROM ("
+                        + "    SELECT user_id, shop_id, order_created_at AS 
ds, order_id FROM my_source"
+                        + " ) AS tmp"
+                        + " GROUP BY (user_id, shop_id, ds)";
+
+        // EXPLAIN succeeds and returns a single non-empty plan. The plan 
content itself is asserted
+        // by the planner tests in TableSinkTest.
+        OperationHandle explainHandle = executeStatement(explainDDL);
+        awaitOperationTermination(service, sessionHandle, explainHandle);
+        List<RowData> explainResults = fetchAllResults(service, sessionHandle, 
explainHandle);
+        assertThat(explainResults).hasSize(1);
+        assertThat(explainResults.get(0).getString(0).toString()).isNotBlank();
+
+        // EXPLAIN is side-effect-free: the materialized table definition is 
unchanged.
+        ResolvedCatalogMaterializedTable newTable = 
getTable(userShopsIdentifier);
+        
assertThat(newTable.getResolvedSchema()).isEqualTo(oldTable.getResolvedSchema());
+        
assertThat(newTable.getExpandedQuery()).isEqualTo(oldTable.getExpandedQuery());
+
+        dropMaterializedTable(userShopsIdentifier);

Review Comment:
   Done! Move `dropMaterializedTable` into a `@AfterEach` and made the drop 
logic with `DROP MT IF EXISTS...`  so the drop does not fail



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to