Skip to content

Commit

Permalink
SQL: Fix edge case: `<field> IN(null)
Browse files Browse the repository at this point in the history
Handle the case when `null` is the only value in the list so that
it's translated to a `MatchNoDocsQuery`.

Followup to: #34750
  • Loading branch information
matriv committed Oct 24, 2018
1 parent 795d57b commit d931ed9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
Expand Up @@ -11,23 +11,28 @@
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.type.DataType;

import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;

import static org.elasticsearch.index.query.QueryBuilders.termsQuery;

public class TermsQuery extends LeafQuery {

private final String term;
private final LinkedHashSet<Object> values;
private final Set<Object> values;

public TermsQuery(Location location, String term, List<Expression> values) {
super(location);
this.term = term;
values.removeIf(e -> e.dataType() == DataType.NULL);
this.values = new LinkedHashSet<>(Foldables.valuesOf(values, values.get(0).dataType()));
this.values.removeIf(Objects::isNull);
if (values.isEmpty()) {
this.values = Collections.emptySet();
} else {
this.values = new LinkedHashSet<>(Foldables.valuesOf(values, values.get(0).dataType()));
}
}

@Override
Expand Down
Expand Up @@ -173,7 +173,7 @@ public void testTranslateInExpression_WhereClause() throws IOException {
assertEquals("keyword:(bar foo lala)", tq.asBuilder().toQuery(createShardContext()).toString());
}

public void testTranslateInExpression_WhereClauseAndNullHAndling() throws IOException {
public void testTranslateInExpression_WhereClauseAndNullHandling() throws IOException {
LogicalPlan p = plan("SELECT * FROM test WHERE keyword IN ('foo', null, 'lala', null, 'foo', concat('la', 'la'))");
assertTrue(p instanceof Project);
assertTrue(p.children().get(0) instanceof Filter);
Expand All @@ -186,6 +186,20 @@ public void testTranslateInExpression_WhereClauseAndNullHAndling() throws IOExce
assertEquals("keyword:(foo lala)", tq.asBuilder().toQuery(createShardContext()).toString());
}

public void testTranslateInExpression_WhereClauseAndNullHandlingEmptyValuesList() throws IOException {
LogicalPlan p = plan("SELECT * FROM test WHERE keyword IN (null)");
assertTrue(p instanceof Project);
assertTrue(p.children().get(0) instanceof Filter);
Expression condition = ((Filter) p.children().get(0)).condition();
assertFalse(condition.foldable());
QueryTranslation translation = QueryTranslator.toQuery(condition, false);
Query query = translation.query;
assertTrue(query instanceof TermsQuery);
TermsQuery tq = (TermsQuery) query;
assertEquals("MatchNoDocsQuery(\"No terms supplied for \"terms\" query.\")",
tq.asBuilder().toQuery(createShardContext()).toString());
}

public void testTranslateInExpressionInvalidValues_WhereClause() {
LogicalPlan p = plan("SELECT * FROM test WHERE keyword IN ('foo', 'bar', keyword)");
assertTrue(p instanceof Project);
Expand Down

0 comments on commit d931ed9

Please sign in to comment.