Skip to content

Commit fdf54dc

Browse files
Handle QueryString function
1 parent 29cce29 commit fdf54dc

File tree

2 files changed

+50
-3
lines changed
  • x-pack/plugin
    • esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/fulltext
    • src/yamlRestTest/resources/rest-api-spec/test/esql

2 files changed

+50
-3
lines changed

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/fulltext/QueryString.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,14 @@
6060
import static org.elasticsearch.index.query.QueryStringQueryBuilder.TIME_ZONE_FIELD;
6161
import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.ParamOrdinal.FIRST;
6262
import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.ParamOrdinal.SECOND;
63-
import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.isNotNullAndFoldable;
63+
import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.isNotNull;
6464
import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.isType;
6565
import static org.elasticsearch.xpack.esql.core.type.DataType.BOOLEAN;
6666
import static org.elasticsearch.xpack.esql.core.type.DataType.FLOAT;
6767
import static org.elasticsearch.xpack.esql.core.type.DataType.INTEGER;
6868
import static org.elasticsearch.xpack.esql.core.type.DataType.KEYWORD;
6969
import static org.elasticsearch.xpack.esql.core.type.DataType.TEXT;
70+
import static org.elasticsearch.xpack.esql.expression.function.FunctionUtils.resolveTypeQuery;
7071

7172
/**
7273
* Full text function that performs a {@link QueryStringQuery} .
@@ -309,9 +310,17 @@ public Expression options() {
309310
public static final Set<DataType> QUERY_DATA_TYPES = Set.of(KEYWORD, TEXT);
310311

311312
private TypeResolution resolveQuery() {
312-
return isType(query(), QUERY_DATA_TYPES::contains, sourceText(), FIRST, "keyword, text").and(
313-
isNotNullAndFoldable(query(), sourceText(), FIRST)
313+
TypeResolution result = isType(query(), QUERY_DATA_TYPES::contains, sourceText(), FIRST, "keyword, text").and(
314+
isNotNull(query(), sourceText(), FIRST)
314315
);
316+
if (result.unresolved()) {
317+
return result;
318+
}
319+
result = resolveTypeQuery(query(), sourceText());
320+
if (result.equals(TypeResolution.TYPE_RESOLVED) == false) {
321+
return result;
322+
}
323+
return TypeResolution.TYPE_RESOLVED;
315324
}
316325

317326
private Map<String, Object> queryStringOptions() throws InvalidArgumentException {

x-pack/plugin/src/yamlRestTest/resources/rest-api-spec/test/esql/230_folding.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,3 +241,41 @@ Foldable query using MULTI_MATCH on name but with non-foldable expression:
241241
- match: { error.type: "verification_exception" }
242242
- contains: { error.reason: "Query must be a valid string in [MULTI_MATCH(CONCAT(\"Bob \", name), name)], found [CONCAT(\"Bob \", name)]" }
243243

244+
---
245+
246+
Foldable query using QSTR on name:
247+
- do:
248+
esql.query:
249+
body:
250+
query: |
251+
FROM employees
252+
| WHERE QSTR(CONCAT("name:", "Bob*"))
253+
| KEEP hire_date, salary, salary_change, salary_change_long, name
254+
| LIMIT 5
255+
- match: { columns.0.name: "hire_date" }
256+
- match: { columns.1.name: "salary" }
257+
- match: { columns.2.name: "salary_change" }
258+
- match: { columns.3.name: "salary_change_long" }
259+
- match: { columns.4.name: "name" }
260+
- length: { values: 1 }
261+
- match: { values.0.0: "2021-01-01T00:00:00.000Z" }
262+
- match: { values.0.1: 60000 }
263+
- match: { values.0.2: 200.5 }
264+
- match: { values.0.3: 200 }
265+
- match: { values.0.4: "Bob Johnson" }
266+
267+
---
268+
269+
Foldable query using QSTR on name but with non-foldable expression:
270+
- do:
271+
catch: bad_request
272+
esql.query:
273+
body:
274+
query: |
275+
FROM employees
276+
| WHERE QSTR(CONCAT(name, "Bob"))
277+
| KEEP hire_date, salary, salary_change, salary_change_long, name
278+
| LIMIT 5
279+
- match: { error.type: "verification_exception" }
280+
- contains: { error.reason: "Query must be a valid string in [QSTR(CONCAT(name, \"Bob\"))], found [CONCAT(name, \"Bob\")]" }
281+

0 commit comments

Comments
 (0)