From 57283192eb32646325a7fd462d029655cb40de8c Mon Sep 17 00:00:00 2001 From: Nhat Nguyen Date: Mon, 2 Oct 2023 22:58:31 -0700 Subject: [PATCH] Release ESQL response in IT tests --- .../xpack/esql/action/EsqlActionIT.java | 824 ++++++++++-------- .../esql/action/EsqlActionRuntimeFieldIT.java | 22 +- .../xpack/esql/action/ManyShardsIT.java | 2 +- 3 files changed, 459 insertions(+), 389 deletions(-) diff --git a/x-pack/plugin/esql/src/internalClusterTest/java/org/elasticsearch/xpack/esql/action/EsqlActionIT.java b/x-pack/plugin/esql/src/internalClusterTest/java/org/elasticsearch/xpack/esql/action/EsqlActionIT.java index 05580e586ef79..a4c13af3d99ad 100644 --- a/x-pack/plugin/esql/src/internalClusterTest/java/org/elasticsearch/xpack/esql/action/EsqlActionIT.java +++ b/x-pack/plugin/esql/src/internalClusterTest/java/org/elasticsearch/xpack/esql/action/EsqlActionIT.java @@ -76,23 +76,27 @@ public void setupIndex() { } public void testProjectConstant() { - EsqlQueryResponse results = run("from test | eval x = 1 | keep x"); - assertThat(results.columns(), equalTo(List.of(new ColumnInfo("x", "integer")))); - assertThat(getValuesList(results).size(), equalTo(40)); - assertThat(getValuesList(results).get(0).get(0), equalTo(1)); + try (EsqlQueryResponse results = run("from test | eval x = 1 | keep x")) { + assertThat(results.columns(), equalTo(List.of(new ColumnInfo("x", "integer")))); + assertThat(getValuesList(results).size(), equalTo(40)); + assertThat(getValuesList(results).get(0).get(0), equalTo(1)); + } + } public void testStatsOverConstant() { - EsqlQueryResponse results = run("from test | eval x = 1 | stats x = count(x)"); - assertThat(results.columns(), equalTo(List.of(new ColumnInfo("x", "long")))); - assertThat(getValuesList(results).size(), equalTo(1)); - assertThat(getValuesList(results).get(0).get(0), equalTo(40L)); + try (EsqlQueryResponse results = run("from test | eval x = 1 | stats x = count(x)")) { + assertThat(results.columns(), equalTo(List.of(new ColumnInfo("x", "long")))); + assertThat(getValuesList(results).size(), equalTo(1)); + assertThat(getValuesList(results).get(0).get(0), equalTo(40L)); + } } public void testRow() { long value = randomLongBetween(0, Long.MAX_VALUE); - EsqlQueryResponse response = run("row " + value); - assertEquals(List.of(List.of(value)), getValuesList(response)); + try (EsqlQueryResponse response = run("row " + value)) { + assertEquals(List.of(List.of(value)), getValuesList(response)); + } } public void testFromStatsGroupingAvgWithSort() { @@ -147,56 +151,58 @@ public void testFromStatsGroupingCountWithAliases() { } private void testFromStatsGroupingCountImpl(String command, String expectedFieldName, String expectedGroupName) { - EsqlQueryResponse results = run(command); - logger.info(results); - Assert.assertEquals(2, results.columns().size()); - - // assert column metadata - ColumnInfo groupColumn = results.columns().get(0); - assertEquals(expectedGroupName, groupColumn.name()); - assertEquals("long", groupColumn.type()); - ColumnInfo valuesColumn = results.columns().get(1); - assertEquals(expectedFieldName, valuesColumn.name()); - assertEquals("long", valuesColumn.type()); - - // assert column values - List> valueValues = getValuesList(results); - assertEquals(2, valueValues.size()); - // This is loathsome, find a declarative way to assert the expected output. - if ((long) valueValues.get(0).get(1) == 1L) { - assertEquals(20L, valueValues.get(0).get(0)); - assertEquals(2L, valueValues.get(1).get(1)); - assertEquals(20L, valueValues.get(1).get(0)); - } else if ((long) valueValues.get(0).get(1) == 2L) { - assertEquals(20L, valueValues.get(1).get(0)); - assertEquals(1L, valueValues.get(1).get(1)); - assertEquals(20L, valueValues.get(0).get(0)); - } else { - fail("Unexpected group value: " + valueValues.get(0).get(1)); + try (EsqlQueryResponse results = run(command)) { + logger.info(results); + Assert.assertEquals(2, results.columns().size()); + + // assert column metadata + ColumnInfo groupColumn = results.columns().get(0); + assertEquals(expectedGroupName, groupColumn.name()); + assertEquals("long", groupColumn.type()); + ColumnInfo valuesColumn = results.columns().get(1); + assertEquals(expectedFieldName, valuesColumn.name()); + assertEquals("long", valuesColumn.type()); + + // assert column values + List> valueValues = getValuesList(results); + assertEquals(2, valueValues.size()); + // This is loathsome, find a declarative way to assert the expected output. + if ((long) valueValues.get(0).get(1) == 1L) { + assertEquals(20L, valueValues.get(0).get(0)); + assertEquals(2L, valueValues.get(1).get(1)); + assertEquals(20L, valueValues.get(1).get(0)); + } else if ((long) valueValues.get(0).get(1) == 2L) { + assertEquals(20L, valueValues.get(1).get(0)); + assertEquals(1L, valueValues.get(1).get(1)); + assertEquals(20L, valueValues.get(0).get(0)); + } else { + fail("Unexpected group value: " + valueValues.get(0).get(1)); + } } } // Grouping where the groupby field is of a date type. public void testFromStatsGroupingByDate() { - EsqlQueryResponse results = run("from test | stats avg(count) by time"); - logger.info(results); - Assert.assertEquals(2, results.columns().size()); - Assert.assertEquals(40, getValuesList(results).size()); - - // assert column metadata - assertEquals("avg(count)", results.columns().get(0).name()); - assertEquals("double", results.columns().get(0).type()); - assertEquals("time", results.columns().get(1).name()); - assertEquals("long", results.columns().get(1).type()); - - // assert column values - List expectedValues = LongStream.range(0, 40).map(i -> epoch + i).sorted().boxed().toList(); - List actualValues = IntStream.range(0, 40) - .mapToLong(i -> (Long) getValuesList(results).get(i).get(1)) - .sorted() - .boxed() - .toList(); - assertEquals(expectedValues, actualValues); + try (EsqlQueryResponse results = run("from test | stats avg(count) by time")) { + logger.info(results); + Assert.assertEquals(2, results.columns().size()); + Assert.assertEquals(40, getValuesList(results).size()); + + // assert column metadata + assertEquals("avg(count)", results.columns().get(0).name()); + assertEquals("double", results.columns().get(0).type()); + assertEquals("time", results.columns().get(1).name()); + assertEquals("long", results.columns().get(1).type()); + + // assert column values + List expectedValues = LongStream.range(0, 40).map(i -> epoch + i).sorted().boxed().toList(); + List actualValues = IntStream.range(0, 40) + .mapToLong(i -> (Long) getValuesList(results).get(i).get(1)) + .sorted() + .boxed() + .toList(); + assertEquals(expectedValues, actualValues); + } } public void testFromGroupingByNumericFieldWithNulls() { @@ -228,25 +234,26 @@ record Group(Long data, Double avg) {} } public void testFromStatsGroupingByKeyword() { - EsqlQueryResponse results = run("from test | stats avg(count) by color"); - logger.info(results); - Assert.assertEquals(2, results.columns().size()); - Assert.assertEquals(3, getValuesList(results).size()); + try (EsqlQueryResponse results = run("from test | stats avg(count) by color")) { + logger.info(results); + Assert.assertEquals(2, results.columns().size()); + Assert.assertEquals(3, getValuesList(results).size()); - // assert column metadata - assertEquals("avg(count)", results.columns().get(0).name()); - assertEquals("double", results.columns().get(0).type()); - assertEquals("color", results.columns().get(1).name()); - assertEquals("keyword", results.columns().get(1).type()); - record Group(String color, double avg) { + // assert column metadata + assertEquals("avg(count)", results.columns().get(0).name()); + assertEquals("double", results.columns().get(0).type()); + assertEquals("color", results.columns().get(1).name()); + assertEquals("keyword", results.columns().get(1).type()); + record Group(String color, double avg) { + } + List expectedGroups = List.of(new Group("blue", 42.0), new Group("green", 44.0), new Group("red", 43)); + List actualGroups = getValuesList(results).stream() + .map(l -> new Group((String) l.get(1), (Double) l.get(0))) + .sorted(comparing(c -> c.color)) + .toList(); + assertThat(actualGroups, equalTo(expectedGroups)); } - List expectedGroups = List.of(new Group("blue", 42.0), new Group("green", 44.0), new Group("red", 43)); - List actualGroups = getValuesList(results).stream() - .map(l -> new Group((String) l.get(1), (Double) l.get(0))) - .sorted(comparing(c -> c.color)) - .toList(); - assertThat(actualGroups, equalTo(expectedGroups)); } public void testFromStatsGroupingByKeywordWithNulls() { @@ -262,69 +269,75 @@ public void testFromStatsGroupingByKeywordWithNulls() { } client().admin().indices().prepareRefresh("test").get(); for (String field : List.of("count", "count_d")) { - EsqlQueryResponse results = run("from test | stats avg = avg(" + field + ") by color"); + try (EsqlQueryResponse results = run("from test | stats avg = avg(" + field + ") by color")) { + logger.info(results); + Assert.assertEquals(2, results.columns().size()); + Assert.assertEquals(5, getValuesList(results).size()); + + // assert column metadata + assertEquals("avg", results.columns().get(0).name()); + assertEquals("double", results.columns().get(0).type()); + assertEquals("color", results.columns().get(1).name()); + assertEquals("keyword", results.columns().get(1).type()); + record Group(String color, Double avg) { + + } + List expectedGroups = List.of( + new Group(null, 120.0), + new Group("blue", 42.0), + new Group("green", 44.0), + new Group("red", 43.0), + new Group("yellow", null) + ); + List actualGroups = getValuesList(results).stream() + .map(l -> new Group((String) l.get(1), (Double) l.get(0))) + .sorted(Comparator.comparing(c -> c.color, Comparator.nullsFirst(String::compareTo))) + .toList(); + assertThat(actualGroups, equalTo(expectedGroups)); + } + } + } + + public void testFromStatsMultipleAggs() { + try ( + EsqlQueryResponse results = run( + "from test | stats a=avg(count), mi=min(count), ma=max(count), s=sum(count), c=count(count) by color" + ) + ) { logger.info(results); - Assert.assertEquals(2, results.columns().size()); - Assert.assertEquals(5, getValuesList(results).size()); + Assert.assertEquals(6, results.columns().size()); + Assert.assertEquals(3, getValuesList(results).size()); // assert column metadata - assertEquals("avg", results.columns().get(0).name()); + assertEquals("a", results.columns().get(0).name()); assertEquals("double", results.columns().get(0).type()); - assertEquals("color", results.columns().get(1).name()); - assertEquals("keyword", results.columns().get(1).type()); - record Group(String color, Double avg) { - - } + assertEquals("mi", results.columns().get(1).name()); + assertEquals("long", results.columns().get(1).type()); + assertEquals("ma", results.columns().get(2).name()); + assertEquals("long", results.columns().get(2).type()); + assertEquals("s", results.columns().get(3).name()); + assertEquals("long", results.columns().get(3).type()); + assertEquals("c", results.columns().get(4).name()); + assertEquals("long", results.columns().get(4).type()); + assertEquals("color", results.columns().get(5).name()); + assertEquals("keyword", results.columns().get(5).type()); + record Group(double avg, long mi, long ma, long s, long c, String color) {} List expectedGroups = List.of( - new Group(null, 120.0), - new Group("blue", 42.0), - new Group("green", 44.0), - new Group("red", 43.0), - new Group("yellow", null) + new Group(42, 42, 42, 420, 10, "blue"), + new Group(44, 44, 44, 440, 10, "green"), + new Group(43, 40, 46, 860, 20, "red") ); + // TODO: each aggregator returns Double now, it should in fact mirror the data type of the fields it's aggregating List actualGroups = getValuesList(results).stream() - .map(l -> new Group((String) l.get(1), (Double) l.get(0))) - .sorted(Comparator.comparing(c -> c.color, Comparator.nullsFirst(String::compareTo))) + .map( + l -> new Group((Double) l.get(0), (Long) l.get(1), (Long) l.get(2), (Long) l.get(3), (Long) l.get(4), (String) l.get(5)) + ) + .sorted(comparing(c -> c.color)) .toList(); assertThat(actualGroups, equalTo(expectedGroups)); } } - public void testFromStatsMultipleAggs() { - EsqlQueryResponse results = run( - "from test | stats a=avg(count), mi=min(count), ma=max(count), s=sum(count), c=count(count) by color" - ); - logger.info(results); - Assert.assertEquals(6, results.columns().size()); - Assert.assertEquals(3, getValuesList(results).size()); - - // assert column metadata - assertEquals("a", results.columns().get(0).name()); - assertEquals("double", results.columns().get(0).type()); - assertEquals("mi", results.columns().get(1).name()); - assertEquals("long", results.columns().get(1).type()); - assertEquals("ma", results.columns().get(2).name()); - assertEquals("long", results.columns().get(2).type()); - assertEquals("s", results.columns().get(3).name()); - assertEquals("long", results.columns().get(3).type()); - assertEquals("c", results.columns().get(4).name()); - assertEquals("long", results.columns().get(4).type()); - assertEquals("color", results.columns().get(5).name()); - assertEquals("keyword", results.columns().get(5).type()); - record Group(double avg, long mi, long ma, long s, long c, String color) {} - List expectedGroups = List.of( - new Group(42, 42, 42, 420, 10, "blue"), - new Group(44, 44, 44, 440, 10, "green"), - new Group(43, 40, 46, 860, 20, "red") - ); - // TODO: each aggregator returns Double now, it should in fact mirror the data type of the fields it's aggregating - List actualGroups = getValuesList(results).stream() - .map(l -> new Group((Double) l.get(0), (Long) l.get(1), (Long) l.get(2), (Long) l.get(3), (Long) l.get(4), (String) l.get(5))) - .sorted(comparing(c -> c.color)) - .toList(); - assertThat(actualGroups, equalTo(expectedGroups)); - } - public void testFromSortWithTieBreakerLimit() { try (EsqlQueryResponse results = run("from test | sort data, count desc, time | limit 5 | keep data, count, time")) { logger.info(results); @@ -342,103 +355,115 @@ public void testFromSortWithTieBreakerLimit() { } public void testFromStatsProjectGroup() { - EsqlQueryResponse results = run("from test | stats avg_count = avg(count) by data | keep data"); - logger.info(results); - assertThat(results.columns().stream().map(ColumnInfo::name).toList(), contains("data")); - assertThat(results.columns().stream().map(ColumnInfo::type).toList(), contains("long")); - assertThat(getValuesList(results), containsInAnyOrder(List.of(1L), List.of(2L))); + try (EsqlQueryResponse results = run("from test | stats avg_count = avg(count) by data | keep data")) { + logger.info(results); + assertThat(results.columns().stream().map(ColumnInfo::name).toList(), contains("data")); + assertThat(results.columns().stream().map(ColumnInfo::type).toList(), contains("long")); + assertThat(getValuesList(results), containsInAnyOrder(List.of(1L), List.of(2L))); + } } public void testRowStatsProjectGroupByInt() { - EsqlQueryResponse results = run("row a = 1, b = 2 | stats count(b) by a | keep a"); - logger.info(results); - assertThat(results.columns().stream().map(ColumnInfo::name).toList(), contains("a")); - assertThat(results.columns().stream().map(ColumnInfo::type).toList(), contains("integer")); - assertThat(getValuesList(results), contains(List.of(1))); + try (EsqlQueryResponse results = run("row a = 1, b = 2 | stats count(b) by a | keep a")) { + logger.info(results); + assertThat(results.columns().stream().map(ColumnInfo::name).toList(), contains("a")); + assertThat(results.columns().stream().map(ColumnInfo::type).toList(), contains("integer")); + assertThat(getValuesList(results), contains(List.of(1))); + } } public void testRowStatsProjectGroupByLong() { - EsqlQueryResponse results = run("row a = 1000000000000, b = 2 | stats count(b) by a | keep a"); - logger.info(results); - assertThat(results.columns().stream().map(ColumnInfo::name).toList(), contains("a")); - assertThat(results.columns().stream().map(ColumnInfo::type).toList(), contains("long")); - assertThat(getValuesList(results), contains(List.of(1000000000000L))); + try (EsqlQueryResponse results = run("row a = 1000000000000, b = 2 | stats count(b) by a | keep a")) { + logger.info(results); + assertThat(results.columns().stream().map(ColumnInfo::name).toList(), contains("a")); + assertThat(results.columns().stream().map(ColumnInfo::type).toList(), contains("long")); + assertThat(getValuesList(results), contains(List.of(1000000000000L))); + } } public void testRowStatsProjectGroupByDouble() { - EsqlQueryResponse results = run("row a = 1.0, b = 2 | stats count(b) by a | keep a"); - logger.info(results); - assertThat(results.columns().stream().map(ColumnInfo::name).toList(), contains("a")); - assertThat(results.columns().stream().map(ColumnInfo::type).toList(), contains("double")); - assertThat(getValuesList(results), contains(List.of(1.0))); + try (EsqlQueryResponse results = run("row a = 1.0, b = 2 | stats count(b) by a | keep a")) { + logger.info(results); + assertThat(results.columns().stream().map(ColumnInfo::name).toList(), contains("a")); + assertThat(results.columns().stream().map(ColumnInfo::type).toList(), contains("double")); + assertThat(getValuesList(results), contains(List.of(1.0))); + } } public void testRowStatsProjectGroupByKeyword() { - EsqlQueryResponse results = run("row a = \"hello\", b = 2 | stats count(b) by a | keep a"); - logger.info(results); - assertThat(results.columns().stream().map(ColumnInfo::name).toList(), contains("a")); - assertThat(results.columns().stream().map(ColumnInfo::type).toList(), contains("keyword")); - assertThat(getValuesList(results), contains(List.of("hello"))); + try (EsqlQueryResponse results = run("row a = \"hello\", b = 2 | stats count(b) by a | keep a")) { + logger.info(results); + assertThat(results.columns().stream().map(ColumnInfo::name).toList(), contains("a")); + assertThat(results.columns().stream().map(ColumnInfo::type).toList(), contains("keyword")); + assertThat(getValuesList(results), contains(List.of("hello"))); + } } public void testFromStatsProjectGroupByDouble() { - EsqlQueryResponse results = run("from test | stats count(count) by data_d | keep data_d"); - logger.info(results); - assertThat(results.columns().stream().map(ColumnInfo::name).toList(), contains("data_d")); - assertThat(results.columns().stream().map(ColumnInfo::type).toList(), contains("double")); - assertThat(getValuesList(results), containsInAnyOrder(List.of(1.0), List.of(2.0))); + try (EsqlQueryResponse results = run("from test | stats count(count) by data_d | keep data_d")) { + logger.info(results); + assertThat(results.columns().stream().map(ColumnInfo::name).toList(), contains("data_d")); + assertThat(results.columns().stream().map(ColumnInfo::type).toList(), contains("double")); + assertThat(getValuesList(results), containsInAnyOrder(List.of(1.0), List.of(2.0))); + } } public void testFromStatsProjectGroupWithAlias() { String query = "from test | stats avg_count = avg(count) by data | eval d2 = data | rename data as d | keep d, d2"; - EsqlQueryResponse results = run(query); - logger.info(results); - assertThat(results.columns().stream().map(ColumnInfo::name).toList(), contains("d", "d2")); - assertThat(results.columns().stream().map(ColumnInfo::type).toList(), contains("long", "long")); - assertThat(getValuesList(results), containsInAnyOrder(List.of(1L, 1L), List.of(2L, 2L))); + try (EsqlQueryResponse results = run(query)) { + logger.info(results); + assertThat(results.columns().stream().map(ColumnInfo::name).toList(), contains("d", "d2")); + assertThat(results.columns().stream().map(ColumnInfo::type).toList(), contains("long", "long")); + assertThat(getValuesList(results), containsInAnyOrder(List.of(1L, 1L), List.of(2L, 2L))); + } } public void testFromStatsProjectAgg() { - EsqlQueryResponse results = run("from test | stats a = avg(count) by data | keep a"); - logger.info(results); - assertThat(results.columns().stream().map(ColumnInfo::name).toList(), contains("a")); - assertThat(results.columns().stream().map(ColumnInfo::type).toList(), contains("double")); - assertThat(getValuesList(results), containsInAnyOrder(List.of(42d), List.of(44d))); + try (EsqlQueryResponse results = run("from test | stats a = avg(count) by data | keep a")) { + logger.info(results); + assertThat(results.columns().stream().map(ColumnInfo::name).toList(), contains("a")); + assertThat(results.columns().stream().map(ColumnInfo::type).toList(), contains("double")); + assertThat(getValuesList(results), containsInAnyOrder(List.of(42d), List.of(44d))); + } } public void testFromStatsProjectAggWithAlias() { - EsqlQueryResponse results = run("from test | stats a = avg(count) by data | rename a as b | keep b"); - logger.info(results); - assertThat(results.columns().stream().map(ColumnInfo::name).toList(), contains("b")); - assertThat(results.columns().stream().map(ColumnInfo::type).toList(), contains("double")); - assertThat(getValuesList(results), containsInAnyOrder(List.of(42d), List.of(44d))); + try (EsqlQueryResponse results = run("from test | stats a = avg(count) by data | rename a as b | keep b")) { + logger.info(results); + assertThat(results.columns().stream().map(ColumnInfo::name).toList(), contains("b")); + assertThat(results.columns().stream().map(ColumnInfo::type).toList(), contains("double")); + assertThat(getValuesList(results), containsInAnyOrder(List.of(42d), List.of(44d))); + } } public void testFromProjectStatsGroupByAlias() { - EsqlQueryResponse results = run("from test | rename data as d | keep d, count | stats avg(count) by d"); - logger.info(results); - assertThat(results.columns().stream().map(ColumnInfo::name).toList(), contains("avg(count)", "d")); - assertThat(results.columns().stream().map(ColumnInfo::type).toList(), contains("double", "long")); - assertThat(getValuesList(results), containsInAnyOrder(List.of(42d, 1L), List.of(44d, 2L))); + try (EsqlQueryResponse results = run("from test | rename data as d | keep d, count | stats avg(count) by d")) { + logger.info(results); + assertThat(results.columns().stream().map(ColumnInfo::name).toList(), contains("avg(count)", "d")); + assertThat(results.columns().stream().map(ColumnInfo::type).toList(), contains("double", "long")); + assertThat(getValuesList(results), containsInAnyOrder(List.of(42d, 1L), List.of(44d, 2L))); + } } public void testFromProjectStatsAggregateAlias() { - EsqlQueryResponse results = run("from test | rename count as c | keep c, data | stats avg(c) by data"); - logger.info(results); - assertThat(results.columns().stream().map(ColumnInfo::name).toList(), contains("avg(c)", "data")); - assertThat(results.columns().stream().map(ColumnInfo::type).toList(), contains("double", "long")); - assertThat(getValuesList(results), containsInAnyOrder(List.of(42d, 1L), List.of(44d, 2L))); + try (EsqlQueryResponse results = run("from test | rename count as c | keep c, data | stats avg(c) by data")) { + logger.info(results); + assertThat(results.columns().stream().map(ColumnInfo::name).toList(), contains("avg(c)", "data")); + assertThat(results.columns().stream().map(ColumnInfo::type).toList(), contains("double", "long")); + assertThat(getValuesList(results), containsInAnyOrder(List.of(42d, 1L), List.of(44d, 2L))); + } } public void testFromEvalStats() { - EsqlQueryResponse results = run("from test | eval ratio = data_d / count_d | stats avg(ratio)"); - logger.info(results); - Assert.assertEquals(1, results.columns().size()); - Assert.assertEquals(1, getValuesList(results).size()); - assertEquals("avg(ratio)", results.columns().get(0).name()); - assertEquals("double", results.columns().get(0).type()); - assertEquals(1, getValuesList(results).get(0).size()); - assertEquals(0.034d, (double) getValuesList(results).get(0).get(0), 0.001d); + try (EsqlQueryResponse results = run("from test | eval ratio = data_d / count_d | stats avg(ratio)")) { + logger.info(results); + Assert.assertEquals(1, results.columns().size()); + Assert.assertEquals(1, getValuesList(results).size()); + assertEquals("avg(ratio)", results.columns().get(0).name()); + assertEquals("double", results.columns().get(0).type()); + assertEquals(1, getValuesList(results).get(0).size()); + assertEquals(0.034d, (double) getValuesList(results).get(0).get(0), 0.001d); + } } public void testUngroupedCountAll() { @@ -485,48 +510,57 @@ public void testGroupedCountAllWithFilter() { public void testFromStatsEvalWithPragma() { assumeTrue("pragmas only enabled on snapshot builds", Build.current().isSnapshot()); - EsqlQueryResponse results = run("from test | stats avg_count = avg(count) | eval x = avg_count + 7"); - logger.info(results); - Assert.assertEquals(1, getValuesList(results).size()); - assertEquals(2, getValuesList(results).get(0).size()); - assertEquals(50, (double) getValuesList(results).get(0).get(results.columns().indexOf(new ColumnInfo("x", "double"))), 1d); - assertEquals(43, (double) getValuesList(results).get(0).get(results.columns().indexOf(new ColumnInfo("avg_count", "double"))), 1d); + try (EsqlQueryResponse results = run("from test | stats avg_count = avg(count) | eval x = avg_count + 7")) { + logger.info(results); + Assert.assertEquals(1, getValuesList(results).size()); + assertEquals(2, getValuesList(results).get(0).size()); + assertEquals(50, (double) getValuesList(results).get(0).get(results.columns().indexOf(new ColumnInfo("x", "double"))), 1d); + assertEquals( + 43, + (double) getValuesList(results).get(0).get(results.columns().indexOf(new ColumnInfo("avg_count", "double"))), + 1d + ); + } } public void testWhere() { - EsqlQueryResponse results = run("from test | where count > 40"); - logger.info(results); - Assert.assertEquals(30, getValuesList(results).size()); - var countIndex = results.columns().indexOf(new ColumnInfo("count", "long")); - for (List values : getValuesList(results)) { - assertThat((Long) values.get(countIndex), greaterThan(40L)); + try (EsqlQueryResponse results = run("from test | where count > 40")) { + logger.info(results); + Assert.assertEquals(30, getValuesList(results).size()); + var countIndex = results.columns().indexOf(new ColumnInfo("count", "long")); + for (List values : getValuesList(results)) { + assertThat((Long) values.get(countIndex), greaterThan(40L)); + } } } public void testProjectWhere() { - EsqlQueryResponse results = run("from test | keep count | where count > 40"); - logger.info(results); - Assert.assertEquals(30, getValuesList(results).size()); - int countIndex = results.columns().indexOf(new ColumnInfo("count", "long")); - for (List values : getValuesList(results)) { - assertThat((Long) values.get(countIndex), greaterThan(40L)); + try (EsqlQueryResponse results = run("from test | keep count | where count > 40")) { + logger.info(results); + Assert.assertEquals(30, getValuesList(results).size()); + int countIndex = results.columns().indexOf(new ColumnInfo("count", "long")); + for (List values : getValuesList(results)) { + assertThat((Long) values.get(countIndex), greaterThan(40L)); + } } } public void testEvalWhere() { - EsqlQueryResponse results = run("from test | eval x = count / 2 | where x > 20"); - logger.info(results); - Assert.assertEquals(30, getValuesList(results).size()); - int countIndex = results.columns().indexOf(new ColumnInfo("x", "long")); - for (List values : getValuesList(results)) { - assertThat((Long) values.get(countIndex), greaterThan(20L)); + try (EsqlQueryResponse results = run("from test | eval x = count / 2 | where x > 20")) { + logger.info(results); + Assert.assertEquals(30, getValuesList(results).size()); + int countIndex = results.columns().indexOf(new ColumnInfo("x", "long")); + for (List values : getValuesList(results)) { + assertThat((Long) values.get(countIndex), greaterThan(20L)); + } } } public void testFilterWithNullAndEval() { - EsqlQueryResponse results = run("row a = 1 | eval b = a + null | where b > 1"); - logger.info(results); - Assert.assertEquals(0, getValuesList(results).size()); + try (EsqlQueryResponse results = run("row a = 1 | eval b = a + null | where b > 1")) { + logger.info(results); + Assert.assertEquals(0, getValuesList(results).size()); + } } public void testStringLength() { @@ -546,101 +580,112 @@ public void testFilterWithNullAndEvalFromIndex() { client().admin().indices().prepareRefresh("test").get(); // sanity - EsqlQueryResponse results = run("from test"); - Assert.assertEquals(41, getValuesList(results).size()); - - results = run("from test | eval newCount = count + 1 | where newCount > 1"); - logger.info(results); - Assert.assertEquals(40, getValuesList(results).size()); - assertThat(results.columns(), hasItem(equalTo(new ColumnInfo("count", "long")))); - assertThat(results.columns(), hasItem(equalTo(new ColumnInfo("count_d", "double")))); - assertThat(results.columns(), hasItem(equalTo(new ColumnInfo("data", "long")))); - assertThat(results.columns(), hasItem(equalTo(new ColumnInfo("data_d", "double")))); - assertThat(results.columns(), hasItem(equalTo(new ColumnInfo("time", "long")))); + try (EsqlQueryResponse results = run("from test")) { + Assert.assertEquals(41, getValuesList(results).size()); + } + try (EsqlQueryResponse results = run("from test | eval newCount = count + 1 | where newCount > 1")) { + logger.info(results); + Assert.assertEquals(40, getValuesList(results).size()); + assertThat(results.columns(), hasItem(equalTo(new ColumnInfo("count", "long")))); + assertThat(results.columns(), hasItem(equalTo(new ColumnInfo("count_d", "double")))); + assertThat(results.columns(), hasItem(equalTo(new ColumnInfo("data", "long")))); + assertThat(results.columns(), hasItem(equalTo(new ColumnInfo("data_d", "double")))); + assertThat(results.columns(), hasItem(equalTo(new ColumnInfo("time", "long")))); + } } public void testMultiConditionalWhere() { - EsqlQueryResponse results = run( - "from test | eval abc = 1+2 | where (abc + count >= 44 or data_d == 2) and data == 1 | keep color, abc" - ); - logger.info(results); - Assert.assertEquals(10, getValuesList(results).size()); - Assert.assertEquals(2, results.columns().size()); - for (List values : getValuesList(results)) { - assertThat((String) values.get(0), equalTo("green")); - assertThat((Integer) values.get(1), equalTo(3)); + try (var results = run("from test | eval abc = 1+2 | where (abc + count >= 44 or data_d == 2) and data == 1 | keep color, abc")) { + logger.info(results); + Assert.assertEquals(10, getValuesList(results).size()); + Assert.assertEquals(2, results.columns().size()); + for (List values : getValuesList(results)) { + assertThat((String) values.get(0), equalTo("green")); + assertThat((Integer) values.get(1), equalTo(3)); + } } } public void testWhereNegatedCondition() { - EsqlQueryResponse results = run("from test | eval abc=1+2 | where abc + count > 45 and data != 1 | keep color, data"); - logger.info(results); - Assert.assertEquals(10, getValuesList(results).size()); - Assert.assertEquals(2, results.columns().size()); - for (List values : getValuesList(results)) { - assertThat((String) values.get(0), equalTo("red")); - assertThat((Long) values.get(1), equalTo(2L)); + try (var results = run("from test | eval abc=1+2 | where abc + count > 45 and data != 1 | keep color, data")) { + logger.info(results); + Assert.assertEquals(10, getValuesList(results).size()); + Assert.assertEquals(2, results.columns().size()); + for (List values : getValuesList(results)) { + assertThat((String) values.get(0), equalTo("red")); + assertThat((Long) values.get(1), equalTo(2L)); + } } } public void testEvalOverride() { - EsqlQueryResponse results = run("from test | eval count = count + 1 | eval count = count + 1"); - logger.info(results); - Assert.assertEquals(40, getValuesList(results).size()); - Assert.assertEquals(1, results.columns().stream().filter(c -> c.name().equals("count")).count()); - int countIndex = results.columns().size() - 1; - Assert.assertEquals(new ColumnInfo("count", "long"), results.columns().get(countIndex)); - for (List values : getValuesList(results)) { - assertThat((Long) values.get(countIndex), greaterThanOrEqualTo(42L)); + try (var results = run("from test | eval count = count + 1 | eval count = count + 1")) { + logger.info(results); + Assert.assertEquals(40, getValuesList(results).size()); + Assert.assertEquals(1, results.columns().stream().filter(c -> c.name().equals("count")).count()); + int countIndex = results.columns().size() - 1; + Assert.assertEquals(new ColumnInfo("count", "long"), results.columns().get(countIndex)); + for (List values : getValuesList(results)) { + assertThat((Long) values.get(countIndex), greaterThanOrEqualTo(42L)); + } } } public void testProjectRename() { - EsqlQueryResponse results = run("from test | eval y = count | rename count as x | keep x, y"); - logger.info(results); - Assert.assertEquals(40, getValuesList(results).size()); - assertThat(results.columns(), contains(new ColumnInfo("x", "long"), new ColumnInfo("y", "long"))); - for (List values : getValuesList(results)) { - assertThat((Long) values.get(0), greaterThanOrEqualTo(40L)); - assertThat(values.get(1), is(values.get(0))); + try (var results = run("from test | eval y = count | rename count as x | keep x, y")) { + logger.info(results); + Assert.assertEquals(40, getValuesList(results).size()); + assertThat(results.columns(), contains(new ColumnInfo("x", "long"), new ColumnInfo("y", "long"))); + for (List values : getValuesList(results)) { + assertThat((Long) values.get(0), greaterThanOrEqualTo(40L)); + assertThat(values.get(1), is(values.get(0))); + } } } public void testProjectRenameEval() { - EsqlQueryResponse results = run("from test | eval y = count | rename count as x | keep x, y | eval x2 = x + 1 | eval y2 = y + 2"); - logger.info(results); - Assert.assertEquals(40, getValuesList(results).size()); - assertThat( - results.columns(), - contains(new ColumnInfo("x", "long"), new ColumnInfo("y", "long"), new ColumnInfo("x2", "long"), new ColumnInfo("y2", "long")) - ); - for (List values : getValuesList(results)) { - assertThat((Long) values.get(0), greaterThanOrEqualTo(40L)); - assertThat(values.get(1), is(values.get(0))); - assertThat(values.get(2), is(((Long) values.get(0)) + 1)); - assertThat(values.get(3), is(((Long) values.get(0)) + 2)); + try (var results = run("from test | eval y = count | rename count as x | keep x, y | eval x2 = x + 1 | eval y2 = y + 2")) { + logger.info(results); + Assert.assertEquals(40, getValuesList(results).size()); + assertThat( + results.columns(), + contains( + new ColumnInfo("x", "long"), + new ColumnInfo("y", "long"), + new ColumnInfo("x2", "long"), + new ColumnInfo("y2", "long") + ) + ); + for (List values : getValuesList(results)) { + assertThat((Long) values.get(0), greaterThanOrEqualTo(40L)); + assertThat(values.get(1), is(values.get(0))); + assertThat(values.get(2), is(((Long) values.get(0)) + 1)); + assertThat(values.get(3), is(((Long) values.get(0)) + 2)); + } } } public void testProjectRenameEvalProject() { - EsqlQueryResponse results = run("from test | eval y = count | rename count as x | keep x, y | eval z = x + y | keep x, y, z"); - logger.info(results); - Assert.assertEquals(40, getValuesList(results).size()); - assertThat(results.columns(), contains(new ColumnInfo("x", "long"), new ColumnInfo("y", "long"), new ColumnInfo("z", "long"))); - for (List values : getValuesList(results)) { - assertThat((Long) values.get(0), greaterThanOrEqualTo(40L)); - assertThat(values.get(1), is(values.get(0))); - assertThat(values.get(2), is((Long) values.get(0) * 2)); + try (var results = run("from test | eval y = count | rename count as x | keep x, y | eval z = x + y | keep x, y, z")) { + logger.info(results); + Assert.assertEquals(40, getValuesList(results).size()); + assertThat(results.columns(), contains(new ColumnInfo("x", "long"), new ColumnInfo("y", "long"), new ColumnInfo("z", "long"))); + for (List values : getValuesList(results)) { + assertThat((Long) values.get(0), greaterThanOrEqualTo(40L)); + assertThat(values.get(1), is(values.get(0))); + assertThat(values.get(2), is((Long) values.get(0) * 2)); + } } } public void testProjectOverride() { - EsqlQueryResponse results = run("from test | eval cnt = count | rename count as data | keep cnt, data"); - logger.info(results); - Assert.assertEquals(40, getValuesList(results).size()); - assertThat(results.columns(), contains(new ColumnInfo("cnt", "long"), new ColumnInfo("data", "long"))); - for (List values : getValuesList(results)) { - assertThat(values.get(1), is(values.get(0))); + try (var results = run("from test | eval cnt = count | rename count as data | keep cnt, data")) { + logger.info(results); + Assert.assertEquals(40, getValuesList(results).size()); + assertThat(results.columns(), contains(new ColumnInfo("cnt", "long"), new ColumnInfo("data", "long"))); + for (List values : getValuesList(results)) { + assertThat(values.get(1), is(values.get(0))); + } } } @@ -708,9 +753,10 @@ public void testRefreshSearchIdleShards() throws Exception { stopped.set(true); indexingThread.join(); } - EsqlQueryResponse results = run("from test_refresh | stats s = sum(value)"); - logger.info(results); - assertThat(getValuesList(results).get(0), equalTo(List.of(totalValues.get()))); + try (EsqlQueryResponse results = run("from test_refresh | stats s = sum(value)")) { + logger.info(results); + assertThat(getValuesList(results).get(0), equalTo(List.of(totalValues.get()))); + } } public void testESFilter() throws Exception { @@ -737,16 +783,19 @@ public void testESFilter() throws Exception { long from = randomBoolean() ? Long.MIN_VALUE : randomLongBetween(-1000, 1000); long to = randomBoolean() ? Long.MAX_VALUE : randomLongBetween(from, from + 1000); QueryBuilder filter = new RangeQueryBuilder("val").from(from, true).to(to, true); - EsqlQueryResponse results = new EsqlQueryRequestBuilder(client(), EsqlQueryAction.INSTANCE).query(command) - .filter(filter) - .pragmas(randomPragmas()) - .get(); - logger.info(results); - OptionalDouble avg = docs.values().stream().filter(v -> from <= v && v <= to).mapToLong(n -> n).average(); - if (avg.isPresent()) { - assertEquals(avg.getAsDouble(), (double) getValuesList(results).get(0).get(0), 0.01d); - } else { - assertThat(getValuesList(results).get(0).get(0), nullValue()); + try ( + EsqlQueryResponse results = new EsqlQueryRequestBuilder(client(), EsqlQueryAction.INSTANCE).query(command) + .filter(filter) + .pragmas(randomPragmas()) + .get() + ) { + logger.info(results); + OptionalDouble avg = docs.values().stream().filter(v -> from <= v && v <= to).mapToLong(n -> n).average(); + if (avg.isPresent()) { + assertEquals(avg.getAsDouble(), (double) getValuesList(results).get(0).get(0), 0.01d); + } else { + assertThat(getValuesList(results).get(0).get(0), nullValue()); + } } } @@ -793,21 +842,23 @@ record Doc(long val, String tag) { } public void testEvalWithNullAndAvg() { - EsqlQueryResponse results = run("from test | eval nullsum = count_d + null | stats avg(nullsum)"); - logger.info(results); - Assert.assertEquals(1, results.columns().size()); - Assert.assertEquals(1, getValuesList(results).size()); - assertEquals("avg(nullsum)", results.columns().get(0).name()); - assertEquals("double", results.columns().get(0).type()); - assertEquals(1, getValuesList(results).get(0).size()); - assertNull(getValuesList(results).get(0).get(0)); + try (EsqlQueryResponse results = run("from test | eval nullsum = count_d + null | stats avg(nullsum)")) { + logger.info(results); + Assert.assertEquals(1, results.columns().size()); + Assert.assertEquals(1, getValuesList(results).size()); + assertEquals("avg(nullsum)", results.columns().get(0).name()); + assertEquals("double", results.columns().get(0).type()); + assertEquals(1, getValuesList(results).get(0).size()); + assertNull(getValuesList(results).get(0).get(0)); + } } public void testFromStatsLimit() { - EsqlQueryResponse results = run("from test | stats ac = avg(count) by data | limit 1"); - logger.info(results); - assertThat(results.columns(), contains(new ColumnInfo("ac", "double"), new ColumnInfo("data", "long"))); - assertThat(getValuesList(results), contains(anyOf(contains(42.0, 1L), contains(44.0, 2L)))); + try (EsqlQueryResponse results = run("from test | stats ac = avg(count) by data | limit 1")) { + logger.info(results); + assertThat(results.columns(), contains(new ColumnInfo("ac", "double"), new ColumnInfo("data", "long"))); + assertThat(getValuesList(results), contains(anyOf(contains(42.0, 1L), contains(44.0, 2L)))); + } } public void testFromLimit() { @@ -819,18 +870,20 @@ public void testFromLimit() { } public void testDropAllColumns() { - EsqlQueryResponse results = run("from test | keep data | drop data | eval a = 1"); - logger.info(results); - assertThat(results.columns(), hasSize(1)); - assertThat(results.columns(), contains(new ColumnInfo("a", "integer"))); - assertThat(getValuesList(results), is(empty())); + try (EsqlQueryResponse results = run("from test | keep data | drop data | eval a = 1")) { + logger.info(results); + assertThat(results.columns(), hasSize(1)); + assertThat(results.columns(), contains(new ColumnInfo("a", "integer"))); + assertThat(getValuesList(results), is(empty())); + } } public void testDropAllColumnsWithStats() { - EsqlQueryResponse results = run("from test | stats g = count(data) | drop g"); - logger.info(results); - assertThat(results.columns(), is(empty())); - assertThat(getValuesList(results), is(empty())); + try (EsqlQueryResponse results = run("from test | stats g = count(data) | drop g")) { + logger.info(results); + assertThat(results.columns(), is(empty())); + assertThat(getValuesList(results), is(empty())); + } } public void testIndexPatterns() throws Exception { @@ -856,35 +909,43 @@ public void testIndexPatterns() throws Exception { .get(); } - EsqlQueryResponse results = run("from test_index_patterns* | stats count(data), sum(count)"); - assertEquals(1, getValuesList(results).size()); - assertEquals(15L, getValuesList(results).get(0).get(0)); - assertEquals(120000L, getValuesList(results).get(0).get(1)); + try (var results = run("from test_index_patterns* | stats count(data), sum(count)")) { + assertEquals(1, getValuesList(results).size()); + assertEquals(15L, getValuesList(results).get(0).get(0)); + assertEquals(120000L, getValuesList(results).get(0).get(1)); + + } - results = run("from test_index_patterns_1,test_index_patterns_2 | stats count(data), sum(count)"); - assertEquals(1, getValuesList(results).size()); - assertEquals(10L, getValuesList(results).get(0).get(0)); - assertEquals(55000L, getValuesList(results).get(0).get(1)); + try (var results = run("from test_index_patterns_1,test_index_patterns_2 | stats count(data), sum(count)")) { + assertEquals(1, getValuesList(results).size()); + assertEquals(10L, getValuesList(results).get(0).get(0)); + assertEquals(55000L, getValuesList(results).get(0).get(1)); + } - results = run("from test_index_patterns_1*,test_index_patterns_2* | stats count(data), sum(count)"); - assertEquals(1, getValuesList(results).size()); - assertEquals(10L, getValuesList(results).get(0).get(0)); - assertEquals(55000L, getValuesList(results).get(0).get(1)); + try (var results = run("from test_index_patterns_1*,test_index_patterns_2* | stats count(data), sum(count)")) { + assertEquals(1, getValuesList(results).size()); + assertEquals(10L, getValuesList(results).get(0).get(0)); + assertEquals(55000L, getValuesList(results).get(0).get(1)); + } - results = run("from test_index_patterns_*,-test_index_patterns_1 | stats count(data), sum(count)"); - assertEquals(1, getValuesList(results).size()); - assertEquals(10L, getValuesList(results).get(0).get(0)); - assertEquals(105000L, getValuesList(results).get(0).get(1)); + try (var results = run("from test_index_patterns_*,-test_index_patterns_1 | stats count(data), sum(count)")) { + assertEquals(1, getValuesList(results).size()); + assertEquals(10L, getValuesList(results).get(0).get(0)); + assertEquals(105000L, getValuesList(results).get(0).get(1)); + } - results = run("from * | stats count(data), sum(count)"); - assertEquals(1, getValuesList(results).size()); - assertEquals(55L, getValuesList(results).get(0).get(0)); - assertEquals(121720L, getValuesList(results).get(0).get(1)); + try (var results = run("from * | stats count(data), sum(count)")) { + assertEquals(1, getValuesList(results).size()); + assertEquals(55L, getValuesList(results).get(0).get(0)); + assertEquals(121720L, getValuesList(results).get(0).get(1)); - results = run("from test_index_patterns_2 | stats count(data), sum(count)"); - assertEquals(1, getValuesList(results).size()); - assertEquals(5L, getValuesList(results).get(0).get(0)); - assertEquals(40000L, getValuesList(results).get(0).get(1)); + } + + try (var results = run("from test_index_patterns_2 | stats count(data), sum(count)")) { + assertEquals(1, getValuesList(results).size()); + assertEquals(5L, getValuesList(results).get(0).get(0)); + assertEquals(40000L, getValuesList(results).get(0).get(1)); + } } public void testOverlappingIndexPatterns() throws Exception { @@ -923,48 +984,52 @@ public void testOverlappingIndexPatterns() throws Exception { public void testEmptyIndex() { assertAcked(client().admin().indices().prepareCreate("test_empty").setMapping("k", "type=keyword", "v", "type=long").get()); - EsqlQueryResponse results = run("from test_empty"); - assertThat(results.columns(), equalTo(List.of(new ColumnInfo("k", "keyword"), new ColumnInfo("v", "long")))); - assertThat(getValuesList(results), empty()); + try (EsqlQueryResponse results = run("from test_empty")) { + assertThat(results.columns(), equalTo(List.of(new ColumnInfo("k", "keyword"), new ColumnInfo("v", "long")))); + assertThat(getValuesList(results), empty()); + } } public void testShowInfo() { - EsqlQueryResponse results = run("show info"); - assertThat( - results.columns(), - equalTo(List.of(new ColumnInfo("version", "keyword"), new ColumnInfo("date", "keyword"), new ColumnInfo("hash", "keyword"))) - ); - assertThat(getValuesList(results).size(), equalTo(1)); - assertThat(getValuesList(results).get(0).get(0), equalTo(Build.current().version())); - assertThat(getValuesList(results).get(0).get(1), equalTo(Build.current().date())); - assertThat(getValuesList(results).get(0).get(2), equalTo(Build.current().hash())); + try (EsqlQueryResponse results = run("show info")) { + assertThat( + results.columns(), + equalTo(List.of(new ColumnInfo("version", "keyword"), new ColumnInfo("date", "keyword"), new ColumnInfo("hash", "keyword"))) + ); + assertThat(getValuesList(results).size(), equalTo(1)); + assertThat(getValuesList(results).get(0).get(0), equalTo(Build.current().version())); + assertThat(getValuesList(results).get(0).get(1), equalTo(Build.current().date())); + assertThat(getValuesList(results).get(0).get(2), equalTo(Build.current().hash())); + } } public void testShowFunctions() { - EsqlQueryResponse results = run("show functions"); - assertThat( - results.columns(), - equalTo( - List.of( - new ColumnInfo("name", "keyword"), - new ColumnInfo("synopsis", "keyword"), - new ColumnInfo("argNames", "keyword"), - new ColumnInfo("argTypes", "keyword"), - new ColumnInfo("argDescriptions", "keyword"), - new ColumnInfo("returnType", "keyword"), - new ColumnInfo("description", "keyword"), - new ColumnInfo("optionalArgs", "boolean"), - new ColumnInfo("variadic", "boolean") + try (EsqlQueryResponse results = run("show functions")) { + assertThat( + results.columns(), + equalTo( + List.of( + new ColumnInfo("name", "keyword"), + new ColumnInfo("synopsis", "keyword"), + new ColumnInfo("argNames", "keyword"), + new ColumnInfo("argTypes", "keyword"), + new ColumnInfo("argDescriptions", "keyword"), + new ColumnInfo("returnType", "keyword"), + new ColumnInfo("description", "keyword"), + new ColumnInfo("optionalArgs", "boolean"), + new ColumnInfo("variadic", "boolean") + ) ) - ) - ); - assertThat(getValuesList(results).size(), equalTo(new EsqlFunctionRegistry().listFunctions().size())); + ); + assertThat(getValuesList(results).size(), equalTo(new EsqlFunctionRegistry().listFunctions().size())); + } } public void testInWithNullValue() { - EsqlQueryResponse results = run("from test | where null in (data, 2) | keep data"); - assertThat(results.columns(), equalTo(List.of(new ColumnInfo("data", "long")))); - assertThat(getValuesList(results).size(), equalTo(0)); + try (EsqlQueryResponse results = run("from test | where null in (data, 2) | keep data")) { + assertThat(results.columns(), equalTo(List.of(new ColumnInfo("data", "long")))); + assertThat(getValuesList(results).size(), equalTo(0)); + } } public void testTopNPushedToLucene() { @@ -1117,7 +1182,7 @@ public void testGroupingMultiValueByOrdinals() { var functions = List.of("min(v)", "max(v)", "count_distinct(v)", "count(v)", "sum(v)", "avg(v)", "percentile(v, 90)"); for (String fn : functions) { String query = String.format(Locale.ROOT, "from %s | stats s = %s by kw", indexName, fn); - run(query); + run(query).close(); } } @@ -1194,14 +1259,15 @@ private void createAlias(List indices, String alias) throws InterruptedE } private void assertNoNestedDocuments(String query, int docsCount, long minValue, long maxValue) { - EsqlQueryResponse results = run(query); - assertThat(results.columns(), contains(new ColumnInfo("data", "long"))); - assertThat(results.columns().size(), is(1)); - assertThat(getValuesList(results).size(), is(docsCount)); - for (List row : getValuesList(results)) { - assertThat(row.size(), is(1)); - // check that all the values returned are the regular ones - assertThat((Long) row.get(0), allOf(greaterThanOrEqualTo(minValue), lessThanOrEqualTo(maxValue))); + try (EsqlQueryResponse results = run(query)) { + assertThat(results.columns(), contains(new ColumnInfo("data", "long"))); + assertThat(results.columns().size(), is(1)); + assertThat(getValuesList(results).size(), is(docsCount)); + for (List row : getValuesList(results)) { + assertThat(row.size(), is(1)); + // check that all the values returned are the regular ones + assertThat((Long) row.get(0), allOf(greaterThanOrEqualTo(minValue), lessThanOrEqualTo(maxValue))); + } } } diff --git a/x-pack/plugin/esql/src/internalClusterTest/java/org/elasticsearch/xpack/esql/action/EsqlActionRuntimeFieldIT.java b/x-pack/plugin/esql/src/internalClusterTest/java/org/elasticsearch/xpack/esql/action/EsqlActionRuntimeFieldIT.java index 4a21cc5a77521..41450be131e2a 100644 --- a/x-pack/plugin/esql/src/internalClusterTest/java/org/elasticsearch/xpack/esql/action/EsqlActionRuntimeFieldIT.java +++ b/x-pack/plugin/esql/src/internalClusterTest/java/org/elasticsearch/xpack/esql/action/EsqlActionRuntimeFieldIT.java @@ -53,14 +53,16 @@ protected Collection> nodePlugins() { public void testLong() throws InterruptedException, IOException { createIndexWithConstRuntimeField("long"); - EsqlQueryResponse response = run("from test | stats sum(const)"); - assertThat(getValuesList(response), equalTo(List.of(List.of((long) SIZE)))); + try (EsqlQueryResponse response = run("from test | stats sum(const)")) { + assertThat(getValuesList(response), equalTo(List.of(List.of((long) SIZE)))); + } } public void testDouble() throws InterruptedException, IOException { createIndexWithConstRuntimeField("double"); - EsqlQueryResponse response = run("from test | stats sum(const)"); - assertThat(getValuesList(response), equalTo(List.of(List.of((double) SIZE)))); + try (EsqlQueryResponse response = run("from test | stats sum(const)")) { + assertThat(getValuesList(response), equalTo(List.of(List.of((double) SIZE)))); + } } public void testKeyword() throws InterruptedException, IOException { @@ -76,8 +78,9 @@ public void testKeyword() throws InterruptedException, IOException { */ public void testKeywordBy() throws InterruptedException, IOException { createIndexWithConstRuntimeField("keyword"); - EsqlQueryResponse response = run("from test | stats max(foo) by const"); - assertThat(getValuesList(response), equalTo(List.of(List.of(SIZE - 1L, "const")))); + try (EsqlQueryResponse response = run("from test | stats max(foo) by const")) { + assertThat(getValuesList(response), equalTo(List.of(List.of(SIZE - 1L, "const")))); + } } public void testBoolean() throws InterruptedException, IOException { @@ -89,9 +92,10 @@ public void testBoolean() throws InterruptedException, IOException { public void testDate() throws InterruptedException, IOException { createIndexWithConstRuntimeField("date"); - EsqlQueryResponse response = run(""" - from test | eval d=date_format("yyyy", const) | stats min (foo) by d"""); - assertThat(getValuesList(response), equalTo(List.of(List.of(0L, "2023")))); + try (EsqlQueryResponse response = run(""" + from test | eval d=date_format("yyyy", const) | stats min (foo) by d""")) { + assertThat(getValuesList(response), equalTo(List.of(List.of(0L, "2023")))); + } } private void createIndexWithConstRuntimeField(String type) throws InterruptedException, IOException { diff --git a/x-pack/plugin/esql/src/internalClusterTest/java/org/elasticsearch/xpack/esql/action/ManyShardsIT.java b/x-pack/plugin/esql/src/internalClusterTest/java/org/elasticsearch/xpack/esql/action/ManyShardsIT.java index c1476c8c52de5..7828ba97ed62b 100644 --- a/x-pack/plugin/esql/src/internalClusterTest/java/org/elasticsearch/xpack/esql/action/ManyShardsIT.java +++ b/x-pack/plugin/esql/src/internalClusterTest/java/org/elasticsearch/xpack/esql/action/ManyShardsIT.java @@ -63,7 +63,7 @@ public void testConcurrentQueries() throws Exception { if (canUseQueryPragmas()) { pragmas.put(randomPragmas().getSettings()).put("exchange_concurrent_clients", between(1, 2)); } - run("from test-* | stats count(user) by tags", new QueryPragmas(pragmas.build())); + run("from test-* | stats count(user) by tags", new QueryPragmas(pragmas.build())).close(); }); } for (Thread thread : threads) {