Skip to content

Commit

Permalink
opt: add Union column ID check to CheckExpr
Browse files Browse the repository at this point in the history
A check has been added to `CheckExpr` that asserts that the output
columns of `Union`s and `UnionAll`s are not reused from the left or
right inputs of the union. Reusing columns in this way is dangerous
(see #58434).

Release note: None
  • Loading branch information
mgartner committed Jan 7, 2021
1 parent 58b3efb commit 80d7605
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions pkg/sql/opt/memo/check_expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,30 @@ func (m *Memo) CheckExpr(e opt.Expr) {
case *SelectExpr:
checkFilters(t.Filters)

case *UnionExpr, *UnionAllExpr:
setPrivate := t.Private().(*SetPrivate)
outColSet := setPrivate.OutCols.ToSet()

// Check that columns on the left side of the union are not reused in
// the output.
leftColSet := setPrivate.LeftCols.ToSet()
if outColSet.Intersects(leftColSet) {
panic(errors.AssertionFailedf(
"union reuses columns in left input: %v",
outColSet.Intersection(leftColSet),
))
}

// Check that columns on the right side of the union are not reused in
// the output.
rightColSet := setPrivate.RightCols.ToSet()
if outColSet.Intersects(rightColSet) {
panic(errors.AssertionFailedf(
"union reuses columns in right input: %v",
outColSet.Intersection(rightColSet),
))
}

case *AggregationsExpr:
var checkAggs func(scalar opt.ScalarExpr)
checkAggs = func(scalar opt.ScalarExpr) {
Expand Down

0 comments on commit 80d7605

Please sign in to comment.