github.com/dolthub/go-mysql-server@v0.18.0/sql/rowexec/union_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 TestUnion(t *testing.T) {
    31  	require := require.New(t)
    32  
    33  	db := memory.NewDatabase("test")
    34  	pro := memory.NewDBProvider(db)
    35  	ctx := newContext(pro)
    36  
    37  	childSchema := sql.NewPrimaryKeySchema(sql.Schema{
    38  		{Name: "name", Type: types.Text, Nullable: true},
    39  		{Name: "email", Type: types.Text, Nullable: true},
    40  	})
    41  	child := memory.NewTable(db, "test", childSchema, nil)
    42  	empty := memory.NewTable(db, "empty", childSchema, nil)
    43  
    44  	rows := []sql.Row{
    45  		sql.NewRow("john", "john@doe.com"),
    46  		sql.NewRow("jane", "jane@doe.com"),
    47  		sql.NewRow("john", "johnx@doe.com"),
    48  		sql.NewRow("martha", "marthax@doe.com"),
    49  		sql.NewRow("martha", "martha@doe.com"),
    50  	}
    51  
    52  	for _, r := range rows {
    53  		require.NoError(child.Insert(ctx, r))
    54  	}
    55  
    56  	name := []sql.Expression{
    57  		expression.NewGetField(0, types.Text, "name", true),
    58  	}
    59  
    60  	cases := []struct {
    61  		node     sql.Node
    62  		expected []string
    63  	}{
    64  		{
    65  			plan.NewSetOp(plan.UnionType, plan.NewProject(name, plan.NewResolvedTable(child, nil, nil)), plan.NewProject(name, plan.NewResolvedTable(child, nil, nil)), false, nil, nil, nil),
    66  			[]string{
    67  				"john", "jane", "john", "martha", "martha",
    68  				"john", "jane", "john", "martha", "martha",
    69  			},
    70  		},
    71  		{
    72  			plan.NewSetOp(plan.UnionType, plan.NewProject(name, plan.NewResolvedTable(empty, nil, nil)), plan.NewProject(name, plan.NewResolvedTable(child, nil, nil)), false, nil, nil, nil),
    73  			[]string{
    74  				"john", "jane", "john", "martha", "martha",
    75  			},
    76  		},
    77  		{
    78  			plan.NewSetOp(plan.UnionType, plan.NewProject(name, plan.NewResolvedTable(child, nil, nil)), plan.NewProject(name, plan.NewResolvedTable(empty, nil, nil)), false, nil, nil, nil),
    79  			[]string{
    80  				"john", "jane", "john", "martha", "martha",
    81  			},
    82  		},
    83  	}
    84  
    85  	for _, c := range cases {
    86  		iter, err := DefaultBuilder.Build(ctx, c.node, nil)
    87  		require.NoError(err)
    88  		require.NotNil(iter)
    89  
    90  		var results []string
    91  		for {
    92  			row, err := iter.Next(ctx)
    93  			if err == io.EOF {
    94  				break
    95  			}
    96  			require.NoError(err)
    97  			result, ok := row[0].(string)
    98  			require.True(ok, "first row column should be string, but is %T", row[0])
    99  			results = append(results, result)
   100  		}
   101  
   102  		require.Equal(c.expected, results)
   103  	}
   104  }