github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/function/aggregation/min_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  	"time"
    20  
    21  	"github.com/stretchr/testify/require"
    22  
    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/types"
    26  )
    27  
    28  func TestMin_Name(t *testing.T) {
    29  	assert := require.New(t)
    30  
    31  	m := NewMin(expression.NewGetField(0, types.Int32, "field", true))
    32  	assert.Equal("MIN(field)", m.String())
    33  }
    34  
    35  func TestMin_Eval_Int32(t *testing.T) {
    36  	assert := require.New(t)
    37  	ctx := sql.NewEmptyContext()
    38  
    39  	m := NewMin(expression.NewGetField(0, types.Int32, "field", true))
    40  	b, _ := m.NewBuffer()
    41  
    42  	b.Update(ctx, sql.NewRow(int32(7)))
    43  	b.Update(ctx, sql.NewRow(int32(2)))
    44  	b.Update(ctx, sql.NewRow(nil))
    45  
    46  	v, err := b.Eval(ctx)
    47  	assert.NoError(err)
    48  	assert.Equal(int32(2), v)
    49  }
    50  
    51  func TestMin_Eval_Text(t *testing.T) {
    52  	assert := require.New(t)
    53  	ctx := sql.NewEmptyContext()
    54  
    55  	m := NewMin(expression.NewGetField(0, types.Text, "field", true))
    56  	b, _ := m.NewBuffer()
    57  
    58  	b.Update(ctx, sql.NewRow("a"))
    59  	b.Update(ctx, sql.NewRow("A"))
    60  	b.Update(ctx, sql.NewRow("b"))
    61  
    62  	v, err := b.Eval(ctx)
    63  	assert.NoError(err)
    64  	assert.Equal("A", v)
    65  }
    66  
    67  func TestMin_Eval_Timestamp(t *testing.T) {
    68  	assert := require.New(t)
    69  	ctx := sql.NewEmptyContext()
    70  
    71  	m := NewMin(expression.NewGetField(0, types.Timestamp, "field", true))
    72  	b, _ := m.NewBuffer()
    73  
    74  	expected, _ := time.Parse(sql.TimestampDatetimeLayout, "2006-01-02 15:04:05")
    75  	someTime, _ := time.Parse(sql.TimestampDatetimeLayout, "2007-01-02 15:04:05")
    76  	otherTime, _ := time.Parse(sql.TimestampDatetimeLayout, "2008-01-02 15:04:05")
    77  
    78  	b.Update(ctx, sql.NewRow(someTime))
    79  	b.Update(ctx, sql.NewRow(expected))
    80  	b.Update(ctx, sql.NewRow(otherTime))
    81  
    82  	v, err := b.Eval(ctx)
    83  	assert.NoError(err)
    84  	assert.Equal(expected, v)
    85  }
    86  
    87  func TestMin_Eval_NULL(t *testing.T) {
    88  	assert := require.New(t)
    89  	ctx := sql.NewEmptyContext()
    90  
    91  	m := NewMin(expression.NewGetField(0, types.Int32, "field", true))
    92  	b, _ := m.NewBuffer()
    93  
    94  	b.Update(ctx, sql.NewRow(nil))
    95  	b.Update(ctx, sql.NewRow(nil))
    96  	b.Update(ctx, sql.NewRow(nil))
    97  
    98  	v, err := b.Eval(ctx)
    99  	assert.NoError(err)
   100  	assert.Equal(nil, v)
   101  }
   102  
   103  func TestMin_Eval_Empty(t *testing.T) {
   104  	assert := require.New(t)
   105  	ctx := sql.NewEmptyContext()
   106  
   107  	m := NewMin(expression.NewGetField(0, types.Int32, "field", true))
   108  	b, _ := m.NewBuffer()
   109  
   110  	v, err := b.Eval(ctx)
   111  	assert.NoError(err)
   112  	assert.Equal(nil, v)
   113  }
   114  
   115  func TestMin_Distinct(t *testing.T) {
   116  	assert := require.New(t)
   117  	ctx := sql.NewEmptyContext()
   118  
   119  	m := NewMin(expression.NewDistinctExpression(expression.NewGetField(0, types.Int32, "field", true)))
   120  	b, _ := m.NewBuffer()
   121  
   122  	require.Equal(t, "MIN(DISTINCT field)", m.String())
   123  
   124  	require.NoError(t, b.Update(ctx, sql.Row{1}))
   125  	require.NoError(t, b.Update(ctx, sql.Row{1}))
   126  	require.NoError(t, b.Update(ctx, sql.Row{2}))
   127  	require.NoError(t, b.Update(ctx, sql.Row{3}))
   128  	require.NoError(t, b.Update(ctx, sql.Row{3}))
   129  
   130  	v, err := b.Eval(ctx)
   131  	assert.NoError(err)
   132  	assert.Equal(1, v)
   133  
   134  	m = NewMin(expression.NewDistinctExpression(expression.NewGetField(0, types.Int32, "field", true)))
   135  	b, _ = m.NewBuffer()
   136  
   137  	require.Equal(t, "MIN(DISTINCT field)", m.String())
   138  
   139  	require.NoError(t, b.Update(ctx, sql.Row{1}))
   140  	require.NoError(t, b.Update(ctx, sql.Row{1}))
   141  	require.NoError(t, b.Update(ctx, sql.Row{2}))
   142  	require.NoError(t, b.Update(ctx, sql.Row{nil}))
   143  	require.NoError(t, b.Update(ctx, sql.Row{nil}))
   144  	require.NoError(t, b.Update(ctx, sql.Row{3}))
   145  
   146  	v, err = b.Eval(ctx)
   147  	assert.NoError(err)
   148  	assert.Equal(1, v)
   149  }