github.com/dolthub/go-mysql-server@v0.18.0/sql/rowexec/process_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 TestQueryProcess(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  	table := memory.NewTable(db.BaseDatabase, "foo", sql.NewPrimaryKeySchema(sql.Schema{
    37  		{Name: "a", Type: types.Int64},
    38  	}), nil)
    39  
    40  	table.Insert(ctx, sql.NewRow(int64(1)))
    41  	table.Insert(ctx, sql.NewRow(int64(2)))
    42  
    43  	var notifications int
    44  
    45  	node := plan.NewQueryProcess(
    46  		plan.NewProject(
    47  			[]sql.Expression{
    48  				expression.NewGetField(0, types.Int64, "a", false),
    49  			},
    50  			plan.NewResolvedTable(table, nil, nil),
    51  		),
    52  		func() {
    53  			notifications++
    54  		},
    55  	)
    56  
    57  	iter, err := DefaultBuilder.Build(ctx, node, nil)
    58  	require.NoError(err)
    59  
    60  	rows, err := sql.RowIterToRows(ctx, iter)
    61  	require.NoError(err)
    62  
    63  	expected := []sql.Row{
    64  		{int64(1)},
    65  		{int64(2)},
    66  	}
    67  
    68  	require.ElementsMatch(expected, rows)
    69  	require.Equal(1, notifications)
    70  }
    71  
    72  func TestProcessTable(t *testing.T) {
    73  	require := require.New(t)
    74  
    75  	db := memory.NewDatabase("test")
    76  	pro := memory.NewDBProvider(db)
    77  	ctx := newContext(pro)
    78  
    79  	table := memory.NewPartitionedTable(db.BaseDatabase, "foo", sql.NewPrimaryKeySchema(sql.Schema{
    80  		{Name: "a", Type: types.Int64},
    81  	}), nil, 2)
    82  
    83  	table.Insert(ctx, sql.NewRow(int64(1)))
    84  	table.Insert(ctx, sql.NewRow(int64(2)))
    85  	table.Insert(ctx, sql.NewRow(int64(3)))
    86  	table.Insert(ctx, sql.NewRow(int64(4)))
    87  
    88  	var partitionDoneNotifications int
    89  	var partitionStartNotifications int
    90  	var rowNextNotifications int
    91  
    92  	node := plan.NewProject(
    93  		[]sql.Expression{
    94  			expression.NewGetField(0, types.Int64, "a", false),
    95  		},
    96  		plan.NewResolvedTable(plan.NewProcessTable(
    97  			table,
    98  			func(partitionName string) {
    99  				partitionDoneNotifications++
   100  			},
   101  			func(partitionName string) {
   102  				partitionStartNotifications++
   103  			},
   104  			func(partitionName string) {
   105  				rowNextNotifications++
   106  			},
   107  		), nil, nil),
   108  	)
   109  
   110  	iter, err := DefaultBuilder.Build(ctx, node, nil)
   111  	require.NoError(err)
   112  
   113  	rows, err := sql.RowIterToRows(ctx, iter)
   114  	require.NoError(err)
   115  
   116  	expected := []sql.Row{
   117  		{int64(1)},
   118  		{int64(2)},
   119  		{int64(3)},
   120  		{int64(4)},
   121  	}
   122  
   123  	require.ElementsMatch(expected, rows)
   124  	require.Equal(2, partitionDoneNotifications)
   125  	require.Equal(2, partitionStartNotifications)
   126  	require.Equal(4, rowNextNotifications)
   127  }