-
Notifications
You must be signed in to change notification settings - Fork 2.5k
[BugFix] Fix Delta Lake and Kudu un-partitioned materialized view query rewrite #76359
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
Merged
+214
−4
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
...m/starrocks/sql/optimizer/rule/transformation/materialization/MvRewriteDeltaLakeTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| // Copyright 2021-present StarRocks, Inc. All rights reserved. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // https://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package com.starrocks.sql.optimizer.rule.transformation.materialization; | ||
|
|
||
| import com.google.common.base.Joiner; | ||
| import com.starrocks.catalog.BaseTableInfo; | ||
| import com.starrocks.catalog.Database; | ||
| import com.starrocks.catalog.DeltaLakeTable; | ||
| import com.starrocks.catalog.MaterializedView; | ||
| import com.starrocks.catalog.MvRefreshArbiter; | ||
| import com.starrocks.catalog.MvUpdateInfo; | ||
| import com.starrocks.catalog.Table; | ||
| import com.starrocks.catalog.mv.MVTimelinessArbiter; | ||
| import com.starrocks.qe.ConnectContext; | ||
| import com.starrocks.server.GlobalStateMgr; | ||
| import com.starrocks.server.MetadataMgr; | ||
| import com.starrocks.sql.plan.ConnectorPlanTestBase; | ||
| import mockit.Mock; | ||
| import mockit.MockUp; | ||
| import org.junit.jupiter.api.Assertions; | ||
| import org.junit.jupiter.api.BeforeAll; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| public class MvRewriteDeltaLakeTest extends MVTestBase { | ||
|
|
||
| @BeforeAll | ||
| public static void beforeClass() throws Exception { | ||
| MVTestBase.beforeClass(); | ||
| ConnectorPlanTestBase.mockCatalog(connectContext, "deltalake_catalog"); | ||
| } | ||
|
|
||
| /** | ||
| * Regression guard for the Delta Lake un-partitioned MV query rewrite bug (issue #11409). | ||
| * | ||
| * <p>{@code DeltaLakePartitionTraits#getUpdatedPartitionNames} used to return {@code null}. The MV timeliness | ||
| * arbiter interprets a null base-table update info as "unknown", returns {@link MvUpdateInfo.MvToRefreshType#FULL} | ||
| * and therefore treats the MV as stale, disabling query rewrite in the default CHECKED consistency mode. | ||
| * | ||
| * <p>After the fix it returns an empty set ("no updated partitions detected"), so the arbiter returns | ||
| * {@link MvUpdateInfo.MvToRefreshType#NO_REFRESH} and the MV stays eligible for rewrite. This test asserts that | ||
| * timeliness decision directly, which is exactly the signal that gates the rewrite (a full end-to-end EXPLAIN | ||
| * assertion is covered by the SQL test {@code test_mv_deltalake_rewrite}). | ||
| */ | ||
| @Test | ||
| public void testDeltaLakeUnPartitionedMvIsNotStaleInCheckedMode() throws Exception { | ||
| // The mocked Delta Lake table has no snapshot, so stub a stable table identifier (used for base-table | ||
| // matching) to avoid an NPE and keep the query/MV base-table identifiers consistent. | ||
| new MockUp<DeltaLakeTable>() { | ||
| @Mock | ||
| public String getTableIdentifier() { | ||
| return Joiner.on(":").join("tbl", "mocked-delta-uuid"); | ||
| } | ||
| }; | ||
| new MockUp<MetadataMgr>() { | ||
| @Mock | ||
| public Optional<Table> getTableWithIdentifier(ConnectContext context, BaseTableInfo baseTableInfo) { | ||
| return Optional.of(new DeltaLakeTable()); | ||
| } | ||
| }; | ||
|
|
||
| String mvName = "mv_delta_checked"; | ||
| // No query_rewrite_consistency property => defaults to CHECKED, the mode in which the bug manifested. | ||
| starRocksAssert.withMaterializedView("create materialized view " + mvName + | ||
| " refresh deferred manual " + | ||
| " as select col1, col2 from deltalake_catalog.deltalake_db.tbl"); | ||
|
|
||
| Database testDb = GlobalStateMgr.getCurrentState().getLocalMetastore().getDb("test"); | ||
| MaterializedView mv = (MaterializedView) GlobalStateMgr.getCurrentState().getLocalMetastore() | ||
| .getTable(testDb.getFullName(), mvName); | ||
|
|
||
| MvUpdateInfo info = MvRefreshArbiter.getMVTimelinessUpdateInfo( | ||
| mv, MVTimelinessArbiter.QueryRewriteParams.ofRefresh()); | ||
| // Before the fix this was FULL (stale => no rewrite); after the fix it must be NO_REFRESH (rewriteable). | ||
| Assertions.assertEquals(MvUpdateInfo.MvToRefreshType.NO_REFRESH, info.getMVToRefreshType()); | ||
| Assertions.assertTrue(info.isValidRewrite()); | ||
|
|
||
| starRocksAssert.dropMaterializedView(mvName); | ||
| } | ||
| } |
107 changes: 107 additions & 0 deletions
107
...va/com/starrocks/sql/optimizer/rule/transformation/materialization/MvRewriteKuduTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| // Copyright 2021-present StarRocks, Inc. All rights reserved. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // https://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package com.starrocks.sql.optimizer.rule.transformation.materialization; | ||
|
|
||
| import com.google.common.collect.ImmutableList; | ||
| import com.starrocks.catalog.BaseTableInfo; | ||
| import com.starrocks.catalog.Column; | ||
| import com.starrocks.catalog.Database; | ||
| import com.starrocks.catalog.KuduTable; | ||
| import com.starrocks.catalog.MaterializedView; | ||
| import com.starrocks.catalog.MvRefreshArbiter; | ||
| import com.starrocks.catalog.MvUpdateInfo; | ||
| import com.starrocks.catalog.Table; | ||
| import com.starrocks.catalog.mv.MVTimelinessArbiter; | ||
| import com.starrocks.connector.kudu.KuduMetadata; | ||
| import com.starrocks.qe.ConnectContext; | ||
| import com.starrocks.server.GlobalStateMgr; | ||
| import com.starrocks.server.MetadataMgr; | ||
| import com.starrocks.sql.plan.ConnectorPlanTestBase; | ||
| import com.starrocks.type.IntegerType; | ||
| import com.starrocks.type.StringType; | ||
| import mockit.Mock; | ||
| import mockit.MockUp; | ||
| import org.junit.jupiter.api.Assertions; | ||
| import org.junit.jupiter.api.BeforeAll; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
|
|
||
| public class MvRewriteKuduTest extends MVTestBase { | ||
|
|
||
| private static KuduTable kuduTable; | ||
|
|
||
| @BeforeAll | ||
| public static void beforeClass() throws Exception { | ||
| MVTestBase.beforeClass(); | ||
| ConnectorPlanTestBase.mockCatalog(connectContext, ConnectorPlanTestBase.MOCK_KUDU_CATALOG_NAME); | ||
|
|
||
| List<Column> columns = ImmutableList.of( | ||
| new Column("col1", IntegerType.INT), | ||
| new Column("col2", StringType.STRING)); | ||
| kuduTable = new KuduTable("localhost:7051", ConnectorPlanTestBase.MOCK_KUDU_CATALOG_NAME, | ||
| "kudu_db", "kudu_tbl", "kudu_tbl", columns, new ArrayList<>()); | ||
|
|
||
| // The mocked kudu0 catalog is backed by a real KuduMetadata pointing at a fake master, so stub the | ||
| // metadata lookups that MV creation / rewrite need to a canned unpartitioned table. | ||
| new MockUp<KuduMetadata>() { | ||
| @Mock | ||
| public Table getTable(ConnectContext context, String dbName, String tblName) { | ||
| return kuduTable; | ||
| } | ||
|
|
||
| @Mock | ||
| public Database getDb(ConnectContext context, String dbName) { | ||
| return new Database(GlobalStateMgr.getCurrentState().getNextId(), dbName); | ||
| } | ||
| }; | ||
| new MockUp<MetadataMgr>() { | ||
| @Mock | ||
| public Optional<Table> getTableWithIdentifier(ConnectContext context, BaseTableInfo baseTableInfo) { | ||
| return Optional.of(kuduTable); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Regression guard for the Kudu external-table MV query rewrite bug (same root cause as the Delta Lake case, | ||
| * issue #11409): {@code KuduPartitionTraits#getUpdatedPartitionNames} used to return {@code null}, so the MV | ||
| * timeliness arbiter returned {@link MvUpdateInfo.MvToRefreshType#FULL} and treated the MV as stale, disabling | ||
| * query rewrite in the default CHECKED consistency mode. After the fix it returns an empty set, so the arbiter | ||
| * returns {@link MvUpdateInfo.MvToRefreshType#NO_REFRESH} and the MV stays eligible for rewrite. | ||
| */ | ||
| @Test | ||
| public void testKuduUnPartitionedMvIsNotStaleInCheckedMode() throws Exception { | ||
| String mvName = "mv_kudu_checked"; | ||
| // No query_rewrite_consistency property => defaults to CHECKED, the mode in which the bug manifested. | ||
| starRocksAssert.withMaterializedView("create materialized view " + mvName + | ||
| " refresh deferred manual " + | ||
| " as select col1, col2 from kudu0.kudu_db.kudu_tbl"); | ||
|
|
||
| Database testDb = GlobalStateMgr.getCurrentState().getLocalMetastore().getDb("test"); | ||
| MaterializedView mv = (MaterializedView) GlobalStateMgr.getCurrentState().getLocalMetastore() | ||
| .getTable(testDb.getFullName(), mvName); | ||
|
|
||
| MvUpdateInfo info = MvRefreshArbiter.getMVTimelinessUpdateInfo( | ||
| mv, MVTimelinessArbiter.QueryRewriteParams.ofRefresh()); | ||
| // Before the fix this was FULL (stale => no rewrite); after the fix it must be NO_REFRESH (rewriteable). | ||
| Assertions.assertEquals(MvUpdateInfo.MvToRefreshType.NO_REFRESH, info.getMVToRefreshType()); | ||
| Assertions.assertTrue(info.isValidRewrite()); | ||
|
|
||
| starRocksAssert.dropMaterializedView(mvName); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.