Skip to content

Commit a3467f5

Browse files
authored
Merge pull request #159774 from cockroachdb/blathers/backport-release-24.3-159722
release-24.3: opt: fix optional filter removal for lookup join
2 parents c8b2ee9 + d3b2bd8 commit a3467f5

File tree

4 files changed

+47
-0
lines changed

4 files changed

+47
-0
lines changed

pkg/sql/opt/lookupjoin/constraint_builder.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,7 @@ func (b *ConstraintBuilder) Build(
347347
if isOptional := allIdx >= len(onFilters); isOptional {
348348
optionalMultiValFilterSuffixLen++
349349
} else {
350+
optionalMultiValFilterSuffixLen = 0
350351
// There's no need to track optional filters for reducing the
351352
// remaining filters because they are not present in the ON
352353
// filters to begin with.

pkg/sql/opt/lookupjoin/testdata/key_cols

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,13 @@ optional: z IN (3, 4) AND y IN (10, 20)
191191
key cols:
192192
x = a
193193

194+
lookup-constraints left=(a int, b int, c int) right=(x int, y int, z int) index=(x, y, z)
195+
x = a AND z IN (3, 4)
196+
optional: y IN (10, 20)
197+
----
198+
lookup expression:
199+
((y IN (10, 20)) AND (z IN (3, 4))) AND (a = x)
200+
194201
lookup-constraints left=(a int, b int) right=(x int, y int, z int) index=(x, y, z)
195202
x = a AND y = b
196203
optional: z > 10

pkg/sql/opt/lookupjoin/testdata/lookup_expr

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,3 +489,10 @@ optional: y IN (10, 20, 30)
489489
----
490490
lookup expression:
491491
((y IN (10, 20, 30)) AND (z > b)) AND (a = x)
492+
493+
lookup-constraints left=(a int, b int, c int) right=(x int, y int, z int) index=(x, y, z)
494+
x = a AND z IN (100, 200, 300)
495+
optional: y IN (10, 20, 30)
496+
----
497+
lookup expression:
498+
((y IN (10, 20, 30)) AND (z IN (100, 200, 300))) AND (a = x)

pkg/sql/opt/xform/testdata/rules/join

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3119,6 +3119,38 @@ left-join (lookup lookup_expr [as=t])
31193119
│ └── column1:1 = w:7 [outer=(1,7), constraints=(/1: (/NULL - ]; /7: (/NULL - ]), fd=(1)==(7), (7)==(1)]
31203120
└── filters (true)
31213121

3122+
# The filters in the lookup expression should contains all columns of the
3123+
# idx_vrw index. Column r, though not in the ON exprs, should still be
3124+
# in the filters, because for the column w that comes behind it in
3125+
# idx_vrw, there is a necessary filter `w IN (10, 20)`.
3126+
opt expect=GenerateLookupJoinsWithFilter
3127+
SELECT * FROM (VALUES (1), (2), (3)) AS q(v) LEFT LOOKUP JOIN lookup_expr t
3128+
ON q.v = t.v WHERE w IN (10, 20)
3129+
----
3130+
inner-join (lookup lookup_expr [as=t])
3131+
├── columns: v:1!null r:2!null k:3!null u:4 v:5!null w:6!null x:7 y:8!null z:9!null
3132+
├── key columns: [2 3] = [2 3]
3133+
├── lookup columns are key
3134+
├── fd: ()-->(9), (2,3)-->(4-8), (1)==(5), (5)==(1)
3135+
├── inner-join (lookup lookup_expr@idx_vrw [as=t])
3136+
│ ├── columns: column1:1!null r:2!null k:3!null v:5!null w:6!null
3137+
│ ├── flags: force lookup join (into right side)
3138+
│ ├── lookup expression
3139+
│ │ └── filters
3140+
│ │ ├── r:2 IN ('east', 'west') [outer=(2), constraints=(/2: [/'east' - /'east'] [/'west' - /'west']; tight)]
3141+
│ │ ├── w:6 IN (10, 20) [outer=(6), constraints=(/6: [/10 - /10] [/20 - /20]; tight)]
3142+
│ │ └── column1:1 = v:5 [outer=(1,5), constraints=(/1: (/NULL - ]; /5: (/NULL - ]), fd=(1)==(5), (5)==(1)]
3143+
│ ├── fd: (2,3)-->(5,6), (1)==(5), (5)==(1)
3144+
│ ├── values
3145+
│ │ ├── columns: column1:1!null
3146+
│ │ ├── cardinality: [3 - 3]
3147+
│ │ ├── (1,)
3148+
│ │ ├── (2,)
3149+
│ │ └── (3,)
3150+
│ └── filters (true)
3151+
└── filters (true)
3152+
3153+
31223154
# Ensure that we constrain all columns in idx_vrw, not just v.
31233155
opt expect=GenerateLookupJoins
31243156
SELECT * FROM (VALUES (1, 10), (2, 20), (3, NULL)) AS q(v, w) LEFT JOIN lookup_expr t

0 commit comments

Comments
 (0)