Skip to content

Commit

Permalink
SQL: Use underlying exact field for LIKE/RLIKE (#39443)
Browse files Browse the repository at this point in the history
Previously, if a text field had an underlying keyword field
the latter was not used instead of the text leading to wrong
results returned by queries filtering with LIKE/RLIKE.

Fixes: #39442
  • Loading branch information
matriv authored Feb 27, 2019
1 parent 69fe79a commit 2a5d0a3
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
2 changes: 2 additions & 0 deletions x-pack/plugin/sql/qa/src/main/resources/docs.csv-spec
Original file line number Diff line number Diff line change
Expand Up @@ -2353,6 +2353,7 @@ SELECT * FROM (SELECT first_name, last_name FROM emp WHERE last_name NOT LIKE '%
first_name | last_name
---------------+---------------
Anneke |Preusig
Alejandro |McAlpine
Anoosh |Peyn
Arumugam |Ossenbruggen
// end::limitationSubSelect
Expand All @@ -2365,6 +2366,7 @@ SELECT first_name, last_name FROM emp WHERE last_name NOT LIKE '%a%' AND first_n
first_name | last_name
---------------+---------------
Anneke |Preusig
Alejandro |McAlpine
Anoosh |Peyn
Arumugam |Ossenbruggen
;
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ protected QueryTranslation asQuery(RegexMatch e, boolean onAggs) {
if (e.field() instanceof FieldAttribute) {
FieldAttribute fa = (FieldAttribute) e.field();
inexact = fa.isInexact();
target = nameOf(inexact ? fa : fa.exactAttribute());
target = nameOf(inexact ? fa.exactAttribute() : fa);
} else {
throw new SqlIllegalArgumentException("Scalar function ({}) not allowed (yet) as arguments for LIKE",
Expressions.name(e.field()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.elasticsearch.xpack.sql.querydsl.query.ExistsQuery;
import org.elasticsearch.xpack.sql.querydsl.query.NotQuery;
import org.elasticsearch.xpack.sql.querydsl.query.Query;
import org.elasticsearch.xpack.sql.querydsl.query.QueryStringQuery;
import org.elasticsearch.xpack.sql.querydsl.query.RangeQuery;
import org.elasticsearch.xpack.sql.querydsl.query.ScriptQuery;
import org.elasticsearch.xpack.sql.querydsl.query.TermQuery;
Expand Down Expand Up @@ -184,6 +185,19 @@ public void testDateRangeCast() {
assertEquals("date", rq.field());
assertEquals(DateUtils.asDateTime("1969-05-13T12:34:56Z"), rq.lower());
}

public void testLikeOnInexact() {
LogicalPlan p = plan("SELECT * FROM test WHERE some.string LIKE '%a%'");
assertTrue(p instanceof Project);
p = ((Project) p).child();
assertTrue(p instanceof Filter);
Expression condition = ((Filter) p).condition();
QueryTranslation qt = QueryTranslator.toQuery(condition, false);
assertEquals(QueryStringQuery.class, qt.query.getClass());
QueryStringQuery qsq = ((QueryStringQuery) qt.query);
assertEquals(1, qsq.fields().size());
assertEquals("some.string.typical", qsq.fields().keySet().iterator().next());
}

public void testLikeConstructsNotSupported() {
LogicalPlan p = plan("SELECT LTRIM(keyword) lt FROM test WHERE LTRIM(keyword) LIKE '%a%'");
Expand Down

0 comments on commit 2a5d0a3

Please sign in to comment.