Conversation
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>
Contributor
|
Closing as this is 100% AI generated. |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
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 arelower(name) = 'x',a > b, or any function or expression on the column side.get_foreign_upper_pathsoffers the aggregate path without checking for such clauses. That path is a bareForeignScanon the upper rel with no local filter. So the remote aggregate runs without the clause and Postgres returns its result as final: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 ofinput_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
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.cargo pgrx test pg17 mysql --features mysql_fdwagainst amysql:8.4container.mainthe new assertion fails withexpected 1, got 5.cargo fmt --checkpasses, andcargo clippy --tests -D warnings(pg17, mysql_fdw) is clean.id = 1,amount > 75,id IN (...)) are unaffected.This PR was prepared with AI assistance (Claude). I reproduced the failure and ran the tests locally as described above.
🤖 Generated with Claude Code