@@ -18,11 +18,10 @@ import (
18
18
"fmt"
19
19
"strings"
20
20
21
- "github.com/dolthub/go-mysql-server/sql/planbuilder"
22
-
23
21
"github.com/dolthub/go-mysql-server/sql"
24
22
"github.com/dolthub/go-mysql-server/sql/expression"
25
23
"github.com/dolthub/go-mysql-server/sql/plan"
24
+ "github.com/dolthub/go-mysql-server/sql/planbuilder"
26
25
"github.com/dolthub/go-mysql-server/sql/transform"
27
26
)
28
27
@@ -429,42 +428,51 @@ func (s *idxScope) visitSelf(n sql.Node) error {
429
428
s .expressions = append (s .expressions , fixExprToScope (e , scopes ... ))
430
429
}
431
430
case * plan.InsertInto :
432
- rightSchema := make (sql.Schema , len (n .Destination .Schema ())* 2 )
433
431
// schema = [oldrow][newrow]
434
- for oldRowIdx , c := range n .Destination .Schema () {
435
- rightSchema [oldRowIdx ] = c
436
- newRowIdx := len (n .Destination .Schema ()) + oldRowIdx
437
- if values , ok := n .Source .(* plan.Values ); ok {
438
- // The source table is either named via VALUES(...) AS ... or has
439
- // the default value planbuilder.OnDupValuesPrefix
432
+ destSch := n .Destination .Schema ()
433
+ srcSch := n .Source .Schema ()
434
+ rightSchema := make (sql.Schema , len (destSch )* 2 )
435
+ if values , isValues := n .Source .(* plan.Values ); isValues {
436
+ for oldRowIdx , c := range destSch {
440
437
newC := c .Copy ()
441
438
newC .Source = values .AliasName
442
439
if values .ColumnNames != nil {
443
440
newC .Name = values .ColumnNames [newC .Name ]
444
441
}
442
+
443
+ newRowIdx := len (destSch ) + oldRowIdx
444
+ rightSchema [oldRowIdx ] = c
445
445
rightSchema [newRowIdx ] = newC
446
- } else if len (n .Destination .Schema ()) != len (n .Source .Schema ()) {
447
- newC := c .Copy ()
448
- newC .Source = planbuilder .OnDupValuesPrefix
449
- rightSchema [newRowIdx ] = newC
450
- } else {
446
+ }
447
+ } else {
448
+ for oldRowIdx , c := range destSch {
451
449
// find source index that aligns with dest column
452
- var matched bool
450
+ var newC * sql. Column
453
451
for j , sourceCol := range n .ColumnNames {
454
452
if strings .EqualFold (c .Name , sourceCol ) {
455
- rightSchema [newRowIdx ] = n .Source .Schema ()[j ]
456
- matched = true
453
+ newC = srcSch [j ]
457
454
break
458
455
}
459
456
}
460
- if ! matched {
461
- // todo: this is only used for load data. load data errors
462
- // without a fallback, and fails to resolve defaults if I
463
- // define the columns upfront.
464
- rightSchema [newRowIdx ] = n .Source .Schema ()[oldRowIdx ]
457
+ // unable to find column, use copy with OnDupValuesPrefix or fallback
458
+ if newC == nil {
459
+ if len (destSch ) != len (srcSch ) {
460
+ newC = c .Copy ()
461
+ newC .Source = planbuilder .OnDupValuesPrefix
462
+ } else {
463
+ // todo: this is only used for load data. load data errors
464
+ // without a fallback, and fails to resolve defaults if I
465
+ // define the columns upfront.
466
+ newC = srcSch [oldRowIdx ]
467
+ }
465
468
}
469
+ newRowIdx := len (destSch ) + oldRowIdx
470
+
471
+ rightSchema [oldRowIdx ] = c
472
+ rightSchema [newRowIdx ] = newC
466
473
}
467
474
}
475
+
468
476
rightScope := & idxScope {}
469
477
rightScope .addSchema (rightSchema )
470
478
dstScope := s .childScopes [0 ]
0 commit comments