Skip to content

Commit

Permalink
opt: add a test-only check for NullsAreDistinct in GroupingPrivate
Browse files Browse the repository at this point in the history
This commit adds a check that NullsAreDistinct is true iff the grouping
operator is UpsertDistinctOn or EnsureUpsertDistinctOn.

Release note: None
  • Loading branch information
rytaft committed Jul 6, 2021
1 parent 464ddb9 commit 19fee46
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions pkg/sql/opt/memo/check_expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ func (m *Memo) CheckExpr(e opt.Expr) {

case *DistinctOnExpr, *EnsureDistinctOnExpr, *UpsertDistinctOnExpr, *EnsureUpsertDistinctOnExpr:
checkErrorOnDup(e.(RelExpr))
checkNullsAreDistinct(e.(RelExpr))

// Check that aggregates can be only FirstAgg or ConstAgg.
for _, item := range *t.Child(1).(*AggregationsExpr) {
Expand All @@ -189,6 +190,7 @@ func (m *Memo) CheckExpr(e opt.Expr) {

case *GroupByExpr, *ScalarGroupByExpr:
checkErrorOnDup(e.(RelExpr))
checkNullsAreDistinct(e.(RelExpr))

// Check that aggregates cannot be FirstAgg.
for _, item := range *t.Child(1).(*AggregationsExpr) {
Expand Down Expand Up @@ -411,6 +413,23 @@ func checkErrorOnDup(e RelExpr) {
}
}

func checkNullsAreDistinct(e RelExpr) {
// Only UpsertDistinctOn and EnsureUpsertDistinctOn should set the
// NullsAreDistinct field to true.
if e.Op() != opt.UpsertDistinctOnOp &&
e.Op() != opt.EnsureUpsertDistinctOnOp &&
e.Private().(*GroupingPrivate).NullsAreDistinct {
panic(errors.AssertionFailedf(
"%s should never set NullsAreDistinct to true", log.Safe(e.Op())))
}
if (e.Op() == opt.UpsertDistinctOnOp ||
e.Op() == opt.EnsureUpsertDistinctOnOp) &&
!e.Private().(*GroupingPrivate).NullsAreDistinct {
panic(errors.AssertionFailedf(
"%s should never set NullsAreDistinct to false", log.Safe(e.Op())))
}
}

func checkOutputCols(e opt.Expr) {
set := opt.ColSet{}

Expand Down

0 comments on commit 19fee46

Please sign in to comment.