Skip to content

[CALCITE-7747] RexSimplify.simplifySearch does not simplify SEARCH(li… - #5226

Open
sbroeder wants to merge 2 commits into
apache:mainfrom
sbroeder:7747
Open

[CALCITE-7747] RexSimplify.simplifySearch does not simplify SEARCH(li…#5226
sbroeder wants to merge 2 commits into
apache:mainfrom
sbroeder:7747

Conversation

@sbroeder

Copy link
Copy Markdown
Contributor

…teral, Sarg) when pointCount >= 2

RexSimplify.simplifySearch only evaluated a SEARCH(subject, Sarg) call against its Sarg when sarg.isPoints() && sarg.pointCount <= 1, with no check for whether the subject itself is a constant. A single-point Sarg with any subject folded correctly, and a multi-point Sarg with a column-reference subject simplified correctly elsewhere, but a literal subject compared against a multi-point Sarg (for example, SEARCH(5, Sarg[1, 2])) fell through every branch unevaluated instead of folding to a boolean constant.

This can occur whenever a rule substitutes a literal for a column reference in a SEARCH condition, such as when
FilterSetOpTransposeRule pushes a filter through a branch of a Union that projects a constant.

Jira Link

CALCITE-7747

Changes Proposed

…teral, Sarg) when pointCount >= 2

RexSimplify.simplifySearch only evaluated a SEARCH(subject, Sarg) call
against its Sarg when sarg.isPoints() && sarg.pointCount <= 1, with no
check for whether the subject itself is a constant. A single-point
Sarg with any subject folded correctly, and a multi-point Sarg with a
column-reference subject simplified correctly elsewhere, but a literal
subject compared against a multi-point Sarg (for example,
SEARCH(5, Sarg[1, 2])) fell through every branch unevaluated instead
of folding to a boolean constant.

This can occur whenever a rule substitutes a literal for a column
reference in a SEARCH condition, such as when
FilterSetOpTransposeRule pushes a filter through a branch of a Union
that projects a constant.

Fix simplifySearch so that when the search subject is itself a
RexLiteral, the call is expanded via the existing RexUtil.expandSearch
and the result is simplified recursively, regardless of point count.
This matches the treatment already given to a single-point Sarg, but
adds the recursive simplify() call needed to fold the expanded
comparison tree down to a constant.
// Expand "SEARCH(x, Sarg([point])" to "x = point"
// and "SEARCH(x, Sarg([])" to "false"
return RexUtil.expandSearch(rexBuilder, null, call);
} else if (searchOperand instanceof RexLiteral || (sarg.isPoints() && sarg.pointCount <= 1)) {

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.

wouldn't this be clearer as two separate ifs?

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.

Yes, agreed. That is better.

@sonarqubecloud

Copy link
Copy Markdown

@mihaibudiu mihaibudiu added the LGTM-will-merge-soon Overall PR looks OK. Only minor things left. label Aug 27, 2026
@mihaibudiu

Copy link
Copy Markdown
Contributor

I think you can squash. But I will wait a bit to merge to see if there are other comments.

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