2525import com .starrocks .sql .optimizer .operator .logical .LogicalScanOperator ;
2626import com .starrocks .sql .optimizer .operator .scalar .ScalarOperator ;
2727import com .starrocks .sql .optimizer .statistics .StatisticsCalculator ;
28+ import com .starrocks .thrift .TExplainLevel ;
2829import com .starrocks .utframe .UtFrameUtils ;
2930import org .junit .Assert ;
3031import 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