Skip to content

Commit

Permalink
Merge pull request #91618 from cockroachdb/blathers/backport-release-…
Browse files Browse the repository at this point in the history
…22.2-90552
  • Loading branch information
Xiang-Gu authored Nov 10, 2022
2 parents 764c899 + 81bfa8c commit b305359
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 17 deletions.
1 change: 1 addition & 0 deletions pkg/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ ALL_TESTS = [
"//pkg/sql/catalog/catprivilege:catprivilege_test",
"//pkg/sql/catalog/colinfo:colinfo_test",
"//pkg/sql/catalog/dbdesc:dbdesc_test",
"//pkg/sql/catalog/descpb:descpb_disallowed_imports_test",
"//pkg/sql/catalog/descpb:descpb_test",
"//pkg/sql/catalog/descs:descs_test",
"//pkg/sql/catalog/funcdesc:funcdesc_test",
Expand Down
7 changes: 6 additions & 1 deletion pkg/sql/catalog/descpb/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ load("@rules_proto//proto:defs.bzl", "proto_library")
load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
load("//build:STRINGER.bzl", "stringer")
load("//pkg/testutils/buildutil:buildutil.bzl", "disallowed_imports_test")

go_library(
name = "descpb",
Expand All @@ -23,7 +24,6 @@ go_library(
deps = [
"//pkg/keys",
"//pkg/sql/catalog/catpb",
"//pkg/sql/parser",
"//pkg/sql/pgwire/pgcode",
"//pkg/sql/pgwire/pgerror",
"//pkg/sql/protoreflect",
Expand Down Expand Up @@ -95,4 +95,9 @@ stringer(
typ = "FormatVersion",
)

disallowed_imports_test(
"descpb",
disallowed_list = ["//pkg/sql/parser"],
)

get_x_data(name = "get_x_data")
15 changes: 0 additions & 15 deletions pkg/sql/catalog/descpb/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,12 @@
package descpb

import (
"github.com/cockroachdb/cockroach/pkg/sql/parser"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/util/errorutil/unimplemented"
"github.com/cockroachdb/errors"
)

// HasNullDefault checks that the column descriptor has a default of NULL.
func (desc *ColumnDescriptor) HasNullDefault() bool {
if !desc.HasDefault() {
return false
}
defaultExpr, err := parser.ParseExpr(*desc.DefaultExpr)
if err != nil {
panic(errors.NewAssertionErrorWithWrappedErrf(err,
"failed to parse default expression %s", *desc.DefaultExpr))
}
return defaultExpr == tree.DNull
}

// HasDefault returns true if the column has a default value.
func (desc *ColumnDescriptor) HasDefault() bool {
return desc.DefaultExpr != nil
Expand Down
6 changes: 5 additions & 1 deletion pkg/sql/catalog/table_elements.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,10 @@ type Column interface {
// HasDefault returns true iff the column has a default expression set.
HasDefault() bool

// HasNullDefault returns true if the column has a default expression and
// that expression is NULL.
HasNullDefault() bool

// GetDefaultExpr returns the column default expression if it exists,
// empty string otherwise.
GetDefaultExpr() string
Expand Down Expand Up @@ -808,7 +812,7 @@ func ColumnNeedsBackfill(col Column) bool {
// - computed columns
// - non-nullable columns (note: if a non-nullable column doesn't have a
// default value, the backfill will fail unless the table is empty).
if col.ColumnDesc().HasNullDefault() {
if col.HasNullDefault() {
return false
}
return col.HasDefault() || !col.IsNullable() || col.IsComputed()
Expand Down
14 changes: 14 additions & 0 deletions pkg/sql/catalog/tabledesc/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/sql/catalog/colinfo"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/schemaexpr"
"github.com/cockroachdb/cockroach/pkg/sql/parser"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/types"
"github.com/cockroachdb/cockroach/pkg/util/protoutil"
Expand Down Expand Up @@ -103,6 +104,19 @@ func (w column) HasDefault() bool {
return w.desc.HasDefault()
}

// HasNullDefault checks that the column descriptor has a default of NULL.
func (w column) HasNullDefault() bool {
if !w.HasDefault() {
return false
}
// We ignore the error because what are we going to do with it? It means
// that the default expressions is not parsable. Somebody with a context
// who needs to use it will be in a better place to log it. If it is not
// parsable, it is not NULL.
defaultExpr, _ := parser.ParseExpr(w.GetDefaultExpr())
return defaultExpr == tree.DNull
}

// GetDefaultExpr returns the column default expression if it exists,
// empty string otherwise.
func (w column) GetDefaultExpr() string {
Expand Down

0 comments on commit b305359

Please sign in to comment.