From 19fee466b38ba0ad01fc7e2e16041d6ad02a9110 Mon Sep 17 00:00:00 2001 From: Rebecca Taft Date: Tue, 6 Jul 2021 09:13:25 -0500 Subject: [PATCH] opt: add a test-only check for NullsAreDistinct in GroupingPrivate This commit adds a check that NullsAreDistinct is true iff the grouping operator is UpsertDistinctOn or EnsureUpsertDistinctOn. Release note: None --- pkg/sql/opt/memo/check_expr.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/pkg/sql/opt/memo/check_expr.go b/pkg/sql/opt/memo/check_expr.go index 0f7514f4fa32..3462a68eaf6f 100644 --- a/pkg/sql/opt/memo/check_expr.go +++ b/pkg/sql/opt/memo/check_expr.go @@ -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) { @@ -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) { @@ -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{}