1 | /* | |
2 | * Copyright OpenSearch Contributors | |
3 | * SPDX-License-Identifier: Apache-2.0 | |
4 | */ | |
5 | ||
6 | package org.opensearch.sql.planner.physical.collector; | |
7 | ||
8 | import com.google.common.collect.ImmutableList; | |
9 | import java.util.Arrays; | |
10 | import java.util.HashMap; | |
11 | import java.util.LinkedHashMap; | |
12 | import java.util.List; | |
13 | import java.util.Map; | |
14 | import java.util.Objects; | |
15 | import java.util.function.Supplier; | |
16 | import java.util.stream.Collectors; | |
17 | import lombok.RequiredArgsConstructor; | |
18 | import org.opensearch.sql.data.model.ExprCollectionValue; | |
19 | import org.opensearch.sql.data.model.ExprTupleValue; | |
20 | import org.opensearch.sql.data.model.ExprValue; | |
21 | import org.opensearch.sql.expression.NamedExpression; | |
22 | import org.opensearch.sql.storage.bindingtuple.BindingTuple; | |
23 | ||
24 | /** | |
25 | * Collect Bucket from {@link BindingTuple}. | |
26 | */ | |
27 | @RequiredArgsConstructor | |
28 | public class BucketCollector implements Collector { | |
29 | ||
30 | /** | |
31 | * Bucket Expression. | |
32 | */ | |
33 | private final NamedExpression bucketExpr; | |
34 | ||
35 | /** | |
36 | * Collector Constructor. | |
37 | */ | |
38 | private final Supplier<Collector> supplier; | |
39 | ||
40 | /** | |
41 | * Map between bucketKey and collector in the bucket. | |
42 | */ | |
43 | private final Map<ExprValue, Collector> collectorMap = new HashMap<>(); | |
44 | ||
45 | /** | |
46 | * Bucket Index. | |
47 | */ | |
48 | private int bucketIndex = 0; | |
49 | ||
50 | /** | |
51 | * Collect Bucket from {@link BindingTuple}. | |
52 | * If bucket not exist, create new bucket and {@link Collector}. | |
53 | * If bucket exist, let {@link Collector} in the bucket collect from {@link BindingTuple}. | |
54 | * | |
55 | * @param input {@link BindingTuple}. | |
56 | */ | |
57 | @Override | |
58 | public void collect(BindingTuple input) { | |
59 | ExprValue bucketKey = bucketKey(input); | |
60 | collectorMap.putIfAbsent(bucketKey, supplier.get()); | |
61 |
1
1. collect : removed call to org/opensearch/sql/planner/physical/collector/Collector::collect → KILLED |
collectorMap.get(bucketKey).collect(input); |
62 | } | |
63 | ||
64 | /** | |
65 | * Bucket Key. | |
66 | * @param tuple {@link BindingTuple}. | |
67 | * @return Bucket Key. | |
68 | */ | |
69 | protected ExprValue bucketKey(BindingTuple tuple) { | |
70 |
1
1. bucketKey : replaced return value with null for org/opensearch/sql/planner/physical/collector/BucketCollector::bucketKey → KILLED |
return bucketExpr.valueOf(tuple); |
71 | } | |
72 | ||
73 | /** | |
74 | * Get result from all the buckets. | |
75 | * | |
76 | * @return list of {@link ExprValue}. | |
77 | */ | |
78 | @Override | |
79 | public List<ExprValue> results() { | |
80 | ExprValue[] buckets = allocateBuckets(); | |
81 | for (Map.Entry<ExprValue, Collector> entry : collectorMap.entrySet()) { | |
82 | ImmutableList.Builder<ExprValue> builder = new ImmutableList.Builder<>(); | |
83 | for (ExprValue tuple : entry.getValue().results()) { | |
84 | LinkedHashMap<String, ExprValue> tmp = new LinkedHashMap<>(); | |
85 | tmp.put(bucketExpr.getNameOrAlias(), entry.getKey()); | |
86 |
1
1. results : removed call to java/util/LinkedHashMap::putAll → KILLED |
tmp.putAll(tuple.tupleValue()); |
87 | builder.add(ExprTupleValue.fromExprValueMap(tmp)); | |
88 | } | |
89 | buckets[locateBucket(entry.getKey())] = new ExprCollectionValue(builder.build()); | |
90 | } | |
91 |
1
1. results : replaced return value with Collections.emptyList for org/opensearch/sql/planner/physical/collector/BucketCollector::results → KILLED |
return Arrays.stream(buckets) |
92 | .filter(Objects::nonNull) | |
93 |
1
1. lambda$results$0 : replaced return value with Stream.empty for org/opensearch/sql/planner/physical/collector/BucketCollector::lambda$results$0 → KILLED |
.flatMap(v -> v.collectionValue().stream()) |
94 | .collect(Collectors.toList()); | |
95 | } | |
96 | ||
97 | /** | |
98 | * Allocates Buckets for building results. | |
99 | * | |
100 | * @return buckets. | |
101 | */ | |
102 | protected ExprValue[] allocateBuckets() { | |
103 |
1
1. allocateBuckets : replaced return value with null for org/opensearch/sql/planner/physical/collector/BucketCollector::allocateBuckets → KILLED |
return new ExprValue[collectorMap.size()]; |
104 | } | |
105 | ||
106 | /** | |
107 | * Current Bucket index in allocated buckets. | |
108 | * | |
109 | * @param value bucket key. | |
110 | * @return index. | |
111 | */ | |
112 | protected int locateBucket(ExprValue value) { | |
113 |
2
1. locateBucket : Replaced integer addition with subtraction → KILLED 2. locateBucket : replaced int return with 0 for org/opensearch/sql/planner/physical/collector/BucketCollector::locateBucket → KILLED |
return bucketIndex++; |
114 | } | |
115 | } | |
Mutations | ||
61 |
1.1 |
|
70 |
1.1 |
|
86 |
1.1 |
|
91 |
1.1 |
|
93 |
1.1 |
|
103 |
1.1 |
|
113 |
1.1 2.2 |