Skip to content

[CALCITE-4645] In Elasticsearch adapter, a range predicate should be translated to a range query#4870

Merged
xuzifu666 merged 1 commit intoapache:mainfrom
xuzifu666:es_ragne_fix
Apr 9, 2026
Merged

[CALCITE-4645] In Elasticsearch adapter, a range predicate should be translated to a range query#4870
xuzifu666 merged 1 commit intoapache:mainfrom
xuzifu666:es_ragne_fix

Conversation

@xuzifu666
Copy link
Copy Markdown
Member

if (isSearchWithComplementedPoints(call)) {
return QueryExpression.create(pair.getKey()).notIn(pair.getValue());
} else if (isSearchWithRange(call)) {
return QueryExpression.create(pair.getKey()).range(pair.getValue());
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.

We already have isSearchWithComplementedPoints to check if it is not in and isSearchWithPoints to check if it is in. Should we can leave range to else?

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.

Make sense,done.

* In Elasticsearch adapter, a range predicate should be translated to a range query</a> is
* fixed. */
public static final boolean CALCITE_4645_FIXED = false;
public static final boolean CALCITE_4645_FIXED = true;
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 we can remove it safty.

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.

Done.

.with(AggregationAndSortTest::createConnection)
.query("select count(*) from view where val1 >= 10 and val1 <=20")
.returns("EXPR$0=1\n");
.returns("EXPR$0=0\n");
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.

This actually is a wrong case.

Copy link
Copy Markdown
Member Author

@xuzifu666 xuzifu666 Apr 7, 2026

Choose a reason for hiding this comment

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

test dataset is :

    String doc1 = "{cat1:'a', cat2:'g', val1:1, cat4:'2018-01-01', cat5:1}";
    String doc2 = "{cat2:'g', cat3:'y', val2:5, cat4:'2019-12-12', cat6:'text1'}";
    String doc3 = "{cat1:'b', cat2:'h', cat3:'z', cat5:2, val1:7, val2:42}";

val1 >= 10 and val1 <=20 , doc1/2/3 can not fit the condition.
So it is right in my view.

return this;
}

private RangeQueryBuilder createRangeQuery(Range<?> range, LiteralExpression literal) {
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.

Can all data types support rewrite to "range query"? Perhaps we should add some cases for different data types

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.

Yes, I added some new test cases, and the results met expectations.

@caicancai
Copy link
Copy Markdown
Member

To be honest, Calcite's Elasticsearch adapter code is riddled with issues. I previously maintained an internal production-ready version of it, which amounted to practically a complete refactor.

@xiedeyantu
Copy link
Copy Markdown
Member

To be honest, Calcite's Elasticsearch adapter code is riddled with issues. I previously maintained an internal production-ready version of it, which amounted to practically a complete refactor.

You may very well be right, but even a small improvement is better than no improvement at all. Perhaps we could also discuss refactoring this section; it seems the community encourages doing so.

Copy link
Copy Markdown
Member

@xiedeyantu xiedeyantu left a comment

Choose a reason for hiding this comment

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

LGTM

@caicancai
Copy link
Copy Markdown
Member

I will review this over the next couple of days.

@caicancai caicancai self-assigned this Apr 7, 2026
}
}

return addFormatIfNecessary(literal, rangeQuery);
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.

In createRangeQuery, directly check if the range endpoint value is GregorianCalendar to determine whether to add a format, instead of relying on literal.value().

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.

For SEARCH/Sarg-type literals, literal.value() follows the isSarg() → sargValue() branch, returning a List<Object>; it will never return a GregorianCalendar. This implies that range queries on date fields will not set the format("date_time") attribute.

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.

OK, modified as your suggestion.

@@ -215,7 +216,8 @@ private static boolean supportedRexCall(RexCall call) {
* @return true if it isSearchWithPoints or isSearchWithComplementedPoints, other false
*/
static boolean canBeTranslatedToTermsQuery(RexCall search) {
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.

The method name may no longer be accurate.

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.

The method name here is not relevant to this modification; it may be modified separately in a more suitable Jira implementation in the future.

@@ -215,7 +216,8 @@ private static boolean supportedRexCall(RexCall call) {
* @return true if it isSearchWithPoints or isSearchWithComplementedPoints, other false
*/
static boolean canBeTranslatedToTermsQuery(RexCall search) {
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.

It looks like JavaDoc is a bit outdated.

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.

Same as above.

@xuzifu666 xuzifu666 added the LGTM-will-merge-soon Overall PR looks OK. Only minor things left. label Apr 8, 2026
@xuzifu666
Copy link
Copy Markdown
Member Author

If no other comments, I will merge this pr in 24H.

@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud bot commented Apr 8, 2026

@xuzifu666 xuzifu666 merged commit 99c8d59 into apache:main Apr 9, 2026
20 of 35 checks passed
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.

3 participants