Skip to content

Commit 1608c9a

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 27b4416 commit 1608c9a

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
@@ -2343,6 +2343,7 @@ SELECT * FROM (SELECT first_name, last_name FROM emp WHERE last_name NOT LIKE '%
23432343
first_name | last_name
23442344
---------------+---------------
23452345
Anneke |Preusig
2346+
Alejandro |McAlpine
23462347
Anoosh |Peyn
23472348
Arumugam |Ossenbruggen
23482349
// end::limitationSubSelect
@@ -2355,6 +2356,7 @@ SELECT first_name, last_name FROM emp WHERE last_name NOT LIKE '%a%' AND first_n
23552356
first_name | last_name
23562357
---------------+---------------
23572358
Anneke |Preusig
2359+
Alejandro |McAlpine
23582360
Anoosh |Peyn
23592361
Arumugam |Ossenbruggen
23602362
;

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)