Skip to content

Commit c1974a0

Browse files
author
James Cor
committed
maybe?
1 parent 48b91d9 commit c1974a0

File tree

2 files changed

+84
-27
lines changed

2 files changed

+84
-27
lines changed

sql/analyzer/fix_exec_indexes.go

Lines changed: 73 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -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

167193
func (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 {

sql/analyzer/inserts.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ import (
3131
func resolveInsertRows(ctx *sql.Context, a *Analyzer, n sql.Node, scope *plan.Scope, sel RuleSelector, qFlags *sql.QueryFlags) (sql.Node, transform.TreeIdentity, error) {
3232
if _, ok := n.(*plan.TriggerExecutor); ok {
3333
return n, transform.SameTree, nil
34-
} else if _, ok := n.(*plan.CreateProcedure); ok {
34+
}
35+
if _, ok := n.(*plan.CreateProcedure); ok {
3536
return n, transform.SameTree, nil
3637
}
3738
// We capture all INSERTs along the tree, such as those inside of block statements.
@@ -50,14 +51,22 @@ func resolveInsertRows(ctx *sql.Context, a *Analyzer, n sql.Node, scope *plan.Sc
5051

5152
source := insert.Source
5253
// TriggerExecutor has already been analyzed
53-
if _, ok := insert.Source.(*plan.TriggerExecutor); !ok && !insert.LiteralValueSource {
54+
if _, isTrigExec := insert.Source.(*plan.TriggerExecutor); !isTrigExec && !insert.LiteralValueSource {
5455
// Analyze the source of the insert independently
5556
if _, ok := insert.Source.(*plan.Values); ok {
5657
scope = scope.NewScope(plan.NewProject(
5758
expression.SchemaToGetFields(insert.Source.Schema()[:len(insert.ColumnNames)], sql.ColSet{}),
5859
plan.NewSubqueryAlias("dummy", "", insert.Source),
5960
))
6061
}
62+
//if proj, ok := insert.Source.(*plan.Project); ok {
63+
// if _, ok := proj.Child.(*plan.GroupBy); ok {
64+
// scope = &plan.Scope{}
65+
// }
66+
// if _, ok := proj.Child.(*plan.Window); ok {
67+
// scope = &plan.Scope{}
68+
// }
69+
//}
6170
source, _, err = a.analyzeWithSelector(ctx, insert.Source, scope, SelectAllBatches, newInsertSourceSelector(sel), qFlags)
6271
if err != nil {
6372
return nil, transform.SameTree, err

0 commit comments

Comments
 (0)