Skip to content

fix: include generation clause when altering a generated column - #274

Open
wilsonchau wants to merge 1 commit into
cloudspannerecosystem:mainfrom
wilsonchau:fix-alter-generated-column
Open

wilsonchau wants to merge 1 commit into
cloudspannerecosystem:mainfrom
wilsonchau:fix-alter-generated-column

Conversation

@wilsonchau

Copy link
Copy Markdown

When a generated column's type changes (for example widening STRING(100) to STRING(256)), the ALTER TABLE ALTER COLUMN statement leaves out the AS (...) [STORED] generation clause. Spanner rejects that statement with "Cannot change generation clause", because it reads the missing clause as an attempt to turn the generated column into a regular one.

eg:

# original:
CREATE TABLE test1 (
  col1 INT64,
  name STRING(100),
  stored_gen STRING(100) AS (LOWER(name)) STORED,
) PRIMARY KEY (col1)

# new
CREATE TABLE test1 (
  col1 INT64,
  name STRING(100),
  stored_gen STRING(256) AS (LOWER(name)) STORED,
) PRIMARY KEY (col1)

# diff before this change (rejected by Spanner)
ALTER TABLE test1 ALTER COLUMN stored_gen STRING(256)

# diff after this change
ALTER TABLE test1 ALTER COLUMN stored_gen STRING(256) AS ( lower ( name ) ) STORED

The fix adds the generation clause to the ALTER COLUMN statement, between NOT NULL and the DEFAULT clause, which is where Spanner's grammar puts it. A new test case covers widening a stored generated column, a virtual generated column and a regular column.

@google-cla

google-cla Bot commented Dec 3, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant