opt: left lookup join returns incorrect results due to check constraints #59615
Labels
A-multiregion
Related to multi-region
A-sql-optimizer
SQL logical planning and optimizations.
C-bug
Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior.
S-0-visible-logical-error
Database stores inconsistent data in some cases, or queries return invalid results silently.
T-multiregion
Consider the following example. First create a table with a check constraint and insert some data:
Now run the following query:
These results are incorrect. There should only be one row with
u.y=2000
, not two. The reason this happens is because in theGenerateLookupJoins
exploration rule, the optimizer creates a cross join with the valid values of the check constraint in order to create a lookup join with the primary index:If we remove the check constraint and try again, we now get correct results due to using a different plan:
Although the cross join plan is correct in the case of an inner join, it is not correct for left joins. As a first step, we should disable the creation of the cross join as input to left lookup joins. Eventually, however, we should still be able to create a left lookup join in this case. I think that requires execution support to allow performing each lookup with multiple spans instead of a single equality condition.
This has implications for multi-region support, because the
crdb_region
column behaves the same way as columnx
in this example. Left joins often show up when buildingUPSERT
s, which is how I ran into this issue in the first place. We need to fix this problem to avoid upserting incorrect data, but we should really find a way to support left lookup joins for this case for performance reasons -- to avoid requiring a full table scan on the primary index.cc @mgartner @RaduBerinde
The text was updated successfully, but these errors were encountered: