Skip to content

Commit c15ba7c

Browse files
[BugFix] Add implicit cast rule for list partitino pruner with generated column (backport #54543) (#57697)
Co-authored-by: shuming.li <ming.moriarty@gmail.com>
1 parent 0cc6976 commit c15ba7c

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

fe/fe-core/src/main/java/com/starrocks/sql/optimizer/rule/transformation/ListPartitionPruner.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,8 +438,10 @@ public ScalarOperator visitInPredicate(InPredicateOperator operator, Void contex
438438
return null;
439439
}
440440

441-
// Fold constants
442441
ScalarOperatorRewriter rewriter = new ScalarOperatorRewriter();
442+
// implicit cast
443+
result = rewriter.rewrite(result, ScalarOperatorRewriter.DEFAULT_TYPE_CAST_RULE);
444+
// fold constant
443445
result = rewriter.rewrite(result, ScalarOperatorRewriter.FOLD_CONSTANT_RULES);
444446
return result;
445447
}

fe/fe-core/src/test/java/com/starrocks/sql/plan/PartitionPruneTest.java

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.starrocks.sql.optimizer.operator.logical.LogicalScanOperator;
2626
import com.starrocks.sql.optimizer.operator.scalar.ScalarOperator;
2727
import com.starrocks.sql.optimizer.statistics.StatisticsCalculator;
28+
import com.starrocks.thrift.TExplainLevel;
2829
import com.starrocks.utframe.UtFrameUtils;
2930
import org.junit.Assert;
3031
import org.junit.BeforeClass;
@@ -75,6 +76,22 @@ public static void beforeClass() throws Exception {
7576
starRocksAssert.ddl("ALTER TABLE t_gen_col ADD PARTITION p2_202402 VALUES IN (('2', '2024-02-01'))");
7677
starRocksAssert.ddl("ALTER TABLE t_gen_col ADD PARTITION p2_202403 VALUES IN (('2', '2024-03-01'))");
7778

79+
// date_trunc('month', hours_add(date_trunc('day', hours_sub(c1, 8)), 8))
80+
starRocksAssert.withTable("CREATE TABLE t_gen_col2 (" +
81+
" c1 datetime NOT NULL," +
82+
" c2 bigint," +
83+
" c3 DATETIME NULL AS date_trunc('month', hours_add(date_trunc('day', hours_sub(c1, 8)), 8)) " +
84+
" ) " +
85+
" DUPLICATE KEY(c1) " +
86+
" PARTITION BY (c2, c3) " +
87+
" PROPERTIES('replication_num'='1')");
88+
starRocksAssert.ddl("ALTER TABLE t_gen_col2 ADD PARTITION p1_202401 VALUES IN (('1', '2024-01-01'))");
89+
starRocksAssert.ddl("ALTER TABLE t_gen_col2 ADD PARTITION p1_202402 VALUES IN (('1', '2024-02-01'))");
90+
starRocksAssert.ddl("ALTER TABLE t_gen_col2 ADD PARTITION p1_202403 VALUES IN (('1', '2024-03-01'))");
91+
starRocksAssert.ddl("ALTER TABLE t_gen_col2 ADD PARTITION p2_202401 VALUES IN (('2', '2024-01-01'))");
92+
starRocksAssert.ddl("ALTER TABLE t_gen_col2 ADD PARTITION p2_202402 VALUES IN (('2', '2024-02-01'))");
93+
starRocksAssert.ddl("ALTER TABLE t_gen_col2 ADD PARTITION p2_202403 VALUES IN (('2', '2024-03-01'))");
94+
7895
starRocksAssert.withTable("CREATE TABLE t_bool_partition (" +
7996
" c1 datetime NOT NULL, " +
8097
" c2 boolean" +
@@ -224,6 +241,7 @@ public void testNullException() throws Exception {
224241
private static Pair<ScalarOperator, LogicalScanOperator> buildConjunctAndScan(String sql) throws Exception {
225242
Pair<String, ExecPlan> pair = UtFrameUtils.getPlanAndFragment(connectContext, sql);
226243
ExecPlan execPlan = pair.second;
244+
System.out.println(execPlan.getExplainString(TExplainLevel.NORMAL));
227245
LogicalScanOperator scanOperator =
228246
(LogicalScanOperator) execPlan.getLogicalPlan().getRoot().inputAt(0).inputAt(0).inputAt(0).getOp();
229247
ScalarOperator predicate = execPlan.getPhysicalPlan().getOp().getPredicate();
@@ -238,9 +256,15 @@ private void testRemovePredicate(String sql, String expected) throws Exception {
238256
Assert.assertEquals(expected, newPredicate.toString());
239257
}
240258

259+
private void testAssertContains(String sql, String expected) throws Exception {
260+
Pair<String, ExecPlan> pair = UtFrameUtils.getPlanAndFragment(connectContext, sql);
261+
ExecPlan execPlan = pair.second;
262+
String plan = execPlan.getExplainString(TExplainLevel.NORMAL);
263+
PlanTestBase.assertContains(plan, expected);
264+
}
265+
241266
@Test
242267
public void testGeneratedColumnPrune_RemovePredicate() throws Exception {
243-
testRemovePredicate("select * from t_gen_col where c1 = '2024-01-01' ", "true");
244268
testRemovePredicate("select * from t_gen_col where c1 = '2024-01-01' and c2 > 100", "true");
245269
testRemovePredicate("select * from t_gen_col where c1 >= '2024-01-01' and c1 <= '2024-01-03' " +
246270
"and c2 > 100", "true");
@@ -258,6 +282,23 @@ public void testGeneratedColumnPrune_RemovePredicate() throws Exception {
258282
"cast(add(2: c2, 100) as double) > add(cast(1: c1 as double), 1)");
259283
}
260284

285+
@Test
286+
public void testGeneratedColumnPrune_RemovePredicate2() throws Exception {
287+
testAssertContains("select * from t_gen_col2 where c1 >= '2024-02-02' ", "partitions=4/6");
288+
testAssertContains("select * from t_gen_col2 where c1 = '2024-02-02' ", "partitions=2/6");
289+
testAssertContains("select * from t_gen_col2 where c1 = '2024-02-02' and c2 > 100", "partitions=0/6");
290+
testAssertContains("select * from t_gen_col2 where c1 >= '2024-02-02' and c1 <= '2024-02-03' " +
291+
"and c2 > 100", "partitions=0/6");
292+
testAssertContains("select * from t_gen_col2 where c2 in (1, 2,3)", "partitions=6/6");
293+
testAssertContains("select * from t_gen_col2 where c2 = cast('123' as int)", "partitions=0/6");
294+
295+
// can not be removed
296+
testAssertContains("select * from t_gen_col2 where c1 = random() and c2 > 100",
297+
"partitions=0/6");
298+
testAssertContains("select * from t_gen_col2 where c2 + 100 > c1 + 1",
299+
"partitions=6/6");
300+
}
301+
261302
@Test
262303
public void testGeneratedColumnPrune() throws Exception {
263304
// c2

0 commit comments

Comments
 (0)