Skip to content

Commit 85527db

Browse files
authored
Merge pull request #3199 from dolthub/angela/alter_table
Add added column to CreateConstraint scope
2 parents f485924 + c24cf80 commit 85527db

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

enginetest/queries/alter_table_queries.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2107,6 +2107,28 @@ 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+
Query: "alter table t add column l int check (l is not null)",
2124+
ExpectedErr: sql.ErrCheckConstraintViolated,
2125+
},
2126+
{
2127+
Query: "alter table t add column m int default 0 check (m != 0)",
2128+
ExpectedErr: sql.ErrCheckConstraintViolated,
2129+
},
2130+
},
2131+
},
21102132
{
21112133
Name: "error cases",
21122134
Assertions: []ScriptTestAssertion{

sql/analyzer/fix_exec_indexes.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,12 @@ type idxScope struct {
159159
childScopes []*idxScope
160160
ids []sql.ColumnId
161161
columns []string
162-
children []sql.Node
163-
expressions []sql.Expression
164-
checks sql.CheckConstraints
162+
// Columns added from AddColumn are not included in the ResolvedTable yet. For columns that are added with a
163+
// constraint, we need to add the new columns to the scope so CreateCheck gets the right exec index
164+
addedColumns sql.Schema
165+
children []sql.Node
166+
expressions []sql.Expression
167+
checks sql.CheckConstraints
165168

166169
triggerScope bool
167170
insertSourceScope bool
@@ -301,6 +304,7 @@ func (s *idxScope) copy() *idxScope {
301304
parentScopes: parentCopy,
302305
columns: varsCopy,
303306
ids: idsCopy,
307+
addedColumns: s.addedColumns,
304308
subqueryScope: s.subqueryScope,
305309
triggerScope: s.triggerScope,
306310
insertSourceScope: s.insertSourceScope,
@@ -399,6 +403,9 @@ func (s *idxScope) visitChildren(n sql.Node) error {
399403
if err != nil {
400404
return err
401405
}
406+
if ac, ok := c.(*plan.AddColumn); ok {
407+
s.addedColumns = append(s.addedColumns, ac.Column())
408+
}
402409
s.childScopes = append(s.childScopes, cScope)
403410
s.children = append(s.children, newC)
404411
}
@@ -565,6 +572,11 @@ func (s *idxScope) visitSelf(n sql.Node) error {
565572
newDef := fixExprToScope(sql.Expression(col.Default), scope)
566573
n.DestSch[colIdx].Default = newDef.(*sql.ColumnDefaultValue)
567574
}
575+
case *plan.CreateCheck:
576+
addedScope := &idxScope{}
577+
addedScope.addSchema(s.addedColumns)
578+
scope := append(append(s.parentScopes, s.childScopes...), addedScope)
579+
s.expressions = append(s.expressions, fixExprToScope(n.Check.Expr, scope...))
568580
default:
569581
// Group By and Window functions already account for the new/old columns present from triggers
570582
// This means that when indexing the Projections, we should not include the trigger scope(s), which are

0 commit comments

Comments
 (0)