Conversation
|
How come there are no plan changes? Is this analysis used in Calcite at all? |
@mihaibudiu RexUtil.pullFactors is currently only used in tests, not in the main optimizer. So the fix corrects the utility method itself but does not change any production query plans. But its value is more about correctness and future use. It could be adopted by RexSimplify or a rule. The same normalization idea could be applied where it actually matters, e.g., inside RexSimplify.simplifyAnds or a predicate-pulling rule, which would then produce plan changes. I will investigate how to make improvements in RexSimplify with minimal cost. |
da3e6dc to
004469e
Compare
004469e to
04cd2a6
Compare
|
Apologies @mihaibudiu , I accidentally squashed the previous commit history with the current changes. Here is an overview of the current modifications: RexSimplify.absorb now recognizes symmetric comparison forms during matching. |
mihaibudiu
left a comment
There was a problem hiding this comment.
If this code is never executed, it means it has very low coverage.
| } | ||
|
|
||
| /** | ||
| * Normalizes a comparison expression so that, when possible, an input ref |
There was a problem hiding this comment.
this only seems to work for comparisons of columns; this should be in the javadoc
There was a problem hiding this comment.
Done, the Javadoc of normalizeComparison now states that only comparisons involving an input ref are normalized, and that other operand kinds (e.g. two CASTs) are returned unchanged, matching the instanceof RexInputRef logic in the implementation.
IMO the code should been executed and covered, in both senses:
|
|



jira: https://issues.apache.org/jira/browse/CALCITE-739
Base on the jira, I added normalizeComparison(RexNode), which canonicalizes binary comparisons (=,!=,<,>,<=,>=) so that:
RexInputRefs, the one with the smaller index appears on the left.In addition applied normalizeComparison in pull, commonFactors, and removeFactor so that equivalent comparisons can be matched as common factors.
Note: I have not implemented the second rule mentioned in the Jira ticket in this PR. The reasons are the risk of exponential expansion resulting from CNF expansion and the overhead of additional implication checks on high-frequency call paths. Although this would handle more complex absorption scenarios, patterns like
(a OR b) AND ((x AND a) OR (y AND b))appear relatively infrequently in actual SQL optimization, whereas the proposed CNF expansion and implication checks would impact all code paths that callpullFactors.