Skip to content

Recognize non-fallible division and modulo operator#28898

Merged
losipiuk merged 1 commit into
trinodb:masterfrom
losipiuk:lukaszos/relax-myfail-semantics-for-divis-3f5635
Mar 30, 2026
Merged

Recognize non-fallible division and modulo operator#28898
losipiuk merged 1 commit into
trinodb:masterfrom
losipiuk:lukaszos/relax-myfail-semantics-for-divis-3f5635

Conversation

@losipiuk
Copy link
Copy Markdown
Member

Current logic for division and modulo on integer types assumed it can always fail, which prohibited pushdown of filter expressions which involved those operators.
With new logic we relax the logic so if we see that divisor is constant and non-zero we asume that operator will never fail and pushdown is possible.

Release notes

(x) This is not user-visible or is docs only, and no release notes are required.
( ) Release notes are required. Please propose a release note for me.
( ) Release notes are required, with the following suggested text:

@cla-bot cla-bot Bot added the cla-signed label Mar 27, 2026
@losipiuk losipiuk requested review from findepi, martint and wendigo March 27, 2026 15:28
@losipiuk losipiuk force-pushed the lukaszos/relax-myfail-semantics-for-divis-3f5635 branch 2 times, most recently from 06607fa to bf4533f Compare March 27, 2026 15:33
@losipiuk losipiuk changed the title Relax myFail semantics for division and modulo Relax mayFail semantics for division and modulo Mar 27, 2026
Comment thread core/trino-main/src/main/java/io/trino/sql/ir/IrExpressions.java Outdated
Comment thread core/trino-main/src/main/java/io/trino/sql/ir/IrExpressions.java Outdated
Comment thread core/trino-main/src/main/java/io/trino/sql/ir/IrExpressions.java Outdated
Comment thread core/trino-main/src/main/java/io/trino/sql/ir/IrExpressions.java
Comment thread core/trino-main/src/main/java/io/trino/sql/ir/IrExpressions.java Outdated
Comment thread core/trino-main/src/main/java/io/trino/sql/ir/IrExpressions.java Outdated
Comment thread core/trino-main/src/main/java/io/trino/sql/ir/IrExpressions.java Outdated
@findepi findepi changed the title Relax mayFail semantics for division and modulo Recognize non-fallible division and modulo operator Mar 27, 2026
@findepi
Copy link
Copy Markdown
Member

findepi commented Mar 27, 2026

I changed PR title to what I suggest commit title could be.

@losipiuk losipiuk force-pushed the lukaszos/relax-myfail-semantics-for-divis-3f5635 branch from bf4533f to f1abc10 Compare March 27, 2026 16:41
@losipiuk losipiuk requested review from findepi and pettyjamesm March 27, 2026 16:42
Copy link
Copy Markdown
Member

@pettyjamesm pettyjamesm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM after that last comment is addressed (or intentionally skipped)

Comment thread core/trino-main/src/main/java/io/trino/sql/ir/IrExpressions.java Outdated
Comment on lines +141 to +142
if (value == null) {
yield false; // dividing by null is fine
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this before switch. It's by necessity same for everyone.

Copy link
Copy Markdown
Member

@pettyjamesm pettyjamesm Mar 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like that might be more brittle. Could there be some other type that supports "division or modulus" that would have different NULL semantics? (i.e.: the default branch)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be unexpected. Generally args for arithmetic operators are not nullable (so we get null as result if we get null as any arg)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think the idea here is that we understand what function is in the plan, and therefore can reason about its execution properties such as "may this call fail?" or "will this be null"

@pettyjamesm are you concerned that someone adds/ operator on some other type that returns non-null (or fails) on null input (perhaps abusing / to implement path-like concatenation of varchars)
this is not impossible indeed.
to guard against that, we would need to change the canCauseDivisionByZeroError(Constant) function to exist with "can fail" on any urecognized type (and change the name).
also, we would need to verify in isModulsOrDivide that operand types are the same (otherwise i could add / with heterogenous types and "fool" the rule)...

however, i would say it's an overkill. in thousand other places we don't do such rigorous checks so doing them here would not feel really right to me.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, probably overkill- just pointing it out since we were here.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, keen eye!

Comment thread core/trino-main/src/main/java/io/trino/sql/ir/IrExpressions.java Outdated
Comment thread core/trino-main/src/main/java/io/trino/sql/ir/IrExpressions.java Outdated
@findepi
Copy link
Copy Markdown
Member

findepi commented Mar 27, 2026

(we discovered the old truth: small PRs yield higher comment density 😉 )

@losipiuk
Copy link
Copy Markdown
Member Author

(we discovered the old truth: small PRs yield higher comment density 😉 )

No - this is more about shitty coders.

@losipiuk losipiuk force-pushed the lukaszos/relax-myfail-semantics-for-divis-3f5635 branch from f1abc10 to 6d0b51f Compare March 27, 2026 17:09
@losipiuk
Copy link
Copy Markdown
Member Author

Hopefully good now

@losipiuk losipiuk force-pushed the lukaszos/relax-myfail-semantics-for-divis-3f5635 branch from 6d0b51f to 03b5568 Compare March 27, 2026 17:42
Comment thread core/trino-main/src/main/java/io/trino/sql/ir/IrExpressions.java Outdated
Comment thread core/trino-main/src/main/java/io/trino/sql/ir/IrExpressions.java Outdated
@losipiuk losipiuk force-pushed the lukaszos/relax-myfail-semantics-for-divis-3f5635 branch from 03b5568 to 426f27f Compare March 27, 2026 18:08
Comment thread core/trino-main/src/main/java/io/trino/sql/ir/IrExpressions.java Outdated
@losipiuk losipiuk force-pushed the lukaszos/relax-myfail-semantics-for-divis-3f5635 branch from 426f27f to 0b2a42f Compare March 27, 2026 18:19
Comment thread core/trino-main/src/main/java/io/trino/sql/ir/IrExpressions.java Outdated
@losipiuk losipiuk force-pushed the lukaszos/relax-myfail-semantics-for-divis-3f5635 branch from 0b2a42f to d10f879 Compare March 27, 2026 18:21
@losipiuk losipiuk force-pushed the lukaszos/relax-myfail-semantics-for-divis-3f5635 branch from d10f879 to 708a19d Compare March 27, 2026 19:12
@github-actions github-actions Bot added the hive Hive connector label Mar 27, 2026
@findepi
Copy link
Copy Markdown
Member

findepi commented Mar 27, 2026

Comment thread core/trino-main/src/main/java/io/trino/sql/ir/IrExpressions.java Outdated
Current logic for division and modulo on integer types assumed it can
always fail, which prohibited pushdown of filter expressions which
involved those operators.
With new logic we relax the logic so if we see that divisor is constant
and non-zero we asume that operator will never fail and pushdown is
possible.
@losipiuk losipiuk force-pushed the lukaszos/relax-myfail-semantics-for-divis-3f5635 branch from 708a19d to 1e0bb5d Compare March 30, 2026 14:14
@losipiuk losipiuk merged commit f754311 into trinodb:master Mar 30, 2026
6 of 68 checks passed
@github-actions github-actions Bot added this to the 481 milestone Mar 30, 2026
@ebyhr ebyhr mentioned this pull request Mar 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

4 participants