@@ -165,6 +165,7 @@ type idxScope struct {
165
165
166
166
triggerScope bool
167
167
insertSourceScope bool
168
+ subqueryScope bool
168
169
}
169
170
170
171
func (s * idxScope ) inTrigger () bool {
@@ -296,10 +297,13 @@ func (s *idxScope) copy() *idxScope {
296
297
copy (idsCopy , s .ids )
297
298
}
298
299
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 ,
303
307
}
304
308
}
305
309
@@ -568,7 +572,7 @@ func (s *idxScope) visitSelf(n sql.Node) error {
568
572
if proj , isProj := n .(* plan.Project ); isProj {
569
573
switch proj .Child .(type ) {
570
574
case * plan.GroupBy , * plan.Window :
571
- if s .inTrigger () && s . inInsertSource () {
575
+ if s .inTrigger () && ! s . subqueryScope {
572
576
for _ , e := range proj .Expressions () {
573
577
s .expressions = append (s .expressions , fixExprToScope (e , s .childScopes ... ))
574
578
}
@@ -581,18 +585,15 @@ func (s *idxScope) visitSelf(n sql.Node) error {
581
585
// default nodes can't see lateral join nodes, unless we're in lateral
582
586
// join and lateral scopes are promoted to parent status
583
587
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
585
589
// We need to account for this extra column in the rows when assigning indexes
586
590
// 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 {
589
592
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 ())
595
595
}
596
+ selScope .columns = append (selScope .columns , e .String ())
596
597
scope = append (scope , selScope )
597
598
}
598
599
s .expressions = append (s .expressions , fixExprToScope (e , scope ... ))
@@ -620,7 +621,6 @@ func (s *idxScope) finalizeSelf(n sql.Node) (sql.Node, error) {
620
621
}
621
622
622
623
s .ids = columnIdsForNode (n )
623
-
624
624
s .addSchema (n .Schema ())
625
625
var err error
626
626
if s .children != nil {
@@ -752,14 +752,17 @@ func fixExprToScope(e sql.Expression, scopes ...*idxScope) sql.Expression {
752
752
// this error for the case of DEFAULT in a `plan.Values`, since we analyze the insert source in isolation (we
753
753
// don't have the destination schema, and column references in default values are determined in the build phase)
754
754
755
+ // TODO: If we don't find a valid index for a field, we should report an error
755
756
idx , _ := newScope .getIdxId (e .Id (), e .String ())
756
757
if idx >= 0 {
757
758
return e .WithIndex (idx ), transform .NewTree , nil
758
759
}
759
760
return e , transform .SameTree , nil
760
761
case * plan.Subquery :
761
762
// 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 )
763
766
if err != nil {
764
767
return nil , transform .SameTree , err
765
768
}
0 commit comments