@@ -161,7 +161,33 @@ type idxScope struct {
161161 children []sql.Node
162162 expressions []sql.Expression
163163 checks sql.CheckConstraints
164- triggerScope bool
164+
165+ triggerScope bool
166+ insertSourceScope bool
167+ }
168+
169+ func (s * idxScope ) inTrigger () bool {
170+ if s == nil {
171+ return false
172+ }
173+ for _ , ps := range s .parentScopes {
174+ if ps .inTrigger () {
175+ return true
176+ }
177+ }
178+ return s .triggerScope
179+ }
180+
181+ func (s * idxScope ) inInsertSource () bool {
182+ if s == nil {
183+ return false
184+ }
185+ for _ , ps := range s .parentScopes {
186+ if ps .inInsertSource () {
187+ return true
188+ }
189+ }
190+ return s .insertSourceScope
165191}
166192
167193func (s * idxScope ) addSchema (sch sql.Schema ) {
@@ -273,6 +299,9 @@ func (s *idxScope) copy() *idxScope {
273299 parentScopes : parentCopy ,
274300 columns : varsCopy ,
275301 ids : idsCopy ,
302+
303+ triggerScope : s .triggerScope ,
304+ insertSourceScope : s .insertSourceScope ,
276305 }
277306}
278307
@@ -339,10 +368,42 @@ func (s *idxScope) visitChildren(n sql.Node) error {
339368 // keep only the first union scope to avoid double counting
340369 s .childScopes = append (s .childScopes , keepScope )
341370 case * plan.InsertInto :
342- newSrc , _ , err := assignIndexesHelper (n .Source , s )
371+ // TODO: special case into sources when in triggers with groupby/window functions
372+ var newSrc sql.Node
373+ var err error
374+
375+ // TODO: do something cleaner
376+ //var isInTrigger bool
377+ //for _, ps := range s.parentScopes {
378+ // if ps.triggerScope {
379+ // isInTrigger = true
380+ // break
381+ // }
382+ //}
383+ //if isInTrigger {
384+ // if proj, isProj := n.Source.(*plan.Project); isProj {
385+ // switch proj.Child.(type) {
386+ // case *plan.GroupBy, *plan.Window:
387+ // subScope := s.copy()
388+ // subScope.parentScopes = subScope.parentScopes[:0]
389+ // newSrc, _, err = assignIndexesHelper(proj, subScope)
390+ // default:
391+ // newSrc, _, err = assignIndexesHelper(n.Source, s)
392+ // }
393+ // } else {
394+ // newSrc, _, err = assignIndexesHelper(n.Source, s)
395+ // }
396+ //} else {
397+ // newSrc, _, err = assignIndexesHelper(n.Source, s)
398+ //}
399+
400+ s .insertSourceScope = true
401+ newSrc , _ , err = assignIndexesHelper (n .Source , s )
343402 if err != nil {
344403 return err
345404 }
405+ s .insertSourceScope = false
406+
346407 newDst , dScope , err := assignIndexesHelper (n .Destination , s )
347408 if err != nil {
348409 return err
@@ -362,15 +423,6 @@ func (s *idxScope) visitChildren(n sql.Node) error {
362423 }
363424 s .children = append (s .children , newC )
364425 }
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- }
374426 default :
375427 for _ , c := range n .Children () {
376428 if _ , ok := c .(* plan.GroupBy ); ok {
@@ -547,22 +599,18 @@ func (s *idxScope) visitSelf(n sql.Node) error {
547599 n .DestSch [colIdx ].Default = newDef .(* sql.ColumnDefaultValue )
548600 }
549601 default :
602+ // TODO: this very specific pattern
550603 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 ... ))
604+ switch proj .Child .(type ) {
605+ case * plan.GroupBy , * plan.Window :
606+ if s .inTrigger () && s .inInsertSource () {
607+ for _ , e := range proj .Expressions () {
608+ // default nodes can't see lateral join nodes, unless we're in lateral
609+ // join and lateral scopes are promoted to parent status
610+ s .expressions = append (s .expressions , fixExprToScope (e , s .childScopes ... ))
611+ }
612+ return nil
564613 }
565- return nil
566614 }
567615 }
568616 if ne , ok := n .(sql.Expressioner ); ok {
0 commit comments