You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
1. Support evaluation of IFNULL/COALESCE expressions over merged aggregation results - [#38990](https://github.com/apache/shardingsphere/pull/38990)
75
+
1. Support evaluation of IFNULL/COALESCE scalar wrappers (with literal or nested-aggregation fallbacks) over merged aggregation results - [#38990](https://github.com/apache/shardingsphere/pull/38990)
Copy file name to clipboardExpand all lines: docs/document/content/reference/sharding/merge.en.md
+18Lines changed: 18 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -67,6 +67,14 @@ Stream group-by merger is different from order-by merger only in two aspects:
67
67
1. It will take out all the data with the same group item from multiple data result sets at once.
68
68
1. It carried out the aggregation calculation according to the aggregation function type.
69
69
70
+
Expression-derived aggregations
71
+
72
+
In many real-world queries, aggregation functions are wrapped inside scalar expressions or conditional functions (for example, `IFNULL(SUM(score), 0)` or `COALESCE(COUNT(id), 0)`). The merger engine now natively evaluates such expression-derived aggregations during both stream and memory merges. Expression nodes that wrap aggregation calls are recognized and the underlying aggregation is computed as part of the usual merge flow; surrounding scalar or conditional expressions are then applied to the aggregated result so that semantics (null coalescing, defaulting, arithmetic, etc.) are preserved.
73
+
74
+
Proper empty / no-route initializations
75
+
76
+
When a group has no matching rows (including cases where a shard has no route for the query), expression-derived aggregations that depend on counts are initialized safely. In particular, `COUNT` cells are initialized to `0` (rather than relying on generic defaults) so that subsequent scalar expressions (e.g., `IFNULL`, `COALESCE`, or arithmetic) produce correct and deterministic results for empty or unrouted groups.
77
+
70
78
For the inconsistency between the grouping item and ordering item, it requires uploading all the data to the memory to group and aggregate, since the relevant data value needed to acquire group information is not continuous, and stream merger is not available. For example, acquire each examinee’s total score through the following SQL and order them from the highest to the lowest:
71
79
72
80
```sql
@@ -87,6 +95,16 @@ The sum aggregation function refers to `SUM` and `COUNT`. They need to sum up al
87
95
88
96
The average aggregation function refers only to `AVG`. It must be calculated through `SUM` and `COUNT` rewritten by SQL, which has been mentioned in the SQL rewriting section.
89
97
98
+
## Performance / Memory Optimization
99
+
100
+
Zero-copy projection pass-through
101
+
102
+
The group-by merger avoids creating redundant collection wrappers (for example, copying expanded projection results into new `ArrayList` instances) when evaluating projections during merges. Instead, the merger streams directly against existing projection context references and iterates over the original projection objects where possible. This zero-copy pass-through reduces temporary allocations and keeps the garbage collection profile low during large merges.
103
+
104
+
Elimination of quadratic overhead
105
+
106
+
Previous implementations performed repeated linked-list scans when resolving projections across expanded projection sets, which could lead to quadratic (`O(N^2)`) behavior for large projection counts. Those linked-list scans have been replaced by optimized linear lookup mappings (e.g., direct index maps or hash-based lookups) so projection evaluation now completes in linear time relative to the number of projections.
107
+
90
108
## Pagination Merger
91
109
92
110
All the merger types above can be paginated. Pagination is the decorator added to other kinds of mergers. ShardingSphere strengthens its ability to paginate the data result set through decorator mode. The pagination merger is responsible for filtering unnecessary data.
Copy file name to clipboardExpand all lines: features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/merge/dql/groupby/AggregationWrapperExpressionEvaluator.java
Copy file name to clipboardExpand all lines: features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/merge/dql/groupby/GroupByMemoryMergedResult.java
0 commit comments