github.com/dolthub/go-mysql-server@v0.18.0/sql/rowexec/bindvar_test.go (about) 1 // Copyright 2020-2021 Dolthub, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package rowexec 16 17 import ( 18 "testing" 19 20 "github.com/stretchr/testify/assert" 21 22 "github.com/dolthub/go-mysql-server/sql" 23 "github.com/dolthub/go-mysql-server/sql/expression" 24 "github.com/dolthub/go-mysql-server/sql/plan" 25 "github.com/dolthub/go-mysql-server/sql/types" 26 ) 27 28 func TestApplyBindings(t *testing.T) { 29 type tc struct { 30 Name string 31 Node sql.Node 32 Bindings map[string]sql.Expression 33 Expected sql.Node 34 } 35 cases := []tc{ 36 tc{ 37 "SingleV1", 38 plan.NewProject( 39 []sql.Expression{ 40 expression.NewStar(), 41 }, 42 plan.NewFilter( 43 expression.NewEquals( 44 expression.NewUnresolvedColumn("foo"), 45 expression.NewBindVar("v1"), 46 ), 47 plan.NewUnresolvedTable("t1", ""), 48 ), 49 ), 50 map[string]sql.Expression{ 51 "v1": expression.NewLiteral("Four score and seven years ago...", types.LongText), 52 }, 53 plan.NewProject( 54 []sql.Expression{ 55 expression.NewStar(), 56 }, 57 plan.NewFilter( 58 expression.NewEquals( 59 expression.NewUnresolvedColumn("foo"), 60 expression.NewLiteral("Four score and seven years ago...", types.LongText), 61 ), 62 plan.NewUnresolvedTable("t1", ""), 63 ), 64 ), 65 }, 66 tc{ 67 "VarNotBound", 68 plan.NewProject( 69 []sql.Expression{ 70 expression.NewStar(), 71 }, 72 plan.NewFilter( 73 expression.NewEquals( 74 expression.NewUnresolvedColumn("foo"), 75 expression.NewBindVar("v1"), 76 ), 77 plan.NewUnresolvedTable("t1", ""), 78 ), 79 ), 80 map[string]sql.Expression{}, 81 plan.NewProject( 82 []sql.Expression{ 83 expression.NewStar(), 84 }, 85 plan.NewFilter( 86 expression.NewEquals( 87 expression.NewUnresolvedColumn("foo"), 88 expression.NewBindVar("v1"), 89 ), 90 plan.NewUnresolvedTable("t1", ""), 91 ), 92 ), 93 }, 94 tc{ 95 "SameVarMultipleTimes", 96 plan.NewProject( 97 []sql.Expression{ 98 expression.NewStar(), 99 }, 100 plan.NewFilter( 101 expression.NewOr( 102 expression.NewAnd( 103 expression.NewEquals( 104 expression.NewUnresolvedColumn("foo"), 105 expression.NewBindVar("strvar"), 106 ), 107 expression.NewEquals( 108 expression.NewUnresolvedColumn("bar"), 109 expression.NewBindVar("strvar"), 110 ), 111 ), 112 expression.NewLessThan( 113 expression.NewUnresolvedColumn("icol"), 114 expression.NewBindVar("intvar"), 115 ), 116 ), 117 plan.NewUnresolvedTable("t1", ""), 118 ), 119 ), 120 map[string]sql.Expression{ 121 "strvar": expression.NewLiteral("Four score and seven years ago...", types.LongText), 122 "intvar": expression.NewLiteral(int8(10), types.Int8), 123 }, 124 plan.NewProject( 125 []sql.Expression{ 126 expression.NewStar(), 127 }, 128 plan.NewFilter( 129 expression.NewOr( 130 expression.NewAnd( 131 expression.NewEquals( 132 expression.NewUnresolvedColumn("foo"), 133 expression.NewLiteral("Four score and seven years ago...", types.LongText), 134 ), 135 expression.NewEquals( 136 expression.NewUnresolvedColumn("bar"), 137 expression.NewLiteral("Four score and seven years ago...", types.LongText), 138 ), 139 ), 140 expression.NewLessThan( 141 expression.NewUnresolvedColumn("icol"), 142 expression.NewLiteral(int8(10), types.Int8), 143 ), 144 ), 145 plan.NewUnresolvedTable("t1", ""), 146 ), 147 ), 148 }, 149 tc{ 150 "Subquery", 151 plan.NewProject( 152 []sql.Expression{ 153 expression.NewStar(), 154 }, 155 plan.NewSubqueryAlias( 156 "a", 157 "select * from foo where bar = :v1", 158 plan.NewProject( 159 []sql.Expression{ 160 expression.NewStar(), 161 }, 162 plan.NewFilter( 163 expression.NewEquals( 164 expression.NewUnresolvedColumn("bar"), 165 expression.NewBindVar("v1"), 166 ), 167 plan.NewUnresolvedTable("foo", ""), 168 ), 169 ), 170 ), 171 ), 172 map[string]sql.Expression{ 173 "v1": expression.NewLiteral("Four score and seven years ago...", types.LongText), 174 }, 175 plan.NewProject( 176 []sql.Expression{ 177 expression.NewStar(), 178 }, 179 plan.NewSubqueryAlias( 180 "a", 181 "select * from foo where bar = :v1", 182 plan.NewProject( 183 []sql.Expression{ 184 expression.NewStar(), 185 }, 186 plan.NewFilter( 187 expression.NewEquals( 188 expression.NewUnresolvedColumn("bar"), 189 expression.NewLiteral("Four score and seven years ago...", types.LongText), 190 ), 191 plan.NewUnresolvedTable("foo", ""), 192 ), 193 ), 194 ), 195 ), 196 }, 197 } 198 199 for _, c := range cases { 200 t.Run(c.Name, func(t *testing.T) { 201 res, _, err := plan.ApplyBindings(c.Node, c.Bindings) 202 if assert.NoError(t, err) { 203 assert.Equal(t, res, c.Expected) 204 } 205 }) 206 } 207 }