Skip to content

Commit a2c07b5

Browse files
committed
SQL: Use underlying exact field for LIKE/RLIKE (#39443)
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
1 parent 55e98f0 commit a2c07b5

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

x-pack/plugin/sql/qa/src/main/resources/docs.csv-spec

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2353,6 +2353,7 @@ SELECT * FROM (SELECT first_name, last_name FROM emp WHERE last_name NOT LIKE '%
23532353
first_name | last_name
23542354
---------------+---------------
23552355
Anneke |Preusig
2356+
Alejandro |McAlpine
23562357
Anoosh |Peyn
23572358
Arumugam |Ossenbruggen
23582359
// end::limitationSubSelect
@@ -2365,6 +2366,7 @@ SELECT first_name, last_name FROM emp WHERE last_name NOT LIKE '%a%' AND first_n
23652366
first_name | last_name
23662367
---------------+---------------
23672368
Anneke |Preusig
2369+
Alejandro |McAlpine
23682370
Anoosh |Peyn
23692371
Arumugam |Ossenbruggen
23702372
;

x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/planner/QueryTranslator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ protected QueryTranslation asQuery(RegexMatch e, boolean onAggs) {
483483
if (e.field() instanceof FieldAttribute) {
484484
FieldAttribute fa = (FieldAttribute) e.field();
485485
inexact = fa.isInexact();
486-
target = nameOf(inexact ? fa : fa.exactAttribute());
486+
target = nameOf(inexact ? fa.exactAttribute() : fa);
487487
} else {
488488
throw new SqlIllegalArgumentException("Scalar function ({}) not allowed (yet) as arguments for LIKE",
489489
Expressions.name(e.field()));

x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/planner/QueryTranslatorTests.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.elasticsearch.xpack.sql.querydsl.query.ExistsQuery;
4040
import org.elasticsearch.xpack.sql.querydsl.query.NotQuery;
4141
import org.elasticsearch.xpack.sql.querydsl.query.Query;
42+
import org.elasticsearch.xpack.sql.querydsl.query.QueryStringQuery;
4243
import org.elasticsearch.xpack.sql.querydsl.query.RangeQuery;
4344
import org.elasticsearch.xpack.sql.querydsl.query.ScriptQuery;
4445
import org.elasticsearch.xpack.sql.querydsl.query.TermQuery;
@@ -184,6 +185,19 @@ public void testDateRangeCast() {
184185
assertEquals("date", rq.field());
185186
assertEquals(DateUtils.asDateTime("1969-05-13T12:34:56Z"), rq.lower());
186187
}
188+
189+
public void testLikeOnInexact() {
190+
LogicalPlan p = plan("SELECT * FROM test WHERE some.string LIKE '%a%'");
191+
assertTrue(p instanceof Project);
192+
p = ((Project) p).child();
193+
assertTrue(p instanceof Filter);
194+
Expression condition = ((Filter) p).condition();
195+
QueryTranslation qt = QueryTranslator.toQuery(condition, false);
196+
assertEquals(QueryStringQuery.class, qt.query.getClass());
197+
QueryStringQuery qsq = ((QueryStringQuery) qt.query);
198+
assertEquals(1, qsq.fields().size());
199+
assertEquals("some.string.typical", qsq.fields().keySet().iterator().next());
200+
}
187201

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

0 commit comments

Comments
 (0)