25
25
import com .starrocks .sql .optimizer .operator .logical .LogicalScanOperator ;
26
26
import com .starrocks .sql .optimizer .operator .scalar .ScalarOperator ;
27
27
import com .starrocks .sql .optimizer .statistics .StatisticsCalculator ;
28
+ import com .starrocks .thrift .TExplainLevel ;
28
29
import com .starrocks .utframe .UtFrameUtils ;
29
30
import org .junit .Assert ;
30
31
import org .junit .BeforeClass ;
@@ -75,6 +76,22 @@ public static void beforeClass() throws Exception {
75
76
starRocksAssert .ddl ("ALTER TABLE t_gen_col ADD PARTITION p2_202402 VALUES IN (('2', '2024-02-01'))" );
76
77
starRocksAssert .ddl ("ALTER TABLE t_gen_col ADD PARTITION p2_202403 VALUES IN (('2', '2024-03-01'))" );
77
78
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
+
78
95
starRocksAssert .withTable ("CREATE TABLE t_bool_partition (" +
79
96
" c1 datetime NOT NULL, " +
80
97
" c2 boolean" +
@@ -224,6 +241,7 @@ public void testNullException() throws Exception {
224
241
private static Pair <ScalarOperator , LogicalScanOperator > buildConjunctAndScan (String sql ) throws Exception {
225
242
Pair <String , ExecPlan > pair = UtFrameUtils .getPlanAndFragment (connectContext , sql );
226
243
ExecPlan execPlan = pair .second ;
244
+ System .out .println (execPlan .getExplainString (TExplainLevel .NORMAL ));
227
245
LogicalScanOperator scanOperator =
228
246
(LogicalScanOperator ) execPlan .getLogicalPlan ().getRoot ().inputAt (0 ).inputAt (0 ).inputAt (0 ).getOp ();
229
247
ScalarOperator predicate = execPlan .getPhysicalPlan ().getOp ().getPredicate ();
@@ -238,9 +256,15 @@ private void testRemovePredicate(String sql, String expected) throws Exception {
238
256
Assert .assertEquals (expected , newPredicate .toString ());
239
257
}
240
258
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
+
241
266
@ Test
242
267
public void testGeneratedColumnPrune_RemovePredicate () throws Exception {
243
- testRemovePredicate ("select * from t_gen_col where c1 = '2024-01-01' " , "true" );
244
268
testRemovePredicate ("select * from t_gen_col where c1 = '2024-01-01' and c2 > 100" , "true" );
245
269
testRemovePredicate ("select * from t_gen_col where c1 >= '2024-01-01' and c1 <= '2024-01-03' " +
246
270
"and c2 > 100" , "true" );
@@ -258,6 +282,23 @@ public void testGeneratedColumnPrune_RemovePredicate() throws Exception {
258
282
"cast(add(2: c2, 100) as double) > add(cast(1: c1 as double), 1)" );
259
283
}
260
284
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
+
261
302
@ Test
262
303
public void testGeneratedColumnPrune () throws Exception {
263
304
// c2
0 commit comments