github.com/dolthub/go-mysql-server@v0.18.0/sql/rowexec/show_indexes_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 "github.com/stretchr/testify/require" 22 23 "github.com/dolthub/go-mysql-server/memory" 24 "github.com/dolthub/go-mysql-server/sql" 25 "github.com/dolthub/go-mysql-server/sql/expression" 26 . "github.com/dolthub/go-mysql-server/sql/plan" 27 "github.com/dolthub/go-mysql-server/sql/planbuilder" 28 "github.com/dolthub/go-mysql-server/sql/types" 29 ) 30 31 func TestShowIndexes(t *testing.T) { 32 ctx := sql.NewEmptyContext() 33 unresolved := NewShowIndexes(NewUnresolvedTable("table-test", "")) 34 require.False(t, unresolved.Resolved()) 35 require.Equal(t, []sql.Node{NewUnresolvedTable("table-test", "")}, unresolved.Children()) 36 37 db := memory.NewDatabase("test") 38 39 tests := []struct { 40 name string 41 table memory.MemTable 42 isExpression bool 43 }{ 44 { 45 name: "test1", 46 table: memory.NewTable(db, "test1", sql.NewPrimaryKeySchema(sql.Schema{ 47 &sql.Column{Name: "foo", Type: types.Int32, Source: "test1", Default: planbuilder.MustStringToColumnDefaultValue(ctx, "0", types.Int32, false), Nullable: false}, 48 }), db.GetForeignKeyCollection()), 49 }, 50 { 51 name: "test2", 52 table: memory.NewTable(db, "test2", sql.NewPrimaryKeySchema(sql.Schema{ 53 &sql.Column{Name: "bar", Type: types.Int64, Source: "test2", Default: planbuilder.MustStringToColumnDefaultValue(ctx, "0", types.Int64, true), Nullable: true}, 54 &sql.Column{Name: "rab", Type: types.Int64, Source: "test2", Default: planbuilder.MustStringToColumnDefaultValue(ctx, "0", types.Int64, false), Nullable: false}, 55 }), db.GetForeignKeyCollection()), 56 }, 57 { 58 name: "test3", 59 table: memory.NewTable(db, "test3", sql.NewPrimaryKeySchema(sql.Schema{ 60 &sql.Column{Name: "baz", Type: types.Text, Source: "test3", Default: planbuilder.MustStringToColumnDefaultValue(ctx, `""`, types.Text, false), Nullable: false}, 61 &sql.Column{Name: "zab", Type: types.Int32, Source: "test3", Default: planbuilder.MustStringToColumnDefaultValue(ctx, "0", types.Int32, true), Nullable: true}, 62 &sql.Column{Name: "bza", Type: types.Int64, Source: "test3", Default: planbuilder.MustStringToColumnDefaultValue(ctx, "0", types.Int64, true), Nullable: true}, 63 }), db.GetForeignKeyCollection()), 64 }, 65 { 66 name: "test4", 67 table: memory.NewTable(db, "test4", sql.NewPrimaryKeySchema(sql.Schema{ 68 &sql.Column{Name: "oof", Type: types.Text, Source: "test4", Default: planbuilder.MustStringToColumnDefaultValue(ctx, `""`, types.Text, false), Nullable: false}, 69 }), db.GetForeignKeyCollection()), 70 }, 71 } 72 73 for _, test := range tests { 74 t.Run(test.name, func(t *testing.T) { 75 db.AddTable(test.name, test.table) 76 77 expressions := make([]sql.Expression, len(test.table.Schema())) 78 for i, col := range test.table.Schema() { 79 var ex sql.Expression = expression.NewGetFieldWithTable(i, 1, col.Type, "", test.name, col.Name, col.Nullable) 80 81 if test.isExpression { 82 ex = expression.NewEquals(ex, expression.NewLiteral("a", types.LongText)) 83 } 84 85 expressions[i] = ex 86 } 87 88 idx := &memory.Index{ 89 DB: "test", 90 TableName: test.table.Name(), 91 Tbl: test.table.(*memory.Table), 92 Name: test.name + "_idx", 93 Exprs: expressions, 94 } 95 96 // Assigning tables and indexes manually. This mimics what happens during analysis 97 showIdxs := NewShowIndexes(NewResolvedTable(test.table, nil, nil)) 98 showIdxs.IndexesToShow = []sql.Index{idx} 99 100 rowIter, err := DefaultBuilder.Build(ctx, showIdxs, nil) 101 assert.NoError(t, err) 102 103 rows, err := sql.RowIterToRows(ctx, rowIter) 104 assert.NoError(t, err) 105 assert.Len(t, rows, len(expressions)) 106 107 for i, row := range rows { 108 var nullable string 109 var columnName, ex interface{} 110 columnName, ex = "NULL", expressions[i].String() 111 if col := GetColumnFromIndexExpr(ex.(string), test.table); col != nil { 112 columnName, ex = col.Name, nil 113 if col.Nullable { 114 nullable = "YES" 115 } 116 } 117 118 expected := sql.NewRow( 119 test.name, 120 1, 121 idx.ID(), 122 i+1, 123 columnName, 124 nil, 125 int64(0), 126 nil, 127 nil, 128 nullable, 129 "BTREE", 130 "", 131 "", 132 "YES", 133 ex, 134 ) 135 136 assert.Equal(t, expected, row) 137 } 138 }) 139 } 140 }