in calcite-adapter/src/main/java/software/amazon/documentdb/jdbc/calcite/adapter/DocumentDbAggregate.java [212:252]
private static String toMongo(final SqlAggFunction aggregation, final List<String> inNames,
final List<Integer> args, final boolean isDistinct) {
// Apart from COUNT(*) which has 0 arguments, supported aggregations should be a called with only 1 argument.
if (!(args.isEmpty() && aggregation == SqlStdOperatorTable.COUNT) && args.size() != 1) {
throw new AssertionError("aggregate with incorrect number of arguments: " + aggregation);
}
// For distinct calls, add to a set and get aggregate after.
if (isDistinct) {
assert args.size() == 1;
final String inName = inNames.get(args.get(0));
return "{$addToSet: " + maybeQuote("$" + inName) + "}";
}
if (aggregation == SqlStdOperatorTable.COUNT) {
if (args.isEmpty()) {
return "{$sum: 1}";
} else {
final String inName = inNames.get(args.get(0));
return "{$sum: {$cond: [ {$gt: " + "[" + maybeQuote("$" + inName) + ", null]}, 1, 0]}}";
}
} else if (aggregation == SqlStdOperatorTable.SUM) {
final String inName = inNames.get(args.get(0));
return "{$push: " + maybeQuote("$" + inName) + "}";
} else if (aggregation == SqlStdOperatorTable.SUM0) {
final String inName = inNames.get(args.get(0));
return "{$sum: " + maybeQuote("$" + inName) + "}";
} else if (aggregation == SqlStdOperatorTable.MIN) {
final String inName = inNames.get(args.get(0));
return "{$min: " + maybeQuote("$" + inName) + "}";
} else if (aggregation == SqlStdOperatorTable.MAX) {
final String inName = inNames.get(args.get(0));
return "{$max: " + maybeQuote("$" + inName) + "}";
} else if (aggregation == SqlStdOperatorTable.AVG) {
final String inName = inNames.get(args.get(0));
return "{$avg: " + maybeQuote("$" + inName) + "}";
} else {
throw new AssertionError("unknown aggregate " + aggregation);
}
}