vitess.io/vitess@v0.16.2/go/vt/vtgate/planbuilder/system_variables.go (about) 1 /* 2 Copyright 2020 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 planbuilder 18 19 import ( 20 "fmt" 21 "sync" 22 23 "vitess.io/vitess/go/vt/sqlparser" 24 "vitess.io/vitess/go/vt/sysvars" 25 "vitess.io/vitess/go/vt/vterrors" 26 "vitess.io/vitess/go/vt/vtgate/evalengine" 27 ) 28 29 type sysvarPlanCache struct { 30 funcs map[string]planFunc 31 once sync.Once 32 } 33 34 func (pc *sysvarPlanCache) initForSettings(systemVariables []sysvars.SystemVariable, f func(setting) planFunc) { 35 for _, sysvar := range systemVariables { 36 if _, alreadyExists := pc.funcs[sysvar.Name]; alreadyExists { 37 panic("bug in set plan init - " + sysvar.Name + " already configured") 38 } 39 40 s := setting{ 41 name: sysvar.Name, 42 boolean: sysvar.IsBoolean, 43 identifierAsString: sysvar.IdentifierAsString, 44 supportSetVar: sysvar.SupportSetVar, 45 storageCase: sysvar.Case, 46 } 47 48 if sysvar.Default != "" { 49 s.defaultValue = pc.parseAndBuildDefaultValue(sysvar) 50 } 51 pc.funcs[sysvar.Name] = f(s) 52 } 53 } 54 55 func (pc *sysvarPlanCache) parseAndBuildDefaultValue(sysvar sysvars.SystemVariable) evalengine.Expr { 56 stmt, err := sqlparser.Parse(fmt.Sprintf("select %s", sysvar.Default)) 57 if err != nil { 58 panic(fmt.Sprintf("bug in set plan init - default value for %s not parsable: %s", sysvar.Name, sysvar.Default)) 59 } 60 sel := stmt.(*sqlparser.Select) 61 aliasedExpr := sel.SelectExprs[0].(*sqlparser.AliasedExpr) 62 def, err := evalengine.Translate(aliasedExpr.Expr, nil) 63 if err != nil { 64 panic(fmt.Sprintf("bug in set plan init - default value for %s not able to convert to evalengine.Expr: %s", sysvar.Name, sysvar.Default)) 65 } 66 return def 67 } 68 69 func (pc *sysvarPlanCache) init() { 70 pc.once.Do(func() { 71 pc.funcs = make(map[string]planFunc) 72 pc.initForSettings(sysvars.ReadOnly, buildSetOpReadOnly) 73 pc.initForSettings(sysvars.IgnoreThese, buildSetOpIgnore) 74 pc.initForSettings(sysvars.UseReservedConn, buildSetOpReservedConn) 75 pc.initForSettings(sysvars.CheckAndIgnore, buildSetOpCheckAndIgnore) 76 pc.initForSettings(sysvars.NotSupported, buildNotSupported) 77 pc.initForSettings(sysvars.VitessAware, buildSetOpVitessAware) 78 }) 79 } 80 81 var sysvarPlanningFuncs sysvarPlanCache 82 83 func (pc *sysvarPlanCache) Get(expr *sqlparser.SetExpr) (planFunc, error) { 84 pc.init() 85 pf, ok := pc.funcs[expr.Var.Name.Lowered()] 86 if !ok { 87 return nil, vterrors.VT05006(sqlparser.String(expr)) 88 } 89 return pf, nil 90 }