Skip to content

Commit 3ac29a7

Browse files
opensearch-trigger-bot[bot]github-actions[bot]sandeshkr419
authored
[Backport 3.3] [Bug Fix] [Star Tree] Fix sub-aggregator casting for search with profile=true (#19669)
* [Bug Fix] [Star Tree] Fix sub-aggregator casting for search with profile=true (#19652) * fix sub-aggregator casting when used with profile=true Signed-off-by: Sandesh Kumar <[email protected]> --------- Signed-off-by: Sandesh Kumar <[email protected]> (cherry picked from commit b9781c1) Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> * fix changelog Removed several fixed issues from CHANGELOG. Signed-off-by: Sandesh Kumar <[email protected]> --------- Signed-off-by: Sandesh Kumar <[email protected]> Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: Sandesh Kumar <[email protected]>
1 parent 66965c6 commit 3ac29a7

File tree

10 files changed

+39
-34
lines changed

10 files changed

+39
-34
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
1010

1111
### Fixed
1212
- Fix issue with updating core with a patch number other than 0 ([#19377](https://github.com/opensearch-project/OpenSearch/pull/19377))
13+
- [Star Tree] Fix sub-aggregator casting for search with profile=true ([19652](https://github.com/opensearch-project/OpenSearch/pull/19652))
1314

1415
### Dependencies
1516

server/src/main/java/org/opensearch/search/aggregations/Aggregator.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,15 @@ public BucketComparator bucketComparator(String key, SortOrder order) {
170170
);
171171
}
172172

173+
/**
174+
* Returns the underlying Aggregator responsible for creating the bucket collector.
175+
* For most aggregators, this is the aggregator itself.
176+
* For wrappers like ProfilingAggregator, it's the delegate.
177+
*/
178+
public Aggregator unwrapAggregator() {
179+
return this;
180+
}
181+
173182
/**
174183
* Compare two buckets by their ordinal.
175184
*

server/src/main/java/org/opensearch/search/aggregations/bucket/histogram/DateHistogramAggregator.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,9 @@ public StarTreeBucketCollector getStarTreeBucketCollector(
335335
@Override
336336
public void setSubCollectors() throws IOException {
337337
for (Aggregator aggregator : subAggregators) {
338-
this.subCollectors.add(((StarTreePreComputeCollector) aggregator).getStarTreeBucketCollector(ctx, starTree, this));
338+
this.subCollectors.add(
339+
((StarTreePreComputeCollector) aggregator.unwrapAggregator()).getStarTreeBucketCollector(ctx, starTree, this)
340+
);
339341
}
340342
}
341343

server/src/main/java/org/opensearch/search/aggregations/bucket/range/RangeAggregator.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,9 @@ public StarTreeBucketCollector getStarTreeBucketCollector(
401401
@Override
402402
public void setSubCollectors() throws IOException {
403403
for (Aggregator aggregator : subAggregators) {
404-
this.subCollectors.add(((StarTreePreComputeCollector) aggregator).getStarTreeBucketCollector(ctx, starTree, this));
404+
this.subCollectors.add(
405+
((StarTreePreComputeCollector) aggregator.unwrapAggregator()).getStarTreeBucketCollector(ctx, starTree, this)
406+
);
405407
}
406408
}
407409

server/src/main/java/org/opensearch/search/aggregations/bucket/terms/GlobalOrdinalsStringTermsAggregator.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,9 @@ public StarTreeBucketCollector getStarTreeBucketCollector(
361361
@Override
362362
public void setSubCollectors() throws IOException {
363363
for (Aggregator aggregator : subAggregators) {
364-
this.subCollectors.add(((StarTreePreComputeCollector) aggregator).getStarTreeBucketCollector(ctx, starTree, this));
364+
this.subCollectors.add(
365+
((StarTreePreComputeCollector) aggregator.unwrapAggregator()).getStarTreeBucketCollector(ctx, starTree, this)
366+
);
365367
}
366368
}
367369

server/src/main/java/org/opensearch/search/aggregations/bucket/terms/MultiTermsAggregator.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -311,9 +311,9 @@ public StarTreeBucketCollector getStarTreeBucketCollector(
311311
@Override
312312
public void setSubCollectors() throws IOException {
313313
for (Aggregator aggregator : subAggregators) {
314-
if (aggregator instanceof StarTreePreComputeCollector collector) {
315-
this.subCollectors.add(collector.getStarTreeBucketCollector(ctx, starTree, this));
316-
}
314+
this.subCollectors.add(
315+
((StarTreePreComputeCollector) aggregator.unwrapAggregator()).getStarTreeBucketCollector(ctx, starTree, this)
316+
);
317317
}
318318
}
319319

server/src/main/java/org/opensearch/search/aggregations/bucket/terms/NumericTermsAggregator.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,9 @@ public StarTreeBucketCollector getStarTreeBucketCollector(
191191
@Override
192192
public void setSubCollectors() throws IOException {
193193
for (Aggregator aggregator : subAggregators) {
194-
this.subCollectors.add(((StarTreePreComputeCollector) aggregator).getStarTreeBucketCollector(ctx, starTree, this));
194+
this.subCollectors.add(
195+
((StarTreePreComputeCollector) aggregator.unwrapAggregator()).getStarTreeBucketCollector(ctx, starTree, this)
196+
);
195197
}
196198
}
197199

server/src/main/java/org/opensearch/search/aggregations/support/AggregationPath.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
import org.opensearch.search.aggregations.InternalAggregations;
4141
import org.opensearch.search.aggregations.bucket.SingleBucketAggregator;
4242
import org.opensearch.search.aggregations.metrics.NumericMetricsAggregator;
43-
import org.opensearch.search.profile.aggregation.ProfilingAggregator;
4443
import org.opensearch.search.sort.SortOrder;
4544

4645
import java.util.ArrayList;
@@ -236,7 +235,7 @@ public Aggregator resolveAggregator(Aggregator root) {
236235
public Aggregator resolveTopmostAggregator(Aggregator root) {
237236
AggregationPath.PathElement token = pathElements.get(0);
238237
// TODO both unwrap and subAggregator are only used here!
239-
Aggregator aggregator = ProfilingAggregator.unwrap(root.subAggregator(token.name));
238+
Aggregator aggregator = root.subAggregator(token.name).unwrapAggregator();
240239
assert (aggregator instanceof SingleBucketAggregator) || (aggregator instanceof NumericMetricsAggregator)
241240
: "this should be picked up before aggregation execution - on validate";
242241
return aggregator;

server/src/main/java/org/opensearch/search/profile/aggregation/ProfilingAggregator.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public class ProfilingAggregator extends Aggregator implements Streamable {
5656
private final AggregationProfiler profiler;
5757
private AggregationProfileBreakdown profileBreakdown;
5858

59-
public ProfilingAggregator(Aggregator delegate, AggregationProfiler profiler) throws IOException {
59+
public ProfilingAggregator(Aggregator delegate, AggregationProfiler profiler) {
6060
this.profiler = profiler;
6161
this.delegate = delegate;
6262
}
@@ -158,19 +158,13 @@ public String toString() {
158158
return delegate.toString();
159159
}
160160

161-
public static Aggregator unwrap(Aggregator agg) {
162-
if (agg instanceof ProfilingAggregator) {
163-
return ((ProfilingAggregator) agg).delegate;
164-
}
165-
return agg;
166-
}
167-
168-
public Aggregator getDelegate() {
169-
return delegate;
170-
}
171-
172161
@Override
173162
public StreamingCostMetrics getStreamingCostMetrics() {
174163
return delegate instanceof Streamable ? ((Streamable) delegate).getStreamingCostMetrics() : StreamingCostMetrics.nonStreamable();
175164
}
165+
166+
@Override
167+
public Aggregator unwrapAggregator() {
168+
return delegate;
169+
}
176170
}

server/src/main/java/org/opensearch/search/streaming/FlushModeResolver.java

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -139,19 +139,13 @@ private static StreamingCostMetrics collectMetrics(Collector collector) {
139139
}
140140

141141
private static Collector[] getChildren(Collector collector) {
142-
if (collector instanceof AggregatorBase) {
143-
return ((AggregatorBase) collector).subAggregators();
144-
}
145-
if (collector instanceof MultiCollector) {
146-
return ((MultiCollector) collector).getCollectors();
147-
}
148-
if (collector instanceof MultiBucketCollector) {
149-
return ((MultiBucketCollector) collector).getCollectors();
150-
}
151-
if (collector instanceof ProfilingAggregator) {
152-
return getChildren(((ProfilingAggregator) collector).getDelegate());
153-
}
154-
return new Collector[0];
142+
return switch (collector) {
143+
case AggregatorBase aggregatorBase -> aggregatorBase.subAggregators();
144+
case MultiCollector multiCollector -> multiCollector.getCollectors();
145+
case MultiBucketCollector multiBucketCollector -> multiBucketCollector.getCollectors();
146+
case ProfilingAggregator profilingAggregator -> getChildren(profilingAggregator.unwrapAggregator());
147+
default -> new Collector[0];
148+
};
155149
}
156150

157151
/**

0 commit comments

Comments
 (0)