Skip to content

Commit 0196445

Browse files
author
James Cor
committed
clean up
1 parent 4dfa431 commit 0196445

File tree

3 files changed

+32
-76
lines changed

3 files changed

+32
-76
lines changed

sql/analyzer/fix_exec_indexes.go

Lines changed: 5 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func assignExecIndexes(ctx *sql.Context, a *Analyzer, n sql.Node, scope *plan.Sc
3232
if !scope.IsEmpty() {
3333
// triggers
3434
s.triggerScope = true
35-
s.insertSourceScope = scope.InInsertSource
35+
s.insertSourceScope = scope.InInsertSource()
3636
s.addSchema(scope.Schema())
3737
s = s.push()
3838
}
@@ -300,9 +300,6 @@ func (s *idxScope) copy() *idxScope {
300300
parentScopes: parentCopy,
301301
columns: varsCopy,
302302
ids: idsCopy,
303-
304-
triggerScope: s.triggerScope,
305-
insertSourceScope: s.insertSourceScope,
306303
}
307304
}
308305

@@ -369,42 +366,10 @@ func (s *idxScope) visitChildren(n sql.Node) error {
369366
// keep only the first union scope to avoid double counting
370367
s.childScopes = append(s.childScopes, keepScope)
371368
case *plan.InsertInto:
372-
// TODO: special case into sources when in triggers with groupby/window functions
373-
var newSrc sql.Node
374-
var err error
375-
376-
// TODO: do something cleaner
377-
//var isInTrigger bool
378-
//for _, ps := range s.parentScopes {
379-
// if ps.triggerScope {
380-
// isInTrigger = true
381-
// break
382-
// }
383-
//}
384-
//if isInTrigger {
385-
// if proj, isProj := n.Source.(*plan.Project); isProj {
386-
// switch proj.Child.(type) {
387-
// case *plan.GroupBy, *plan.Window:
388-
// subScope := s.copy()
389-
// subScope.parentScopes = subScope.parentScopes[:0]
390-
// newSrc, _, err = assignIndexesHelper(proj, subScope)
391-
// default:
392-
// newSrc, _, err = assignIndexesHelper(n.Source, s)
393-
// }
394-
// } else {
395-
// newSrc, _, err = assignIndexesHelper(n.Source, s)
396-
// }
397-
//} else {
398-
// newSrc, _, err = assignIndexesHelper(n.Source, s)
399-
//}
400-
401-
s.insertSourceScope = true
402-
newSrc, _, err = assignIndexesHelper(n.Source, s)
369+
newSrc, _, err := assignIndexesHelper(n.Source, s)
403370
if err != nil {
404371
return err
405372
}
406-
s.insertSourceScope = false
407-
408373
newDst, dScope, err := assignIndexesHelper(n.Destination, s)
409374
if err != nil {
410375
return err
@@ -426,9 +391,6 @@ func (s *idxScope) visitChildren(n sql.Node) error {
426391
}
427392
default:
428393
for _, c := range n.Children() {
429-
if _, ok := c.(*plan.GroupBy); ok {
430-
print()
431-
}
432394
newC, cScope, err := assignIndexesHelper(c, s)
433395
if err != nil {
434396
return err
@@ -600,7 +562,9 @@ func (s *idxScope) visitSelf(n sql.Node) error {
600562
n.DestSch[colIdx].Default = newDef.(*sql.ColumnDefaultValue)
601563
}
602564
default:
603-
// TODO: this very specific pattern
565+
// Group By and Window functions already account for the new/old columns present from triggers
566+
// This means that when indexing the Projections, we should not include the trigger scope(s), which are
567+
// within s.parentScopes.
604568
if proj, isProj := n.(*plan.Project); isProj {
605569
switch proj.Child.(type) {
606570
case *plan.GroupBy, *plan.Window:
@@ -771,9 +735,6 @@ func fixExprToScope(e sql.Expression, scopes ...*idxScope) sql.Expression {
771735
// don't have the destination schema, and column references in default values are determined in the build phase)
772736

773737
idx, _ := newScope.getIdxId(e.Id(), e.String())
774-
if e.String() == "modeldisambig.id_modelinfo" && e.Index() == 1 {
775-
print()
776-
}
777738
if idx >= 0 {
778739
return e.WithIndex(idx), transform.NewTree, nil
779740
}

sql/analyzer/inserts.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,7 @@ func resolveInsertRows(ctx *sql.Context, a *Analyzer, n sql.Node, scope *plan.Sc
5959
plan.NewSubqueryAlias("dummy", "", insert.Source),
6060
))
6161
}
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-
//}
70-
if scope != nil {
71-
scope.InInsertSource = true // TODO: use a setter?
72-
}
62+
scope.SetInInsertSource(true)
7363
source, _, err = a.analyzeWithSelector(ctx, insert.Source, scope, SelectAllBatches, newInsertSourceSelector(sel), qFlags)
7464
if err != nil {
7565
return nil, transform.SameTree, err

sql/plan/scope.go

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,7 @@ type Scope struct {
4545
joinSiblings []sql.Node
4646
JoinTrees []string
4747

48-
InInsertSource bool
49-
}
50-
51-
func (s *Scope) SetJoin(b bool) {
52-
if s == nil {
53-
return
54-
}
55-
s.inJoin = b
56-
}
57-
58-
func (s *Scope) SetLateralJoin(b bool) {
59-
if s == nil {
60-
return
61-
}
62-
s.inLateralJoin = b
48+
inInsertSource bool
6349
}
6450

6551
func (s *Scope) IsEmpty() bool {
@@ -320,18 +306,37 @@ func (s *Scope) Schema() sql.Schema {
320306
return schema
321307
}
322308

323-
func (s *Scope) InJoin() bool {
309+
func (s *Scope) SetJoin(b bool) {
324310
if s == nil {
325-
return false
311+
return
326312
}
327-
return s.inJoin
313+
s.inJoin = b
328314
}
329315

330-
func (s *Scope) InLateralJoin() bool {
316+
func (s *Scope) SetLateralJoin(b bool) {
331317
if s == nil {
332-
return false
318+
return
319+
}
320+
s.inLateralJoin = b
321+
}
322+
323+
func (s *Scope) SetInInsertSource(b bool) {
324+
if s == nil {
325+
return
333326
}
334-
return s.inLateralJoin
327+
s.inInsertSource = b
328+
}
329+
330+
func (s *Scope) InJoin() bool {
331+
return s != nil && s.inJoin
332+
}
333+
334+
func (s *Scope) InLateralJoin() bool {
335+
return s != nil && s.inLateralJoin
336+
}
337+
338+
func (s *Scope) InInsertSource() bool {
339+
return s != nil && s.inInsertSource
335340
}
336341

337342
func (s *Scope) JoinSiblings() []sql.Node {

0 commit comments

Comments
 (0)