github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/analyze_expr.go (about) 1 // Copyright 2017 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 "context" 15 16 "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" 17 "github.com/cockroachdb/cockroach/pkg/sql/sqlbase" 18 "github.com/cockroachdb/cockroach/pkg/sql/types" 19 ) 20 21 // analyzeExpr performs semantic analysis of an expression, including: 22 // - replacing sub-queries by a sql.subquery node; 23 // - resolving names (optional); 24 // - type checking (with optional type enforcement); 25 // - normalization. 26 // The parameters sources and IndexedVars, if both are non-nil, indicate 27 // name resolution should be performed. The IndexedVars map will be filled 28 // as a result. 29 func (p *planner) analyzeExpr( 30 ctx context.Context, 31 raw tree.Expr, 32 source *sqlbase.DataSourceInfo, 33 iVarHelper tree.IndexedVarHelper, 34 expectedType *types.T, 35 requireType bool, 36 typingContext string, 37 ) (tree.TypedExpr, error) { 38 // Perform optional name resolution. 39 resolved := raw 40 if source != nil { 41 var err error 42 resolved, err = p.resolveNames(raw, source, iVarHelper) 43 if err != nil { 44 return nil, err 45 } 46 } 47 48 // Type check. 49 var typedExpr tree.TypedExpr 50 var err error 51 p.semaCtx.IVarContainer = iVarHelper.Container() 52 if requireType { 53 typedExpr, err = tree.TypeCheckAndRequire(ctx, resolved, &p.semaCtx, 54 expectedType, typingContext) 55 } else { 56 typedExpr, err = tree.TypeCheck(ctx, resolved, &p.semaCtx, expectedType) 57 } 58 p.semaCtx.IVarContainer = nil 59 if err != nil { 60 return nil, err 61 } 62 63 // Normalize. 64 return p.txCtx.NormalizeExpr(p.EvalContext(), typedExpr) 65 }