Skip to content

Commit

Permalink
sql/schemachanger: Support enum types for DSC impl of ALTER COLUMN ..…
Browse files Browse the repository at this point in the history
… ALTER TYPE

Previously, attempts to alter a column type using an enum were blocked
due to failures during backfill, which would result in the error 'cannot
resolve types in DistSQL by name.' This change resolves that issue by
ensuring that all type names are resolved when populating the USING
expression for the alter

Epic: CRDB-25314
Closes cockroachdb#132936
Release note: none
  • Loading branch information
spilchen committed Oct 29, 2024
1 parent ba425ba commit 146a5cc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
2 changes: 1 addition & 1 deletion pkg/sql/logictest/testdata/logic_test/enums
Original file line number Diff line number Diff line change
Expand Up @@ -1090,7 +1090,7 @@ statement ok
CREATE TABLE enum_data_type (x STRING);
INSERT INTO enum_data_type VALUES ('notagreeting')

statement error pq: invalid input value for enum greeting: "notagreeting"
statement error pq: .*invalid input value for enum greeting: "notagreeting"
ALTER TABLE enum_data_type ALTER COLUMN x SET DATA TYPE greeting USING x::greeting

statement ok
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,14 +284,6 @@ func handleGeneralColumnConversion(
"old active version; ALTER COLUMN TYPE requires backfill. Reverting to legacy handling"))
}

// TODO(#132936): Not yet supported in the DSC. Throwing an error to trigger
// fallback to legacy.
if newColType.Type.Family() == types.EnumFamily {
panic(scerrors.NotImplementedErrorf(t,
"backfilling during ALTER COLUMN TYPE for an enum column "+
"type is not supported"))
}

// Generate the ID of the new column we are adding.
newColID := b.NextTableColumnID(tbl)
newColType.ColumnID = newColID
Expand Down Expand Up @@ -447,7 +439,7 @@ func getComputeExpressionForBackfill(
return
}

_, _, _, err = schemaexpr.DequalifyAndValidateExprImpl(b, expr, newColType.Type,
typedExpr, _, _, err := schemaexpr.DequalifyAndValidateExprImpl(b, expr, newColType.Type,
tree.AlterColumnTypeUsingExpr, b.SemaCtx(), volatility.Volatile, tn, b.ClusterSettings().Version.ActiveVersion(b),
func() colinfo.ResultColumns {
return getNonDropResultColumns(b, tableID)
Expand All @@ -456,6 +448,14 @@ func getComputeExpressionForBackfill(
return columnLookupFn(b, tableID, columnName)
},
)
if err != nil {
return
}

// Return the updated expression from DequalifyAndValidateExprImpl. For expressions
// involving user-defined types like enums, the types will be resolved to ensure
// they can be used during backfill.
expr, err = parser.ParseExpr(typedExpr)
return
}

Expand Down

0 comments on commit 146a5cc

Please sign in to comment.