@@ -173,19 +173,33 @@ func resolveColumnDefaults(ctx *sql.Context, _ *Analyzer, n sql.Node, _ *plan.Sc
173173 col := sch [index ]
174174
175175 eWrapper := expression .WrapExpression (node .Default )
176- newExpr , same , err := resolveColumnDefault (ctx , col , eWrapper )
176+ newExpr , sameColumnDefault , err := resolveColumnDefault (ctx , col , eWrapper )
177177 if err != nil {
178178 return node , transform .SameTree , err
179179 }
180- if same {
181- return node , transform .SameTree , nil
182- }
183180
184181 newNode , err := node .WithDefault (newExpr )
185182 if err != nil {
186183 return node , transform .SameTree , err
187184 }
188- return newNode , transform .NewTree , nil
185+
186+ // After resolving the new column default value, ensure all column defaults in the target schema are resolved
187+ newNode , sameTargetSchema , err := transform .OneNodeExpressions (newNode , func (e sql.Expression ) (sql.Expression , transform.TreeIdentity , error ) {
188+ eWrapper , ok := e .(* expression.Wrapper )
189+ if ! ok {
190+ return e , transform .SameTree , nil
191+ }
192+
193+ col , err := lookupColumnForTargetSchema (ctx , node , colIndex )
194+ if err != nil {
195+ return nil , transform .SameTree , err
196+ }
197+ colIndex ++
198+
199+ return resolveColumnDefault (ctx , col , eWrapper )
200+ })
201+
202+ return newNode , sameColumnDefault && sameTargetSchema , err
189203 case sql.SchemaTarget :
190204 return transform .OneNodeExpressions (n , func (e sql.Expression ) (sql.Expression , transform.TreeIdentity , error ) {
191205 eWrapper , ok := e .(* expression.Wrapper )
@@ -338,7 +352,7 @@ func lookupColumnForTargetSchema(_ *sql.Context, node sql.SchemaTarget, colIndex
338352 if index == - 1 {
339353 return nil , sql .ErrTableColumnNotFound .New (n2 .Table , n2 .ColumnName )
340354 }
341- return n2 . Schema () [index ], nil
355+ return schema [index ], nil
342356 default :
343357 if colIndex < len (schema ) {
344358 return schema [colIndex ], nil
0 commit comments