Skip to content

Commit

Permalink
opt: prevent creation of invalid streaming set operations
Browse files Browse the repository at this point in the history
Prior to this commit, it was possible to generate a streaming
set operation with an empty ordering, due to using interesting
orderings involving input columns that were not output by the
set operation. This commit fixes the problem by removing those
orderings from consideration.

Fixes #73084

Release note (bug fix): Fixed an internal error that could occur
during planning for some set operations (e.g., UNION, INTERSECT or
EXCEPT) when at least one side of the set operation was ordered on
a column that was not output by the set operation. This bug was
first introduced in 21.2.0 and does not exist in prior versions.
  • Loading branch information
rytaft committed Nov 24, 2021
1 parent 0331536 commit bd6ec0c
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
11 changes: 9 additions & 2 deletions pkg/sql/opt/ordering/interesting_orderings.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,16 @@ func interestingOrderingsForSetOp(rel memo.RelExpr) props.OrderingSet {
// LocalityOptimizedSearchOp does not support passing through orderings.
return nil
}
ordLeft := DeriveInterestingOrderings(rel.Child(0).(memo.RelExpr))
ordRight := DeriveInterestingOrderings(rel.Child(1).(memo.RelExpr))
leftChild := rel.Child(0).(memo.RelExpr)
rightChild := rel.Child(1).(memo.RelExpr)
ordLeft := DeriveInterestingOrderings(leftChild)
ordRight := DeriveInterestingOrderings(rightChild)
private := rel.Private().(*memo.SetPrivate)

// We can only keep orderings on output columns.
ordLeft.RestrictToCols(private.LeftCols.ToSet(), &leftChild.Relational().FuncDeps)
ordRight.RestrictToCols(private.RightCols.ToSet(), &rightChild.Relational().FuncDeps)

ordLeft = ordLeft.RemapColumns(private.LeftCols, private.OutCols)
ordRight = ordRight.RemapColumns(private.RightCols, private.OutCols)

Expand Down
43 changes: 43 additions & 0 deletions pkg/sql/opt/xform/testdata/rules/set
Original file line number Diff line number Diff line change
Expand Up @@ -276,3 +276,46 @@ memo (optimized, ~8KB, required=[presentation: k:13,u:14,v:15,w:16])
└── []
├── best: (scan kuvw,cols=(7-10))
└── cost: 1104.82

# Regression test for #73084. Ensure that we do not create empty streaming set
# op orderings.
exec-ddl
CREATE TABLE table1 (
id INT64 PRIMARY KEY,
date TIMESTAMP DEFAULT now()
)
----

opt expect-not=GenerateStreamingSetOp
(SELECT id FROM table1 ORDER BY date ASC LIMIT 1)
UNION
(SELECT id FROM table1 ORDER BY date DESC LIMIT 1)
----
union
├── columns: id:9!null
├── left columns: table1.id:1
├── right columns: table1.id:5
├── cardinality: [0 - 2]
├── key: (9)
├── top-k
│ ├── columns: table1.id:1!null date:2
│ ├── internal-ordering: +2
│ ├── k: 1
│ ├── cardinality: [0 - 1]
│ ├── key: ()
│ ├── fd: ()-->(1,2)
│ └── scan table1
│ ├── columns: table1.id:1!null date:2
│ ├── key: (1)
│ └── fd: (1)-->(2)
└── top-k
├── columns: table1.id:5!null date:6
├── internal-ordering: -6
├── k: 1
├── cardinality: [0 - 1]
├── key: ()
├── fd: ()-->(5,6)
└── scan table1
├── columns: table1.id:5!null date:6
├── key: (5)
└── fd: (5)-->(6)

0 comments on commit bd6ec0c

Please sign in to comment.