diff --git a/processing/src/main/java/org/apache/druid/query/groupby/GroupByQueryQueryToolChest.java b/processing/src/main/java/org/apache/druid/query/groupby/GroupByQueryQueryToolChest.java index b5712ebdda38..078d1bb18da1 100644 --- a/processing/src/main/java/org/apache/druid/query/groupby/GroupByQueryQueryToolChest.java +++ b/processing/src/main/java/org/apache/druid/query/groupby/GroupByQueryQueryToolChest.java @@ -47,6 +47,7 @@ import org.apache.druid.query.FrameSignaturePair; import org.apache.druid.query.IterableRowsCursorHelper; import org.apache.druid.query.Query; +import org.apache.druid.query.QueryContexts; import org.apache.druid.query.QueryDataSource; import org.apache.druid.query.QueryPlus; import org.apache.druid.query.QueryResourceId; @@ -271,6 +272,9 @@ private Sequence mergeGroupByResultsWithoutPushDown( if (((QueryDataSource) dataSource).getQuery().getContext() != null) { subqueryContext.putAll(((QueryDataSource) dataSource).getQuery().getContext()); } + if (canPerformSubquery(((QueryDataSource) dataSource).getQuery())) { + subqueryContext.put(QueryContexts.FINALIZE_KEY, true); + } subqueryContext.put(GroupByQuery.CTX_KEY_SORT_BY_DIMS_FIRST, false); subquery = (GroupByQuery) ((QueryDataSource) dataSource).getQuery().withOverriddenContext(subqueryContext);