github.com/dolthub/go-mysql-server@v0.18.0/sql/rowexec/subquery_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 TestSubquery(t *testing.T) { 30 require := require.New(t) 31 db := memory.NewDatabase("test") 32 pro := memory.NewDBProvider(db) 33 ctx := newContext(pro) 34 35 table := memory.NewTable(db, "", sql.PrimaryKeySchema{}, nil) 36 require.NoError(table.Insert(ctx, nil)) 37 38 subquery := plan.NewSubquery(plan.NewProject( 39 []sql.Expression{ 40 expression.NewLiteral("one", types.LongText), 41 }, 42 plan.NewResolvedTable(table, nil, nil), 43 ), "select 'one'").WithExecBuilder(DefaultBuilder) 44 45 value, err := subquery.Eval(ctx, nil) 46 require.NoError(err) 47 require.Equal(value, "one") 48 } 49 50 func TestSubqueryTooManyRows(t *testing.T) { 51 require := require.New(t) 52 db := memory.NewDatabase("test") 53 pro := memory.NewDBProvider(db) 54 ctx := newContext(pro) 55 56 table := memory.NewTable(db, "", sql.PrimaryKeySchema{}, nil) 57 require.NoError(table.Insert(ctx, nil)) 58 require.NoError(table.Insert(ctx, nil)) 59 60 subquery := plan.NewSubquery(plan.NewProject( 61 []sql.Expression{ 62 expression.NewLiteral("one", types.LongText), 63 }, 64 plan.NewResolvedTable(table, nil, nil), 65 ), "select 'one'").WithExecBuilder(DefaultBuilder) 66 67 _, err := subquery.Eval(ctx, nil) 68 require.Error(err) 69 } 70 71 func TestSubqueryMultipleRows(t *testing.T) { 72 require := require.New(t) 73 74 db := memory.NewDatabase("test") 75 pro := memory.NewDBProvider(db) 76 ctx := newContext(pro) 77 78 table := memory.NewTable(db, "foo", sql.NewPrimaryKeySchema(sql.Schema{ 79 {Name: "t", Source: "foo", Type: types.Text}, 80 }), nil) 81 82 require.NoError(table.Insert(ctx, sql.Row{"one"})) 83 require.NoError(table.Insert(ctx, sql.Row{"two"})) 84 require.NoError(table.Insert(ctx, sql.Row{"three"})) 85 86 subquery := plan.NewSubquery(plan.NewProject( 87 []sql.Expression{ 88 expression.NewGetField(0, types.Text, "t", false), 89 }, 90 plan.NewResolvedTable(table, nil, nil), 91 ), "select t from foo").WithExecBuilder(DefaultBuilder) 92 93 values, err := subquery.EvalMultiple(ctx, nil) 94 require.NoError(err) 95 require.Equal(values, []interface{}{"one", "two", "three"}) 96 }