-
Notifications
You must be signed in to change notification settings - Fork 14k
[FLINK-39957][table] Support EXPLAIN of CREATE [OR ALTER] MATERIALIZED TABLE #28510
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
7a0b485
cf2643a
fd4a3dc
34091c4
a128d52
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,7 @@ | |
| import org.apache.flink.configuration.Configuration; | ||
| import org.apache.flink.runtime.testutils.MiniClusterResourceConfiguration; | ||
| import org.apache.flink.table.api.config.TableConfigOptions; | ||
| import org.apache.flink.table.catalog.CatalogBaseTable; | ||
| import org.apache.flink.table.catalog.CatalogMaterializedTable.RefreshMode; | ||
| import org.apache.flink.table.catalog.CatalogMaterializedTable.RefreshStatus; | ||
| import org.apache.flink.table.catalog.ObjectIdentifier; | ||
|
|
@@ -62,6 +63,7 @@ | |
| import static org.apache.flink.table.gateway.service.MaterializedTableTestUtils.executeStatement; | ||
| import static org.apache.flink.table.gateway.service.MaterializedTableTestUtils.getContinuousRefreshHandler; | ||
| import static org.apache.flink.table.gateway.service.MaterializedTableTestUtils.getTable; | ||
| import static org.apache.flink.table.gateway.service.MaterializedTableTestUtils.verifyExplainPlan; | ||
| import static org.apache.flink.table.gateway.service.utils.SqlGatewayServiceTestUtil.awaitOperationTermination; | ||
| import static org.apache.flink.table.gateway.service.utils.SqlGatewayServiceTestUtil.fetchAllResults; | ||
| import static org.apache.flink.test.util.TestUtils.waitUntilAllTasksAreRunning; | ||
|
|
@@ -293,6 +295,39 @@ void testConvertTableLeftSuspendedWhenRefreshJobFailsToStart() throws Exception | |
| assertThat(suspendedMaterializedTable.getRefreshStatus()).isSameAs(RefreshStatus.SUSPENDED); | ||
| } | ||
|
|
||
| @Test | ||
| void testExplainConvertTableToMaterializedTable() throws Exception { | ||
| // pre-create a regular table that CREATE OR ALTER would convert in place. | ||
| String createRegularTableDDL = | ||
| "CREATE TABLE users_shops (\n" | ||
| + " user_id BIGINT,\n" | ||
| + " shop_id BIGINT,\n" | ||
| + " payment_amount_cents BIGINT\n" | ||
| + ")\n" | ||
| + "WITH ('connector' = 'values')"; | ||
| OperationHandle createTableHandle = | ||
| executeStatement(service, sessionHandle, createRegularTableDDL); | ||
| awaitOperationTermination(service, sessionHandle, createTableHandle); | ||
|
|
||
| String explainDDL = | ||
| "EXPLAIN CREATE OR ALTER MATERIALIZED TABLE users_shops" | ||
| + " FRESHNESS = INTERVAL '30' SECOND" | ||
| + " AS SELECT user_id, shop_id, payment_amount_cents FROM datagenSource"; | ||
|
|
||
| verifyExplainPlan( | ||
| service, | ||
| sessionHandle, | ||
| fileSystemCatalogName, | ||
| explainDDL, | ||
| "/explain/testExplainConvertTableToMaterializedTable.out"); | ||
|
|
||
| // EXPLAIN is side-effect-free: the entry is still a regular table, not converted. | ||
| assertThat( | ||
| service.getTable(sessionHandle, getObjectIdentifier("users_shops")) | ||
| .getTableKind()) | ||
| .isSameAs(CatalogBaseTable.TableKind.TABLE); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use static import if it requires more than 2 names |
||
| } | ||
|
|
||
| private SessionHandle initializeSession() { | ||
| SessionHandle sessionHandle = service.openSession(defaultSessionEnvironment); | ||
| String catalogDDL = | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,13 +21,20 @@ | |
| import org.apache.flink.configuration.Configuration; | ||
| import org.apache.flink.table.catalog.ObjectIdentifier; | ||
| import org.apache.flink.table.catalog.ResolvedCatalogMaterializedTable; | ||
| import org.apache.flink.table.data.RowData; | ||
| import org.apache.flink.table.gateway.api.SqlGatewayService; | ||
| import org.apache.flink.table.gateway.api.operation.OperationHandle; | ||
| import org.apache.flink.table.gateway.api.session.SessionHandle; | ||
| import org.apache.flink.table.planner.utils.TableTestUtil; | ||
| import org.apache.flink.table.refresh.ContinuousRefreshHandler; | ||
| import org.apache.flink.table.refresh.ContinuousRefreshHandlerSerializer; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.List; | ||
|
|
||
| import static org.apache.flink.table.gateway.service.utils.SqlGatewayServiceTestUtil.awaitOperationTermination; | ||
| import static org.apache.flink.table.gateway.service.utils.SqlGatewayServiceTestUtil.fetchAllResults; | ||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| /** Helpers shared between the materialized table gateway ITCases. */ | ||
| final class MaterializedTableTestUtils { | ||
|
|
@@ -50,4 +57,26 @@ static ContinuousRefreshHandler getContinuousRefreshHandler( | |
| return ContinuousRefreshHandlerSerializer.INSTANCE.deserialize( | ||
| resolvedTable.getSerializedRefreshHandler(), classLoader); | ||
| } | ||
|
|
||
| /** | ||
| * Runs an EXPLAIN statement through the gateway and asserts its full plan equals the given | ||
| * golden resource. The per-test catalog name is normalized to {@code $CATALOG} before | ||
| * comparison. | ||
| */ | ||
| static void verifyExplainPlan( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do we need to create another plan verifier if there is already existing framework for this? |
||
| SqlGatewayService service, | ||
| SessionHandle sessionHandle, | ||
| String catalogName, | ||
| String explainStatement, | ||
| String expectedResource) | ||
| throws Exception { | ||
| OperationHandle handle = executeStatement(service, sessionHandle, explainStatement); | ||
| awaitOperationTermination(service, sessionHandle, handle); | ||
| List<RowData> results = fetchAllResults(service, sessionHandle, handle); | ||
| assertThat(results).hasSize(1); | ||
| String actual = | ||
| TableTestUtil.replaceStageId( | ||
| results.get(0).getString(0).toString().replace(catalogName, "$CATALOG")); | ||
| assertThat(actual).isEqualTo(TableTestUtil.readFromResource(expectedResource)); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| == Abstract Syntax Tree == | ||
| LogicalSink(table=[$CATALOG.test_db.users_shops], fields=[user_id, shop_id, ds, order_cnt]) | ||
| +- LogicalAggregate(group=[{0, 1, 2}], order_cnt=[COUNT($3)]) | ||
| +- LogicalProject(user_id=[$1], shop_id=[$2], ds=[$3], order_id=[$0]) | ||
| +- LogicalTableScan(table=[[$CATALOG, test_db, my_source]]) | ||
|
|
||
| == Optimized Physical Plan == | ||
| Sink(table=[$CATALOG.test_db.users_shops], fields=[user_id, shop_id, order_created_at, order_cnt]) | ||
| +- GroupAggregate(groupBy=[user_id, shop_id, order_created_at], select=[user_id, shop_id, order_created_at, COUNT(order_id) AS order_cnt]) | ||
| +- Exchange(distribution=[hash[user_id, shop_id, order_created_at]]) | ||
| +- TableSourceScan(table=[[$CATALOG, test_db, my_source]], fields=[order_id, user_id, shop_id, order_created_at]) | ||
|
|
||
| == Optimized Execution Plan == | ||
| Sink(table=[$CATALOG.test_db.users_shops], fields=[user_id, shop_id, order_created_at, order_cnt]) | ||
| +- GroupAggregate(groupBy=[user_id, shop_id, order_created_at], select=[user_id, shop_id, order_created_at, COUNT(order_id) AS order_cnt]) | ||
| +- Exchange(distribution=[hash[user_id, shop_id, order_created_at]]) | ||
| +- TableSourceScan(table=[[$CATALOG, test_db, my_source]], fields=[order_id, user_id, shop_id, order_created_at]) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| == Abstract Syntax Tree == | ||
| LogicalSink(table=[$CATALOG.test_db.users_shops], fields=[user_id, shop_id, payment_amount_cents]) | ||
| +- LogicalProject(user_id=[$2], shop_id=[$3], payment_amount_cents=[$8]) | ||
| +- LogicalTableScan(table=[[$CATALOG, test_db, datagenSource]]) | ||
|
|
||
| == Optimized Physical Plan == | ||
| Sink(table=[$CATALOG.test_db.users_shops], fields=[user_id, shop_id, payment_amount_cents]) | ||
| +- Calc(select=[user_id, shop_id, payment_amount_cents]) | ||
| +- TableSourceScan(table=[[$CATALOG, test_db, datagenSource]], fields=[order_id, order_number, user_id, shop_id, product_id, status, order_type, order_created_at, payment_amount_cents]) | ||
|
|
||
| == Optimized Execution Plan == | ||
| Sink(table=[$CATALOG.test_db.users_shops], fields=[user_id, shop_id, payment_amount_cents]) | ||
| +- Calc(select=[user_id, shop_id, payment_amount_cents]) | ||
| +- TableSourceScan(table=[[$CATALOG, test_db, datagenSource]], fields=[order_id, order_number, user_id, shop_id, product_id, status, order_type, order_created_at, payment_amount_cents]) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| == Abstract Syntax Tree == | ||
| LogicalSink(table=[$CATALOG.test_db.users_shops], fields=[user_id, shop_id, ds, order_cnt]) | ||
| +- LogicalAggregate(group=[{0, 1, 2}], order_cnt=[COUNT($3)]) | ||
| +- LogicalProject(user_id=[$1], shop_id=[$2], ds=[$3], order_id=[$0]) | ||
| +- LogicalTableScan(table=[[$CATALOG, test_db, my_source]]) | ||
|
|
||
| == Optimized Physical Plan == | ||
| Sink(table=[$CATALOG.test_db.users_shops], fields=[user_id, shop_id, order_created_at, order_cnt]) | ||
| +- GroupAggregate(groupBy=[user_id, shop_id, order_created_at], select=[user_id, shop_id, order_created_at, COUNT(order_id) AS order_cnt]) | ||
| +- Exchange(distribution=[hash[user_id, shop_id, order_created_at]]) | ||
| +- TableSourceScan(table=[[$CATALOG, test_db, my_source]], fields=[order_id, user_id, shop_id, order_created_at]) | ||
|
|
||
| == Optimized Execution Plan == | ||
| Sink(table=[$CATALOG.test_db.users_shops], fields=[user_id, shop_id, order_created_at, order_cnt]) | ||
| +- GroupAggregate(groupBy=[user_id, shop_id, order_created_at], select=[user_id, shop_id, order_created_at, COUNT(order_id) AS order_cnt]) | ||
| +- Exchange(distribution=[hash[user_id, shop_id, order_created_at]]) | ||
| +- TableSourceScan(table=[[$CATALOG, test_db, my_source]], fields=[order_id, user_id, shop_id, order_created_at]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is there a reason
MaterializedTableOperationis not aModifyOperationhowever after that we call
callModifyOperations?