1717
1818package org .apache .doris .nereids .rules .expression .rules ;
1919
20+ import org .apache .doris .analysis .LiteralExpr ;
2021import org .apache .doris .catalog .ListPartitionItem ;
2122import org .apache .doris .catalog .PartitionInfo ;
2223import org .apache .doris .catalog .PartitionItem ;
24+ import org .apache .doris .catalog .PartitionKey ;
2325import org .apache .doris .catalog .RangePartitionItem ;
2426import org .apache .doris .nereids .CascadesContext ;
2527import org .apache .doris .nereids .trees .expressions .Cast ;
4345/**
4446 * PartitionPruner
4547 */
48+ @ Data
4649public class PartitionPruner extends DefaultExpressionRewriter <Void > {
4750 private final List <OnePartitionEvaluator > partitions ;
4851 private final Expression partitionPredicate ;
@@ -115,6 +118,12 @@ public static List<Long> prune(List<Slot> partitionSlots, Expression partitionPr
115118 PartitionTableType partitionTableType ) {
116119 partitionPredicate = TryEliminateUninterestedPredicates .rewrite (
117120 partitionPredicate , ImmutableSet .copyOf (partitionSlots ), cascadesContext );
121+ if (cascadesContext .getConnectContext ().getSessionVariable ().inToMinmaxParitionRewriteThreshold > 0
122+ && canRewriteToMinMax (idToPartitions )) {
123+ double rangeLength = totalRangeLength (idToPartitions );
124+ partitionPredicate = rewritePartitionPredicateForRangePartition (
125+ partitionPredicate , rangeLength , cascadesContext );
126+ }
118127
119128 List <OnePartitionEvaluator > evaluators = idToPartitions .entrySet ()
120129 .stream ()
@@ -127,6 +136,85 @@ public static List<Long> prune(List<Slot> partitionSlots, Expression partitionPr
127136 return partitionPruner .prune ();
128137 }
129138
139+ private static boolean isRangePartition (Map <Long , PartitionItem > idToPartitions ) {
140+ if (idToPartitions .isEmpty ()) {
141+ return false ;
142+ }
143+ PartitionItem item = idToPartitions .values ().iterator ().next ();
144+ return item instanceof RangePartitionItem ;
145+ }
146+
147+ private static boolean canRewriteToMinMax (Map <Long , PartitionItem > idToPartitions ) {
148+ if (isRangePartition (idToPartitions )) {
149+ RangePartitionItem item = (RangePartitionItem ) idToPartitions .values ().iterator ().next ();
150+ if (item == null ) {
151+ return false ;
152+ }
153+ PartitionKey k1 = item .getItems ().upperEndpoint ();
154+ if (k1 .getKeys ().size () != 1 ) {
155+ return false ; // multi keys or hive keys
156+ }
157+ LiteralExpr bound = k1 .getKeys ().get (0 );
158+ if (bound instanceof org .apache .doris .analysis .DateLiteral ) {
159+ return true ;
160+ }
161+ }
162+ return false ;
163+ }
164+
165+ private static double getUpperBoundFromRangePartitionItem (RangePartitionItem item ) {
166+ org .apache .doris .analysis .DateLiteral d1 =
167+ (org .apache .doris .analysis .DateLiteral ) item .getItems ().upperEndpoint ().getKeys ().get (0 );
168+ return d1 .getDouble ();
169+ }
170+
171+ private static double getLowerBoundFromRangePartitionItem (RangePartitionItem item ) {
172+ org .apache .doris .analysis .DateLiteral d1 =
173+ (org .apache .doris .analysis .DateLiteral ) item .getItems ().lowerEndpoint ().getKeys ().get (0 );
174+ return d1 .getDouble ();
175+ }
176+
177+ // for range partition, total range is from the end of the first partition to the beginning of the last partition
178+ private static double totalRangeLength (Map <Long , PartitionItem > idToPartitions ) {
179+ if (idToPartitions .size () == 1 ) {
180+ Long id = idToPartitions .keySet ().iterator ().next ();
181+ RangePartitionItem item = (RangePartitionItem ) idToPartitions .get (id );
182+ double d1 = getUpperBoundFromRangePartitionItem (item );
183+ double d2 = getLowerBoundFromRangePartitionItem (item );
184+ return Math .max (1 , d1 - d2 );
185+ }
186+
187+ Long id = idToPartitions .keySet ().iterator ().next ();
188+ RangePartitionItem firstItem = (RangePartitionItem ) idToPartitions .get (id );
189+ RangePartitionItem lastItem = firstItem ;
190+ double min = getLowerBoundFromRangePartitionItem (firstItem );
191+ double max = getUpperBoundFromRangePartitionItem (lastItem );
192+ for (Long pid : idToPartitions .keySet ()) {
193+ RangePartitionItem item = (RangePartitionItem ) idToPartitions .get (pid );
194+ double lower = getLowerBoundFromRangePartitionItem (item );
195+ if (lower < min ) {
196+ min = lower ;
197+ firstItem = item ;
198+ } else {
199+ double upper = getUpperBoundFromRangePartitionItem (item );
200+ if (upper > max ) {
201+ max = upper ;
202+ lastItem = item ;
203+ }
204+ }
205+ }
206+ double endOfFirstItem = getUpperBoundFromRangePartitionItem (firstItem );
207+ double beginOfLastItem = getLowerBoundFromRangePartitionItem (lastItem );
208+ return Math .max (1 , beginOfLastItem - endOfFirstItem );
209+ }
210+
211+ private static Expression rewritePartitionPredicateForRangePartition (Expression partitionPredicate , double range ,
212+ CascadesContext ctx ) {
213+ partitionPredicate = OrToIn .INSTANCE .rewrite (partitionPredicate , null );
214+ RewriteRangePartitionPredicate rewriter = new RewriteRangePartitionPredicate (range );
215+ return partitionPredicate .accept (rewriter , ctx );
216+ }
217+
130218 /**
131219 * convert partition item to partition evaluator
132220 */
0 commit comments