/
ReduceExpressionsRule.java
1208 lines (1092 loc) · 46.5 KB
/
ReduceExpressionsRule.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to you under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.calcite.rel.rules;
import org.apache.calcite.plan.RelOptCluster;
import org.apache.calcite.plan.RelOptPredicateList;
import org.apache.calcite.plan.RelOptRuleCall;
import org.apache.calcite.plan.RelOptUtil;
import org.apache.calcite.plan.RelRule;
import org.apache.calcite.rel.RelCollation;
import org.apache.calcite.rel.RelCollations;
import org.apache.calcite.rel.RelFieldCollation;
import org.apache.calcite.rel.RelNode;
import org.apache.calcite.rel.core.Calc;
import org.apache.calcite.rel.core.Filter;
import org.apache.calcite.rel.core.Join;
import org.apache.calcite.rel.core.Project;
import org.apache.calcite.rel.core.Window;
import org.apache.calcite.rel.logical.LogicalCalc;
import org.apache.calcite.rel.logical.LogicalFilter;
import org.apache.calcite.rel.logical.LogicalProject;
import org.apache.calcite.rel.logical.LogicalWindow;
import org.apache.calcite.rel.metadata.RelMetadataQuery;
import org.apache.calcite.rel.type.RelDataTypeFactory;
import org.apache.calcite.rex.RexBuilder;
import org.apache.calcite.rex.RexCall;
import org.apache.calcite.rex.RexCorrelVariable;
import org.apache.calcite.rex.RexDynamicParam;
import org.apache.calcite.rex.RexExecutor;
import org.apache.calcite.rex.RexFieldAccess;
import org.apache.calcite.rex.RexInputRef;
import org.apache.calcite.rex.RexLiteral;
import org.apache.calcite.rex.RexLocalRef;
import org.apache.calcite.rex.RexNode;
import org.apache.calcite.rex.RexOver;
import org.apache.calcite.rex.RexProgram;
import org.apache.calcite.rex.RexProgramBuilder;
import org.apache.calcite.rex.RexRangeRef;
import org.apache.calcite.rex.RexShuttle;
import org.apache.calcite.rex.RexSimplify;
import org.apache.calcite.rex.RexSubQuery;
import org.apache.calcite.rex.RexUnknownAs;
import org.apache.calcite.rex.RexUtil;
import org.apache.calcite.rex.RexVisitorImpl;
import org.apache.calcite.sql.SqlAggFunction;
import org.apache.calcite.sql.SqlKind;
import org.apache.calcite.sql.SqlOperator;
import org.apache.calcite.sql.fun.SqlRowOperator;
import org.apache.calcite.sql.type.SqlTypeName;
import org.apache.calcite.tools.RelBuilder;
import org.apache.calcite.tools.RelBuilderFactory;
import org.apache.calcite.util.ImmutableBitSet;
import org.apache.calcite.util.Pair;
import org.apache.calcite.util.Util;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.immutables.value.Value;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Deque;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
/**
* Collection of planner rules that apply various simplifying transformations on
* RexNode trees. Currently, there are two transformations:
*
* <ul>
* <li>Constant reduction, which evaluates constant subtrees, replacing them
* with a corresponding RexLiteral
* <li>Removal of redundant casts, which occurs when the argument into the cast
* is the same as the type of the resulting cast expression
* </ul>
*
* @param <C> Configuration type
*/
public abstract class ReduceExpressionsRule<C extends ReduceExpressionsRule.Config>
extends RelRule<C>
implements SubstitutionRule {
//~ Static fields/initializers ---------------------------------------------
/**
* Regular expression that matches the description of all instances of this
* rule and {@link ValuesReduceRule} also. Use
* it to prevent the planner from invoking these rules.
*/
public static final Pattern EXCLUSION_PATTERN =
Pattern.compile("Reduce(Expressions|Values)Rule.*");
/**
* Rule that reduces constants inside a {@link org.apache.calcite.rel.core.Filter}.
* If the condition is a constant, the filter is removed (if TRUE) or replaced with
* an empty {@link org.apache.calcite.rel.core.Values} (if FALSE or NULL).
*
* @see CoreRules#FILTER_REDUCE_EXPRESSIONS
*/
public static class FilterReduceExpressionsRule
extends ReduceExpressionsRule<FilterReduceExpressionsRule.FilterReduceExpressionsRuleConfig> {
/** Creates a FilterReduceExpressionsRule. */
protected FilterReduceExpressionsRule(FilterReduceExpressionsRuleConfig config) {
super(config);
}
@Deprecated // to be removed before 2.0
public FilterReduceExpressionsRule(Class<? extends Filter> filterClass,
RelBuilderFactory relBuilderFactory) {
this(FilterReduceExpressionsRuleConfig.DEFAULT.withRelBuilderFactory(relBuilderFactory)
.as(FilterReduceExpressionsRuleConfig.class)
.withOperandFor(filterClass)
.withMatchNullability(true)
.as(FilterReduceExpressionsRuleConfig.class));
}
@Deprecated // to be removed before 2.0
public FilterReduceExpressionsRule(Class<? extends Filter> filterClass,
boolean matchNullability, RelBuilderFactory relBuilderFactory) {
this(FilterReduceExpressionsRuleConfig.DEFAULT.withRelBuilderFactory(relBuilderFactory)
.as(FilterReduceExpressionsRuleConfig.class)
.withOperandFor(filterClass)
.withMatchNullability(matchNullability)
.as(FilterReduceExpressionsRuleConfig.class));
}
@Override public void onMatch(RelOptRuleCall call) {
final Filter filter = call.rel(0);
final List<RexNode> expList =
Lists.newArrayList(filter.getCondition());
RexNode newConditionExp;
boolean reduced;
final RelMetadataQuery mq = call.getMetadataQuery();
final RelOptPredicateList predicates =
mq.getPulledUpPredicates(filter.getInput());
if (reduceExpressions(filter, expList, predicates, true,
config.matchNullability(), config.treatDynamicCallsAsConstant())) {
assert expList.size() == 1;
newConditionExp = expList.get(0);
reduced = true;
} else {
// No reduction, but let's still test the original
// predicate to see if it was already a constant,
// in which case we don't need any runtime decision
// about filtering.
newConditionExp = filter.getCondition();
reduced = false;
}
// Even if no reduction, let's still test the original
// predicate to see if it was already a constant,
// in which case we don't need any runtime decision
// about filtering.
if (newConditionExp.isAlwaysTrue()) {
call.transformTo(
filter.getInput());
} else if (newConditionExp instanceof RexLiteral
|| RexUtil.isNullLiteral(newConditionExp, true)) {
call.transformTo(createEmptyRelOrEquivalent(call, filter));
} else if (reduced) {
call.transformTo(call.builder()
.push(filter.getInput())
.filter(newConditionExp).build());
} else {
if (newConditionExp instanceof RexCall) {
boolean reverse = newConditionExp.getKind() == SqlKind.NOT;
if (reverse) {
newConditionExp = ((RexCall) newConditionExp).getOperands().get(0);
}
reduceNotNullableFilter(call, filter, newConditionExp, reverse);
}
return;
}
// New plan is absolutely better than old plan.
call.getPlanner().prune(filter);
}
/**
* For static schema systems, a filter that is always false or null can be
* replaced by a values operator that produces no rows, as the schema
* information can just be taken from the input Rel. In dynamic schema
* environments, the filter might have an unknown input type, in these cases
* they must define a system specific alternative to a Values operator, such
* as inserting a limit 0 instead of a filter on top of the original input.
*
* <p>The default implementation of this method is to call
* {@link RelBuilder#empty}, which for the static schema will be optimized
* to an empty
* {@link org.apache.calcite.rel.core.Values}.
*
* @param input rel to replace, assumes caller has already determined
* equivalence to Values operation for 0 records or a
* false filter.
* @return equivalent but less expensive replacement rel
*/
protected RelNode createEmptyRelOrEquivalent(RelOptRuleCall call, Filter input) {
return call.builder().push(input).empty().build();
}
private void reduceNotNullableFilter(
RelOptRuleCall call,
Filter filter,
RexNode rexNode,
boolean reverse) {
// If the expression is a IS [NOT] NULL on a non-nullable
// column, then we can either remove the filter or replace
// it with an Empty.
boolean alwaysTrue;
switch (rexNode.getKind()) {
case IS_NULL:
case IS_UNKNOWN:
alwaysTrue = false;
break;
case IS_NOT_NULL:
alwaysTrue = true;
break;
default:
return;
}
if (reverse) {
alwaysTrue = !alwaysTrue;
}
RexNode operand = ((RexCall) rexNode).getOperands().get(0);
if (operand instanceof RexInputRef) {
RexInputRef inputRef = (RexInputRef) operand;
if (!inputRef.getType().isNullable()) {
if (alwaysTrue) {
call.transformTo(filter.getInput());
} else {
call.transformTo(createEmptyRelOrEquivalent(call, filter));
}
// New plan is absolutely better than old plan.
call.getPlanner().prune(filter);
}
}
}
/** Rule configuration. */
@Value.Immutable
public interface FilterReduceExpressionsRuleConfig extends ReduceExpressionsRule.Config {
FilterReduceExpressionsRuleConfig DEFAULT = ImmutableFilterReduceExpressionsRuleConfig.of()
.withMatchNullability(true)
.withOperandFor(LogicalFilter.class)
.withDescription("ReduceExpressionsRule(Filter)")
.as(FilterReduceExpressionsRuleConfig.class);
@Override default FilterReduceExpressionsRule toRule() {
return new FilterReduceExpressionsRule(this);
}
}
}
/** Rule that reduces constants inside a
* {@link org.apache.calcite.rel.core.Project}.
*
* @see CoreRules#PROJECT_REDUCE_EXPRESSIONS */
public static class ProjectReduceExpressionsRule
extends ReduceExpressionsRule<
ProjectReduceExpressionsRule.ProjectReduceExpressionsRuleConfig> {
/** Creates a ProjectReduceExpressionsRule. */
protected ProjectReduceExpressionsRule(ProjectReduceExpressionsRuleConfig config) {
super(config);
}
@Deprecated // to be removed before 2.0
public ProjectReduceExpressionsRule(Class<? extends Project> projectClass,
RelBuilderFactory relBuilderFactory) {
this(ProjectReduceExpressionsRuleConfig.DEFAULT.withRelBuilderFactory(relBuilderFactory)
.as(ProjectReduceExpressionsRuleConfig.class)
.withOperandFor(projectClass)
.as(ProjectReduceExpressionsRuleConfig.class));
}
@Deprecated // to be removed before 2.0
public ProjectReduceExpressionsRule(Class<? extends Project> projectClass,
boolean matchNullability, RelBuilderFactory relBuilderFactory) {
this(ProjectReduceExpressionsRuleConfig.DEFAULT.withRelBuilderFactory(relBuilderFactory)
.as(ProjectReduceExpressionsRuleConfig.class)
.withOperandFor(projectClass)
.withMatchNullability(matchNullability)
.as(ProjectReduceExpressionsRuleConfig.class));
}
@Override public void onMatch(RelOptRuleCall call) {
final Project project = call.rel(0);
final RelMetadataQuery mq = call.getMetadataQuery();
final RelOptPredicateList predicates =
mq.getPulledUpPredicates(project.getInput());
final List<RexNode> expList =
Lists.newArrayList(project.getProjects());
if (reduceExpressions(project, expList, predicates, false,
config.matchNullability(), config.treatDynamicCallsAsConstant())) {
assert !project.getProjects().equals(expList)
: "Reduced expressions should be different from original expressions";
call.transformTo(
call.builder()
.push(project.getInput())
.project(expList, project.getRowType().getFieldNames())
.build());
// New plan is absolutely better than old plan.
call.getPlanner().prune(project);
}
}
/** Rule configuration. */
@Value.Immutable
public interface ProjectReduceExpressionsRuleConfig extends ReduceExpressionsRule.Config {
ProjectReduceExpressionsRuleConfig DEFAULT = ImmutableProjectReduceExpressionsRuleConfig.of()
.withMatchNullability(true)
.withOperandFor(LogicalProject.class)
.withDescription("ReduceExpressionsRule(Project)")
.as(ProjectReduceExpressionsRuleConfig.class);
@Override default ProjectReduceExpressionsRule toRule() {
return new ProjectReduceExpressionsRule(this);
}
}
}
/** Rule that reduces constants inside a {@link Join}.
*
* @see CoreRules#JOIN_REDUCE_EXPRESSIONS */
public static class JoinReduceExpressionsRule
extends ReduceExpressionsRule<JoinReduceExpressionsRule.JoinReduceExpressionsRuleConfig> {
/** Creates a JoinReduceExpressionsRule. */
protected JoinReduceExpressionsRule(JoinReduceExpressionsRuleConfig config) {
super(config);
}
@Deprecated // to be removed before 2.0
public JoinReduceExpressionsRule(Class<? extends Join> joinClass,
RelBuilderFactory relBuilderFactory) {
this(JoinReduceExpressionsRuleConfig.DEFAULT.withRelBuilderFactory(relBuilderFactory)
.as(JoinReduceExpressionsRuleConfig.class)
.withOperandFor(joinClass)
.withMatchNullability(true)
.as(JoinReduceExpressionsRuleConfig.class));
}
@Deprecated // to be removed before 2.0
public JoinReduceExpressionsRule(Class<? extends Join> joinClass,
boolean matchNullability, RelBuilderFactory relBuilderFactory) {
this(JoinReduceExpressionsRuleConfig.DEFAULT.withRelBuilderFactory(relBuilderFactory)
.as(JoinReduceExpressionsRuleConfig.class)
.withOperandFor(joinClass)
.withMatchNullability(matchNullability)
.as(JoinReduceExpressionsRuleConfig.class));
}
@Override public void onMatch(RelOptRuleCall call) {
final Join join = call.rel(0);
final List<RexNode> expList = Lists.newArrayList(join.getCondition());
final int fieldCount = join.getLeft().getRowType().getFieldCount();
final RelMetadataQuery mq = call.getMetadataQuery();
final RelOptPredicateList leftPredicates =
mq.getPulledUpPredicates(join.getLeft());
final RelOptPredicateList rightPredicates =
mq.getPulledUpPredicates(join.getRight());
final RexBuilder rexBuilder = join.getCluster().getRexBuilder();
final RelOptPredicateList predicates =
leftPredicates.union(rexBuilder,
rightPredicates.shift(rexBuilder, fieldCount));
if (!reduceExpressions(join, expList, predicates, true,
config.matchNullability(), config.treatDynamicCallsAsConstant())) {
return;
}
call.transformTo(
join.copy(
join.getTraitSet(),
expList.get(0),
join.getLeft(),
join.getRight(),
join.getJoinType(),
join.isSemiJoinDone()));
// New plan is absolutely better than old plan.
call.getPlanner().prune(join);
}
/** Rule configuration. */
@Value.Immutable
public interface JoinReduceExpressionsRuleConfig extends ReduceExpressionsRule.Config {
JoinReduceExpressionsRuleConfig DEFAULT = ImmutableJoinReduceExpressionsRuleConfig.of()
.withMatchNullability(false)
.withOperandFor(Join.class)
.withDescription("ReduceExpressionsRule(Join)")
.as(JoinReduceExpressionsRuleConfig.class);
@Override default JoinReduceExpressionsRule toRule() {
return new JoinReduceExpressionsRule(this);
}
}
}
/**
* Rule that reduces constants inside a {@link Calc}.
*
* @see CoreRules#CALC_REDUCE_EXPRESSIONS
*/
public static class CalcReduceExpressionsRule
extends ReduceExpressionsRule<CalcReduceExpressionsRule.CalcReduceExpressionsRuleConfig> {
/** Creates a CalcReduceExpressionsRule. */
protected CalcReduceExpressionsRule(CalcReduceExpressionsRuleConfig config) {
super(config);
}
@Deprecated // to be removed before 2.0
public CalcReduceExpressionsRule(Class<? extends Calc> calcClass,
RelBuilderFactory relBuilderFactory) {
this(CalcReduceExpressionsRuleConfig.DEFAULT.withRelBuilderFactory(relBuilderFactory)
.as(CalcReduceExpressionsRuleConfig.class)
.withOperandFor(calcClass)
.withMatchNullability(true)
.as(CalcReduceExpressionsRuleConfig.class));
}
@Deprecated // to be removed before 2.0
public CalcReduceExpressionsRule(Class<? extends Calc> calcClass,
boolean matchNullability, RelBuilderFactory relBuilderFactory) {
this(CalcReduceExpressionsRuleConfig.DEFAULT.withRelBuilderFactory(relBuilderFactory)
.as(CalcReduceExpressionsRuleConfig.class)
.withOperandFor(calcClass)
.withMatchNullability(matchNullability)
.as(CalcReduceExpressionsRuleConfig.class));
}
@Override public void onMatch(RelOptRuleCall call) {
Calc calc = call.rel(0);
RexProgram program = calc.getProgram();
final List<RexNode> exprList = program.getExprList();
// Form a list of expressions with sub-expressions fully expanded.
final List<RexNode> expandedExprList = new ArrayList<>();
final RexShuttle shuttle =
new RexShuttle() {
@Override public RexNode visitLocalRef(RexLocalRef localRef) {
return expandedExprList.get(localRef.getIndex());
}
};
for (RexNode expr : exprList) {
expandedExprList.add(expr.accept(shuttle));
}
final RelOptPredicateList predicates = RelOptPredicateList.EMPTY;
if (reduceExpressions(calc, expandedExprList, predicates, false,
config.matchNullability(), config.treatDynamicCallsAsConstant())) {
final RexProgramBuilder builder =
new RexProgramBuilder(
calc.getInput().getRowType(),
calc.getCluster().getRexBuilder());
final List<RexLocalRef> list = new ArrayList<>();
for (RexNode expr : expandedExprList) {
list.add(builder.registerInput(expr));
}
if (program.getCondition() != null) {
final int conditionIndex =
program.getCondition().getIndex();
final RexNode newConditionExp =
expandedExprList.get(conditionIndex);
if (newConditionExp.isAlwaysTrue()) {
// condition is always TRUE - drop it.
} else if (newConditionExp instanceof RexLiteral
|| RexUtil.isNullLiteral(newConditionExp, true)) {
// condition is always NULL or FALSE - replace calc
// with empty.
call.transformTo(createEmptyRelOrEquivalent(call, calc));
return;
} else {
builder.addCondition(list.get(conditionIndex));
}
}
int k = 0;
for (RexLocalRef projectExpr : program.getProjectList()) {
final int index = projectExpr.getIndex();
builder.addProject(
list.get(index).getIndex(),
program.getOutputRowType().getFieldNames().get(k++));
}
call.transformTo(
calc.copy(calc.getTraitSet(), calc.getInput(), builder.getProgram()));
// New plan is absolutely better than old plan.
call.getPlanner().prune(calc);
}
}
/**
* For static schema systems, a filter that is always false or null can be
* replaced by a values operator that produces no rows, as the schema
* information can just be taken from the input Rel. In dynamic schema
* environments, the filter might have an unknown input type, in these cases
* they must define a system specific alternative to a Values operator, such
* as inserting a limit 0 instead of a filter on top of the original input.
*
* <p>The default implementation of this method is to call
* {@link RelBuilder#empty}, which for the static schema will be optimized
* to an Immutable.Config.of()
* {@link org.apache.calcite.rel.core.Values}.
*
* @param input rel to replace, assumes caller has already determined
* equivalence to Values operation for 0 records or a
* false filter.
* @return equivalent but less expensive replacement rel
*/
protected RelNode createEmptyRelOrEquivalent(RelOptRuleCall call, Calc input) {
return call.builder().push(input).empty().build();
}
/** Rule configuration. */
@Value.Immutable
public interface CalcReduceExpressionsRuleConfig extends ReduceExpressionsRule.Config {
CalcReduceExpressionsRuleConfig DEFAULT = ImmutableCalcReduceExpressionsRuleConfig.of()
.withMatchNullability(true)
.withOperandFor(LogicalCalc.class)
.withDescription("ReduceExpressionsRule(Calc)")
.as(CalcReduceExpressionsRuleConfig.class);
@Override default CalcReduceExpressionsRule toRule() {
return new CalcReduceExpressionsRule(this);
}
}
}
/** Rule that reduces constants inside a {@link Window}.
*
* @see CoreRules#WINDOW_REDUCE_EXPRESSIONS */
public static class WindowReduceExpressionsRule
extends ReduceExpressionsRule<WindowReduceExpressionsRule.WindowReduceExpressionsRuleConfig> {
/** Creates a WindowReduceExpressionsRule. */
protected WindowReduceExpressionsRule(WindowReduceExpressionsRuleConfig config) {
super(config);
}
@Deprecated // to be removed before 2.0
public WindowReduceExpressionsRule(Class<? extends Window> windowClass,
boolean matchNullability, RelBuilderFactory relBuilderFactory) {
this(WindowReduceExpressionsRuleConfig.DEFAULT.withRelBuilderFactory(relBuilderFactory)
.as(WindowReduceExpressionsRuleConfig.class)
.withOperandFor(windowClass)
.withMatchNullability(matchNullability)
.as(WindowReduceExpressionsRuleConfig.class));
}
@Override public void onMatch(RelOptRuleCall call) {
LogicalWindow window = call.rel(0);
RexBuilder rexBuilder = window.getCluster().getRexBuilder();
final RelMetadataQuery mq = call.getMetadataQuery();
final RelOptPredicateList predicates = mq
.getPulledUpPredicates(window.getInput());
boolean reduced = false;
final List<Window.Group> groups = new ArrayList<>();
for (Window.Group group : window.groups) {
List<Window.RexWinAggCall> aggCalls = new ArrayList<>();
for (Window.RexWinAggCall aggCall : group.aggCalls) {
final List<RexNode> expList = new ArrayList<>(aggCall.getOperands());
if (reduceExpressions(window, expList, predicates)) {
aggCall = new Window.RexWinAggCall(
(SqlAggFunction) aggCall.getOperator(), aggCall.type, expList,
aggCall.ordinal, aggCall.distinct, aggCall.ignoreNulls);
reduced = true;
}
aggCalls.add(aggCall);
}
final ImmutableBitSet.Builder keyBuilder = ImmutableBitSet.builder();
for (Integer key : group.keys) {
if (!predicates.constantMap.containsKey(
rexBuilder.makeInputRef(window.getInput(), key))) {
keyBuilder.set(key);
}
}
final ImmutableBitSet keys = keyBuilder.build();
reduced |= keys.cardinality() != group.keys.cardinality();
final List<RelFieldCollation> collationsList = group.orderKeys
.getFieldCollations().stream()
.filter(fc ->
!predicates.constantMap.containsKey(
rexBuilder.makeInputRef(window.getInput(),
fc.getFieldIndex())))
.collect(Collectors.toList());
boolean collationReduced =
group.orderKeys.getFieldCollations().size() != collationsList.size();
reduced |= collationReduced;
RelCollation relCollation = collationReduced
? RelCollations.of(collationsList)
: group.orderKeys;
groups.add(
new Window.Group(keys, group.isRows, group.lowerBound,
group.upperBound, relCollation, aggCalls));
}
if (reduced) {
call.transformTo(LogicalWindow
.create(window.getTraitSet(), window.getInput(),
window.getConstants(), window.getRowType(), groups));
call.getPlanner().prune(window);
}
}
/** Rule configuration. */
@Value.Immutable
public interface WindowReduceExpressionsRuleConfig extends ReduceExpressionsRule.Config {
WindowReduceExpressionsRuleConfig DEFAULT = ImmutableWindowReduceExpressionsRuleConfig.of()
.withMatchNullability(true)
.withOperandFor(LogicalWindow.class)
.withDescription("ReduceExpressionsRule(Window)")
.as(WindowReduceExpressionsRuleConfig.class);
@Override default WindowReduceExpressionsRule toRule() {
return new WindowReduceExpressionsRule(this);
}
}
}
//~ Constructors -----------------------------------------------------------
/** Creates a ReduceExpressionsRule. */
protected ReduceExpressionsRule(C config) {
super(config);
}
//~ Methods ----------------------------------------------------------------
/**
* Reduces a list of expressions.
*
* @param rel Relational expression
* @param expList List of expressions, modified in place
* @param predicates Constraints known to hold on input expressions
* @return whether reduction found something to change, and succeeded
*/
protected static boolean reduceExpressions(RelNode rel, List<RexNode> expList,
RelOptPredicateList predicates) {
return reduceExpressions(rel, expList, predicates, false, true, false);
}
@Deprecated // to be removed before 2.0
protected static boolean reduceExpressions(RelNode rel, List<RexNode> expList,
RelOptPredicateList predicates, boolean unknownAsFalse) {
return reduceExpressions(rel, expList, predicates, unknownAsFalse, true, false);
}
/**
* Reduces a list of expressions.
*
* <p>The {@code matchNullability} flag comes into play when reducing a
* expression whose type is nullable. Suppose we are reducing an expression
* {@code CASE WHEN 'a' = 'a' THEN 1 ELSE NULL END}. Before reduction the
* type is {@code INTEGER} (nullable), but after reduction the literal 1 has
* type {@code INTEGER NOT NULL}.
*
* <p>In some situations it is more important to preserve types; in this
* case you should use {@code matchNullability = true} (which used to be
* the default behavior of this method), and it will cast the literal to
* {@code INTEGER} (nullable).
*
* <p>In other situations, you would rather propagate the new stronger type,
* because it may allow further optimizations later; pass
* {@code matchNullability = false} and no cast will be added, but you may
* need to adjust types elsewhere in the expression tree.
*
* @param rel Relational expression
* @param expList List of expressions, modified in place
* @param predicates Constraints known to hold on input expressions
* @param unknownAsFalse Whether UNKNOWN will be treated as FALSE
* @param matchNullability Whether Calcite should add a CAST to a literal
* resulting from simplification and expression if the
* expression had nullable type and the literal is
* NOT NULL
* @param treatDynamicCallsAsConstant Whether to treat dynamic functions as
* constants
*
* @return whether reduction found something to change, and succeeded
*/
protected static boolean reduceExpressions(RelNode rel, List<RexNode> expList,
RelOptPredicateList predicates, boolean unknownAsFalse,
boolean matchNullability, boolean treatDynamicCallsAsConstant) {
final RelOptCluster cluster = rel.getCluster();
final RexBuilder rexBuilder = cluster.getRexBuilder();
final List<RexNode> originExpList = Lists.newArrayList(expList);
final RexExecutor executor =
Util.first(cluster.getPlanner().getExecutor(), RexUtil.EXECUTOR);
final RexSimplify simplify =
new RexSimplify(rexBuilder, predicates, executor);
// Simplify predicates in place
final RexUnknownAs unknownAs = RexUnknownAs.falseIf(unknownAsFalse);
final boolean reduced = reduceExpressionsInternal(rel, simplify, unknownAs,
expList, predicates, treatDynamicCallsAsConstant);
boolean simplified = false;
for (int i = 0; i < expList.size(); i++) {
final RexNode expr2 =
simplify.simplifyPreservingType(expList.get(i), unknownAs,
matchNullability);
if (!expr2.equals(expList.get(i))) {
expList.set(i, expr2);
simplified = true;
}
}
if (reduced && simplified) {
return !originExpList.equals(expList);
}
return reduced || simplified;
}
protected static boolean reduceExpressionsInternal(RelNode rel,
RexSimplify simplify, RexUnknownAs unknownAs, List<RexNode> expList,
RelOptPredicateList predicates, boolean treatDynamicCallsAsConstant) {
// Replace predicates on CASE to CASE on predicates.
boolean changed = new CaseShuttle().mutate(expList);
// Find reducible expressions.
final List<RexNode> constExps = new ArrayList<>();
List<Boolean> addCasts = new ArrayList<>();
findReducibleExps(rel.getCluster().getTypeFactory(), expList,
predicates.constantMap, constExps, addCasts, treatDynamicCallsAsConstant);
if (constExps.isEmpty()) {
return changed;
}
final List<RexNode> constExps2 = Lists.newArrayList(constExps);
if (!predicates.constantMap.isEmpty()) {
final List<Map.Entry<RexNode, RexNode>> pairs =
Lists.newArrayList(predicates.constantMap.entrySet());
RexReplacer replacer =
new RexReplacer(simplify, unknownAs, Pair.left(pairs),
Pair.right(pairs), Collections.nCopies(pairs.size(), false));
replacer.mutate(constExps2);
}
// Compute the values they reduce to.
RexExecutor executor = rel.getCluster().getPlanner().getExecutor();
if (executor == null) {
// Cannot reduce expressions: caller has not set an executor in their
// environment. Caller should execute something like the following before
// invoking the planner:
//
// final RexExecutorImpl executor =
// new RexExecutorImpl(Schemas.createDataContext(null));
// rootRel.getCluster().getPlanner().setExecutor(executor);
return changed;
}
final List<RexNode> reducedValues = new ArrayList<>();
executor.reduce(simplify.rexBuilder, constExps2, reducedValues);
// Use RexNode.digest to judge whether each newly generated RexNode
// is equivalent to the original one.
if (RexUtil.strings(constExps).equals(RexUtil.strings(reducedValues))) {
return changed;
}
// For Project, we have to be sure to preserve the result
// types, so always cast regardless of the expression type.
// For other RelNodes like Filter, in general, this isn't necessary,
// and the presence of casts could hinder other rules such as sarg
// analysis, which require bare literals. But there are special cases,
// like when the expression is a UDR argument, that need to be
// handled as special cases.
if (rel instanceof Project) {
addCasts = Collections.nCopies(reducedValues.size(), true);
}
new RexReplacer(simplify, unknownAs, constExps, reducedValues, addCasts)
.mutate(expList);
return true;
}
/**
* Locates expressions that can be reduced to literals or converted to
* expressions with redundant casts removed.
*
* @param typeFactory Type factory
* @param exps list of candidate expressions to be examined for
* reduction
* @param constants List of expressions known to be constant
* @param constExps returns the list of expressions that can be constant
* reduced
* @param addCasts indicator for each expression that can be constant
* reduced, whether a cast of the resulting reduced
* expression is potentially necessary
* @param treatDynamicCallsAsConstant Whether to treat dynamic functions as
* constants
*/
protected static void findReducibleExps(RelDataTypeFactory typeFactory,
List<RexNode> exps, ImmutableMap<RexNode, RexNode> constants,
List<RexNode> constExps, List<Boolean> addCasts, boolean treatDynamicCallsAsConstant) {
ReducibleExprLocator gardener =
new ReducibleExprLocator(typeFactory, constants, constExps,
addCasts, treatDynamicCallsAsConstant);
for (RexNode exp : exps) {
gardener.analyze(exp);
}
assert constExps.size() == addCasts.size();
}
/** Creates a map containing each (e, constant) pair that occurs within
* a predicate list.
*
* @param clazz Class of expression that is considered constant
* @param rexBuilder Rex builder
* @param predicates Predicate list
* @param <C> what to consider a constant: {@link RexLiteral} to use a narrow
* definition of constant, or {@link RexNode} to use
* {@link RexUtil#isConstant(RexNode)}
* @return Map from values to constants
*
* @deprecated Use {@link RelOptPredicateList#constantMap}
*/
@Deprecated // to be removed before 2.0
public static <C extends RexNode> ImmutableMap<RexNode, C> predicateConstants(
Class<C> clazz, RexBuilder rexBuilder, RelOptPredicateList predicates) {
return RexUtil.predicateConstants(clazz, rexBuilder,
predicates.pulledUpPredicates);
}
/** Pushes predicates into a CASE.
*
* <p>We have a loose definition of 'predicate': any boolean expression will
* do, except CASE. For example '(CASE ...) = 5' or '(CASE ...) IS NULL'.
*/
public static RexCall pushPredicateIntoCase(RexCall call) {
if (call.getType().getSqlTypeName() != SqlTypeName.BOOLEAN) {
return call;
}
switch (call.getKind()) {
case CASE:
case AND:
case OR:
return call; // don't push CASE into CASE!
case EQUALS: {
// checks that the EQUALS operands may be split and
// doesn't push EQUALS into CASE
List<RexNode> equalsOperands = call.getOperands();
ImmutableBitSet left = RelOptUtil.InputFinder.bits(equalsOperands.get(0));
ImmutableBitSet right = RelOptUtil.InputFinder.bits(equalsOperands.get(1));
if (!left.isEmpty() && !right.isEmpty() && left.intersect(right).isEmpty()) {
return call;
}
break;
}
default:
break;
}
int caseOrdinal = -1;
final List<RexNode> operands = call.getOperands();
for (int i = 0; i < operands.size(); i++) {
RexNode operand = operands.get(i);
if (operand.getKind() == SqlKind.CASE) {
caseOrdinal = i;
}
}
if (caseOrdinal < 0) {
return call;
}
// Convert
// f(CASE WHEN p1 THEN v1 ... END, arg)
// to
// CASE WHEN p1 THEN f(v1, arg) ... END
final RexCall case_ = (RexCall) operands.get(caseOrdinal);
final List<RexNode> nodes = new ArrayList<>();
for (int i = 0; i < case_.getOperands().size(); i++) {
RexNode node = case_.getOperands().get(i);
if (!RexUtil.isCasePredicate(case_, i)) {
node = substitute(call, caseOrdinal, node);
}
nodes.add(node);
}
return case_.clone(call.getType(), nodes);
}
/** Converts op(arg0, ..., argOrdinal, ..., argN) to op(arg0,..., node, ..., argN). */
protected static RexNode substitute(RexCall call, int ordinal, RexNode node) {
final List<RexNode> newOperands = Lists.newArrayList(call.getOperands());
newOperands.set(ordinal, node);
return call.clone(call.getType(), newOperands);
}
//~ Inner Classes ----------------------------------------------------------
/**
* Replaces expressions with their reductions. Note that we only have to
* look for RexCall, since nothing else is reducible in the first place.
*/
protected static class RexReplacer extends RexShuttle {
private final RexSimplify simplify;
private final List<RexNode> reducibleExps;
private final List<RexNode> reducedValues;
private final List<Boolean> addCasts;
RexReplacer(
RexSimplify simplify,
RexUnknownAs unknownAs,
List<RexNode> reducibleExps,
List<RexNode> reducedValues,
List<Boolean> addCasts) {
this.simplify = simplify;
this.reducibleExps = reducibleExps;
this.reducedValues = reducedValues;
this.addCasts = addCasts;
}
@Override public RexNode visitInputRef(RexInputRef inputRef) {
RexNode node = visit(inputRef);
if (node == null) {
return super.visitInputRef(inputRef);
}
return node;
}
@Override public RexNode visitCall(RexCall call) {
RexNode node = visit(call);
if (node != null) {
return node;
}
node = super.visitCall(call);
return node;
}
private @Nullable RexNode visit(final RexNode call) {
int i = reducibleExps.indexOf(call);
if (i == -1) {
return null;
}
RexNode replacement = reducedValues.get(i);
if (addCasts.get(i)
&& (replacement.getType() != call.getType())) {
// Handle change from nullable to NOT NULL by claiming
// that the result is still nullable, even though
// we know it isn't.
//
// Also, we cannot reduce CAST('abc' AS VARCHAR(4)) to 'abc'.
// If we make 'abc' of type VARCHAR(4), we may later encounter
// the same expression in a Project's digest where it has
// type VARCHAR(3), and that's wrong.
replacement =
simplify.rexBuilder.makeAbstractCast(call.getType(), replacement);
}
return replacement;
}
}
/**
* Helper class used to locate expressions that either can be reduced to
* literals or contain redundant casts.
*/
protected static class ReducibleExprLocator extends RexVisitorImpl<Void> {
/** Whether an expression is constant, and if so, whether it can be
* reduced to a simpler constant. */
enum Constancy {
NON_CONSTANT, REDUCIBLE_CONSTANT, IRREDUCIBLE_CONSTANT
}
private final boolean treatDynamicCallsAsConstant;
private final List<Constancy> stack = new ArrayList<>();
private final ImmutableMap<RexNode, RexNode> constants;
private final List<RexNode> constExprs;
private final List<Boolean> addCasts;
private final Deque<SqlOperator> parentCallTypeStack = new ArrayDeque<>();
ReducibleExprLocator(RelDataTypeFactory typeFactory,
ImmutableMap<RexNode, RexNode> constants, List<RexNode> constExprs,
List<Boolean> addCasts, boolean treatDynamicCallsAsConstant) {
// go deep
super(true);
this.constants = constants;
this.constExprs = constExprs;
this.addCasts = addCasts;
this.treatDynamicCallsAsConstant = treatDynamicCallsAsConstant;
}