Skip to content

Commit 08d813d

Browse files
committed
Add added column to CreateConstraint scope
1 parent f485924 commit 08d813d

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

enginetest/queries/alter_table_queries.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2107,6 +2107,20 @@ var AddColumnScripts = []ScriptTest{
21072107
},
21082108
},
21092109
},
2110+
{
2111+
Name: "add column with check constraint",
2112+
SetUpScript: []string{
2113+
"create table t (i int primary key, j int)",
2114+
"insert into t values (1, 2)",
2115+
},
2116+
Assertions: []ScriptTestAssertion{
2117+
{
2118+
Query: "alter table t add column k int check (k > 0)",
2119+
// TODO: this should be 1 rowsAffected https://github.com/dolthub/dolt/issues/9606
2120+
Expected: []sql.Row{{types.NewOkResult(0)}},
2121+
},
2122+
},
2123+
},
21102124
{
21112125
Name: "error cases",
21122126
Assertions: []ScriptTestAssertion{

sql/analyzer/fix_exec_indexes.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ type idxScope struct {
159159
childScopes []*idxScope
160160
ids []sql.ColumnId
161161
columns []string
162+
addedColumns sql.Schema
162163
children []sql.Node
163164
expressions []sql.Expression
164165
checks sql.CheckConstraints
@@ -301,6 +302,7 @@ func (s *idxScope) copy() *idxScope {
301302
parentScopes: parentCopy,
302303
columns: varsCopy,
303304
ids: idsCopy,
305+
addedColumns: s.addedColumns,
304306
subqueryScope: s.subqueryScope,
305307
triggerScope: s.triggerScope,
306308
insertSourceScope: s.insertSourceScope,
@@ -399,6 +401,9 @@ func (s *idxScope) visitChildren(n sql.Node) error {
399401
if err != nil {
400402
return err
401403
}
404+
if ac, ok := c.(*plan.AddColumn); ok {
405+
s.addedColumns = append(s.addedColumns, ac.Column())
406+
}
402407
s.childScopes = append(s.childScopes, cScope)
403408
s.children = append(s.children, newC)
404409
}
@@ -565,6 +570,11 @@ func (s *idxScope) visitSelf(n sql.Node) error {
565570
newDef := fixExprToScope(sql.Expression(col.Default), scope)
566571
n.DestSch[colIdx].Default = newDef.(*sql.ColumnDefaultValue)
567572
}
573+
case *plan.CreateCheck:
574+
addedScope := &idxScope{}
575+
addedScope.addSchema(s.addedColumns)
576+
scope := append(append(s.parentScopes, s.childScopes...), addedScope)
577+
s.expressions = append(s.expressions, fixExprToScope(n.Check.Expr, scope...))
568578
default:
569579
// Group By and Window functions already account for the new/old columns present from triggers
570580
// This means that when indexing the Projections, we should not include the trigger scope(s), which are

0 commit comments

Comments
 (0)