Skip to content

Commit

Permalink
opt: simplify assignment cast logic for inserts
Browse files Browse the repository at this point in the history
Creation of invalid assignment cast errors has been abstracted to a
helper function for reuse for other mutations in a future commit.

Release note: None
  • Loading branch information
mgartner committed Dec 9, 2021
1 parent b2048ec commit f86da8b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
11 changes: 3 additions & 8 deletions pkg/sql/opt/optbuilder/mutation_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
"github.com/cockroachdb/cockroach/pkg/sql/sem/builtins"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sqlerrors"
"github.com/cockroachdb/cockroach/pkg/sql/sqltelemetry"
"github.com/cockroachdb/cockroach/pkg/sql/types"
"github.com/cockroachdb/cockroach/pkg/util"
Expand Down Expand Up @@ -1403,8 +1404,6 @@ func checkDatumTypeFitsColumnType(col *cat.Column, typ *types.T) {
// identical to its target column's type, an assignment cast must be performed.
// If the value's type is the correct type, the assignment cast is a no-op.
func (mb *mutationBuilder) addAssignmentCasts(inScope *scope, outTypes []*types.T) *scope {
expr := inScope.expr

projectionScope := inScope.push()
projectionScope.cols = make([]scopeColumn, 0, len(inScope.cols))
for i := 0; i < len(inScope.cols); i++ {
Expand All @@ -1415,11 +1414,7 @@ func (mb *mutationBuilder) addAssignmentCasts(inScope *scope, outTypes []*types.
if !tree.ValidCast(srcType, targetType, tree.CastContextAssignment) {
ord := mb.tabID.ColumnOrdinal(mb.targetColList[i])
colName := string(mb.tab.Column(ord).ColName())
err := pgerror.Newf(pgcode.DatatypeMismatch,
"value type %s doesn't match type %s of column %q",
srcType, targetType, tree.ErrNameString(colName))
err = errors.WithHint(err, "you will need to rewrite or cast the expression")
panic(err)
panic(sqlerrors.NewInvalidAssignmentCastError(srcType, targetType, colName))
}

// Create a new column which casts the input column to the correct
Expand All @@ -1429,7 +1424,7 @@ func (mb *mutationBuilder) addAssignmentCasts(inScope *scope, outTypes []*types.
mb.b.synthesizeColumn(projectionScope, inScope.cols[i].name, outTypes[i], nil /* expr */, cast)
}

projectionScope.expr = mb.b.constructProject(expr, projectionScope.cols)
projectionScope.expr = mb.b.constructProject(inScope.expr, projectionScope.cols)
return projectionScope
}

Expand Down
1 change: 1 addition & 0 deletions pkg/sql/sqlerrors/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ go_library(
"//pkg/sql/pgwire/pgcode",
"//pkg/sql/pgwire/pgerror",
"//pkg/sql/sem/tree",
"//pkg/sql/types",
"@com_github_cockroachdb_errors//:errors",
],
)
17 changes: 17 additions & 0 deletions pkg/sql/sqlerrors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"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/sql/types"
"github.com/cockroachdb/errors"
)

Expand Down Expand Up @@ -49,6 +50,22 @@ func NewNonNullViolationError(columnName string) error {
return pgerror.Newf(pgcode.NotNullViolation, "null value in column %q violates not-null constraint", columnName)
}

// NewInvalidAssignmentCastError creates an error that is used when a mutation
// cannot be performed because there is not a valid assignment cast from a
// value's type to the type of the target column.
func NewInvalidAssignmentCastError(
sourceType *types.T, targetType *types.T, targetColName string,
) error {
return errors.WithHint(
pgerror.Newf(
pgcode.DatatypeMismatch,
"value type %s doesn't match type %s of column %q",
sourceType, targetType, tree.ErrNameString(targetColName),
),
"you will need to rewrite or cast the expression",
)
}

// NewGeneratedAlwaysAsIdentityColumnOverrideError creates an error for
// explicitly writing a column created with `GENERATED ALWAYS AS IDENTITY`
// syntax.
Expand Down

0 comments on commit f86da8b

Please sign in to comment.