Skip to content

Commit

Permalink
push exact field name extraction into the equality expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
not-napoleon committed Feb 13, 2024
1 parent a17f9fe commit e8224d5
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
package org.elasticsearch.xpack.ql.expression.predicate.operator.comparison;

import org.elasticsearch.xpack.ql.expression.Expression;
import org.elasticsearch.xpack.ql.expression.TypedAttribute;
import org.elasticsearch.xpack.ql.expression.predicate.Negatable;
import org.elasticsearch.xpack.ql.expression.predicate.operator.comparison.BinaryComparisonProcessor.BinaryComparisonOperation;
import org.elasticsearch.xpack.ql.planner.ExpressionTranslator;
import org.elasticsearch.xpack.ql.querydsl.query.Query;
import org.elasticsearch.xpack.ql.querydsl.query.RangeQuery;
import org.elasticsearch.xpack.ql.querydsl.query.TermQuery;
Expand Down Expand Up @@ -59,6 +61,9 @@ protected boolean isCommutative() {

@Override
public Query getQuery(String name, Object value, String format, boolean isDateLiteralComparison, ZoneId zoneId) {
if (left() instanceof TypedAttribute typedLeft) {
name = ExpressionTranslator.pushableAttributeName(typedLeft);
}
if (isDateLiteralComparison) {
// dates equality uses a range query because it's the one that has a "format" parameter
return new RangeQuery(source(), name, value, true, value, true, format, zoneId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
package org.elasticsearch.xpack.ql.expression.predicate.operator.comparison;

import org.elasticsearch.xpack.ql.expression.Expression;
import org.elasticsearch.xpack.ql.expression.TypedAttribute;
import org.elasticsearch.xpack.ql.expression.predicate.Negatable;
import org.elasticsearch.xpack.ql.expression.predicate.operator.comparison.BinaryComparisonProcessor.BinaryComparisonOperation;
import org.elasticsearch.xpack.ql.planner.ExpressionTranslator;
import org.elasticsearch.xpack.ql.querydsl.query.NotQuery;
import org.elasticsearch.xpack.ql.querydsl.query.Query;
import org.elasticsearch.xpack.ql.querydsl.query.RangeQuery;
Expand Down Expand Up @@ -56,7 +58,9 @@ protected boolean isCommutative() {

@Override
public Query getQuery(String name, Object value, String format, boolean isDateLiteralComparison, ZoneId zoneId) {
Query query;
if (left() instanceof TypedAttribute typedLeft) {
name = ExpressionTranslator.pushableAttributeName(typedLeft);
}
if (isDateLiteralComparison) {
// dates equality uses a range query because it's the one that has a "format" parameter
return new NotQuery(source(), new RangeQuery(source(), name, value, true, value, true, format, zoneId));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

import org.elasticsearch.xpack.ql.expression.Expression;
import org.elasticsearch.xpack.ql.expression.Nullability;
import org.elasticsearch.xpack.ql.expression.TypedAttribute;
import org.elasticsearch.xpack.ql.expression.predicate.operator.comparison.BinaryComparisonProcessor.BinaryComparisonOperation;
import org.elasticsearch.xpack.ql.planner.ExpressionTranslator;
import org.elasticsearch.xpack.ql.querydsl.query.Query;
import org.elasticsearch.xpack.ql.querydsl.query.RangeQuery;
import org.elasticsearch.xpack.ql.querydsl.query.TermQuery;
Expand Down Expand Up @@ -58,6 +60,9 @@ protected boolean isCommutative() {

@Override
public Query getQuery(String name, Object value, String format, boolean isDateLiteralComparison, ZoneId zoneId) {
if (left() instanceof TypedAttribute typedLeft) {
name = ExpressionTranslator.pushableAttributeName(typedLeft);
}
if (isDateLiteralComparison) {
// dates equality uses a range query because it's the one that has a "format" parameter
return new RangeQuery(source(), name, value, true, value, true, format, zoneId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
import org.elasticsearch.xpack.ql.expression.predicate.operator.comparison.BinaryComparison;
import org.elasticsearch.xpack.ql.expression.predicate.operator.comparison.Equals;
import org.elasticsearch.xpack.ql.expression.predicate.operator.comparison.In;
import org.elasticsearch.xpack.ql.expression.predicate.operator.comparison.NotEquals;
import org.elasticsearch.xpack.ql.expression.predicate.operator.comparison.NullEquals;
import org.elasticsearch.xpack.ql.expression.predicate.regex.Like;
import org.elasticsearch.xpack.ql.expression.predicate.regex.RLike;
import org.elasticsearch.xpack.ql.expression.predicate.regex.RegexMatch;
Expand Down Expand Up @@ -280,7 +278,6 @@ public static Query doTranslate(BinaryComparison bc, TranslatorHandler handler)

static Query translate(BinaryComparison bc, TranslatorHandler handler) {
TypedAttribute attribute = checkIsPushableAttribute(bc.left());
Source source = bc.source();
String name = handler.nameOf(attribute);
Object value = valueOf(bc.right());
String format = null;
Expand Down Expand Up @@ -320,14 +317,8 @@ static Query translate(BinaryComparison bc, TranslatorHandler handler) {
if (DataTypes.isDateTime(attribute.dataType())) {
zoneId = bc.zoneId();
}
if (bc instanceof Equals || bc instanceof NullEquals || bc instanceof NotEquals) {
name = pushableAttributeName(attribute);
}

return bc.getQuery(name, value, format, isDateLiteralComparison, zoneId);

// throw new QlIllegalArgumentException("Don't know how to translate binary comparison [{}] in [{}]", bc.right().nodeString(),
// bc);
}
}

Expand Down

0 comments on commit e8224d5

Please sign in to comment.