Skip to content

Commit 49d65d3

Browse files
craig[bot]mgartner
craig[bot]
andcommitted
Merge #140034
140034: opt: do not project extra columns above partial index scans r=mgartner a=mgartner The `GeneratePartialIndexScans` rule no longer projects all non-indexed columns held constant by the partial index predicate. Now, only columns produced by the original scan are produced. This fixes an issue where the generated Project expression could have more output columns than the other expressions in its group, causing a test-only assertion to fail. Fixes #140019 There is no release note because this has caused no known correctness bugs. Release note: None Co-authored-by: Marcus Gartner <[email protected]>
2 parents 34b5772 + d66bd15 commit 49d65d3

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

pkg/sql/opt/xform/scan_index_iter.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,9 @@ func (it *scanIndexIter) ForEachStartingAfter(ord int, f enumerateIndexFunc) {
307307
isCovering = true
308308

309309
// Build a projection only for constant columns not in the
310-
// index.
311-
constCols = constCols.Difference(indexCols)
310+
// index and produced by the original scan.
311+
constCols.DifferenceWith(indexCols)
312+
constCols.IntersectionWith(it.scanPrivate.Cols)
312313
constProj = it.buildConstProjectionsFromPredicate(predFilters, constCols)
313314
}
314315
}

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

+37
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,43 @@ project
557557
├── columns: k:1!null
558558
└── key: (1)
559559

560+
exec-ddl
561+
CREATE TABLE t140019 (
562+
k INT PRIMARY KEY,
563+
i INT,
564+
j INT,
565+
INDEX (k) WHERE i = 0 AND j = 1
566+
)
567+
----
568+
569+
# Regression test for #140019. Do not project unnecessary columns, like i=0,
570+
# that are held constant by the partial index predicate.
571+
opt disable=SimplifyZeroCardinalityGroup
572+
SELECT NULL FROM t140019 WHERE false GROUP BY j
573+
----
574+
project
575+
├── columns: "?column?":6
576+
├── cardinality: [0 - 0]
577+
├── fd: ()-->(6)
578+
├── distinct-on
579+
│ ├── columns: j:3
580+
│ ├── grouping columns: j:3
581+
│ ├── cardinality: [0 - 0]
582+
│ ├── key: (3)
583+
│ └── select
584+
│ ├── columns: j:3
585+
│ ├── cardinality: [0 - 0]
586+
│ ├── project
587+
│ │ ├── columns: j:3!null
588+
│ │ ├── fd: ()-->(3)
589+
│ │ ├── scan t140019@t140019_k_idx,partial
590+
│ │ └── projections
591+
│ │ └── 1 [as=j:3]
592+
│ └── filters
593+
│ └── false [constraints=(contradiction; tight)]
594+
└── projections
595+
└── NULL [as="?column?":6]
596+
560597
# --------------------------------------------------
561598
# GenerateConstrainedScans
562599
# --------------------------------------------------

0 commit comments

Comments
 (0)