@@ -165,6 +165,7 @@ type idxScope struct {
165165
166166 triggerScope bool
167167 insertSourceScope bool
168+ subqueryScope bool
168169}
169170
170171func (s * idxScope ) inTrigger () bool {
@@ -296,10 +297,13 @@ func (s *idxScope) copy() *idxScope {
296297 copy (idsCopy , s .ids )
297298 }
298299 return & idxScope {
299- lateralScopes : lateralCopy ,
300- parentScopes : parentCopy ,
301- columns : varsCopy ,
302- ids : idsCopy ,
300+ lateralScopes : lateralCopy ,
301+ parentScopes : parentCopy ,
302+ columns : varsCopy ,
303+ ids : idsCopy ,
304+ subqueryScope : s .subqueryScope ,
305+ triggerScope : s .triggerScope ,
306+ insertSourceScope : s .insertSourceScope ,
303307 }
304308}
305309
@@ -568,7 +572,7 @@ func (s *idxScope) visitSelf(n sql.Node) error {
568572 if proj , isProj := n .(* plan.Project ); isProj {
569573 switch proj .Child .(type ) {
570574 case * plan.GroupBy , * plan.Window :
571- if s .inTrigger () && s . inInsertSource () {
575+ if s .inTrigger () && ! s . subqueryScope {
572576 for _ , e := range proj .Expressions () {
573577 s .expressions = append (s .expressions , fixExprToScope (e , s .childScopes ... ))
574578 }
@@ -581,18 +585,15 @@ func (s *idxScope) visitSelf(n sql.Node) error {
581585 // default nodes can't see lateral join nodes, unless we're in lateral
582586 // join and lateral scopes are promoted to parent status
583587 for _ , e := range ne .Expressions () {
584- // OrderedAggregations are special as they append results to the outer scope row
588+ // OrderedAggregations are special as they append a new field to the outer scope row
585589 // We need to account for this extra column in the rows when assigning indexes
586590 // Example: gms/expression/function/aggregation/group_concat.go:groupConcatBuffer.Update()
587- if ordAgg , isOrdAgg := e .(sql.OrderedAggregation ); isOrdAgg {
588- selExprs := ordAgg .OutputExpressions ()
591+ if _ , isOrdAgg := e .(sql.OrderedAggregation ); isOrdAgg {
589592 selScope := & idxScope {}
590- for _ , expr := range selExprs {
591- selScope .columns = append (selScope .columns , expr .String ())
592- if gf , isGf := expr .(* expression.GetField ); isGf {
593- selScope .ids = append (selScope .ids , gf .Id ())
594- }
593+ if idExpr , isIdExpr := e .(sql.IdExpression ); isIdExpr {
594+ selScope .ids = append (selScope .ids , idExpr .Id ())
595595 }
596+ selScope .columns = append (selScope .columns , e .String ())
596597 scope = append (scope , selScope )
597598 }
598599 s .expressions = append (s .expressions , fixExprToScope (e , scope ... ))
@@ -620,7 +621,6 @@ func (s *idxScope) finalizeSelf(n sql.Node) (sql.Node, error) {
620621 }
621622
622623 s .ids = columnIdsForNode (n )
623-
624624 s .addSchema (n .Schema ())
625625 var err error
626626 if s .children != nil {
@@ -752,14 +752,17 @@ func fixExprToScope(e sql.Expression, scopes ...*idxScope) sql.Expression {
752752 // this error for the case of DEFAULT in a `plan.Values`, since we analyze the insert source in isolation (we
753753 // don't have the destination schema, and column references in default values are determined in the build phase)
754754
755+ // TODO: If we don't find a valid index for a field, we should report an error
755756 idx , _ := newScope .getIdxId (e .Id (), e .String ())
756757 if idx >= 0 {
757758 return e .WithIndex (idx ), transform .NewTree , nil
758759 }
759760 return e , transform .SameTree , nil
760761 case * plan.Subquery :
761762 // this |outScope| prepends the subquery scope
762- newQ , _ , err := assignIndexesHelper (e .Query , newScope .push ())
763+ subqueryScope := newScope .push ()
764+ subqueryScope .subqueryScope = true
765+ newQ , _ , err := assignIndexesHelper (e .Query , subqueryScope )
763766 if err != nil {
764767 return nil , transform .SameTree , err
765768 }
0 commit comments