924060929 commented on code in PR #70003:
URL: https://github.com/apache/doris/pull/70003#discussion_r4581602982
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/SimplifyArithmeticComparisonRule.java:
##########
@@ -132,6 +155,18 @@ private static List<Expression>
tryRearrangeChildren(Expression left, Expression
Expression newChild =
oppositeOperator.getConstructor(Expression.class, Expression.class)
.newInstance(right, leftLiteral);
+ if (newChild instanceof BoundFunction) {
+ // Date arithmetic has a finite range. For example, days_sub(d, 1)
< MAX must not
+ // become d < days_add(MAX, 1). Check the inverse on FE before
accepting the rewrite.
+ // NULL and expressions that cannot fold are not valid date
boundaries. This also
+ // excludes a boundary supplied by a column, which can contain MAX
at execution time.
+ newChild = FoldConstantRuleOnFE.evaluate(
+ TypeCoercionUtils.processBoundFunction((BoundFunction)
newChild), context);
+ if (!(newChild instanceof DateLiteral)) {
Review Comment:
[P2] Preserve errors from the original date operation as well
A valid inverse boundary does not prove that removing the original operation
is safe. In a `+08:00` session, consider this reduced plan:
```text
Filter(days_sub(ts, 1) > TIMESTAMPTZ '9999-12-30 12:00:00+00:00')
Scan(ts = TIMESTAMPTZ '9999-12-31 23:00:00+00:00')
```
The stored UTC timestamp is valid, but its session-local year is 10000. This
PR's new source-local-year check makes the original `days_sub(ts, 1)` throw an
out-of-range error. The inverse `days_add(cutoff, 1)` succeeds, however, so
this guard accepts `ts > TIMESTAMPTZ '9999-12-31 12:00:00+00:00'`, which
evaluates to TRUE and eliminates the error. Before this PR, the original
subtraction used UTC arithmetic and succeeded.
I reproduced the original error, the accepted rewrite, and its TRUE result
using the compiled FE classes at `c158d92470f`; this was an expression-level
probe, not an end-to-end SQL run. Please preserve the original comparison
unless its actual input domain is proven valid as well, and add coverage for a
valid inverse combined with an invalid original operation. This is distinct
from the earlier invalid-inverse report: here the inverse is valid, and the
original error is lost.
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/DateAddSubMonotonic.java:
##########
@@ -17,12 +17,23 @@
package org.apache.doris.nereids.trees.expressions.functions;
+import org.apache.doris.nereids.trees.expressions.functions.scalar.DaysAdd;
+import org.apache.doris.nereids.trees.expressions.functions.scalar.DaysSub;
import org.apache.doris.nereids.trees.expressions.literal.Literal;
+import org.apache.doris.nereids.types.TimeStampTzType;
+import org.apache.doris.nereids.util.DateUtils;
/** monotonicity for XX_ADD XX_SUB */
public interface DateAddSubMonotonic extends Monotonic {
@Override
default boolean isMonotonic(Literal lower, Literal upper) {
+ // At a fall-back, 01:50-04 precedes 01:10-05, but adding one local day
+ // reverses their UTC order. Fixed-offset sessions retain the original
guarantee.
+ if ((this instanceof DaysAdd || this instanceof DaysSub)
Review Comment:
Calendar DAY arithmetic now depends on the bound input type and session
timezone. Could we keep ownership of those semantics in `DaysAdd`/`DaysSub`,
instead of making the shared monotonicity interface depend on its concrete
implementations?
I suggest overriding `isMonotonic` in those functions, sharing the
calendar-DAY check where appropriate, and keeping `DateAddSubMonotonic` as the
common default. Partition-range evaluation and runtime-filter classification
should continue to consume that monotonicity property.
Comparison rearrangement needs a separate contract: being monotonic does not
establish an exact inverse, and having an inverse does not establish that
removing the original evaluation preserves range errors. The existing whitelist
excludes month/year arithmetic, but the additional `isMonotonic` check is still
only one prerequisite. A separate opt-in function capability should describe
whether the inverse comparison is exact and the input-domain requirements.
`SimplifyArithmeticComparisonRule` should consume that contract and validate
the concrete boundary, rather than treating monotonicity as the rearrangement
contract.
This can be done without splitting expression classes. For this fix,
conservatively declining TIMESTAMPTZ DAY rearrangement at the function level is
reasonable until its domain/error-preservation conditions can be proven. Actual
date-range validation should remain in the FE/BE arithmetic implementations;
the optimizer should reuse FE evaluation to validate a candidate boundary.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]