Skip to content

[CALCITE-7728] Linq4j can simplify expressions without regards for 'safety' - #5192

Merged
mihaibudiu merged 1 commit into
apache:mainfrom
mihaibudiu:issue7728
Aug 19, 2026
Merged

[CALCITE-7728] Linq4j can simplify expressions without regards for 'safety'#5192
mihaibudiu merged 1 commit into
apache:mainfrom
mihaibudiu:issue7728

Conversation

@mihaibudiu

Copy link
Copy Markdown
Contributor

Jira Link

CALCITE-7728

Changes Proposed

This PR adds a cheap visitor to approximate expression "safety", similar to the other layers of the representation. The visitor is used to avoid optimizing expressions that may throw.

@mihaibudiu mihaibudiu changed the title [CALCITE-7728] linq4j can simplify expressions without regards for 'safety' [CALCITE-7728] Linq4j can simplify expressions without regards for 'safety' Aug 19, 2026
*
* <p>The analysis is deliberately cheap and conservative. Any method call or
* constructor may throw; so may division, remainder and checked arithmetic.
* Everything else - reading a variable, a field or an array element, and

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

accessing an array element could lead to IndexOutOfBoundException, should we be "hiding" those?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

After a complete audit of all operations that can throw, this visitor will become more complicated, but hopefully safe. I will submit a new commit.

@mihaibudiu

Copy link
Copy Markdown
Contributor Author

@rubenada you were right, the analysis wasn't conservative enough. I added a second commit which covers all the Expression kinds supported today (and the future ones are treated conservatively).

assertMayThrow(Expressions.convert_(i, Integer.class), false);
assertMayThrow(Expressions.convert_(str, Object.class), false);

// A cast that may raise ClassCastException, and unboxing

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: this comment seems incomplete? ... , and unboxing
Do you mean ... , and unboxing may throw NPE ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I will fix this and squash the commits at the same time.

@rubenada rubenada left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM.

Thanks for the thorough tests that you added!

…afety'

Signed-off-by: Mihai Budiu <mbudiu@feldera.com>
@mihaibudiu mihaibudiu added the LGTM-will-merge-soon Overall PR looks OK. Only minor things left. label Aug 19, 2026
@sonarqubecloud

Copy link
Copy Markdown

@mihaibudiu
mihaibudiu merged commit 9be85f9 into apache:main Aug 19, 2026
20 of 35 checks passed
@mihaibudiu
mihaibudiu deleted the issue7728 branch August 19, 2026 20:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

LGTM-will-merge-soon Overall PR looks OK. Only minor things left.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants