morrySnow opened a new pull request, #67699:
URL: https://github.com/apache/doris/pull/67699

   ## Problem
   
   Nereids can eliminate an inner join against the wrong primary-key table. If 
a child table has a foreign key to table `p1`, and an unrelated table `p2` has 
a key column with the same schema as `p1`, a query joining the child table to 
`p2` can lose the join. Rows that have no match in `p2` are then returned 
incorrectly.
   
   ## Root cause
   
   `ForeignKeyContext` represented constraints and slot lineage with 
unqualified `Column` objects. `Column.equals()` compares column schema 
attributes but does not include catalog, database, or table identity. 
Consequently, a constraint from `child.fk` to `p1.id` compared equal to a 
candidate mapping from `child.fk` to the same-shaped `p2.id`.
   
   ## How to reproduce
   
   1. Create `p1(id)` and `p2(id)` with identical primary-key definitions.
   2. Create `child(fk, payload)` with a foreign key from `child.fk` to `p1.id`.
   3. Insert `(1)` into `p1`, `(2)` into `p2`, and `(1, 7)` into `child`.
   4. Run:
   
   ```sql
   SELECT child.payload
   FROM child INNER JOIN p2 ON child.fk = p2.id;
   ```
   
   The correct result is empty because `p2` has no row with `id = 1`. Before 
this change, the optimizer removed `p2` and the join and returned `7`.
   
   ## Fix
   
   Qualify every column used by foreign-key proofs with its owning 
`TableIdentifier`. Constraint collection, primary-key tracking, slot lineage 
through aliases, and final constraint matching now compare both table identity 
and column schema. This keeps the existing elimination for the declared target 
table while rejecting an unrelated table with an identical column definition.
   
   ## Tests
   
   - Added a negative FE unit test with an unrelated same-schema primary-key 
table.
   - Kept the existing positive tests for elimination against the actual 
referenced table.
   - Ran:
   
   ```text
   ./run-fe-ut.sh --run 
org.apache.doris.nereids.rules.rewrite.EliminateJoinByFkTest
   ```
   
   Result: 12 tests passed, 0 failures, 0 errors.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to