Skip to content

Commit 5f4eff5

Browse files
committed
Add full text functions usage in STATS.. WHERE
1 parent e0b7c8b commit 5f4eff5

File tree

18 files changed

+255
-29
lines changed

18 files changed

+255
-29
lines changed

x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/lucene/LuceneQueryEvaluator.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public record ShardConfig(Query query, IndexSearcher searcher) {}
5454
private final List<ShardState> perShardState;
5555

5656
protected LuceneQueryEvaluator(BlockFactory blockFactory, ShardConfig[] shards) {
57+
assert shards.length > 0 : "LuceneQueryEvaluator expects shard configs";
5758
this.blockFactory = blockFactory;
5859
this.shards = shards;
5960
this.perShardState = new ArrayList<>(Collections.nCopies(shards.length, null));

x-pack/plugin/esql/qa/testFixtures/src/main/resources/kql-function.csv-spec

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,3 +189,28 @@ book_no:keyword
189189
7140
190190
2714
191191
;
192+
193+
testKqlInStatsNonPushable
194+
required_capability: kql_function
195+
required_capability: full_text_functions_in_stats_where
196+
197+
from books
198+
| where length(title) > 40
199+
| stats c = count(*) where kql("title:Lord")
200+
;
201+
202+
c:long
203+
3
204+
;
205+
206+
testKqlInStatsPushable
207+
required_capability: kql_function
208+
required_capability: full_text_functions_in_stats_where
209+
210+
from books
211+
| stats c = count(*) where kql("author:tolkien")
212+
;
213+
214+
c:long
215+
22
216+
;

x-pack/plugin/esql/qa/testFixtures/src/main/resources/match-function.csv-spec

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,3 +755,29 @@ book_no:keyword
755755
7140
756756
2714
757757
;
758+
759+
testMatchInStatsNonPushable
760+
required_capability: match_function
761+
required_capability: full_text_functions_in_stats_where
762+
763+
from books
764+
| where length(title) > 40
765+
| stats c = count(*) where match(title, "Lord")
766+
;
767+
768+
c:long
769+
3
770+
;
771+
772+
testMatchInStatsPushable
773+
required_capability: match_function
774+
required_capability: full_text_functions_in_stats_where
775+
776+
from books
777+
| stats c = count(*) where match(author, "tolkien")
778+
;
779+
780+
c:long
781+
22
782+
;
783+

x-pack/plugin/esql/qa/testFixtures/src/main/resources/match-operator.csv-spec

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -757,3 +757,29 @@ from semantic_text
757757
host:keyword | semantic_text_field:text | language_name:keyword | language_code:integer
758758
"host1" | live long and prosper | English | 1
759759
;
760+
761+
762+
testMatchInStatsNonPushable
763+
required_capability: match_operator_colon
764+
required_capability: full_text_functions_in_stats_where
765+
766+
from books
767+
| where length(title) > 40
768+
| stats c = count(*) where title:"Lord"
769+
;
770+
771+
c:long
772+
3
773+
;
774+
775+
testMatchInStatsPushable
776+
required_capability: match_operator_colon
777+
required_capability: full_text_functions_in_stats_where
778+
779+
from books
780+
| stats c = count(*) where author:"tolkien"
781+
;
782+
783+
c:long
784+
22
785+
;

x-pack/plugin/esql/qa/testFixtures/src/main/resources/qstr-function.csv-spec

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,3 +210,45 @@ book_no:keyword | title:text
210210
7480 | The Hobbit
211211
// end::qstr-with-options-result[]
212212
;
213+
214+
215+
from books
216+
| where (match(title, "lord") and ratings > 4.5) or (match(author, "dostoevsky") and length(title) > 50)
217+
| keep book_no
218+
;
219+
ignoreOrder: true
220+
221+
book_no:keyword
222+
2675
223+
2924
224+
4023
225+
1937
226+
7140
227+
2714
228+
;
229+
230+
testQstrInStatsNonPushable
231+
required_capability: qstr_function
232+
required_capability: full_text_functions_in_stats_where
233+
234+
from books
235+
| where length(title) > 40
236+
| stats c = count(*) where qstr("title:Lord")
237+
;
238+
239+
c:long
240+
3
241+
;
242+
243+
testQstrInStatsPushable
244+
required_capability: qstr_function
245+
required_capability: full_text_functions_in_stats_where
246+
247+
from books
248+
| stats c = count(*) where qstr("author:tolkien")
249+
;
250+
251+
c:long
252+
22
253+
;
254+

x-pack/plugin/esql/src/internalClusterTest/java/org/elasticsearch/xpack/esql/plugin/KqlFunctionIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public void testKqlQueryWithinEval() {
6666
""";
6767

6868
var error = expectThrows(VerificationException.class, () -> run(query));
69-
assertThat(error.getMessage(), containsString("[KQL] function is only supported in WHERE commands"));
69+
assertThat(error.getMessage(), containsString("[KQL] function is only supported in WHERE and STATS ... WHERE commands"));
7070
}
7171

7272
public void testInvalidKqlQueryEof() {

x-pack/plugin/esql/src/internalClusterTest/java/org/elasticsearch/xpack/esql/plugin/MatchFunctionIT.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,14 +246,35 @@ public void testWhereMatchWithRow() {
246246
);
247247
}
248248

249+
public void testMatchWithStats() {
250+
var errorQuery = """
251+
FROM test
252+
| STATS c = count(*) BY match(content, "fox")
253+
""";
254+
255+
var error = expectThrows(ElasticsearchException.class, () -> run(errorQuery));
256+
assertThat(error.getMessage(), containsString("[MATCH] function is only supported in WHERE and STATS ... WHERE commands"));
257+
258+
var query = """
259+
FROM test
260+
| STATS c = count(*) WHERE match(content, "fox"), d = count(*) WHERE match(content, "dog")
261+
""";
262+
263+
try (var resp = run(query)) {
264+
assertColumnNames(resp.columns(), List.of("c", "d"));
265+
assertColumnTypes(resp.columns(), List.of("long", "long"));
266+
assertValues(resp.values(), List.of(List.of(2L, 4L)));
267+
}
268+
}
269+
249270
public void testMatchWithinEval() {
250271
var query = """
251272
FROM test
252273
| EVAL matches_query = match(content, "fox")
253274
""";
254275

255276
var error = expectThrows(VerificationException.class, () -> run(query));
256-
assertThat(error.getMessage(), containsString("[MATCH] function is only supported in WHERE commands"));
277+
assertThat(error.getMessage(), containsString("[MATCH] function is only supported in WHERE and STATS ... WHERE commands"));
257278
}
258279

259280
private void createAndPopulateIndex() {

x-pack/plugin/esql/src/internalClusterTest/java/org/elasticsearch/xpack/esql/plugin/MatchOperatorIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ public void testMatchWithinEval() {
354354
""";
355355

356356
var error = expectThrows(VerificationException.class, () -> run(query));
357-
assertThat(error.getMessage(), containsString("[:] operator is only supported in WHERE commands"));
357+
assertThat(error.getMessage(), containsString("[:] operator is only supported in WHERE and STATS ... WHERE commands"));
358358
}
359359

360360
public void testMatchWithNonTextField() {

x-pack/plugin/esql/src/internalClusterTest/java/org/elasticsearch/xpack/esql/plugin/QueryStringIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public void testQueryStringWithinEval() {
6363
""";
6464

6565
var error = expectThrows(VerificationException.class, () -> run(query));
66-
assertThat(error.getMessage(), containsString("[QSTR] function is only supported in WHERE commands"));
66+
assertThat(error.getMessage(), containsString("[QSTR] function is only supported in WHERE and STATS ... WHERE commands"));
6767
}
6868

6969
public void testInvalidQueryStringEof() {

x-pack/plugin/esql/src/internalClusterTest/java/org/elasticsearch/xpack/esql/plugin/TermIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public void testTermWithinEval() {
5757
""";
5858

5959
var error = expectThrows(VerificationException.class, () -> run(query));
60-
assertThat(error.getMessage(), containsString("[Term] function is only supported in WHERE commands"));
60+
assertThat(error.getMessage(), containsString("[Term] function is only supported in WHERE and STATS ... WHERE commands"));
6161
}
6262

6363
public void testMultipleTerm() {

0 commit comments

Comments
 (0)