Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

schemachanger: Annotate all tables if ALTER TABLE IF EXISTS on non-existent table #99839

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/alter_table
Original file line number Diff line number Diff line change
Expand Up @@ -3184,3 +3184,13 @@ INSERT INTO t_98269 VALUES (0);

statement error pgcode 0A000 .* cluster_logical_timestamp\(\): nil txn in cluster context
ALTER TABLE t_98269 ADD COLUMN j DECIMAL NOT NULL DEFAULT cluster_logical_timestamp();

# In #99185, we saw test failures that result from adding a foreign key
# constraint to a non-existent table with IF EXISTS because we require all tables
# in the stmt should be marked as non-existent or fully resolved. We didn't do
# anything to the referenced table name, so the validation logic complained that
# referenced table name is not fully resolved nor marked as non-existent.
subtest alter_non_existent_table_with_if_exists

statement ok
ALTER TABLE IF EXISTS t_non_existent_99185 ADD FOREIGN KEY (i) REFERENCES t_other_99185 (i);
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,12 @@ func AlterTable(b BuildCtx, n *tree.AlterTable) {
})
_, target, tbl := scpb.FindTable(elts)
if tbl == nil {
b.MarkNameAsNonExistent(&tn)
// Mark all table names (`tn` and others) in this ALTER TABLE stmt as non-existent.
tree.NewFmtCtx(tree.FmtSimple, tree.FmtReformatTableNames(func(
ctx *tree.FmtCtx, name *tree.TableName,
) {
b.MarkNameAsNonExistent(name)
})).FormatNode(n)
return
}
if target != scpb.ToPublic {
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/sem/tree/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ func FmtPlaceholderFormat(placeholderFn func(_ *FmtCtx, _ *Placeholder)) FmtCtxO
}
}

// FmtReformatTableNames modifies FmtCtx to to substitute the printing of table
// FmtReformatTableNames modifies FmtCtx to substitute the printing of table
// naFmtParsable using the provided function.
func FmtReformatTableNames(tableNameFmt func(*FmtCtx, *TableName)) FmtCtxOption {
return func(ctx *FmtCtx) {
Expand Down