github.com/dolthub/go-mysql-server@v0.18.0/sql/rowexec/project_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 "io" 19 "testing" 20 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/types" 28 ) 29 30 func TestProject(t *testing.T) { 31 require := require.New(t) 32 33 childSchema := sql.NewPrimaryKeySchema(sql.Schema{ 34 {Name: "col1", Type: types.Text, Nullable: true}, 35 {Name: "col2", Type: types.Text, Nullable: true}, 36 }) 37 38 db := memory.NewDatabase("test") 39 pro := memory.NewDBProvider(db) 40 ctx := newContext(pro) 41 42 child := memory.NewTable(db.BaseDatabase, "test", childSchema, nil) 43 child.Insert(ctx, sql.NewRow("col1_1", "col2_1")) 44 child.Insert(ctx, sql.NewRow("col1_2", "col2_2")) 45 p := plan.NewProject( 46 []sql.Expression{expression.NewGetField(1, types.Text, "col2", true)}, 47 plan.NewResolvedTable(child, nil, nil), 48 ) 49 require.Equal(1, len(p.Children())) 50 schema := sql.NewPrimaryKeySchema(sql.Schema{ 51 {Name: "col2", Type: types.Text, Nullable: true}, 52 }) 53 require.Equal(schema.Schema, p.Schema()) 54 iter, err := DefaultBuilder.Build(ctx, p, nil) 55 require.NoError(err) 56 require.NotNil(iter) 57 row, err := iter.Next(ctx) 58 require.NoError(err) 59 require.NotNil(row) 60 require.Equal(1, len(row)) 61 require.Equal("col2_1", row[0]) 62 row, err = iter.Next(ctx) 63 require.NoError(err) 64 require.NotNil(row) 65 require.Equal(1, len(row)) 66 require.Equal("col2_2", row[0]) 67 row, err = iter.Next(ctx) 68 require.Equal(io.EOF, err) 69 require.Nil(row) 70 71 p = plan.NewProject(nil, plan.NewResolvedTable(child, nil, nil)) 72 require.Equal(0, len(p.Schema())) 73 74 p = plan.NewProject([]sql.Expression{ 75 expression.NewAlias("foo", expression.NewGetField(1, types.Text, "col2", true)), 76 }, plan.NewResolvedTable(child, nil, nil)) 77 schema = sql.NewPrimaryKeySchema(sql.Schema{ 78 {Name: "foo", Type: types.Text, Nullable: true}, 79 }) 80 require.Equal(schema.Schema, p.Schema()) 81 } 82 83 func BenchmarkProject(b *testing.B) { 84 require := require.New(b) 85 ctx := sql.NewEmptyContext() 86 87 for i := 0; i < b.N; i++ { 88 d := plan.NewProject([]sql.Expression{ 89 expression.NewGetField(0, types.Text, "strfield", true), 90 expression.NewGetField(1, types.Float64, "floatfield", true), 91 expression.NewGetField(2, types.Boolean, "boolfield", false), 92 expression.NewGetField(3, types.Int32, "intfield", false), 93 expression.NewGetField(4, types.Int64, "bigintfield", false), 94 expression.NewGetField(5, types.Blob, "blobfield", false), 95 }, plan.NewResolvedTable(benchtable, nil, nil)) 96 97 iter, err := DefaultBuilder.Build(ctx, d, nil) 98 require.NoError(err) 99 require.NotNil(iter) 100 101 for { 102 _, err := iter.Next(ctx) 103 if err == io.EOF { 104 break 105 } 106 107 require.NoError(err) 108 } 109 } 110 }