github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/function/aggregation/count_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 aggregation
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/stretchr/testify/require"
    21  
    22  	"github.com/dolthub/go-mysql-server/sql"
    23  	"github.com/dolthub/go-mysql-server/sql/expression"
    24  	"github.com/dolthub/go-mysql-server/sql/types"
    25  )
    26  
    27  func TestCountEval1(t *testing.T) {
    28  	require := require.New(t)
    29  	ctx := sql.NewEmptyContext()
    30  
    31  	c := NewCount(expression.NewLiteral(1, types.Int32))
    32  	b, _ := c.NewBuffer()
    33  	require.Equal(int64(0), evalBuffer(t, b))
    34  
    35  	require.NoError(b.Update(ctx, nil))
    36  	require.NoError(b.Update(ctx, sql.NewRow("foo")))
    37  	require.NoError(b.Update(ctx, sql.NewRow(1)))
    38  	require.NoError(b.Update(ctx, sql.NewRow(nil)))
    39  	require.NoError(b.Update(ctx, sql.NewRow(1, 2, 3)))
    40  	require.Equal(int64(5), evalBuffer(t, b))
    41  }
    42  
    43  func TestCountEvalStar(t *testing.T) {
    44  	require := require.New(t)
    45  	ctx := sql.NewEmptyContext()
    46  
    47  	c := NewCount(expression.NewStar())
    48  	b, _ := c.NewBuffer()
    49  	require.Equal(int64(0), evalBuffer(t, b))
    50  
    51  	require.NoError(b.Update(ctx, nil))
    52  	require.NoError(b.Update(ctx, sql.NewRow("foo")))
    53  	require.NoError(b.Update(ctx, sql.NewRow(1)))
    54  	require.NoError(b.Update(ctx, sql.NewRow(nil)))
    55  	require.NoError(b.Update(ctx, sql.NewRow(1, 2, 3)))
    56  	require.Equal(int64(5), evalBuffer(t, b))
    57  }
    58  
    59  func TestCountEvalString(t *testing.T) {
    60  	require := require.New(t)
    61  	ctx := sql.NewEmptyContext()
    62  
    63  	c := NewCount(expression.NewGetField(0, types.Text, "", true))
    64  	b, _ := c.NewBuffer()
    65  	require.Equal(int64(0), evalBuffer(t, b))
    66  
    67  	require.NoError(b.Update(ctx, sql.NewRow("foo")))
    68  	require.Equal(int64(1), evalBuffer(t, b))
    69  
    70  	require.NoError(b.Update(ctx, sql.NewRow(nil)))
    71  	require.Equal(int64(1), evalBuffer(t, b))
    72  }
    73  
    74  func TestCountDistinctEval1(t *testing.T) {
    75  	require := require.New(t)
    76  	ctx := sql.NewEmptyContext()
    77  
    78  	c := NewCountDistinct(expression.NewLiteral(1, types.Int32))
    79  	b, _ := c.NewBuffer()
    80  	require.Equal(int64(0), evalBuffer(t, b))
    81  
    82  	require.NoError(b.Update(ctx, nil))
    83  	require.NoError(b.Update(ctx, sql.NewRow("foo")))
    84  	require.NoError(b.Update(ctx, sql.NewRow(1)))
    85  	require.NoError(b.Update(ctx, sql.NewRow(nil)))
    86  	require.NoError(b.Update(ctx, sql.NewRow(1, 2, 3)))
    87  	require.Equal(int64(1), evalBuffer(t, b))
    88  }
    89  
    90  func TestCountDistinctEvalStar(t *testing.T) {
    91  	require := require.New(t)
    92  	ctx := sql.NewEmptyContext()
    93  
    94  	c := NewCountDistinct(expression.NewStar())
    95  	b, _ := c.NewBuffer()
    96  	require.Equal(int64(0), evalBuffer(t, b))
    97  
    98  	require.NoError(b.Update(ctx, nil))
    99  	require.NoError(b.Update(ctx, sql.NewRow("foo")))
   100  	require.NoError(b.Update(ctx, sql.NewRow(1)))
   101  	require.NoError(b.Update(ctx, sql.NewRow(nil)))
   102  	require.NoError(b.Update(ctx, sql.NewRow(1, 2, 3)))
   103  	require.Equal(int64(4), evalBuffer(t, b))
   104  }
   105  
   106  func TestCountDistinctEvalString(t *testing.T) {
   107  	require := require.New(t)
   108  	ctx := sql.NewEmptyContext()
   109  
   110  	c := NewCountDistinct(expression.NewGetField(0, types.Text, "", true))
   111  	b, _ := c.NewBuffer()
   112  	require.Equal(int64(0), evalBuffer(t, b))
   113  
   114  	require.NoError(b.Update(ctx, sql.NewRow("foo")))
   115  	require.Equal(int64(1), evalBuffer(t, b))
   116  
   117  	require.NoError(b.Update(ctx, sql.NewRow(nil)))
   118  	require.NoError(b.Update(ctx, sql.NewRow("foo")))
   119  	require.NoError(b.Update(ctx, sql.NewRow("bar")))
   120  	require.Equal(int64(2), evalBuffer(t, b))
   121  }