-
Notifications
You must be signed in to change notification settings - Fork 106
Feat: support agg_columns=["*"] for COUNT(*) in aggregate reconcile #2424
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
moomindani
wants to merge
6
commits into
main
Choose a base branch
from
feat/recon-aggregate-count-star-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b006f67
Feat: support agg_columns=["*"] for COUNT(*) in aggregate reconcile
moomindani 7210cae
Add unit tests for COUNT(*) aggregate reconcile support
moomindani 6d9b335
Merge remote-tracking branch 'origin/main' into feat/recon-aggregate-…
moomindani 4150170
Move COUNT(*) star handling into _normalize_agg per review
moomindani 4a579f4
Merge remote-tracking branch 'origin/main' into feat/recon-aggregate-…
moomindani 59107db
Rename _is_star arg from c to col to satisfy pylint invalid-name
moomindani File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
tests/unit/reconcile/query_builder/test_aggregate_query.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| from databricks.labs.lakebridge.reconcile.query_builder.aggregate_query import AggregateQueryBuilder | ||
| from databricks.labs.lakebridge.reconcile.recon_config import Aggregate, Table | ||
| from databricks.labs.lakebridge.transpiler.sqlglot.dialect_utils import get_dialect | ||
|
|
||
|
|
||
| def _build_table(aggregates: list[Aggregate]) -> Table: | ||
| return Table(source_name="supplier", target_name="target_supplier", aggregates=aggregates) | ||
|
|
||
|
|
||
| def test_count_star_emits_unquoted_star(fake_databricks_datasource, normalize_config_service): | ||
| """Aggregate(agg_columns=["*"], type="count") must produce COUNT(*), not COUNT(`*`).""" | ||
| table_conf = _build_table([Aggregate(agg_columns=["*"], type="count")]) | ||
| normalized = normalize_config_service.normalize_recon_table_config(table_conf) | ||
|
|
||
| rules = AggregateQueryBuilder( | ||
| normalized, [], "source", get_dialect("databricks"), fake_databricks_datasource | ||
| ).build_queries() | ||
|
|
||
| assert len(rules) == 1 | ||
| sql = rules[0].query | ||
| assert "count(*)" in sql.lower() | ||
| # Regression: the column-name normalizer must not emit COUNT(`*`) | ||
| assert "count(`*`)" not in sql.lower() | ||
| # Alias of the aggregate column survives normalization | ||
| assert "source_count_*" in sql.lower() | ||
|
|
||
|
|
||
| def test_count_star_normalized_input_emits_unquoted_star(fake_databricks_datasource, normalize_config_service): | ||
| """The same fix must hold when agg_columns is already in ansi-normalized form (`*`).""" | ||
| table_conf = _build_table([Aggregate(agg_columns=["`*`"], type="count")]) | ||
| normalized = normalize_config_service.normalize_recon_table_config(table_conf) | ||
|
|
||
| rules = AggregateQueryBuilder( | ||
| normalized, [], "target", get_dialect("databricks"), fake_databricks_datasource | ||
| ).build_queries() | ||
|
|
||
| assert len(rules) == 1 | ||
| sql = rules[0].query | ||
| assert "count(*)" in sql.lower() | ||
| assert "count(`*`)" not in sql.lower() | ||
|
|
||
|
|
||
| def test_count_star_alongside_named_column(fake_databricks_datasource, normalize_config_service): | ||
| """COUNT(*) and COUNT(<col>) must coexist in a single aggregate query.""" | ||
| table_conf = _build_table([Aggregate(agg_columns=["*", "s_acctbal"], type="count")]) | ||
| normalized = normalize_config_service.normalize_recon_table_config(table_conf) | ||
|
|
||
| rules = AggregateQueryBuilder( | ||
| normalized, [], "source", get_dialect("databricks"), fake_databricks_datasource | ||
| ).build_queries() | ||
|
|
||
| sql = rules[0].query.lower() | ||
| assert "count(*)" in sql | ||
| assert "count(`s_acctbal`)" in sql | ||
| # The * branch must not pollute the named-column branch with backticks around * | ||
| assert "count(`*`)" not in sql | ||
|
|
||
|
|
||
| def test_star_with_non_count_aggregate_is_unchanged(fake_databricks_datasource, normalize_config_service): | ||
| """The fast-path must only apply to type='count'. Other aggregates keep the existing behavior.""" | ||
| table_conf = _build_table([Aggregate(agg_columns=["*"], type="sum")]) | ||
| normalized = normalize_config_service.normalize_recon_table_config(table_conf) | ||
|
|
||
| rules = AggregateQueryBuilder( | ||
| normalized, [], "source", get_dialect("databricks"), fake_databricks_datasource | ||
| ).build_queries() | ||
|
|
||
| sql = rules[0].query.lower() | ||
| # Existing path quotes the identifier; we must not silently turn this into sum(*) | ||
| assert "sum(*)" not in sql | ||
| assert "sum(`*`)" in sql |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I reviewed this in the original PR