Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ public void populateIndex() {
"memory",
"type=long,time_series_metric=gauge",
"request_count",
"type=integer,time_series_metric=counter"
"type=integer,time_series_metric=counter",
"description",
"type=text,index=true"
)
.get();
Map<String, String> hostToClusters = new HashMap<>();
Expand Down Expand Up @@ -145,7 +147,9 @@ public void populateIndex() {
"memory",
doc.memory.getBytes(),
"request_count",
doc.requestCount
doc.requestCount,
"description",
"host = " + doc.host
)
.get();
}
Expand Down Expand Up @@ -200,6 +204,23 @@ public void testImplicitAggregate() {
}
}

public void testGroupByTextField() {
List<String> sortedGroups = docs.stream().map(d -> d.host).distinct().sorted().toList();
client().admin().indices().prepareRefresh("hosts").get();
for (String fn : List.of("count", "count_distinct", "sum", "avg", "max", "min")) {
try (var resp = run("TS hosts | STATS " + fn + "(cpu) BY description | SORT description | LIMIT 1")) {
List<List<Object>> rows = EsqlTestUtils.getValuesList(resp);
assertThat(rows, hasSize(1));
assertThat(rows.get(0).get(1), equalTo("host = " + sortedGroups.get(0)));
}
}
try (var resp = run("TS hosts | STATS sum(rate(request_count)) BY description | SORT description | LIMIT 1")) {
List<List<Object>> rows = EsqlTestUtils.getValuesList(resp);
assertThat(rows, hasSize(1));
assertThat(rows.get(0).get(1), equalTo("host = " + sortedGroups.get(0)));
}
}

public void testRateWithoutGrouping() {
record RateKey(String cluster, String host) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@
package org.elasticsearch.xpack.esql.optimizer;

import org.elasticsearch.xpack.esql.common.Failures;
import org.elasticsearch.xpack.esql.core.expression.Alias;
import org.elasticsearch.xpack.esql.core.expression.Attribute;
import org.elasticsearch.xpack.esql.core.type.DataType;
import org.elasticsearch.xpack.esql.expression.function.aggregate.Values;
import org.elasticsearch.xpack.esql.optimizer.rules.physical.ProjectAwayColumns;
import org.elasticsearch.xpack.esql.plan.QueryPlan;
import org.elasticsearch.xpack.esql.plan.logical.TimeSeriesAggregate;
import org.elasticsearch.xpack.esql.plan.physical.EsQueryExec;

import java.util.List;
Expand Down Expand Up @@ -72,7 +76,13 @@ private static void verifyOutputNotChanged(QueryPlan<?> optimizedPlan, List<Attr
// We perform an optimizer run on every fragment. LookupJoinExec also contains such a fragment,
// and currently it only contains an EsQueryExec after optimization.
boolean hasLookupJoinExec = optimizedPlan instanceof EsQueryExec esQueryExec && esQueryExec.indexMode() == LOOKUP;
boolean ignoreError = hasProjectAwayColumns || hasLookupJoinExec;
// Time-series groupings are rewritten as VALUES, changing the output type from TEXT to KEYWORD.
// TODO: Grouping fields in aggregations should not have the TEXT data type.
boolean hasTextGroupingInTimeSeries = optimizedPlan.anyMatch(
a -> a instanceof TimeSeriesAggregate ts
&& ts.aggregates().stream().anyMatch(g -> Alias.unwrap(g) instanceof Values v && v.field().dataType() == DataType.TEXT)
);
boolean ignoreError = hasProjectAwayColumns || hasLookupJoinExec || hasTextGroupingInTimeSeries;
if (ignoreError == false) {
failures.add(
fail(
Expand Down