Skip to content
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 @@ -84,6 +84,26 @@ public Double getDouble(String alias) {
}
}

/**
* Returns a result value for the given alias.
*
* @param alias A custom alias provided in the query or an autogenerated alias in the form of
* 'property_\d'
* @return An aggregation result value as a {@link Number} for the given alias.
*/
public Number getNumber(String alias) {
Value<?> value = properties.get(alias);
switch (value.getType()) {
case LONG:
return (Long) value.get();
case DOUBLE:
return (Double) value.get();
default:
throw new RuntimeException(
String.format("Unsupported type %s received for alias '%s'.", value.getType(), alias));
}
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,23 @@ public void shouldThrowRuntimeExceptionOnUnknownTypes() {
RuntimeException e2 =
assertThrows(RuntimeException.class, () -> aggregationResult.getDouble("qty_avg"));
assertThat(e2.getMessage()).isEqualTo("Unsupported type BOOLEAN received for alias 'qty_avg'.");

RuntimeException e3 =
assertThrows(RuntimeException.class, () -> aggregationResult.getNumber("qty_avg"));
assertThat(e3.getMessage()).isEqualTo("Unsupported type BOOLEAN received for alias 'qty_avg'.");
}

@Test
public void shouldGetDoubleAggregatedResultValueAsNumber() {
AggregationResult aggregationResult =
new AggregationResult(ImmutableMap.of("qty_avg", DoubleValue.of(45.9322)));
assertThat(aggregationResult.getNumber("qty_avg")).isEqualTo(45.9322);
}

@Test
public void shouldGetLongAggregatedResultValueAsNumber() {
AggregationResult aggregationResult =
new AggregationResult(ImmutableMap.of("count", LongValue.of(50)));
assertThat(aggregationResult.getNumber("count")).isEqualTo(50);
}
}