Skip to content

fix: skip aggregate pushdown when a WHERE clause stays local - #653

Closed
breken-ai wants to merge 1 commit into
supabase:mainfrom
breken-ai:fix/agg-pushdown-skips-local-quals
Closed

breken-ai wants to merge 1 commit into
supabase:mainfrom
breken-ai:fix/agg-pushdown-skips-local-quals

Conversation

@breken-ai

Copy link
Copy Markdown

What kind of change does this PR introduce?

Bug fix (aggregate pushdown, supabase-wrappers/src/upper.rs).

What is the current behavior?

When the framework can't turn a WHERE clause into a Qual, Postgres normally evaluates it locally on each scanned row. Examples are lower(name) = 'x', a > b, or any function or expression on the column side.

get_foreign_upper_paths offers the aggregate path without checking for such clauses. That path is a bare ForeignScan on the upper rel with no local filter. So the remote aggregate runs without the clause and Postgres returns its result as final:

-- mysql_agg.orders has 5 rows, one of them Carol
SELECT count(*) FROM mysql_agg.orders WHERE lower(name) = 'carol';
-- returns 5, expected 1

This affects every FDW that supports aggregate pushdown (MySQL, ClickHouse, BigQuery). When only some clauses can be pushed, the pushable ones are applied and the rest are silently dropped. For example, status = 'inactive' AND lower(name) = 'eve' sums every inactive order.

What is the new behavior?

The aggregate path is only offered when every restriction clause on the foreign table was extracted as a qual (state.quals.len() equals the length of input_rel->baserestrictinfo). Otherwise the planner uses the normal scan with a local filter and a local Aggregate, so the result is correct. Queries whose WHERE clauses are all pushable are pushed down exactly as before.

Additional context

  • Tests: new cases in mysql_fdw/tests.rs (mysql_aggregate_pushdown_test) check both results and plans:
    • COUNT(*) ... WHERE lower(name) = 'carol' returns 1 and is not pushed down.
    • SUM(amount) ... WHERE status = 'inactive' AND lower(name) = 'eve' returns 150 and is not pushed down.
  • Red/green: cargo pgrx test pg17 mysql --features mysql_fdw against a mysql:8.4 container.
    • On main the new assertion fails with expected 1, got 5.
    • With this change all 3 mysql tests pass.
  • Checks: cargo fmt --check passes, and cargo clippy --tests -D warnings (pg17, mysql_fdw) is clean.
  • The existing pushed-down cases with simple WHERE clauses (id = 1, amount > 75, id IN (...)) are unaffected.
  • Full-query pushdown support for FDW #615 (full-query pushdown) computes a similar "unpushed quals" signal for its new path. It does not change the existing aggregate path, so the two don't overlap.

This PR was prepared with AI assistance (Claude). I reproduced the failure and ran the tests locally as described above.

🤖 Generated with Claude Code

A WHERE clause the framework can't turn into a Qual (for example
lower(name) = 'x' or a > b) is normally evaluated locally on each row.
The aggregate upper path has no local filter, so the remote aggregate ran
without that clause and returned a wrong result, e.g.
SELECT count(*) FROM ft WHERE lower(name) = 'carol' counted every row.

Only offer the aggregate path when every restriction clause on the
foreign table was extracted as a qual.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@imor

imor commented Sep 27, 2026

Copy link
Copy Markdown
Contributor

Closing as this is 100% AI generated.

@imor imor closed this Sep 27, 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.

2 participants