Skip to content

Commit eb83918

Browse files
committed
Preserve alias-based group by dims
1 parent 680e25a commit eb83918

File tree

2 files changed

+65
-5
lines changed

2 files changed

+65
-5
lines changed

test/sql/measures.test

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,34 @@ FROM daily_orders_v
761761
2 320.0 840.0
762762
3 270.0 840.0
763763

764+
# =============================================================================
765+
# Test: GROUP BY alias for expression dimension
766+
# =============================================================================
767+
768+
statement ok
769+
CREATE TABLE monthly_sales (order_date DATE, region TEXT, amount DOUBLE);
770+
771+
statement ok
772+
INSERT INTO monthly_sales VALUES
773+
('2023-01-05', 'US', 100), ('2023-01-12', 'EU', 50),
774+
('2023-02-03', 'US', 200), ('2023-02-20', 'EU', 20);
775+
776+
statement ok
777+
CREATE VIEW monthly_sales_v AS
778+
SELECT DATE_TRUNC('month', order_date) AS month, region, SUM(amount) AS MEASURE revenue
779+
FROM monthly_sales
780+
GROUP BY DATE_TRUNC('month', order_date), region;
781+
782+
query IIRR rowsort
783+
SEMANTIC SELECT month, region, AGGREGATE(revenue), AGGREGATE(revenue) AT (ALL region) AS month_total
784+
FROM monthly_sales_v
785+
;
786+
----
787+
2023-01-01 EU 50.0 150.0
788+
2023-01-01 US 100.0 150.0
789+
2023-02-01 EU 20.0 220.0
790+
2023-02-01 US 200.0 220.0
791+
764792
# =============================================================================
765793
# Test: Multi-fact JOINs (wide tables)
766794
# =============================================================================

yardstick-rs/src/sql/measures.rs

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,6 +1054,7 @@ fn group_by_matches_view(outer_cols: &[String], view_cols: &[String]) -> bool {
10541054
fn filter_group_by_cols_for_measure(
10551055
outer_cols: &[String],
10561056
view_cols: &[String],
1057+
dimension_exprs: &HashMap<String, String>,
10571058
) -> Vec<String> {
10581059
if view_cols.is_empty() {
10591060
return outer_cols.to_vec();
@@ -1064,9 +1065,34 @@ fn filter_group_by_cols_for_measure(
10641065
.map(|col| normalize_group_by_col(col))
10651066
.collect();
10661067

1068+
let mut alias_expr_norms: Vec<(String, String)> = Vec::new();
1069+
alias_expr_norms.reserve(dimension_exprs.len());
1070+
for (alias, expr) in dimension_exprs.iter() {
1071+
alias_expr_norms.push((
1072+
normalize_group_by_col(alias),
1073+
normalize_group_by_col(expr),
1074+
));
1075+
}
1076+
10671077
outer_cols
10681078
.iter()
1069-
.filter(|col| view_set.contains(&normalize_group_by_col(col)))
1079+
.filter(|col| {
1080+
let normalized_outer = normalize_group_by_col(col);
1081+
if view_set.contains(&normalized_outer) {
1082+
return true;
1083+
}
1084+
1085+
if let Some(expr) = dimension_exprs.get(&normalized_outer) {
1086+
let normalized_expr = normalize_group_by_col(expr);
1087+
if view_set.contains(&normalized_expr) {
1088+
return true;
1089+
}
1090+
}
1091+
1092+
alias_expr_norms.iter().any(|(alias_norm, expr_norm)| {
1093+
expr_norm == &normalized_outer && view_set.contains(alias_norm)
1094+
})
1095+
})
10701096
.cloned()
10711097
.collect()
10721098
}
@@ -3436,8 +3462,11 @@ pub fn expand_aggregate_with_at(sql: &str) -> AggregateExpandResult {
34363462
for (measure_name, modifiers, start, end) in patterns {
34373463
// Look up which view contains this measure (for JOIN support)
34383464
let resolved = resolve_measure_source(&measure_name, &primary_table_name);
3439-
let measure_group_by_cols =
3440-
filter_group_by_cols_for_measure(&group_by_cols, &resolved.view_group_by_cols);
3465+
let measure_group_by_cols = filter_group_by_cols_for_measure(
3466+
&group_by_cols,
3467+
&resolved.view_group_by_cols,
3468+
&resolved.dimension_exprs,
3469+
);
34413470

34423471
// Non-decomposable measures are recomputed from base rows (including AT modifiers)
34433472

@@ -3551,8 +3580,11 @@ pub fn expand_aggregate_with_at(sql: &str) -> AggregateExpandResult {
35513580

35523581
for (measure_name, start, end) in plain_calls {
35533582
let resolved = resolve_measure_source(&measure_name, &primary_table_name);
3554-
let measure_group_by_cols =
3555-
filter_group_by_cols_for_measure(&group_by_cols, &resolved.view_group_by_cols);
3583+
let measure_group_by_cols = filter_group_by_cols_for_measure(
3584+
&group_by_cols,
3585+
&resolved.view_group_by_cols,
3586+
&resolved.dimension_exprs,
3587+
);
35563588

35573589

35583590
// For derived measures, use the expanded expression; otherwise use AGG(measure_name)

0 commit comments

Comments
 (0)