vitess.io/vitess@v0.16.2/go/vt/vtgate/planbuilder/vstream.go (about) 1 /* 2 Copyright 2021 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 "strconv" 21 "strings" 22 23 "vitess.io/vitess/go/vt/vtgate/planbuilder/plancontext" 24 25 "vitess.io/vitess/go/vt/key" 26 topodatapb "vitess.io/vitess/go/vt/proto/topodata" 27 "vitess.io/vitess/go/vt/sqlparser" 28 "vitess.io/vitess/go/vt/vterrors" 29 "vitess.io/vitess/go/vt/vtgate/engine" 30 ) 31 32 const defaultLimit = 100 33 34 func buildVStreamPlan(stmt *sqlparser.VStream, vschema plancontext.VSchema) (*planResult, error) { 35 table, _, destTabletType, dest, err := vschema.FindTable(stmt.Table) 36 if err != nil { 37 return nil, err 38 } 39 // TODO: do we need this restriction? 40 if destTabletType != topodatapb.TabletType_PRIMARY { 41 return nil, vterrors.VT09009(destTabletType) 42 } 43 if dest == nil { 44 dest = key.DestinationAllShards{} 45 } 46 var pos string 47 if stmt.Where != nil { 48 pos, err = getVStreamStartPos(stmt) 49 if err != nil { 50 return nil, err 51 } 52 } 53 limit := defaultLimit 54 if stmt.Limit != nil { 55 count, ok := stmt.Limit.Rowcount.(*sqlparser.Literal) 56 if ok { 57 limit, _ = strconv.Atoi(count.Val) 58 } 59 } 60 61 return newPlanResult(&engine.VStream{ 62 Keyspace: table.Keyspace, 63 TargetDestination: dest, 64 TableName: table.Name.CompliantName(), 65 Position: pos, 66 Limit: limit, 67 }), nil 68 } 69 70 func getVStreamStartPos(stmt *sqlparser.VStream) (string, error) { 71 var colName, pos string 72 if stmt.Where != nil { 73 switch v := stmt.Where.Expr.(type) { 74 case *sqlparser.ComparisonExpr: 75 if v.Operator == sqlparser.GreaterThanOp { 76 switch c := v.Left.(type) { 77 case *sqlparser.ColName: 78 switch val := v.Right.(type) { 79 case *sqlparser.Literal: 80 pos = val.Val 81 } 82 colName = strings.ToLower(c.Name.String()) 83 if colName != "pos" { 84 return "", vterrors.VT03017() 85 } 86 } 87 } else { 88 return "", vterrors.VT03017() 89 } 90 default: 91 return "", vterrors.VT03017() 92 } 93 } 94 return pos, nil 95 }