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