Skip to content

Fix cartesian product when filtering on two FKs to the same target - #1708

Merged
collerek merged 2 commits into
masterfrom
fix/1706-cartesian-product-multi-fk
Jun 8, 2026
Merged

collerek merged 2 commits into
masterfrom
fix/1706-cartesian-product-multi-fk

Conversation

@collerek

@collerek collerek commented Jun 8, 2026

Copy link
Copy Markdown
Collaborator

When two relation chains converge on the same target via the same final relation (e.g. a_port__switch and z_port__switch), the filter side chose which path got a complex alias by relation_str length while the join builder uses depth-first order — so equal-length siblings were aliased inconsistently and the WHERE clause referenced a table the join never created, producing a cartesian product. Complex aliases also linger in the globally shared alias manager, letting a later simpler query pick one up.

The filter now walks relation paths in the same depth-first order as the join (first occurrence keeps the basic alias, later ones go complex) and only reroutes filters whose key is a duplicate registered for the current query. Added regression tests covering the no-cartesian case, cross-query state isolation, and correct row results.

Closes #1706

…rget (#1706)

The filter (WHERE) side chose which of two converging relation paths got a
complex alias by relation_str length, while the join builder uses depth-first
order, so equal-length siblings (e.g. a_port__switch / z_port__switch) were
assigned opposite aliases and the filter referenced a table the join never
created. Complex aliases also linger in the globally shared alias manager, so a
later, simpler query could pick one up and reference an unjoined table.

Walk relation paths in the same depth-first order as the join (first occurrence
keeps the basic alias, later ones go complex) and only reroute filters whose key
is a duplicate registered for the current query.
@codspeed

codspeed Bot commented Jun 8, 2026

Copy link
Copy Markdown

Merging this PR will degrade performance by 11.66%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

❌ 1 (👁 1) regressed benchmark
✅ 97 untouched benchmarks

Performance Changes

Mode Benchmark BASE HEAD Efficiency
👁 WallTime test_get_or_create_when_create[40] 417.6 ms 472.8 ms -11.66%

Comparing fix/1706-cartesian-product-multi-fk (de81cbb) with master (e6f68fb)

Open in CodSpeed

Bump version to 0.26.0 and require ormar-utils>=0.2.0 so installs pull the
stable-ABI (abi3) wheel that works on Python 3.10+ including 3.14 (#1702).
Roll the accumulated Unreleased notes into the 0.26.0 section and add the
cartesian-product fix (#1706) and the ormar-utils requirement bump (#1702).
@collerek
collerek merged commit a7e7fde into master Jun 8, 2026
11 checks passed
@collerek
collerek deleted the fix/1706-cartesian-product-multi-fk branch June 8, 2026 16:33
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.

Unexpected JOIN that causes cartesian product

1 participant