github.com/dolthub/go-mysql-server@v0.18.0/sql/rowexec/common_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 "bytes" 19 "context" 20 "fmt" 21 "io" 22 "testing" 23 24 "github.com/stretchr/testify/require" 25 26 "github.com/dolthub/go-mysql-server/memory" 27 "github.com/dolthub/go-mysql-server/sql" 28 "github.com/dolthub/go-mysql-server/sql/plan" 29 "github.com/dolthub/go-mysql-server/sql/types" 30 ) 31 32 func newContext(provider *memory.DbProvider) *sql.Context { 33 return sql.NewContext(context.Background(), sql.WithSession(memory.NewSession(sql.NewBaseSession(), provider))) 34 } 35 36 var benchtable = func() *memory.Table { 37 db := memory.NewDatabase("test") 38 pro := memory.NewDBProvider(db) 39 40 schema := sql.NewPrimaryKeySchema(sql.Schema{ 41 {Name: "strfield", Type: types.Text, Nullable: true}, 42 {Name: "floatfield", Type: types.Float64, Nullable: true}, 43 {Name: "boolfield", Type: types.Boolean, Nullable: false}, 44 {Name: "intfield", Type: types.Int32, Nullable: false}, 45 {Name: "bigintfield", Type: types.Int64, Nullable: false}, 46 {Name: "blobfield", Type: types.Blob, Nullable: false}, 47 }) 48 t := memory.NewTable(db.BaseDatabase, "test", schema, nil) 49 50 for i := 0; i < 100; i++ { 51 n := fmt.Sprint(i) 52 boolVal := int8(0) 53 if i%2 == 0 { 54 boolVal = 1 55 } 56 err := t.Insert( 57 newContext(pro), 58 sql.NewRow( 59 repeatStr(n, i%10+1), 60 float64(i), 61 boolVal, 62 int32(i), 63 int64(i), 64 repeatBytes(n, 100+(i%100)), 65 ), 66 ) 67 if err != nil { 68 panic(err) 69 } 70 71 if i%2 == 0 { 72 err := t.Insert( 73 newContext(pro), 74 sql.NewRow( 75 repeatStr(n, i%10+1), 76 float64(i), 77 boolVal, 78 int32(i), 79 int64(i), 80 repeatBytes(n, 100+(i%100)), 81 ), 82 ) 83 if err != nil { 84 panic(err) 85 } 86 } 87 } 88 89 return t 90 }() 91 92 func repeatStr(str string, n int) string { 93 var buf bytes.Buffer 94 for i := 0; i < n; i++ { 95 buf.WriteString(str) 96 } 97 return buf.String() 98 } 99 100 func repeatBytes(str string, n int) []byte { 101 var buf bytes.Buffer 102 for i := 0; i < n; i++ { 103 buf.WriteString(str) 104 } 105 return buf.Bytes() 106 } 107 108 func assertRows(t *testing.T, ctx *sql.Context, iter sql.RowIter, expected int64) { 109 t.Helper() 110 require := require.New(t) 111 112 var rows int64 113 for { 114 _, err := iter.Next(ctx) 115 if err == io.EOF { 116 break 117 } 118 119 if err != nil { 120 require.NoError(err) 121 } 122 123 rows++ 124 } 125 126 require.Equal(expected, rows) 127 } 128 129 func collectRows(t *testing.T, ctx *sql.Context, node sql.Node) []sql.Row { 130 t.Helper() 131 132 iter, err := DefaultBuilder.Build(ctx, node, nil) 133 require.NoError(t, err) 134 135 var rows []sql.Row 136 for { 137 row, err := iter.Next(ctx) 138 if err == io.EOF { 139 return rows 140 } 141 require.NoError(t, err) 142 rows = append(rows, row) 143 } 144 } 145 146 func TestIsUnary(t *testing.T) { 147 require := require.New(t) 148 db := memory.NewDatabase("test") 149 table := memory.NewTable(db.BaseDatabase, "foo", sql.PrimaryKeySchema{}, nil) 150 151 require.True(plan.IsUnary(plan.NewFilter(nil, plan.NewResolvedTable(table, nil, nil)))) 152 require.False(plan.IsUnary(plan.NewCrossJoin( 153 plan.NewResolvedTable(table, nil, nil), 154 plan.NewResolvedTable(table, nil, nil), 155 ))) 156 } 157 158 func TestIsBinary(t *testing.T) { 159 require := require.New(t) 160 db := memory.NewDatabase("test") 161 table := memory.NewTable(db.BaseDatabase, "foo", sql.PrimaryKeySchema{}, nil) 162 163 require.False(plan.IsBinary(plan.NewFilter(nil, plan.NewResolvedTable(table, nil, nil)))) 164 require.True(plan.IsBinary(plan.NewCrossJoin( 165 plan.NewResolvedTable(table, nil, nil), 166 plan.NewResolvedTable(table, nil, nil), 167 ))) 168 }