xiedeyantu commented on code in PR #4870:
URL: https://github.com/apache/calcite/pull/4870#discussion_r3045031948
##########
elasticsearch/src/main/java/org/apache/calcite/adapter/elasticsearch/PredicateAnalyzer.java:
##########
@@ -406,6 +414,8 @@ private QueryExpression binary(RexCall call) {
case SEARCH:
if (isSearchWithComplementedPoints(call)) {
return QueryExpression.create(pair.getKey()).notIn(pair.getValue());
+ } else if (isSearchWithRange(call)) {
+ return QueryExpression.create(pair.getKey()).range(pair.getValue());
Review Comment:
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`?
##########
core/src/main/java/org/apache/calcite/util/Bug.java:
##########
@@ -169,7 +169,7 @@ public abstract class Bug {
* <a
href="https://issues.apache.org/jira/browse/CALCITE-4645">[CALCITE-4645]
* 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;
Review Comment:
I think we can remove it safty.
##########
elasticsearch/src/test/java/org/apache/calcite/adapter/elasticsearch/AggregationAndSortTest.java:
##########
@@ -115,21 +115,16 @@ private static Connection createConnection() throws
SQLException {
return connection;
}
- /**
- * Currently the patterns like below will be converted to Search in range
- * which is not supported in elastic search adapter.
- * (val1 >= 10 and val1 <= 20)
- * (val1 <= 10 or val1 >=20)
- * (val1 <= 10) or (val1 > 15 and val1 <= 20)
- * So disable this test case until the translation from Search in range
- * to rang Query in ES is implemented.
+ /** Test case for
+ * <a
href="https://issues.apache.org/jira/browse/CALCITE-4645">[CALCITE-4645]
+ * In Elasticsearch adapter, a range predicate should be translated to a
range query</a>.
*/
@Test void searchInRange() {
Assumptions.assumeTrue(Bug.CALCITE_4645_FIXED, "CALCITE-4645");
CalciteAssert.that()
.with(AggregationAndSortTest::createConnection)
.query("select count(*) from view where val1 >= 10 and val1 <=20")
- .returns("EXPR$0=1\n");
+ .returns("EXPR$0=0\n");
Review Comment:
This actually is a wrong case.
##########
elasticsearch/src/main/java/org/apache/calcite/adapter/elasticsearch/PredicateAnalyzer.java:
##########
@@ -899,6 +915,59 @@ private SimpleQueryExpression(NamedFieldExpression rel) {
builder = boolQuery().mustNot(termsQuery(getFieldReference(), iterable));
return this;
}
+
+ @Override public QueryExpression range(LiteralExpression literal) {
+ final Sarg<?> sarg =
requireNonNull(literal.literal.getValueAs(Sarg.class), "Sarg");
+ final Set<? extends Range<?>> ranges = sarg.rangeSet.asRanges();
+
+ if (ranges.isEmpty()) {
+ throw new PredicateAnalyzerException("Range query expects at least one
range");
+ }
+
+ if (ranges.size() == 1) {
+ // Single range, create simple range query
+ final Range<?> range = ranges.iterator().next();
+ builder = createRangeQuery(range, literal);
+ } else {
+ // Multiple ranges, create bool query with should clauses (OR)
+ final BoolQueryBuilder boolQuery = boolQuery();
+ for (Range<?> range : ranges) {
+ boolQuery.should(createRangeQuery(range, literal));
+ }
+ builder = boolQuery;
+ }
+ return this;
+ }
+
+ private RangeQueryBuilder createRangeQuery(Range<?> range,
LiteralExpression literal) {
Review Comment:
Can all data types support rewrite to "range query"? Perhaps we should add
some cases for different data types
--
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]