@@ -362,8 +362,20 @@ func (s *idxScope) visitChildren(n sql.Node) error {
362362 }
363363 s .children = append (s .children , newC )
364364 }
365+ case * plan.GroupBy :
366+ for _ , c := range n .Children () {
367+ newC , cScope , err := assignIndexesHelper (c , s )
368+ if err != nil {
369+ return err
370+ }
371+ s .childScopes = append (s .childScopes , cScope )
372+ s .children = append (s .children , newC )
373+ }
365374 default :
366375 for _ , c := range n .Children () {
376+ if _ , ok := c .(* plan.GroupBy ); ok {
377+ print ()
378+ }
367379 newC , cScope , err := assignIndexesHelper (c , s )
368380 if err != nil {
369381 return err
@@ -535,6 +547,24 @@ func (s *idxScope) visitSelf(n sql.Node) error {
535547 n .DestSch [colIdx ].Default = newDef .(* sql.ColumnDefaultValue )
536548 }
537549 default :
550+ if proj , isProj := n .(* plan.Project ); isProj {
551+ if _ , isGb := proj .Child .(* plan.GroupBy ); isGb {
552+ for _ , e := range proj .Expressions () {
553+ // default nodes can't see lateral join nodes, unless we're in lateral
554+ // join and lateral scopes are promoted to parent status
555+ s .expressions = append (s .expressions , fixExprToScope (e , s .childScopes ... ))
556+ }
557+ return nil
558+ }
559+ if _ , isGb := proj .Child .(* plan.Window ); isGb {
560+ for _ , e := range proj .Expressions () {
561+ // default nodes can't see lateral join nodes, unless we're in lateral
562+ // join and lateral scopes are promoted to parent status
563+ s .expressions = append (s .expressions , fixExprToScope (e , s .childScopes ... ))
564+ }
565+ return nil
566+ }
567+ }
538568 if ne , ok := n .(sql.Expressioner ); ok {
539569 scope := append (s .parentScopes , s .childScopes ... )
540570 for _ , e := range ne .Expressions () {
@@ -694,6 +724,12 @@ func fixExprToScope(e sql.Expression, scopes ...*idxScope) sql.Expression {
694724 // don't have the destination schema, and column references in default values are determined in the build phase)
695725
696726 idx , _ := newScope .getIdxId (e .Id (), e .String ())
727+ if e .String () == "ref_tbl.id" {
728+ print ()
729+ }
730+ if e .String () == "new.id" {
731+ print ()
732+ }
697733 if idx >= 0 {
698734 return e .WithIndex (idx ), transform .NewTree , nil
699735 }
0 commit comments