@@ -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