Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions enginetest/queries/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -2367,6 +2367,26 @@ var QueryTests = []QueryTest{
Query: `SELECT * FROM mytable WHERE i in (i)`,
Expected: []sql.Row{{1, "first row"}, {2, "second row"}, {3, "third row"}},
},
{
Query: `SELECT * FROM mytable WHERE i in (1, 1, 1, 1, 1)`,
Expected: []sql.Row{{1, "first row"}},
},
{
Query: `SELECT * FROM mytable WHERE i NOT in (1, 1)`,
Expected: []sql.Row{{2, "second row"}, {3, "third row"}},
},
{
Query: `SELECT * FROM mytable WHERE i in (i, i)`,
Copy link
Contributor

@max-hoffman max-hoffman Mar 28, 2023

Choose a reason for hiding this comment

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

When I look at the usage of NewIndexedTableAccess and related constructors, i think we're missing applyIndexesFromOuterScope, which will index an outer scope filter in something like: select * from (select * from mytable) sq where sq.i in (1,1). Probably same pattern for multi-relation subquery would be good: select * from (select a.i from mytable a cross join mytable b) sq where sq.i in (1,1));

Expected: []sql.Row{{1, "first row"}, {2, "second row"}, {3, "third row"}},
},
{
Query: `SELECT * FROM (select * from mytable) sq WHERE sq.i in (1, 1)`,
Expected: []sql.Row{{1, "first row"}},
},
{
Query: `SELECT * FROM (select a.i from mytable a cross join mytable b) sq WHERE sq.i in (1, 1)`,
Expected: []sql.Row{{1}, {1}, {1}},
},
{
Query: "SELECT * from mytable WHERE 4 IN (i + 2)",
Expected: []sql.Row{{2, "second row"}},
Expand Down
58 changes: 35 additions & 23 deletions enginetest/queries/query_plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ var PlanTests = []QueryPlanTest{
" │ └─ TUPLE(2 (tinyint), 3 (tinyint))\n" +
" └─ IndexedTableAccess(uv)\n" +
" ├─ index: [uv.u]\n" +
" ├─ static: [{[3, 3]}, {[2, 2]}]\n" +
" ├─ static: [{[2, 2]}, {[3, 3]}]\n" +
" └─ columns: [u v]\n" +
"",
},
Expand Down Expand Up @@ -2389,7 +2389,19 @@ inner join pq on true
" │ └─ TUPLE(1 (tinyint), 2 (tinyint), 3 (tinyint), 4 (tinyint))\n" +
" └─ IndexedTableAccess(mytable)\n" +
" ├─ index: [mytable.i]\n" +
" ├─ static: [{[2, 2]}, {[3, 3]}, {[4, 4]}, {[1, 1]}]\n" +
" ├─ static: [{[1, 1]}, {[2, 2]}, {[3, 3]}, {[4, 4]}]\n" +
" └─ columns: [i s]\n" +
"",
},
{
Query: `SELECT * FROM mytable WHERE i in (1, 1)`,
ExpectedPlan: "Filter\n" +
" ├─ HashIn\n" +
" │ ├─ mytable.i:0!null\n" +
" │ └─ TUPLE(1 (tinyint), 1 (tinyint))\n" +
" └─ IndexedTableAccess(mytable)\n" +
" ├─ index: [mytable.i]\n" +
" ├─ static: [{[1, 1]}]\n" +
" └─ columns: [i s]\n" +
"",
},
Expand All @@ -2401,7 +2413,7 @@ inner join pq on true
" │ └─ TUPLE(NULL (bigint), 2 (tinyint), 3 (tinyint), 4 (tinyint))\n" +
" └─ IndexedTableAccess(mytable)\n" +
" ├─ index: [mytable.i]\n" +
" ├─ static: [{[3, 3]}, {[4, 4]}, {[2, 2]}]\n" +
" ├─ static: [{[2, 2]}, {[3, 3]}, {[4, 4]}]\n" +
" └─ columns: [i s]\n" +
"",
},
Expand Down Expand Up @@ -2712,7 +2724,7 @@ inner join pq on true
" │ └─ TableAlias(a)\n" +
" │ └─ IndexedTableAccess(mytable)\n" +
" │ ├─ index: [mytable.i]\n" +
" │ ├─ static: [{[432, 432]}, {[7, 7]}, {[2, 2]}]\n" +
" │ ├─ static: [{[2, 2]}, {[7, 7]}, {[432, 432]}]\n" +
" │ └─ columns: [i s]\n" +
" └─ TableAlias(b)\n" +
" └─ Table\n" +
Expand Down Expand Up @@ -7193,7 +7205,7 @@ With c as (
" │ └─ TableAlias(t1)\n" +
" │ └─ IndexedTableAccess(mytable)\n" +
" │ ├─ index: [mytable.i]\n" +
" │ ├─ static: [{[3, 3]}, {[2, 2]}]\n" +
" │ ├─ static: [{[2, 2]}, {[3, 3]}]\n" +
" │ └─ columns: [i s]\n" +
" └─ HashLookup\n" +
" ├─ source: TUPLE(e.i:0!null)\n" +
Expand All @@ -7214,7 +7226,7 @@ With c as (
" │ └─ TableAlias(t2)\n" +
" │ └─ IndexedTableAccess(mytable)\n" +
" │ ├─ index: [mytable.i]\n" +
" │ ├─ static: [{[2, 2]}, {[1, 1]}]\n" +
" │ ├─ static: [{[1, 1]}, {[2, 2]}]\n" +
" │ └─ columns: [i s]\n" +
" └─ HashLookup\n" +
" ├─ source: TUPLE(b.i:2!null)\n" +
Expand Down Expand Up @@ -14487,7 +14499,7 @@ WHERE id IN ('1','2','3')`,
" │ └─ TUPLE(1 (longtext), 2 (longtext), 3 (longtext))\n" +
" └─ IndexedTableAccess(QYWQD)\n" +
" ├─ index: [QYWQD.id]\n" +
" ├─ static: [{[2, 2]}, {[3, 3]}, {[1, 1]}]\n" +
" ├─ static: [{[1, 1]}, {[2, 2]}, {[3, 3]}]\n" +
" └─ columns: [id wnunu hhvlx hvhrz ykssu fhcyt]\n" +
"",
},
Expand Down Expand Up @@ -14524,7 +14536,7 @@ WHERE id IN ('1', '2', '3')`,
" │ └─ TUPLE(1 (longtext), 2 (longtext), 3 (longtext))\n" +
" └─ IndexedTableAccess(QYWQD)\n" +
" ├─ index: [QYWQD.id]\n" +
" ├─ static: [{[2, 2]}, {[3, 3]}, {[1, 1]}]\n" +
" ├─ static: [{[1, 1]}, {[2, 2]}, {[3, 3]}]\n" +
" └─ columns: [id wnunu hhvlx hvhrz ykssu fhcyt]\n" +
"",
},
Expand All @@ -14540,7 +14552,7 @@ WHERE LUEVY IN ('1', '2', '3')`,
" │ └─ TUPLE(1 (longtext), 2 (longtext), 3 (longtext))\n" +
" └─ IndexedTableAccess(AMYXQ)\n" +
" ├─ index: [AMYXQ.LUEVY]\n" +
" ├─ static: [{[2, 2]}, {[3, 3]}, {[1, 1]}]\n" +
" ├─ static: [{[1, 1]}, {[2, 2]}, {[3, 3]}]\n" +
" └─ columns: [id gxlub luevy xqdyt amyxq oztqf z35gy kkgn5]\n" +
"",
},
Expand All @@ -14556,7 +14568,7 @@ WHERE id IN ('1', '2', '3')`,
" │ └─ TUPLE(1 (longtext), 2 (longtext), 3 (longtext))\n" +
" └─ IndexedTableAccess(HGMQ6)\n" +
" ├─ index: [HGMQ6.id]\n" +
" ├─ static: [{[2, 2]}, {[3, 3]}, {[1, 1]}]\n" +
" ├─ static: [{[1, 1]}, {[2, 2]}, {[3, 3]}]\n" +
" └─ columns: [id gxlub luevy m22qn tjpt7 arn5p xosd4 ide43 hmw4h zbt6r fsdy2 lt7k6 sppyd qcgts teuja qqv4m fhcyt]\n" +
"",
},
Expand All @@ -14572,7 +14584,7 @@ WHERE id IN ('1', '2', '3')`,
" │ └─ TUPLE(1 (longtext), 2 (longtext), 3 (longtext))\n" +
" └─ IndexedTableAccess(HDDVB)\n" +
" ├─ index: [HDDVB.id]\n" +
" ├─ static: [{[2, 2]}, {[3, 3]}, {[1, 1]}]\n" +
" ├─ static: [{[1, 1]}, {[2, 2]}, {[3, 3]}]\n" +
" └─ columns: [id fv24e uj6xy m22qn nz4mq etpqv pruv2 ykssu fhcyt]\n" +
"",
},
Expand All @@ -14588,7 +14600,7 @@ WHERE LUEVY IN ('1', '2', '3')`,
" │ └─ TUPLE(1 (longtext), 2 (longtext), 3 (longtext))\n" +
" └─ IndexedTableAccess(FLQLP)\n" +
" ├─ index: [FLQLP.LUEVY]\n" +
" ├─ static: [{[2, 2]}, {[3, 3]}, {[1, 1]}]\n" +
" ├─ static: [{[1, 1]}, {[2, 2]}, {[3, 3]}]\n" +
" └─ columns: [id fz2r5 luevy m22qn ove3e nrurt oca7e xmm6q v5dpx s3q3y zrv3b fhcyt]\n" +
"",
},
Expand All @@ -14604,7 +14616,7 @@ WHERE id IN ('1', '2', '3')`,
" │ └─ TUPLE(1 (longtext), 2 (longtext), 3 (longtext))\n" +
" └─ IndexedTableAccess(FLQLP)\n" +
" ├─ index: [FLQLP.id]\n" +
" ├─ static: [{[2, 2]}, {[3, 3]}, {[1, 1]}]\n" +
" ├─ static: [{[1, 1]}, {[2, 2]}, {[3, 3]}]\n" +
" └─ columns: [id fz2r5 luevy m22qn ove3e nrurt oca7e xmm6q v5dpx s3q3y zrv3b fhcyt]\n" +
"",
},
Expand All @@ -14620,7 +14632,7 @@ WHERE id IN ('1', '2', '3')`,
" │ └─ TUPLE(1 (longtext), 2 (longtext), 3 (longtext))\n" +
" └─ IndexedTableAccess(FLQLP)\n" +
" ├─ index: [FLQLP.id]\n" +
" ├─ static: [{[2, 2]}, {[3, 3]}, {[1, 1]}]\n" +
" ├─ static: [{[1, 1]}, {[2, 2]}, {[3, 3]}]\n" +
" └─ columns: [id fz2r5 luevy m22qn ove3e nrurt oca7e xmm6q v5dpx s3q3y zrv3b fhcyt]\n" +
"",
},
Expand Down Expand Up @@ -14763,7 +14775,7 @@ WHERE
" │ │ └─ TUPLE(1 (longtext), 2 (longtext), 3 (longtext))\n" +
" │ └─ IndexedTableAccess(YK2GW)\n" +
" │ ├─ index: [YK2GW.id]\n" +
" │ ├─ static: [{[2, 2]}, {[3, 3]}, {[1, 1]}]\n" +
" │ ├─ static: [{[1, 1]}, {[2, 2]}, {[3, 3]}]\n" +
" │ └─ columns: [id ftqlq tuxml paef5 rucy4 tpnj6 lbl53 nb3qs eo7iv muhjf fm34l ty5rf zhtlh npb7w sx3hh isbnf ya7yb c5ykb qk7kt ffge6 fiigj sh3nc ntena m4aub x5air sab6m g5qi5 zvqvd ykssu fhcyt]\n" +
" └─ BEGIN .. END\n" +
" └─ IF BLOCK\n" +
Expand Down Expand Up @@ -14946,7 +14958,7 @@ WHERE
" │ │ └─ TUPLE(1 (longtext), 2 (longtext), 3 (longtext))\n" +
" │ └─ IndexedTableAccess(TDRVG)\n" +
" │ ├─ index: [TDRVG.id]\n" +
" │ ├─ static: [{[2, 2]}, {[3, 3]}, {[1, 1]}]\n" +
" │ ├─ static: [{[1, 1]}, {[2, 2]}, {[3, 3]}]\n" +
" │ └─ columns: [id sshpj sfj6l]\n" +
" └─ BEGIN .. END\n" +
" └─ IF BLOCK\n" +
Expand Down Expand Up @@ -15089,7 +15101,7 @@ WHERE
" │ │ └─ TableAlias(ufc)\n" +
" │ │ └─ IndexedTableAccess(SISUT)\n" +
" │ │ ├─ index: [SISUT.id]\n" +
" │ │ ├─ static: [{[2, 2]}, {[3, 3]}, {[1, 1]}]\n" +
" │ │ ├─ static: [{[1, 1]}, {[2, 2]}, {[3, 3]}]\n" +
" │ │ └─ columns: [id t4ibq zh72s amyxq ktnz2 hiid2 dn3oq vvknb sh7tp srzzo qz6vt]\n" +
" │ └─ HashLookup\n" +
" │ ├─ source: TUPLE(ufc.ZH72S:2)\n" +
Expand Down Expand Up @@ -15236,7 +15248,7 @@ WHERE
" │ └─ TableAlias(ums)\n" +
" │ └─ IndexedTableAccess(FG26Y)\n" +
" │ ├─ index: [FG26Y.id]\n" +
" │ ├─ static: [{[2, 2]}, {[3, 3]}, {[1, 1]}]\n" +
" │ ├─ static: [{[1, 1]}, {[2, 2]}, {[3, 3]}]\n" +
" │ └─ columns: [id t4ibq ner ber hr mmr qz6vt]\n" +
" └─ BEGIN .. END\n" +
" ├─ IF BLOCK\n" +
Expand Down Expand Up @@ -15361,7 +15373,7 @@ WHERE
" │ └─ TableAlias(ums)\n" +
" │ └─ IndexedTableAccess(FG26Y)\n" +
" │ ├─ index: [FG26Y.id]\n" +
" │ ├─ static: [{[2, 2]}, {[3, 3]}, {[1, 1]}]\n" +
" │ ├─ static: [{[1, 1]}, {[2, 2]}, {[3, 3]}]\n" +
" │ └─ columns: [id t4ibq ner ber hr mmr qz6vt]\n" +
" └─ BEGIN .. END\n" +
" ├─ IF BLOCK\n" +
Expand Down Expand Up @@ -15486,7 +15498,7 @@ WHERE
" │ └─ TableAlias(ums)\n" +
" │ └─ IndexedTableAccess(FG26Y)\n" +
" │ ├─ index: [FG26Y.id]\n" +
" │ ├─ static: [{[2, 2]}, {[3, 3]}, {[1, 1]}]\n" +
" │ ├─ static: [{[1, 1]}, {[2, 2]}, {[3, 3]}]\n" +
" │ └─ columns: [id t4ibq ner ber hr mmr qz6vt]\n" +
" └─ BEGIN .. END\n" +
" ├─ IF BLOCK\n" +
Expand Down Expand Up @@ -15611,7 +15623,7 @@ WHERE
" │ └─ TableAlias(ums)\n" +
" │ └─ IndexedTableAccess(FG26Y)\n" +
" │ ├─ index: [FG26Y.id]\n" +
" │ ├─ static: [{[2, 2]}, {[3, 3]}, {[1, 1]}]\n" +
" │ ├─ static: [{[1, 1]}, {[2, 2]}, {[3, 3]}]\n" +
" │ └─ columns: [id t4ibq ner ber hr mmr qz6vt]\n" +
" └─ BEGIN .. END\n" +
" ├─ IF BLOCK\n" +
Expand Down Expand Up @@ -15722,7 +15734,7 @@ WHERE
" │ └─ TableAlias(umf)\n" +
" │ └─ IndexedTableAccess(NZKPM)\n" +
" │ ├─ index: [NZKPM.id]\n" +
" │ ├─ static: [{[2, 2]}, {[3, 3]}, {[1, 1]}]\n" +
" │ ├─ static: [{[1, 1]}, {[2, 2]}, {[3, 3]}]\n" +
" │ └─ columns: [id t4ibq fgg57 sshpj nla6o sfj6l tjpt7 arn5p sypkf ivfmk ide43 az6sp fsdy2 xosd4 hmw4h s76om vaf zroh6 qcgts lnfm6 tvawl hdlcl bhhw6 fhcyt qz6vt]\n" +
" └─ BEGIN .. END\n" +
" └─ IF BLOCK\n" +
Expand Down Expand Up @@ -17409,7 +17421,7 @@ WHERE
" │ └─ TableAlias(TVTJS)\n" +
" │ └─ IndexedTableAccess(HU5A5)\n" +
" │ ├─ index: [HU5A5.id]\n" +
" │ ├─ static: [{[2, 2]}, {[3, 3]}, {[1, 1]}]\n" +
" │ ├─ static: [{[1, 1]}, {[2, 2]}, {[3, 3]}]\n" +
" │ └─ columns: [id tofpn i3vta sfj6l v5dpx ljlum idpk7 no52d zrv3b vyo5e swcqv ykssu fhcyt]\n" +
" └─ BEGIN .. END\n" +
" ├─ IF BLOCK\n" +
Expand Down
10 changes: 9 additions & 1 deletion sql/analyzer/indexes.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,14 @@ func getIndexes(
if lookup.IsEmpty() {
return nil, nil
}
newRanges, err := sql.RemoveOverlappingRanges(lookup.Ranges...)
if err != nil {
return nil, nil
}
newLookup := sql.IndexLookup{Index: idx, Ranges: newRanges}
if err != nil {
return nil, err
}

getField := expression.ExtractGetField(cmp.Left())
if getField == nil {
Expand All @@ -158,7 +166,7 @@ func getIndexes(
result[getField.Table()] = &indexLookup{
fields: []sql.Expression{e},
indexes: []sql.Index{idx},
lookup: lookup,
lookup: newLookup,
expr: e,
}
}
Expand Down