github.com/dolthub/go-mysql-server@v0.18.0/sql/rowexec/having_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 TestHaving(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.NewHaving(
    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  
    61  	require.Equal(1, len(f.Children()))
    62  
    63  	iter, err := DefaultBuilder.Build(ctx, f, nil)
    64  	require.NoError(err)
    65  	require.NotNil(iter)
    66  
    67  	row, err := iter.Next(ctx)
    68  	require.NoError(err)
    69  	require.NotNil(row)
    70  
    71  	require.Equal("col1_1", row[0])
    72  	require.Equal("col2_1", row[1])
    73  
    74  	row, err = iter.Next(ctx)
    75  	require.NotNil(err)
    76  	require.Nil(row)
    77  
    78  	f = plan.NewHaving(
    79  		expression.NewEquals(
    80  			expression.NewGetField(2, types.Int32, "col3", true),
    81  			expression.NewLiteral(int32(1111), types.Int32),
    82  		),
    83  		plan.NewResolvedTable(child, nil, nil),
    84  	)
    85  
    86  	iter, err = DefaultBuilder.Build(ctx, f, nil)
    87  	require.NoError(err)
    88  	require.NotNil(iter)
    89  
    90  	row, err = iter.Next(ctx)
    91  	require.NoError(err)
    92  	require.NotNil(row)
    93  
    94  	require.Equal(int32(1111), row[2])
    95  	require.Equal(int64(2222), row[3])
    96  
    97  	f = plan.NewHaving(
    98  		expression.NewEquals(
    99  			expression.NewGetField(3, types.Int64, "col4", true),
   100  			expression.NewLiteral(int64(4444), types.Int64),
   101  		),
   102  		plan.NewResolvedTable(child, nil, nil),
   103  	)
   104  
   105  	iter, err = DefaultBuilder.Build(ctx, f, nil)
   106  	require.NoError(err)
   107  	require.NotNil(iter)
   108  
   109  	row, err = iter.Next(ctx)
   110  	require.NoError(err)
   111  	require.NotNil(row)
   112  
   113  	require.Equal(int32(3333), row[2])
   114  	require.Equal(int64(4444), row[3])
   115  }