Skip to content

Commit 8fed6e8

Browse files
craig[bot]mgartner
andcommitted
Merge #147490
147490: opt: do not produce unneeded PK cols in inverted index scans r=mgartner a=mgartner A minor bug has been fixed where an inverted index scan could produce more PK columns than necessary. This caused test-only assertion failures but does not cause any known, user-visible bugs. The bug was caused by the incorrect assumption that all inverted index scans need to produces all PK columns. In actuality, an inverted index scan can produce a subset of PK columns if the original scan produces a subset of PK columns and neither an inverted filter nor an index join are needed. Fixes #143070 Release note: None Co-authored-by: Marcus Gartner <[email protected]>
2 parents fc5ad3c + 2e0b93c commit 8fed6e8

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

pkg/sql/opt/xform/select_funcs.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -968,17 +968,33 @@ func (c *CustomFuncs) generateInvertedIndexScansImpl(
968968
pkCols = c.PrimaryKeyCols(scanPrivate.Table)
969969
}
970970

971+
// Start with a new Scan that produces only the PK columns produced by
972+
// the original scan.
973+
// NOTE: Intersection is used intentionally to avoid mutating pkCols.
974+
newScanPrivate.Cols = pkCols.Intersection(scanPrivate.Cols)
975+
976+
// We will need an index join above the scan if the original scan
977+
// produces non-PK columns.
978+
needIndexJoin := !scanPrivate.Cols.SubsetOf(newScanPrivate.Cols)
971979
// We will need an inverted filter above the scan if the spanExpr might
972980
// produce duplicate primary keys or requires at least one UNION or
973981
// INTERSECTION. In this case, we must scan both the primary key columns
974982
// and the inverted key column.
975983
needInvertedFilter := !spanExpr.Unique || spanExpr.Operator != inverted.None
976-
newScanPrivate.Cols = pkCols.Copy()
984+
985+
// An index join or an inverted filter require all PK columns.
986+
if needIndexJoin || needInvertedFilter {
987+
newScanPrivate.Cols.UnionWith(pkCols)
988+
}
989+
977990
var invertedCol opt.ColumnID
978991
if needInvertedFilter {
992+
// If we need an inverted filter, then we must also produce the
993+
// inverted key column.
979994
invertedCol = scanPrivate.Table.ColumnID(index.InvertedColumn().Ordinal())
980995
newScanPrivate.Cols.Add(invertedCol)
981996
}
997+
982998
sb.SetScan(&newScanPrivate)
983999

9841000
// Add an inverted filter if needed.
@@ -990,9 +1006,7 @@ func (c *CustomFuncs) generateInvertedIndexScansImpl(
9901006
// be applied above the scan, and one that requires columns not produced
9911007
// by the scan.
9921008
filters = sb.AddSelectAfterSplit(filters, pkCols)
993-
if !scanPrivate.Cols.SubsetOf(newScanPrivate.Cols) {
994-
// Add an index join if the scan does not produce all the needed
995-
// columns.
1009+
if needIndexJoin {
9961010
sb.AddIndexJoin(scanPrivate.Cols)
9971011
}
9981012
// Add the remaining filters, if any.

pkg/sql/opt/xform/testdata/rules/select

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8567,7 +8567,7 @@ CREATE TABLE t136078 (
85678567
)
85688568
----
85698569

8570-
opt set=(optimizer_use_trigram_similarity_optimization=false)
8570+
opt expect=GenerateInvertedIndexScans set=(optimizer_use_trigram_similarity_optimization=false)
85718571
SELECT b FROM t136078 WHERE 'abc' % col3_8
85728572
----
85738573
project
@@ -8598,6 +8598,36 @@ project
85988598
└── filters
85998599
└── 'abc' % lower(b:2) [outer=(2), stable]
86008600

8601+
exec-ddl
8602+
CREATE TABLE t143070 (
8603+
a STRING,
8604+
b STRING,
8605+
c STRING AS (b) VIRTUAL,
8606+
PRIMARY KEY (a, b),
8607+
INVERTED INDEX (c gin_trgm_ops)
8608+
)
8609+
----
8610+
8611+
# Regression test for #143070. The inverted index scan should not produce
8612+
# unneeded PK cols.
8613+
opt expect=GenerateInvertedIndexScans
8614+
SELECT 1 FROM t143070 WHERE c LIKE 'aaa'
8615+
----
8616+
project
8617+
├── columns: "?column?":7!null
8618+
├── fd: ()-->(7)
8619+
├── select
8620+
│ ├── columns: b:2!null
8621+
│ ├── fd: ()-->(2)
8622+
│ ├── scan t143070@t143070_c_idx,inverted
8623+
│ │ ├── columns: b:2!null
8624+
│ │ └── inverted constraint: /6/1/2
8625+
│ │ └── spans: ["aaa", "aaa"]
8626+
│ └── filters
8627+
│ └── b:2 LIKE 'aaa' [outer=(2), constraints=(/2: [/'aaa' - /'aaa']; tight), fd=()-->(2)]
8628+
└── projections
8629+
└── 1 [as="?column?":7]
8630+
86018631

86028632
# --------------------------------------------------
86038633
# GenerateMinimalInvertedIndexScans

0 commit comments

Comments
 (0)