Skip to content

Remove duplicates from IN-list before turning it to an ExplodeExpression - #3608

Merged
hazefully merged 4 commits into
FoundationDB:mainfrom
hazefully:fix-in-predicate-duplicates-in-sql
Sep 23, 2025
Merged

hazefully merged 4 commits into
FoundationDB:mainfrom
hazefully:fix-in-predicate-duplicates-in-sql

Conversation

@hazefully

Copy link
Copy Markdown
Contributor

This PR makes use of the new wrapper value introduced in 4.5.10.0, ArrayDistinctValue, to remove duplicate values from the list provided to an IN predicate in SQL before transforming the IN predicate to an ExplodeExpression. This prevents duplicate result values if the planner ends up implementing the query graph as a nested loop join between the ExplodeExpression of the IN list and the left operand of the IN predicate.

Note: This is a breaking change that is not compatible with versions of fdb-record-layer < 4.5.10.0.

Fixes #3559.

@hazefully hazefully added the bug fix Change that fixes a bug label Sep 18, 2025
This commit makes use of the new wrapper value introduced in 4.5.10.0,
ArrayDistinctValue, to remove duplicate values from the list provided to
an IN predicate in SQL before transforming the IN predicate to an
ExplodeExpression. This prevents duplicate result values if the planner
ends up implementing the query graph as a nested loop join between the
ExplodeExpression of the IN list and the left operand of the IN
predicate.

This is a breaking change that is not compatible with versions of
fdb-record-layer < 4.5.10.0.
@hazefully
hazefully force-pushed the fix-in-predicate-duplicates-in-sql branch from cab55f1 to 8d28bd5 Compare September 18, 2025 15:49
# TODO TODO (IN expansion in cascades planner should distinct the in-list to avoid duplicates)
# LONG value matched against IN list of repeated LONG values
- query: select a, b from ta where b in (1, 1, 1, 1)
- explain: "ISCAN(F1 <,>) | FLATMAP q0 -> { EXPLODE arrayDistinct(promote(@c10 AS ARRAY(LONG))) | FILTER q0.B EQUALS _ AS q1 RETURN (q0.A AS A, q0.B AS B) }"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added an explain here and to the other tests checking duplicates since the duplicate bug only happens with nested loop joins, so if anything changes the planning the assumption these tests make about old versions will be incorrect.

@hazefully
hazefully marked this pull request as ready for review September 18, 2025 15:50
@hazefully
hazefully requested a review from normen662 September 19, 2025 13:49
- explain: "ISCAN(F1 <,>) | FLATMAP q0 -> { EXPLODE arrayDistinct(promote(@c10 AS ARRAY(LONG))) | FILTER q0.B EQUALS _ AS q1 RETURN (q0.A AS A, q0.B AS B) }"
- initialVersionLessThan: !current_version
- unorderedResult: [{A: 6, 3}, {A: 7, 2}, {A: 8, 1}, {A: 8, 1}]
# IN expansion in cascades planner distincts the in-list since this version preventing duplicate results with nested loop joins

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't have to leave a comment why a thing now works the way it should have worked from the beginning. Maybe you can rewrite these comments in a way that they explain why the old results are in fact the wrong results, instead of explaining why the new result is the new result :-)

@hazefully
hazefully requested a review from normen662 September 22, 2025 15:59
@hazefully
hazefully merged commit a4f84bd into FoundationDB:main Sep 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug fix Change that fixes a bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Duplicate elements provided to an IN predicate can lead to duplicate results

2 participants