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] Fix mv partition prune failed when partition column predicate with function #46786

Merged
merged 1 commit into from
Aug 8, 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 @@ -22,6 +22,7 @@
import com.starrocks.analysis.Expr;
import com.starrocks.analysis.FunctionCallExpr;
import com.starrocks.analysis.LiteralExpr;
import com.starrocks.analysis.SlotRef;
import com.starrocks.catalog.Column;
import com.starrocks.catalog.ExpressionRangePartitionInfo;
import com.starrocks.catalog.ExpressionRangePartitionInfoV2;
Expand Down Expand Up @@ -408,6 +409,8 @@ private static boolean isNeedFurtherPrune(List<Long> candidatePartitions, Logica
return (FunctionSet.DATE_TRUNC.equalsIgnoreCase(functionName)
|| FunctionSet.TIME_SLICE.equalsIgnoreCase(functionName))
&& !exprPartitionInfo.getIdToRange(true).containsKey(candidatePartitions.get(0));
} else if (partitionExpr.size() == 1 && partitionExpr.get(0) instanceof SlotRef) {
return !exprPartitionInfo.getIdToRange(true).containsKey(candidatePartitions.get(0));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we handle all partition exprs? eg: date_trunc/str2date/cast?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This pr is to make partition prune behavior consisitent to base table and mv, eg: date_format can be used to prune partition to base table, but not to mv.

Currently, it's consistent for base table and mv on these 3 functions:

  1. date_trunc can apply partition prune to base table and mv
  2. str2date can't apply partition prune to base table and mv
  3. cast can't apply partition prune to base table and mv

We can add partition prune support to str2date/cast in another pr.

Copy link
Contributor

@LiShuMing LiShuMing Aug 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got your meaning. So this pr only optimizes partition pruning with slot ref partitions.

}
} else if (partitionInfo instanceof ExpressionRangePartitionInfoV2) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ public static void beforeClass() throws Exception {
" PARTITION p5 values less than (\"3000\"))" +
" distributed by hash(c1)" +
" properties (\"replication_num\"=\"1\");");

starRocksAssert.withTable("create table test_base_part3(c1 int, c2 bigint, c3 date, c4 bigint)" +
" partition by range(c3) (" +
" partition p1 values less than (\"20240603\")," +
" partition p2 values less than (\"20240604\")," +
" partition p3 values less than (\"20240605\")," +
" PARTITION p4 values less than (\"20240606\")," +
" PARTITION p5 values less than (\"20240607\"))" +
" distributed by hash(c1)" +
" properties (\"replication_num\"=\"1\");");
}

// MV's partition columns is the same with the base table, and mv has partition filter predicates.
Expand Down Expand Up @@ -493,6 +503,25 @@ public void testBucketPrune_SingleTable2() throws Exception {
starRocksAssert.dropMaterializedView("partial_mv_8");
}

@Test
public void testPartitionPrune_WithFunc() throws Exception {
starRocksAssert.withMaterializedView("CREATE MATERIALIZED VIEW `partial_mv_14`\n" +
"COMMENT \"MATERIALIZED_VIEW\"\n" +
"PARTITION BY (`c3`)\n" +
"REFRESH MANUAL\n" +
"AS SELECT `c3`, sum(`c4`) AS `total`\n" +
"FROM `test_mv`.`test_base_part3`\n" +
"GROUP BY `c3`;");
refreshMaterializedView(MATERIALIZED_DB_NAME, "partial_mv_14");

sql("select c3, sum(c4) from test_base_part3 where date_format(c3,'%Y%m%d')='20240602' group by c3")
.contains("TABLE: partial_mv_14\n" +
" PREAGGREGATION: ON\n" +
" PREDICATES: '%Y%m%d') = '20240602', date_format(col$: c3\n" +
" partitions=1/5");
starRocksAssert.dropMaterializedView("partial_mv_14");
}

@Test
public void testPartitionWithNull() throws Exception {
{
Expand Down
Loading