Skip to content

Commit

Permalink
[BugFix] MV rewrite may generate wrong plans if query only contains c…
Browse files Browse the repository at this point in the history
…onstant call operators (backport #50757) (#50777)

Signed-off-by: shuming.li <[email protected]>
Co-authored-by: shuming.li <[email protected]>
  • Loading branch information
mergify[bot] and LiShuMing committed Sep 9, 2024
1 parent 963eac4 commit dd82d0f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
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");
}
}

0 comments on commit dd82d0f

Please sign in to comment.