xuzifu666 commented on code in PR #4870:
URL: https://github.com/apache/calcite/pull/4870#discussion_r3049054984
##########
elasticsearch/src/main/java/org/apache/calcite/adapter/elasticsearch/PredicateAnalyzer.java:
##########
@@ -215,7 +216,8 @@ private static boolean supportedRexCall(RexCall call) {
* @return true if it isSearchWithPoints or
isSearchWithComplementedPoints, other false
*/
static boolean canBeTranslatedToTermsQuery(RexCall search) {
Review Comment:
The method name here is not relevant to this modification; it may be
modified separately in a more suitable Jira implementation in the future.
##########
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 a 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) {
+ final RangeQueryBuilder rangeQuery = rangeQuery(getFieldReference());
+
+ // Handle lower bound
+ if (range.hasLowerBound()) {
+ final Object lowerValue =
+ literal.sargPointValue(range.lowerEndpoint(),
+ literal.literal.getType().getSqlTypeName());
+ if (range.lowerBoundType() == BoundType.CLOSED) {
+ rangeQuery.gte(lowerValue);
+ } else {
+ rangeQuery.gt(lowerValue);
+ }
+ }
+
+ // Handle upper bound
+ if (range.hasUpperBound()) {
+ final Object upperValue =
+ literal.sargPointValue(range.upperEndpoint(),
+ literal.literal.getType().getSqlTypeName());
+ if (range.upperBoundType() == BoundType.CLOSED) {
+ rangeQuery.lte(upperValue);
+ } else {
+ rangeQuery.lt(upperValue);
+ }
+ }
+
+ return addFormatIfNecessary(literal, rangeQuery);
Review Comment:
OK, modified as your suggestion.
##########
elasticsearch/src/main/java/org/apache/calcite/adapter/elasticsearch/PredicateAnalyzer.java:
##########
@@ -215,7 +216,8 @@ private static boolean supportedRexCall(RexCall call) {
* @return true if it isSearchWithPoints or
isSearchWithComplementedPoints, other false
*/
static boolean canBeTranslatedToTermsQuery(RexCall search) {
Review Comment:
Same as above.
--
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]