Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Fix bug, support multiple aggregation #771

Merged
merged 1 commit into from
Oct 14, 2020
Merged
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 @@ -19,6 +19,7 @@

import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -43,6 +44,7 @@ public class ElasticsearchAggregationResponseParser {
public static List<Map<String, Object>> parse(Aggregations aggregations) {
List<Aggregation> aggregationList = aggregations.asList();
ImmutableList.Builder<Map<String, Object>> builder = new ImmutableList.Builder<>();
Map<String, Object> noBucketMap = new HashMap<>();

for (Aggregation aggregation : aggregationList) {
if (aggregation instanceof CompositeAggregation) {
Expand All @@ -51,11 +53,12 @@ public static List<Map<String, Object>> parse(Aggregations aggregations) {
builder.add(parse(bucket));
}
} else {
builder.add(parseInternal(aggregation));
noBucketMap.putAll(parseInternal(aggregation));
}

}
return builder.build();
// Todo, there is no better way to difference the with/without bucket from aggregations result.
return noBucketMap.isEmpty() ? builder.build() : Collections.singletonList(noBucketMap);
}

private static Map<String, Object> parse(CompositeAggregation.Bucket bucket) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ void no_bucket_two_metric_should_pass() {
+ " }\n"
+ "}";
assertThat(parse(response),
containsInAnyOrder(entry("max", 40d), entry("min", 20d)));
contains(entry("max", 40d,"min", 20d)));
}

@Test
Expand Down Expand Up @@ -157,4 +157,8 @@ public List<Map<String, Object>> parse(String json) {
public Map<String, Object> entry(String name, Object value) {
return ImmutableMap.of(name, value);
}

public Map<String, Object> entry(String name, Object value, String name2, Object value2) {
return ImmutableMap.of(name, value, name2, value2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,17 @@ public void testGroupByNullValue() throws IOException {
);
}

//Todo. The column of agg function is in random order. This is because we create the project
// all operator from the symbol table which can't maintain the original column order.
@Test
public void testMultipleAggregationFunction() throws IOException {
JSONObject response = executeQuery(String.format(
"source=%s | stats min(age), max(age)",
TEST_INDEX_ACCOUNT));
verifySchema(response, schema("min(age)", null, "long"),
schema("max(age)", null, "long"));
verifyDataRows(response, rows(20, 40));
}

@Test
public void testStatsWithNull() throws IOException {
Expand Down