vitess.io/vitess@v0.16.2/go/vt/vtgate/semantics/semantic_state_test.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 semantics 18 19 import ( 20 "fmt" 21 "testing" 22 23 "github.com/stretchr/testify/assert" 24 "github.com/stretchr/testify/require" 25 26 querypb "vitess.io/vitess/go/vt/proto/query" 27 "vitess.io/vitess/go/vt/sqlparser" 28 "vitess.io/vitess/go/vt/vtgate/vindexes" 29 ) 30 31 func TestBindingAndExprEquality(t *testing.T) { 32 tests := []struct { 33 expressions string 34 equal bool 35 }{{ 36 expressions: "t1_id+1, t1.t1_id+1", 37 equal: true, 38 }, { 39 expressions: "t2_id+1, t1_id+1", 40 equal: false, 41 }, { 42 expressions: "(t1_id+1)+1, t1.t1_id+1+1", 43 equal: true, 44 }} 45 46 for _, test := range tests { 47 t.Run(test.expressions, func(t *testing.T) { 48 parse, err := sqlparser.Parse(fmt.Sprintf("select %s from t1, t2", test.expressions)) 49 require.NoError(t, err) 50 st, err := Analyze(parse, "db", fakeSchemaInfoTest()) 51 require.NoError(t, err) 52 exprs := parse.(*sqlparser.Select).SelectExprs 53 a := exprs[0].(*sqlparser.AliasedExpr).Expr 54 b := exprs[1].(*sqlparser.AliasedExpr).Expr 55 assert.Equal(t, st.EqualsExpr(a, b), test.equal) 56 }) 57 } 58 } 59 60 func fakeSchemaInfoTest() *FakeSI { 61 cols1 := []vindexes.Column{{ 62 Name: sqlparser.NewIdentifierCI("t1_id"), 63 Type: querypb.Type_INT64, 64 }} 65 cols2 := []vindexes.Column{{ 66 Name: sqlparser.NewIdentifierCI("t2_id"), 67 Type: querypb.Type_INT64, 68 }} 69 70 si := &FakeSI{ 71 Tables: map[string]*vindexes.Table{ 72 "t1": {Name: sqlparser.NewIdentifierCS("t1"), Columns: cols1, ColumnListAuthoritative: true, Keyspace: ks2}, 73 "t2": {Name: sqlparser.NewIdentifierCS("t2"), Columns: cols2, ColumnListAuthoritative: true, Keyspace: ks3}, 74 }, 75 } 76 return si 77 }