Skip to content

Commit

Permalink
Merge pull request #91692 from DrewKimball/backport22.1-87562
Browse files Browse the repository at this point in the history
release-22.1: opt: add scan constant columns to ordering if they are in the output
  • Loading branch information
DrewKimball authored Nov 10, 2022
2 parents 38ef2ad + 4391191 commit 1605f26
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
12 changes: 9 additions & 3 deletions pkg/sql/opt/ordering/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,20 @@ func scanBuildProvided(expr memo.RelExpr, required *props.OrderingChoice) opt.Or
// We generate the longest ordering that this scan can provide, then we trim
// it. This is the longest prefix of index columns that are output by the scan
// (ignoring constant columns, in the case of constrained scans).
// We start the for loop at the exact prefix since all columns in the exact
// prefix are constant and can be ignored.
outCols := expr.Relational().OutputCols
constCols := fds.ComputeClosure(opt.ColSet{})
numCols := index.KeyColumnCount()
provided := make(opt.Ordering, 0, numCols)
for i := scan.ExactPrefix; i < numCols; i++ {
for i := 0; i < numCols; i++ {
indexCol := index.Column(i)
colID := scan.Table.ColumnID(indexCol.Ordinal())
if i < scan.ExactPrefix && !outCols.Contains(colID) {
// All columns in the exact prefix are constant and can be ignored as long
// as they are not in the output of the scan. If an exact-prefix column is
// in the output, it may still be constant, but can only be ignored if the
// FDs "know" it is constant. This latter case is handled below as normal.
continue
}
if constCols.Contains(colID) {
// Column constrained to a constant, ignore.
continue
Expand Down
32 changes: 32 additions & 0 deletions pkg/sql/opt/xform/testdata/physprops/ordering
Original file line number Diff line number Diff line change
Expand Up @@ -2709,3 +2709,35 @@ sort
├── cardinality: [0 - 0]
├── key: ()
└── fd: ()-->(6,12,23)

# Regression test for #83793 - include scan columns that are constrained to be
# constant in the provided ordering when they are in the output of the scan.
exec-ddl
CREATE TABLE t83793 (
a INT,
b STRING AS (a::STRING) STORED,
c STRING AS (a::STRING) VIRTUAL,
UNIQUE (b, a)
);
----

opt format=hide-all
SELECT NULL FROM t83793
WHERE NOT (c NOT SIMILAR TO '')
GROUP BY b HAVING bool_and(NULL);
----
project
├── select
│ ├── group-by (streaming)
│ │ ├── project
│ │ │ ├── scan t83793@t83793_b_a_key
│ │ │ │ └── constraint: /2/1: [/'' - /'']
│ │ │ └── projections
│ │ │ └── NULL
│ │ └── aggregations
│ │ └── bool-and
│ │ └── column7
│ └── filters
│ └── bool_and
└── projections
└── NULL

0 comments on commit 1605f26

Please sign in to comment.