Skip to content

Commit a2bf795

Browse files
authored
Merge pull request #140131 from cockroachdb/blathers/backport-release-25.1-140034
release-25.1: opt: do not project extra columns above partial index scans
2 parents 40d7683 + d4715dc commit a2bf795

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
@@ -306,8 +306,9 @@ func (it *scanIndexIter) ForEachStartingAfter(ord int, f enumerateIndexFunc) {
306306
isCovering = true
307307

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

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)