Skip to content

fix: use the commutator when swapping qual operands - #676

Open
wenerme wants to merge 1 commit into
supabase:mainfrom
wenerme:fix/qual-commutator-swap
Open

wenerme wants to merge 1 commit into
supabase:mainfrom
wenerme:fix/qual-commutator-swap

Conversation

@wenerme

@wenerme wenerme commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

Summary

extract_from_op_expr swaps const op col into col op const so the column is on the left, but kept the original operator. 25 < age was pushed down as age < 25, i.e. with the comparison reversed:

  • scans returned the wrong rows from the remote side, which the local recheck then filtered out, so queries silently returned fewer (often zero) rows
  • with aggregate pushdown, which has no local recheck, the aggregate itself was computed over the wrong rows

The operator is now replaced by its commutator (oprcom) when swapping. Operators without a commutator are still not swapped. $1 < col (PARAM_EXTERN) goes through the same path and is fixed as well.

Validation

  • New mysql_swapped_qual_test covers <, >, <=, >=, =, <> and a timestamp comparison written as const op col.
  • With rust 1.97.1 / pgrx 0.19.2: cargo fmt --check, cargo clippy -D warnings (pg15 mysql_fdw), cargo pgrx test mysql tests on pg15 and pg18 pass.
  • Reverting the fix makes the test fail (25 < age returns [] instead of [2, 3]).

extract_from_op_expr swaps `const op col` into `col op const` so the
column is on the left, but kept the original operator. `10 < col` was
therefore pushed down as `col < 10`: the remote side returned the wrong
rows and the local recheck filtered them out, so scans silently returned
fewer (often zero) rows. With aggregate pushdown, which has no recheck,
the aggregate itself was computed over the wrong rows.

The operator is now replaced by its commutator (`oprcom`) when swapping.
@wenerme
wenerme requested a review from a team as a code owner November 5, 2026 03:44

This branch has not been deployed

No deployments
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.

1 participant