github.com/dolthub/go-mysql-server@v0.18.0/sql/rowexec/filter_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/types" 27 ) 28 29 func TestFilter(t *testing.T) { 30 require := require.New(t) 31 32 db := memory.NewDatabase("test") 33 pro := memory.NewDBProvider(db) 34 ctx := newContext(pro) 35 36 childSchema := sql.NewPrimaryKeySchema(sql.Schema{ 37 {Name: "col1", Type: types.Text, Nullable: true}, 38 {Name: "col2", Type: types.Text, Nullable: true}, 39 {Name: "col3", Type: types.Int32, Nullable: true}, 40 {Name: "col4", Type: types.Int64, Nullable: true}, 41 }) 42 child := memory.NewTable(db.BaseDatabase, "test", childSchema, nil) 43 44 rows := []sql.Row{ 45 sql.NewRow("col1_1", "col2_1", int32(1111), int64(2222)), 46 sql.NewRow("col1_2", "col2_2", int32(3333), int64(4444)), 47 sql.NewRow("col1_3", "col2_3", nil, int64(4444)), 48 } 49 50 for _, r := range rows { 51 require.NoError(child.Insert(ctx, r)) 52 } 53 54 f := plan.NewFilter( 55 expression.NewEquals( 56 expression.NewGetField(0, types.Text, "col1", true), 57 expression.NewLiteral("col1_1", types.LongText)), 58 plan.NewResolvedTable(child, nil, nil)) 59 60 require.Equal(1, len(f.Children())) 61 62 iter, err := DefaultBuilder.Build(ctx, f, nil) 63 require.NoError(err) 64 require.NotNil(iter) 65 66 row, err := iter.Next(ctx) 67 require.NoError(err) 68 require.NotNil(row) 69 70 require.Equal("col1_1", row[0]) 71 require.Equal("col2_1", row[1]) 72 73 row, err = iter.Next(ctx) 74 require.NotNil(err) 75 require.Nil(row) 76 77 f = plan.NewFilter(expression.NewEquals( 78 expression.NewGetField(2, types.Int32, "col3", true), 79 expression.NewLiteral(int32(1111), 80 types.Int32)), plan.NewResolvedTable(child, nil, nil)) 81 82 iter, err = DefaultBuilder.Build(ctx, f, nil) 83 require.NoError(err) 84 require.NotNil(iter) 85 86 row, err = iter.Next(ctx) 87 require.NoError(err) 88 require.NotNil(row) 89 90 require.Equal(int32(1111), row[2]) 91 require.Equal(int64(2222), row[3]) 92 93 f = plan.NewFilter(expression.NewEquals( 94 expression.NewGetField(3, types.Int64, "col4", true), 95 expression.NewLiteral(int64(4444), types.Int64)), 96 plan.NewResolvedTable(child, nil, nil)) 97 98 iter, err = DefaultBuilder.Build(ctx, f, nil) 99 require.NoError(err) 100 require.NotNil(iter) 101 102 row, err = iter.Next(ctx) 103 require.NoError(err) 104 require.NotNil(row) 105 106 require.Equal(int32(3333), row[2]) 107 require.Equal(int64(4444), row[3]) 108 }