vitess.io/vitess@v0.16.2/go/vt/vtgate/planbuilder/symtab_test.go (about) 1 /* 2 Copyright 2019 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 "testing" 21 22 "github.com/stretchr/testify/require" 23 24 querypb "vitess.io/vitess/go/vt/proto/query" 25 "vitess.io/vitess/go/vt/sqlparser" 26 ) 27 28 /* 29 func TestSymtabAddVSchemaTable(t *testing.T) { 30 tname := sqlparser.TableName{Name: sqlparser.NewIdentifierCS("t")} 31 rb := &route{} 32 33 null, _ := vindexes.CreateVindex("null", "null", nil) 34 35 tcases := []struct { 36 in *vindexes.Table 37 authoritative bool 38 vindex []string 39 err string 40 }{{ 41 // Single table. 42 in: &vindexes.Table{ 43 Columns: []vindexes.Column{{ 44 Name: sqlparser.NewIdentifierCI("C1"), 45 }, { 46 Name: sqlparser.NewIdentifierCI("C2"), 47 }}, 48 }, 49 authoritative: false, 50 vindex: []string{}, 51 }, { 52 // Column vindex specified. 53 in: &vindexes.Table{ 54 ColumnVindexes: []*vindexes.ColumnVindex{{ 55 Columns: []sqlparser.IdentifierCI{sqlparser.NewIdentifierCI("C1")}, 56 Vindex: null, 57 }}, 58 Columns: []vindexes.Column{{ 59 Name: sqlparser.NewIdentifierCI("C1"), 60 }, { 61 Name: sqlparser.NewIdentifierCI("C2"), 62 }}, 63 }, 64 authoritative: false, 65 vindex: []string{"c1"}, 66 }, { 67 // Multi-column vindex. 68 in: &vindexes.Table{ 69 ColumnVindexes: []*vindexes.ColumnVindex{{ 70 Columns: []sqlparser.IdentifierCI{ 71 sqlparser.NewIdentifierCI("C1"), 72 sqlparser.NewIdentifierCI("C2"), 73 }, 74 Vindex: null, 75 }}, 76 Columns: []vindexes.Column{{ 77 Name: sqlparser.NewIdentifierCI("C1"), 78 }, { 79 Name: sqlparser.NewIdentifierCI("C2"), 80 }}, 81 }, 82 authoritative: false, 83 vindex: []string{"c1"}, 84 }, { 85 // AutoIncrement. 86 in: &vindexes.Table{ 87 AutoIncrement: &vindexes.AutoIncrement{ 88 Column: sqlparser.NewIdentifierCI("C1"), 89 }, 90 Columns: []vindexes.Column{{ 91 Name: sqlparser.NewIdentifierCI("C1"), 92 }, { 93 Name: sqlparser.NewIdentifierCI("C2"), 94 }}, 95 }, 96 authoritative: false, 97 vindex: []string{}, 98 }, { 99 // Column vindex specifies a column not in list. 100 in: &vindexes.Table{ 101 ColumnVindexes: []*vindexes.ColumnVindex{{ 102 Columns: []sqlparser.IdentifierCI{sqlparser.NewIdentifierCI("C1")}, 103 Vindex: null, 104 }}, 105 Columns: []vindexes.Column{{ 106 Name: sqlparser.NewIdentifierCI("C2"), 107 }}, 108 }, 109 authoritative: false, 110 vindex: []string{"c1"}, 111 }, { 112 // Column vindex specifies columns with none in list. 113 in: &vindexes.Table{ 114 ColumnVindexes: []*vindexes.ColumnVindex{{ 115 Columns: []sqlparser.IdentifierCI{ 116 sqlparser.NewIdentifierCI("C1"), 117 sqlparser.NewIdentifierCI("C2"), 118 }, 119 Vindex: null, 120 }}, 121 }, 122 authoritative: false, 123 vindex: []string{"c1"}, 124 }, { 125 // AutoIncrement specifies a column not in list. 126 in: &vindexes.Table{ 127 AutoIncrement: &vindexes.AutoIncrement{ 128 Column: sqlparser.NewIdentifierCI("C1"), 129 }, 130 Columns: []vindexes.Column{{ 131 Name: sqlparser.NewIdentifierCI("C2"), 132 }}, 133 }, 134 authoritative: false, 135 vindex: []string{}, 136 }, { 137 // Two column vindexes. 138 in: &vindexes.Table{ 139 ColumnVindexes: []*vindexes.ColumnVindex{{ 140 Columns: []sqlparser.IdentifierCI{ 141 sqlparser.NewIdentifierCI("C1"), 142 }, 143 Vindex: null, 144 }, { 145 Columns: []sqlparser.IdentifierCI{ 146 sqlparser.NewIdentifierCI("C2"), 147 }, 148 Vindex: null, 149 }}, 150 }, 151 authoritative: false, 152 vindex: []string{"c1", "c2"}, 153 }} 154 155 out := []string{"c1", "c2"} 156 for _, tcase := range tcases { 157 st := newSymtab() 158 vindexMap, err := st.AddVSchemaTable(tname, tcase.in, rb) 159 tcasein, _ := json.Marshal(tcase.in) 160 if err != nil { 161 if err.Error() != tcase.err { 162 t.Errorf("st.AddVSchemaTable(%s) err: %v, want %s", tcasein, err, tcase.err) 163 } 164 continue 165 } else if tcase.err != "" { 166 t.Errorf("st.AddVSchemaTable(%s) succeeded, want error: %s", tcasein, tcase.err) 167 continue 168 } 169 tab := st.tables[tname] 170 for _, col := range out { 171 if tab.columns[col] == nil { 172 t.Errorf("st.AddVSchemaTable(%s): column %s not found", tcasein, col) 173 } 174 } 175 for _, col := range tcase.vindex { 176 c := tab.columns[col] 177 if c == nil { 178 t.Errorf("st.AddVSchemaTable(%s): column %s not found", tcasein, col) 179 } 180 if _, ok := vindexMap[c]; !ok { 181 t.Errorf("st.AddVSchemaTable(%s).vindexMap: column %s not found", tcasein, col) 182 } 183 } 184 if tab.isAuthoritative != tcase.authoritative { 185 t.Errorf("st.AddVSchemaTable(%s).authoritative: %v want %v", tcasein, tab.isAuthoritative, tcase.authoritative) 186 } 187 } 188 } 189 */ 190 191 func TestGetReturnType(t *testing.T) { 192 tests := []struct { 193 input sqlparser.Expr 194 output querypb.Type 195 expectedErr error 196 }{{ 197 input: &sqlparser.FuncExpr{Name: sqlparser.NewIdentifierCI("Abs"), Exprs: sqlparser.SelectExprs{ 198 &sqlparser.AliasedExpr{ 199 Expr: &sqlparser.ColName{ 200 Name: sqlparser.NewIdentifierCI("A"), 201 Metadata: &column{ 202 typ: querypb.Type_DECIMAL, 203 }, 204 }, 205 }, 206 }}, 207 output: querypb.Type_DECIMAL, 208 }, { 209 input: &sqlparser.Count{}, 210 output: querypb.Type_INT64, 211 }, { 212 input: &sqlparser.CountStar{}, 213 output: querypb.Type_INT64, 214 }} 215 216 for _, test := range tests { 217 t.Run(sqlparser.String(test.input), func(t *testing.T) { 218 got, err := GetReturnType(test.input) 219 if test.expectedErr != nil { 220 require.EqualError(t, err, test.expectedErr.Error()) 221 } else { 222 require.NoError(t, err) 223 require.Equal(t, test.output, got) 224 } 225 }) 226 } 227 }