Skip to content

fix(supabase-wrappers): skip aggregate pushdown when WHERE is not fully pushed - #679

Closed
hsusul wants to merge 1 commit into
supabase:mainfrom
hsusul:fix/agg-pushdown-local-conds
Closed

hsusul wants to merge 1 commit into
supabase:mainfrom
hsusul:fix/agg-pushdown-local-conds

Conversation

@hsusul

@hsusul hsusul commented Nov 8, 2026

Copy link
Copy Markdown

Why

SELECT count(*) FROM ft WHERE <cond> can return the count of the whole table when <cond> is not one the framework turns into a Qual. The aggregate is pushed down anyway, it replaces the scan, and nothing re-checks the condition. Grouping sets are pushed down as one group the same way. Both give a wrong number with no error.

Fixes #659.

What changed

get_foreign_upper_paths now skips aggregate pushdown when:

  • any clause in the input relation's baserestrictinfo was not extracted into state.quals. extract_quals makes at most one Qual per RestrictInfo, so comparing the two lengths checks this exactly. A plain WHERE id = 1 is still pushed down.
  • the query has grouping sets.

postgres_fdw refuses both cases the same way in foreign_grouping_ok.

Tests

New cases in clickhouse_aggregate_pushdown_test:

  • COUNT(*) ... WHERE name || '' = 'alice' returns 2 and runs as a local aggregate. On main it returns 4.
  • GROUP BY GROUPING SETS ((), ()) yields two rows of 4. On main it yields one row.

Run locally on pg15 against a native ClickHouse server:

  • cargo pgrx test --features "clickhouse_fdw pg15" pg15: 19 passed
  • cargo test -p supabase-wrappers --no-default-features --features pg15 --lib: 36 passed
  • RUSTFLAGS="-D warnings" cargo clippy --tests --no-deps --features "clickhouse_fdw pg15" in wrappers/: clean
  • cargo fmt --check -p supabase-wrappers -p wrappers: clean

Not run: the other native FDW suites.

…ly pushed

A pushed-down aggregate replaces the scan, so nothing re-checks restrictions
that were not extracted as quals, and the FDW aggregates rows the WHERE clause
should have removed. Grouping sets were also pushed down as a single group.

Only push down when every baserestrictinfo clause became a qual and the query
has no grouping sets.

Fixes supabase#659
@hsusul
hsusul requested a review from a team as a code owner November 8, 2026 23:12
@hsusul

hsusul commented Nov 8, 2026

Copy link
Copy Markdown
Author

Closing as a duplicate of #675, which covers the same WHERE and GROUPING SETS checks and was opened first. #680 (the double free from #660) is separate and stays open.

@hsusul hsusul closed this Nov 8, 2026
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.

Aggregate pushdown ignores the WHERE clause and GROUPING SETS: wrong results

1 participant