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
This commit is contained in:
parent
55e98f08d8
commit
a2c07b5011
|
@ -2353,6 +2353,7 @@ SELECT * FROM (SELECT first_name, last_name FROM emp WHERE last_name NOT LIKE '%
|
||||||
first_name | last_name
|
first_name | last_name
|
||||||
---------------+---------------
|
---------------+---------------
|
||||||
Anneke |Preusig
|
Anneke |Preusig
|
||||||
|
Alejandro |McAlpine
|
||||||
Anoosh |Peyn
|
Anoosh |Peyn
|
||||||
Arumugam |Ossenbruggen
|
Arumugam |Ossenbruggen
|
||||||
// end::limitationSubSelect
|
// end::limitationSubSelect
|
||||||
|
@ -2365,6 +2366,7 @@ SELECT first_name, last_name FROM emp WHERE last_name NOT LIKE '%a%' AND first_n
|
||||||
first_name | last_name
|
first_name | last_name
|
||||||
---------------+---------------
|
---------------+---------------
|
||||||
Anneke |Preusig
|
Anneke |Preusig
|
||||||
|
Alejandro |McAlpine
|
||||||
Anoosh |Peyn
|
Anoosh |Peyn
|
||||||
Arumugam |Ossenbruggen
|
Arumugam |Ossenbruggen
|
||||||
;
|
;
|
||||||
|
|
|
@ -483,7 +483,7 @@ final class QueryTranslator {
|
||||||
if (e.field() instanceof FieldAttribute) {
|
if (e.field() instanceof FieldAttribute) {
|
||||||
FieldAttribute fa = (FieldAttribute) e.field();
|
FieldAttribute fa = (FieldAttribute) e.field();
|
||||||
inexact = fa.isInexact();
|
inexact = fa.isInexact();
|
||||||
target = nameOf(inexact ? fa : fa.exactAttribute());
|
target = nameOf(inexact ? fa.exactAttribute() : fa);
|
||||||
} else {
|
} else {
|
||||||
throw new SqlIllegalArgumentException("Scalar function ({}) not allowed (yet) as arguments for LIKE",
|
throw new SqlIllegalArgumentException("Scalar function ({}) not allowed (yet) as arguments for LIKE",
|
||||||
Expressions.name(e.field()));
|
Expressions.name(e.field()));
|
||||||
|
|
|
@ -39,6 +39,7 @@ import org.elasticsearch.xpack.sql.querydsl.agg.GroupByDateHistogram;
|
||||||
import org.elasticsearch.xpack.sql.querydsl.query.ExistsQuery;
|
import org.elasticsearch.xpack.sql.querydsl.query.ExistsQuery;
|
||||||
import org.elasticsearch.xpack.sql.querydsl.query.NotQuery;
|
import org.elasticsearch.xpack.sql.querydsl.query.NotQuery;
|
||||||
import org.elasticsearch.xpack.sql.querydsl.query.Query;
|
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.RangeQuery;
|
||||||
import org.elasticsearch.xpack.sql.querydsl.query.ScriptQuery;
|
import org.elasticsearch.xpack.sql.querydsl.query.ScriptQuery;
|
||||||
import org.elasticsearch.xpack.sql.querydsl.query.TermQuery;
|
import org.elasticsearch.xpack.sql.querydsl.query.TermQuery;
|
||||||
|
@ -185,6 +186,19 @@ public class QueryTranslatorTests extends ESTestCase {
|
||||||
assertEquals(DateUtils.asDateTime("1969-05-13T12:34:56Z"), rq.lower());
|
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() {
|
public void testLikeConstructsNotSupported() {
|
||||||
LogicalPlan p = plan("SELECT LTRIM(keyword) lt FROM test WHERE LTRIM(keyword) LIKE '%a%'");
|
LogicalPlan p = plan("SELECT LTRIM(keyword) lt FROM test WHERE LTRIM(keyword) LIKE '%a%'");
|
||||||
assertTrue(p instanceof Project);
|
assertTrue(p instanceof Project);
|
||||||
|
|
Loading…
Reference in New Issue