@@ -410,11 +410,6 @@ func resolveStar(ctx *sql.Context, a *Analyzer, n sql.Node) (sql.Node, error) {
410410 })
411411}
412412
413- type columnInfo struct {
414- idx int
415- col * sql.Column
416- }
417-
418413// maybeAlias is a wrapper on UnresolvedColumn used only to defer the
419414// resolution of the column because it could be an alias and that
420415// phase of the analyzer has not run yet.
@@ -444,20 +439,23 @@ func resolveColumns(ctx *sql.Context, a *Analyzer, n sql.Node) (sql.Node, error)
444439 return n , nil
445440 }
446441
447- colMap := make (map [string ][]columnInfo )
448- idx := 0
442+ colMap := make (map [string ][]* sql.Column )
449443 for _ , child := range n .Children () {
450444 if ! child .Resolved () {
451445 return n , nil
452446 }
453447
454448 for _ , col := range child .Schema () {
455- colMap [col .Name ] = append (colMap [col .Name ], columnInfo {idx , col })
456- idx ++
449+ colMap [col .Name ] = append (colMap [col .Name ], col )
457450 }
458451 }
459452
460- return n .TransformExpressionsUp (func (e sql.Expression ) (sql.Expression , error ) {
453+ expressioner , ok := n .(sql.Expressioner )
454+ if ! ok {
455+ return n , nil
456+ }
457+
458+ return expressioner .TransformExpressions (func (e sql.Expression ) (sql.Expression , error ) {
461459 a .Log ("transforming expression of type: %T" , e )
462460 if e .Resolved () {
463461 return e , nil
@@ -468,7 +466,7 @@ func resolveColumns(ctx *sql.Context, a *Analyzer, n sql.Node) (sql.Node, error)
468466 return e , nil
469467 }
470468
471- columnsInfo , ok := colMap [uc .Name ()]
469+ columns , ok := colMap [uc .Name ()]
472470 if ! ok {
473471 if uc .Table () != "" {
474472 return nil , ErrColumnTableNotFound .New (uc .Table (), uc .Name ())
@@ -482,11 +480,11 @@ func resolveColumns(ctx *sql.Context, a *Analyzer, n sql.Node) (sql.Node, error)
482480 }
483481 }
484482
485- var ci columnInfo
483+ var col * sql. Column
486484 var found bool
487- for _ , c := range columnsInfo {
488- if c .col . Source == uc .Table () {
489- ci = c
485+ for _ , c := range columns {
486+ if c .Source == uc .Table () {
487+ col = c
490488 found = true
491489 break
492490 }
@@ -505,14 +503,30 @@ func resolveColumns(ctx *sql.Context, a *Analyzer, n sql.Node) (sql.Node, error)
505503 }
506504 }
507505
508- a .Log ("column resolved to %q.%q" , ci .col .Source , ci .col .Name )
506+ var schema sql.Schema
507+ switch n := n .(type ) {
508+ // If expressioner and unary node we must take the
509+ // child's schema to correctly select the indexes
510+ // in the row is going to be evaluated in this node
511+ case * plan.Project , * plan.Filter , * plan.GroupBy , * plan.Sort :
512+ schema = n .Children ()[0 ].Schema ()
513+ default :
514+ schema = n .Schema ()
515+ }
516+
517+ idx := schema .IndexOf (col .Name , col .Source )
518+ if idx < 0 {
519+ return nil , ErrColumnNotFound .New (col .Name )
520+ }
521+
522+ a .Log ("column resolved to %q.%q" , col .Source , col .Name )
509523
510524 return expression .NewGetFieldWithTable (
511- ci . idx ,
512- ci . col .Type ,
513- ci . col .Source ,
514- ci . col .Name ,
515- ci . col .Nullable ,
525+ idx ,
526+ col .Type ,
527+ col .Source ,
528+ col .Name ,
529+ col .Nullable ,
516530 ), nil
517531 })
518532 })
0 commit comments