Skip to content

Commit b624244

Browse files
committed
Simplify logic for cacheSubqueryAliasesInJoins. This seems to result in the exact same plans, but requires less tracking.
1 parent 521c5d9 commit b624244

File tree

1 file changed

+8
-22
lines changed

1 file changed

+8
-22
lines changed

sql/analyzer/resolve_subqueries.go

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -291,24 +291,18 @@ func analyzeSubqueryAlias(ctx *sql.Context, a *Analyzer, sqa *plan.SubqueryAlias
291291
// node on top of those nodes. The left-most child of a join root is an exception
292292
// that cannot be cached.
293293
func cacheSubqueryAliasesInJoins(ctx *sql.Context, a *Analyzer, n sql.Node, scope *plan.Scope, sel RuleSelector, qFlags *sql.QueryFlags) (sql.Node, transform.TreeIdentity, error) {
294-
foundFirstRel := false
295-
var recurse func(n sql.Node, parentCached, inJoin bool, rootJoinT1 *bool) (sql.Node, transform.TreeIdentity, error)
296-
recurse = func(n sql.Node, parentCached, inJoin bool, foundFirstRel *bool) (sql.Node, transform.TreeIdentity, error) {
294+
var recurse func(n sql.Node, parentCached, inJoin, leftChild bool) (sql.Node, transform.TreeIdentity, error)
295+
recurse = func(n sql.Node, parentCached, inJoin, leftChild bool) (sql.Node, transform.TreeIdentity, error) {
297296
_, isOp := n.(sql.OpaqueNode)
298297
var isCacheableSq bool
299298
var isCachedRs bool
300299
var isMax1Row bool
301-
// If we enter a new join, we use this stack-allocated variable to track whether or not we've encountered
302-
// a nameable child in that node.
303-
childFoundFirstRel := false
304300
switch n := n.(type) {
305301
case *plan.JoinNode:
306-
if !inJoin {
307-
inJoin = true
308-
foundFirstRel = &childFoundFirstRel
309-
}
302+
inJoin = true
303+
leftChild = true
310304
case *plan.SubqueryAlias:
311-
isCacheableSq = n.CanCacheResults()
305+
isCacheableSq = n.CanCacheResults() && !leftChild
312306
case *plan.CachedResults:
313307
isCachedRs = true
314308
case *plan.Max1Row:
@@ -320,26 +314,18 @@ func cacheSubqueryAliasesInJoins(ctx *sql.Context, a *Analyzer, n sql.Node, scop
320314
doCache := isCacheableSq && inJoin && !parentCached
321315
childInJoin := inJoin && !isOp
322316

323-
if inJoin && !(*foundFirstRel) {
324-
switch n.(type) {
325-
case sql.Nameable:
326-
doCache = false
327-
*foundFirstRel = true
328-
default:
329-
}
330-
}
331-
332317
children := n.Children()
333318
var newChildren []sql.Node
334319
for i, c := range children {
335-
child, same, _ := recurse(c, doCache || isCachedRs || isMax1Row, childInJoin, foundFirstRel)
320+
child, same, _ := recurse(c, doCache || isCachedRs || isMax1Row, childInJoin, leftChild)
336321
if !same {
337322
if newChildren == nil {
338323
newChildren = make([]sql.Node, len(children))
339324
copy(newChildren, children)
340325
}
341326
newChildren[i] = child
342327
}
328+
leftChild = false
343329
}
344330

345331
if len(newChildren) == 0 && !doCache {
@@ -355,5 +341,5 @@ func cacheSubqueryAliasesInJoins(ctx *sql.Context, a *Analyzer, n sql.Node, scop
355341
}
356342
return ret, transform.NewTree, nil
357343
}
358-
return recurse(n, false, false, &foundFirstRel)
344+
return recurse(n, false, false, false)
359345
}

0 commit comments

Comments
 (0)