vitess.io/vitess@v0.16.2/go/vt/vtgate/planbuilder/operators/expressions.go (about) 1 /* 2 Copyright 2022 The Vitess Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package operators 18 19 import ( 20 "vitess.io/vitess/go/vt/sqlparser" 21 "vitess.io/vitess/go/vt/vterrors" 22 "vitess.io/vitess/go/vt/vtgate/planbuilder/plancontext" 23 "vitess.io/vitess/go/vt/vtgate/semantics" 24 ) 25 26 // BreakExpressionInLHSandRHS takes an expression and 27 // extracts the parts that are coming from one of the sides into `ColName`s that are needed 28 func BreakExpressionInLHSandRHS( 29 ctx *plancontext.PlanningContext, 30 expr sqlparser.Expr, 31 lhs semantics.TableSet, 32 ) (bvNames []string, columns []*sqlparser.ColName, rewrittenExpr sqlparser.Expr, err error) { 33 rewrittenExpr = sqlparser.CopyOnRewrite(expr, nil, func(cursor *sqlparser.CopyOnWriteCursor) { 34 node, ok := cursor.Node().(*sqlparser.ColName) 35 if !ok { 36 return 37 } 38 deps := ctx.SemTable.RecursiveDeps(node) 39 if deps.IsEmpty() { 40 err = vterrors.VT13001("unknown column. has the AST been copied?") 41 cursor.StopTreeWalk() 42 return 43 } 44 if !deps.IsSolvedBy(lhs) { 45 return 46 } 47 48 node.Qualifier.Qualifier = sqlparser.NewIdentifierCS("") 49 columns = append(columns, node) 50 bvName := node.CompliantName() 51 bvNames = append(bvNames, bvName) 52 arg := sqlparser.NewArgument(bvName) 53 // we are replacing one of the sides of the comparison with an argument, 54 // but we don't want to lose the type information we have, so we copy it over 55 ctx.SemTable.CopyExprInfo(node, arg) 56 cursor.Replace(arg) 57 }, nil).(sqlparser.Expr) 58 59 if err != nil { 60 return nil, nil, nil, err 61 } 62 ctx.JoinPredicates[expr] = append(ctx.JoinPredicates[expr], rewrittenExpr) 63 return 64 }