github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/subquery.go (about) 1 // Copyright 2015 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 package sql 12 13 import ( 14 "github.com/cockroachdb/cockroach/pkg/sql/rowexec" 15 "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" 16 "github.com/cockroachdb/errors" 17 ) 18 19 // subquery represents a subquery expression in an expression tree 20 // after it has been converted to a query plan. It is stored in 21 // planTop.subqueryPlans. 22 type subquery struct { 23 // subquery is used to show the subquery SQL when explaining plans. 24 subquery tree.NodeFormatter 25 execMode rowexec.SubqueryExecMode 26 expanded bool 27 started bool 28 plan planMaybePhysical 29 result tree.Datum 30 } 31 32 // EvalSubquery is called by `tree.Eval()` method implementations to 33 // retrieve the Datum result of a subquery. 34 func (p *planner) EvalSubquery(expr *tree.Subquery) (result tree.Datum, err error) { 35 if expr.Idx == 0 { 36 return nil, errors.AssertionFailedf("subquery %q was not processed", expr) 37 } 38 if expr.Idx < 0 || expr.Idx-1 >= len(p.curPlan.subqueryPlans) { 39 return nil, errors.AssertionFailedf("invalid index %d for %q", expr.Idx, expr) 40 } 41 42 s := &p.curPlan.subqueryPlans[expr.Idx-1] 43 if !s.started { 44 return nil, errors.AssertionFailedf("subquery %d (%q) not started prior to evaluation", expr.Idx, expr) 45 } 46 return s.result, nil 47 }