Skip to content

Fix HAS(...) == 1 breaking multi-level aggregations (#575) - #589

Merged
hadia206 merged 9 commits into
mainfrom
Hadia/fix_has
Oct 7, 2026
Merged

hadia206 merged 9 commits into
mainfrom
Hadia/fix_has

Conversation

@hadia206

@hadia206 hadia206 commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Linked ticket

Closes #575

Type of change

  • Bug fix
  • New feature
  • Refactor
  • Docs / config

What changed and why?

HAS(...) == 1 broke multi-level aggregationson relationships marked "always matches": false, producing invalid SQL with a malformed, doubly-nested SUM(SUM(...)).

Root cause: an optimizer pass (pull_aggregates_above_joins) assumed an aggregate's input was always raw data, but in this case it was actually an already-aggregated result so it got aggregated a second time.

Fix: changed how that pass adds an aggregate's input into the new plan, it now references the input's output columns by name.

How I tested this?

  • Added a reproducible test against a new Chinook test dataset from the issue.

Notes for reviewers

@hadia206
hadia206 marked this pull request as ready for review October 6, 2026 18:10
@hadia206
hadia206 requested review from a team, john-sanchez31, juankx-bodo and knassre-bodo and removed request for a team October 6, 2026 18:10
@@ -1,10 +1,10 @@
ROOT(columns=[('name', anything_c_name), ('avg_diff', avg_day_diff)], orderings=[(avg_day_diff):desc_last], limit=5:numeric)
AGGREGATE(keys={'c_custkey': o_custkey}, aggregations={'anything_c_name': ANYTHING(c_name), 'avg_day_diff': AVG(day_diff)})
PROJECT(columns={'c_name': c_name, 'day_diff': DATEDIFF('days':string, PREV(args=[o_orderdate], partition=[o_custkey], order=[(o_orderdate):asc_last]), o_orderdate), 'o_custkey': o_custkey})

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The bug fix allowed the optimization to be be correctly applied to these 2 queries, producing more efficient plans for them.

@john-sanchez31 john-sanchez31 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Just couple of comments below

Comment thread tests/test_metadata/chinook_graph.json Outdated
"Customer Churn Risk / Recency": "The elapsed time in days between the current date and a customer's most recent invoice_date."
}
}
] No newline at end of file

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a newline here

new_join_columns[join_name] = add_input_name(col_expr, agg_alias)
new_join_columns[join_name] = ColumnReference(
col_name, col_expr.data_type, agg_alias
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a comment about this change?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a comment already in the code, above the loop.

@hadia206
hadia206 merged commit 4867111 into main Oct 7, 2026
20 checks passed
@hadia206
hadia206 deleted the Hadia/fix_has branch October 7, 2026 18:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

HAS(...) == 1 breaks multi-level aggregations when always matches: false

3 participants