Skip to content
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

[BugFix] MV rewrite may generate wrong plans if query only contains constant call operators (backport #50757) #50777

Merged
merged 2 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,14 @@ public ScalarOperator visitCall(CallOperator call, Void context) {
return rewritten;
}
}

// If count(1)/sum(1) cannot be rewritten by mv's defined equivalents, return null directly,
// otherwise it may cause a wrong plan.
// mv : SELECT 1, count(distinct k1) from tbl1;
// query : SELECT count(1) from tbl1;
// MV should not rewrite the query.
if (call.isAggregate() && call.isConstant()) {
return null;
}
return super.visitCall(call, context);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -356,4 +356,46 @@ public void testRewriteWithCaseWhen() {
}
});
}

@Test
public void testWrongMVRewrite() throws Exception {
starRocksAssert.withTable("CREATE TABLE `tbl1` (\n" +
" `k1` date,\n" +
" `k2` decimal64(18, 2),\n" +
" `k3` varchar(255),\n" +
" `v1` varchar(255)\n" +
") ENGINE=OLAP \n" +
"DUPLICATE KEY(`k1`, `k2`, `k3`)\n" +
"DISTRIBUTED BY RANDOM\n" +
"PROPERTIES (\n" +
"\"replication_num\" = \"1\"\n" +
");");
{
starRocksAssert.withMaterializedView("CREATE MATERIALIZED VIEW `mv1` \n" +
"DISTRIBUTED BY RANDOM\n" +
"REFRESH ASYNC\n" +
"PROPERTIES (\n" +
"\"replication_num\" = \"1\"\n" +
")\n" +
"AS SELECT 1, count(distinct k1) from tbl1");
sql("select count(distinct k1) from tbl1").contains("mv1");
sql("select count(1) from tbl1").notContain("mv1");
sql("select count(*) from tbl1").notContain("mv1");
starRocksAssert.dropMaterializedView("mv1");
}
{
starRocksAssert.withMaterializedView("CREATE MATERIALIZED VIEW `mv1` \n" +
"DISTRIBUTED BY RANDOM\n" +
"REFRESH ASYNC\n" +
"PROPERTIES (\n" +
"\"replication_num\" = \"1\"\n" +
")\n" +
"AS SELECT 1, count(1) from tbl1");
sql("select count(distinct k1) from tbl1").notContain("mv1");
sql("select count(1) from tbl1").contains("mv1");
sql("select count(*) from tbl1").contains("mv1");
starRocksAssert.dropMaterializedView("mv1");
}
starRocksAssert.dropTable("tbl1");
}
}
Loading