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

Bug fix, order by doesn't work when group by field has alias #766

Merged
merged 1 commit into from
Oct 5, 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 @@ -501,6 +501,31 @@ public void orderByAliasDescTest() throws IOException {
rows(493));
}

@Test
public void orderByGroupFieldWithAlias() throws IOException {
// ORDER BY field name
JSONObject response = executeJdbcRequest(String.format("SELECT gender as g, COUNT(*) as count "
+ "FROM %s GROUP BY gender ORDER BY gender", TEST_INDEX_ACCOUNT));

verifySchema(response,
schema("g", "g", "text"),
schema("count", "count", "integer"));
verifyDataRowsInOrder(response,
rows("f", 493),
rows("m", 507));

// ORDER BY field alias
response = executeJdbcRequest(String.format("SELECT gender as g, COUNT(*) as count "
+ "FROM %s GROUP BY gender ORDER BY g", TEST_INDEX_ACCOUNT));

verifySchema(response,
schema("g", "g", "text"),
schema("count", "count", "integer"));
verifyDataRowsInOrder(response,
rows("f", 493),
rows("m", 507));
}

@Test
public void limitTest() throws IOException {
JSONObject response = executeJdbcRequest(String.format("SELECT COUNT(*) FROM %s " +
Expand Down
1 change: 1 addition & 0 deletions integ-test/src/test/resources/correctness/bugfixes/765.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT Carrier AS c, COUNT(*) AS count FROM kibana_sample_data_flights GROUP BY Carrier ORDER BY Carrier
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@

public class AggMaker {

/**
* The mapping bettwen group fieldName or Alias to the KVValue.
*/
private Map<String, KVValue> groupMap = new HashMap<>();
private Where where;

Expand Down Expand Up @@ -108,7 +111,11 @@ public AggregationBuilder makeGroupAgg(Field field) throws SqlParseException {
} else {
String termName = (Strings.isNullOrEmpty(field.getAlias())) ? field.getName() : field.getAlias();
TermsAggregationBuilder termsBuilder = AggregationBuilders.terms(termName).field(field.getName());
groupMap.put(termName, new KVValue("KEY", termsBuilder));
final KVValue kvValue = new KVValue("KEY", termsBuilder);
groupMap.put(termName, kvValue);
// map the field name with KVValue if it is not yet. The use case is when alias exist,
// the termName is different with fieldName, both of them should be included in the map.
groupMap.putIfAbsent(field.getName(), kvValue);
return termsBuilder;
}
}
Expand Down