github.com/dolthub/go-mysql-server@v0.18.0/sql/rowexec/showcolumns_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/require" 21 22 "github.com/dolthub/go-mysql-server/memory" 23 "github.com/dolthub/go-mysql-server/sql" 24 "github.com/dolthub/go-mysql-server/sql/expression" 25 . "github.com/dolthub/go-mysql-server/sql/plan" 26 "github.com/dolthub/go-mysql-server/sql/planbuilder" 27 "github.com/dolthub/go-mysql-server/sql/types" 28 ) 29 30 func TestShowColumns(t *testing.T) { 31 require := require.New(t) 32 ctx := sql.NewEmptyContext() 33 34 db := memory.NewDatabase("mydb") 35 schema := sql.Schema{ 36 {Name: "a", Source: "foo", Type: types.Text, PrimaryKey: true}, 37 {Name: "b", Source: "foo", Type: types.Int64, Nullable: true}, 38 {Name: "c", Source: "foo", Type: types.Int64, Default: planbuilder.MustStringToColumnDefaultValue(ctx, "1", types.Int64, false)}, 39 } 40 table := NewResolvedTable(memory.NewTable(db.BaseDatabase, "foo", sql.NewPrimaryKeySchema(schema), nil), nil, nil) 41 42 showColumns, err := NewShowColumns(false, table).WithTargetSchema(schema) 43 require.NoError(err) 44 45 iter, err := DefaultBuilder.Build(ctx, showColumns, nil) 46 require.NoError(err) 47 48 rows, err := sql.RowIterToRows(ctx, iter) 49 require.NoError(err) 50 51 expected := []sql.Row{ 52 {"a", "text", "NO", "PRI", "NULL", ""}, 53 {"b", "bigint", "YES", "", "NULL", ""}, 54 {"c", "bigint", "NO", "", "1", ""}, 55 } 56 57 require.Equal(expected, rows) 58 } 59 60 func TestShowColumnsWithIndexes(t *testing.T) { 61 require := require.New(t) 62 ctx := sql.NewEmptyContext() 63 64 db := memory.NewDatabase("mydb") 65 schema := sql.Schema{ 66 {Name: "a", Source: "foo", Type: types.Text, PrimaryKey: true}, 67 {Name: "b", Source: "foo", Type: types.Int64, Nullable: true}, 68 {Name: "c", Source: "foo", Type: types.Int64, Default: planbuilder.MustStringToColumnDefaultValue(ctx, "1", types.Int64, false)}, 69 {Name: "d", Source: "foo", Type: types.Int64, Nullable: true}, 70 {Name: "e", Source: "foo", Type: types.Int64, Default: planbuilder.MustStringToColumnDefaultValue(ctx, "1", types.Int64, false)}, 71 } 72 memTable := memory.NewTable(db.BaseDatabase, "foo", sql.NewPrimaryKeySchema(schema), nil) 73 table := NewResolvedTable(memTable, nil, nil) 74 75 showColumns, err := NewShowColumns(false, table).WithTargetSchema(schema) 76 require.NoError(err) 77 78 // Assign indexes. This mimics what happens during analysis 79 showColumns.(*ShowColumns).Indexes = []sql.Index{ 80 &memory.Index{ 81 DB: "mydb", 82 TableName: "foo", 83 Tbl: memTable, 84 Name: "a", 85 Exprs: []sql.Expression{ 86 expression.NewGetFieldWithTable(0, 1, types.Int64, "", "foo", "b", true), 87 expression.NewGetFieldWithTable(0, 1, types.Int64, "", "foo", "c", true), 88 }, 89 Unique: true, 90 }, 91 &memory.Index{ 92 DB: "mydb", 93 TableName: "foo", 94 Tbl: memTable, 95 Name: "b", 96 Exprs: []sql.Expression{ 97 expression.NewGetFieldWithTable(0, 1, types.Int64, "", "foo", "d", true), 98 expression.NewGetFieldWithTable(0, 1, types.Int64, "", "foo", "e", true), 99 }, 100 Unique: false, 101 }, 102 } 103 104 iter, err := DefaultBuilder.Build(ctx, showColumns, nil) 105 require.NoError(err) 106 107 rows, err := sql.RowIterToRows(ctx, iter) 108 require.NoError(err) 109 110 expected := []sql.Row{ 111 {"a", "text", "NO", "PRI", "NULL", ""}, 112 {"b", "bigint", "YES", "UNI", "NULL", ""}, 113 {"c", "bigint", "NO", "", "1", ""}, 114 {"d", "bigint", "YES", "MUL", "NULL", ""}, 115 {"e", "bigint", "NO", "", "1", ""}, 116 } 117 118 require.Equal(expected, rows) 119 120 // Test the precedence of key type. PRI > UNI > MUL 121 showColumns.(*ShowColumns).Indexes = append(showColumns.(*ShowColumns).Indexes, 122 &memory.Index{ 123 DB: "mydb", 124 TableName: "foo", 125 Tbl: memTable, 126 Name: "c", 127 Exprs: []sql.Expression{ 128 expression.NewGetFieldWithTable(0, 1, types.Int64, "", "foo", "a", true), 129 expression.NewGetFieldWithTable(0, 1, types.Int64, "", "foo", "b", true), 130 }, 131 Unique: true, 132 }, 133 &memory.Index{ 134 DB: "mydb", 135 TableName: "foo", 136 Tbl: memTable, 137 Name: "d", 138 Exprs: []sql.Expression{ 139 expression.NewGetFieldWithTable(0, 1, types.Int64, "", "foo", "b", true), 140 expression.NewGetFieldWithTable(0, 1, types.Int64, "", "foo", "d", true), 141 }, 142 Unique: false, 143 }, 144 ) 145 146 iter, err = DefaultBuilder.Build(sql.NewEmptyContext(), showColumns, nil) 147 require.NoError(err) 148 149 rows, err = sql.RowIterToRows(ctx, iter) 150 require.NoError(err) 151 152 require.Equal(expected, rows) 153 } 154 155 func TestShowColumnsFull(t *testing.T) { 156 require := require.New(t) 157 ctx := sql.NewEmptyContext() 158 159 db := memory.NewDatabase("mydb") 160 schema := sql.Schema{ 161 {Name: "a", Type: types.Text, PrimaryKey: true}, 162 {Name: "b", Type: types.Int64, Nullable: true}, 163 {Name: "c", Type: types.Int64, Default: planbuilder.MustStringToColumnDefaultValue(ctx, "1", types.Int64, false), Comment: "a comment"}, 164 } 165 table := NewResolvedTable(memory.NewTable(db.BaseDatabase, "foo", sql.NewPrimaryKeySchema(schema), nil), nil, nil) 166 167 showColumns, err := NewShowColumns(true, table).WithTargetSchema(schema) 168 require.NoError(err) 169 170 iter, err := DefaultBuilder.Build(ctx, showColumns, nil) 171 require.NoError(err) 172 173 rows, err := sql.RowIterToRows(ctx, iter) 174 require.NoError(err) 175 176 expected := []sql.Row{ 177 {"a", "text", "utf8mb4_0900_bin", "NO", "PRI", "NULL", "", "", ""}, 178 {"b", "bigint", nil, "YES", "", "NULL", "", "", ""}, 179 {"c", "bigint", nil, "NO", "", "1", "", "", "a comment"}, 180 } 181 182 require.Equal(expected, rows) 183 }