MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / grouping_sets_explicit_shapes

Function grouping_sets_explicit_shapes

nodedb/tests/sql_grouping_sets.rs:168–223  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

166/// exactly those three row-shape categories.
167#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
168async fn grouping_sets_explicit_shapes() {
169 let server = TestServer::start().await;
170 create_orders(&server).await;
171
172 let rows = server
173 .query_rows(
174 "SELECT region, country, SUM(sales) AS total \
175 FROM orders \
176 GROUP BY GROUPING SETS ((region, country), (region), ())",
177 )
178 .await
179 .unwrap();
180
181 // Set 1: (region, country) — 5 rows (US×2 merged, CA, DE, FR) but US has 2 rows
182 // so should be 4 unique combinations: US→300, CA→150, DE→300, FR→250
183 // Set 2: (region) — 2 rows: AMER→450, EMEA→550
184 // Set 3: () — 1 row: grand total 1000
185 // Total = 7
186 assert_eq!(
187 rows.len(),
188 7,
189 "expected 7 rows for explicit GROUPING SETS, got {}: {:?}",
190 rows.len(),
191 rows
192 );
193
194 // Grand-total row (both NULL).
195 let grand = rows
196 .iter()
197 .filter(|r| {
198 r.first().map(|s| s.is_empty()).unwrap_or(false)
199 && r.get(1).map(|s| s.is_empty()).unwrap_or(false)
200 })
201 .count();
202 assert_eq!(grand, 1, "expected 1 grand-total row");
203
204 // Regional rows (country NULL, region non-empty).
205 let regional = rows
206 .iter()
207 .filter(|r| {
208 r.first().map(|s| !s.is_empty()).unwrap_or(false)
209 && r.get(1).map(|s| s.is_empty()).unwrap_or(false)
210 })
211 .count();
212 assert_eq!(regional, 2, "expected 2 regional subtotals");
213
214 // Fully-grouped rows (both non-empty).
215 let detailed = rows
216 .iter()
217 .filter(|r| {
218 r.first().map(|s| !s.is_empty()).unwrap_or(false)
219 && r.get(1).map(|s| !s.is_empty()).unwrap_or(false)
220 })
221 .count();
222 assert_eq!(detailed, 4, "expected 4 (region, country) rows");
223}
224
225/// `GROUPING(col)` returns 0 for real values and 1 for NULL-filled positions.

Callers

nothing calls this directly

Calls 7

create_ordersFunction · 0.85
query_rowsMethod · 0.80
firstMethod · 0.80
countMethod · 0.45
iterMethod · 0.45
is_emptyMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected