diff --git a/fe/fe-core/src/main/java/com/starrocks/sql/optimizer/rewrite/OptOlapPartitionPruner.java b/fe/fe-core/src/main/java/com/starrocks/sql/optimizer/rewrite/OptOlapPartitionPruner.java index c93ee455db269..4126e93ebbc8f 100644 --- a/fe/fe-core/src/main/java/com/starrocks/sql/optimizer/rewrite/OptOlapPartitionPruner.java +++ b/fe/fe-core/src/main/java/com/starrocks/sql/optimizer/rewrite/OptOlapPartitionPruner.java @@ -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; @@ -408,6 +409,8 @@ private static boolean isNeedFurtherPrune(List 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)); } } else if (partitionInfo instanceof ExpressionRangePartitionInfoV2) { return false; diff --git a/fe/fe-core/src/test/java/com/starrocks/planner/MaterializedViewWithPartitionTest.java b/fe/fe-core/src/test/java/com/starrocks/planner/MaterializedViewWithPartitionTest.java index 833fb31cf4683..324d32ca135fe 100644 --- a/fe/fe-core/src/test/java/com/starrocks/planner/MaterializedViewWithPartitionTest.java +++ b/fe/fe-core/src/test/java/com/starrocks/planner/MaterializedViewWithPartitionTest.java @@ -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. @@ -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 { {