You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@drill.apache.org by vo...@apache.org on 2021/08/05 17:23:19 UTC
[drill] 06/13: DRILL-7971: More cleanup
This is an automated email from the ASF dual-hosted git repository.
volodymyr pushed a commit to branch mongo
in repository https://gitbox.apache.org/repos/asf/drill.git
commit 7e1661837a7f317cf4dc18464b84c7b0a3391b29
Author: Volodymyr Vysotskyi <vv...@gmail.com>
AuthorDate: Wed Jul 14 19:58:31 2021 +0300
DRILL-7971: More cleanup
---
.../exec/store/mongo/MongoAggregateUtils.java | 74 +++++++++-------------
1 file changed, 29 insertions(+), 45 deletions(-)
diff --git a/contrib/storage-mongo/src/main/java/org/apache/drill/exec/store/mongo/MongoAggregateUtils.java b/contrib/storage-mongo/src/main/java/org/apache/drill/exec/store/mongo/MongoAggregateUtils.java
index f5ebb9a..e196258 100644
--- a/contrib/storage-mongo/src/main/java/org/apache/drill/exec/store/mongo/MongoAggregateUtils.java
+++ b/contrib/storage-mongo/src/main/java/org/apache/drill/exec/store/mongo/MongoAggregateUtils.java
@@ -18,27 +18,20 @@ import org.bson.BsonString;
import org.bson.Document;
import org.bson.conversions.Bson;
-import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.BiFunction;
+import java.util.stream.Collectors;
+import java.util.stream.StreamSupport;
public class MongoAggregateUtils {
public static List<String> mongoFieldNames(RelDataType rowType) {
- return SqlValidatorUtil.uniquify(
- new AbstractList<String>() {
- @Override public String get(int index) {
- final String name = rowType.getFieldList().get(index).getName();
- return name.startsWith("$") ? "_" + name.substring(2) : name;
- }
-
- @Override public int size() {
- return rowType.getFieldCount();
- }
- },
- SqlValidatorUtil.EXPR_SUGGESTER, true);
+ List<String> renamed = rowType.getFieldNames().stream()
+ .map(name -> name.startsWith("$") ? "_" + name.substring(2) : name)
+ .collect(Collectors.toList());
+ return SqlValidatorUtil.uniquify(renamed, true);
}
static String maybeQuote(String s) {
@@ -64,61 +57,52 @@ public class MongoAggregateUtils {
}
public static List<Bson> getAggregateOperations(Aggregate aggregate, RelDataType rowType) {
- List<BsonField> docList = new ArrayList<>();
List<String> inNames = mongoFieldNames(rowType);
List<String> outNames = mongoFieldNames(aggregate.getRowType());
- int i = 0;
Object id;
if (aggregate.getGroupSet().cardinality() == 1) {
String inName = inNames.get(aggregate.getGroupSet().nth(0));
id = "$" + inName;
} else {
- List<BsonElement> elements = new ArrayList<>();
- for (int group : aggregate.getGroupSet()) {
- String inName = inNames.get(group);
- elements.add(new BsonElement(inName, new BsonString("$" + inName)));
- }
+ List<BsonElement> elements =
+ StreamSupport.stream(aggregate.getGroupSet().spliterator(), false)
+ .map(inNames::get)
+ .map(inName -> new BsonElement(inName, new BsonString("$" + inName)))
+ .collect(Collectors.toList());
id = new BsonDocument(elements);
}
- i += aggregate.getGroupSet().cardinality();
+ int outNameIndex = aggregate.getGroupSet().cardinality();
+ List<BsonField> accumList = new ArrayList<>();
for (AggregateCall aggCall : aggregate.getAggCallList()) {
- docList.add(bsonAggregate(inNames, outNames.get(i++), aggCall));
+ accumList.add(bsonAggregate(inNames, outNames.get(outNameIndex++), aggCall));
}
- List<Bson> docAggList = new ArrayList<>();
- docAggList.add(Aggregates.group(id, docList).toBsonDocument());
- List<BsonElement> fixups;
+ List<Bson> operationsList = new ArrayList<>();
+ operationsList.add(Aggregates.group(id, accumList).toBsonDocument());
+ List<BsonElement> projectFields = new ArrayList<>();
if (aggregate.getGroupSet().cardinality() == 1) {
- fixups = new AbstractList<BsonElement>() {
- @Override public BsonElement get(int index) {
- String outName = outNames.get(index);
- return new BsonElement(maybeQuote(outName),
- new BsonString("$" + (index == 0 ? "_id" : outName)));
- }
-
- @Override public int size() {
- return outNames.size();
- }
- };
+ for (int index = 0; index < outNames.size(); index++) {
+ String outName = outNames.get(index);
+ projectFields.add(new BsonElement(maybeQuote(outName),
+ new BsonString("$" + (index == 0 ? "_id" : outName))));
+ }
} else {
- fixups = new ArrayList<>();
- fixups.add(new BsonElement("_id", new BsonInt32(0)));
- i = 0;
+ projectFields.add(new BsonElement("_id", new BsonInt32(0)));
for (int group : aggregate.getGroupSet()) {
- fixups.add(
+ projectFields.add(
new BsonElement(maybeQuote(outNames.get(group)),
new BsonString("$_id." + outNames.get(group))));
- ++i;
}
+ outNameIndex = aggregate.getGroupSet().cardinality();
for (AggregateCall ignored : aggregate.getAggCallList()) {
- String outName = outNames.get(i++);
- fixups.add(new BsonElement(maybeQuote(outName), new BsonString("$" + outName)));
+ String outName = outNames.get(outNameIndex++);
+ projectFields.add(new BsonElement(maybeQuote(outName), new BsonString("$" + outName)));
}
}
if (!aggregate.getGroupSet().isEmpty()) {
- docAggList.add(Aggregates.project(new BsonDocument(fixups)).toBsonDocument());
+ operationsList.add(Aggregates.project(new BsonDocument(projectFields)).toBsonDocument());
}
- return docAggList;
+ return operationsList;
}
private static BsonField bsonAggregate(List<String> inNames, String outName, AggregateCall aggCall) {