github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/function/aggregation/bitwise_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 TestBitAnd_String(t *testing.T) {
    28  	assert := require.New(t)
    29  	m := NewBitAnd(expression.NewGetField(0, types.Int32, "field", true))
    30  	assert.Equal("BITAND(field)", m.String())
    31  }
    32  
    33  func TestBitAnd_Eval_Int(t *testing.T) {
    34  	assert := require.New(t)
    35  	ctx := sql.NewEmptyContext()
    36  
    37  	m := NewBitAnd(expression.NewGetField(0, types.Int64, "field", true))
    38  	b, _ := m.NewBuffer()
    39  
    40  	b.Update(ctx, sql.NewRow(1))
    41  	b.Update(ctx, sql.NewRow(3))
    42  	b.Update(ctx, sql.NewRow(7))
    43  
    44  	v, err := b.Eval(ctx)
    45  	assert.NoError(err)
    46  	assert.Equal(uint64(1), v)
    47  }
    48  
    49  func TestBitAnd_Eval_Float64(t *testing.T) {
    50  	assert := require.New(t)
    51  	ctx := sql.NewEmptyContext()
    52  
    53  	m := NewBitAnd(expression.NewGetField(0, types.Float64, "field", true))
    54  	b, _ := m.NewBuffer()
    55  
    56  	b.Update(ctx, sql.NewRow(1.123123))
    57  	b.Update(ctx, sql.NewRow(3.3452345))
    58  	b.Update(ctx, sql.NewRow(7.1123123))
    59  
    60  	v, err := b.Eval(ctx)
    61  	assert.NoError(err)
    62  	assert.Equal(uint64(1), v)
    63  }
    64  
    65  func TestBitAnd_Eval_Text(t *testing.T) {
    66  	assert := require.New(t)
    67  	ctx := sql.NewEmptyContext()
    68  
    69  	m := NewBitAnd(expression.NewGetField(0, types.Text, "field", true))
    70  	b, _ := m.NewBuffer()
    71  
    72  	b.Update(ctx, sql.NewRow("a"))
    73  	b.Update(ctx, sql.NewRow("A"))
    74  	b.Update(ctx, sql.NewRow("b"))
    75  
    76  	v, err := b.Eval(ctx)
    77  	assert.NoError(err)
    78  	assert.Equal(uint64(0), v)
    79  }
    80  
    81  func TestBitAnd_Eval_NULL(t *testing.T) {
    82  	assert := require.New(t)
    83  	ctx := sql.NewEmptyContext()
    84  
    85  	m := NewBitAnd(expression.NewGetField(0, types.Int32, "field", true))
    86  	b, _ := m.NewBuffer()
    87  
    88  	b.Update(ctx, sql.NewRow(nil))
    89  	b.Update(ctx, sql.NewRow(nil))
    90  	b.Update(ctx, sql.NewRow(nil))
    91  
    92  	v, err := b.Eval(ctx)
    93  	assert.NoError(err)
    94  	assert.Equal(^uint64(0), v)
    95  }
    96  
    97  func TestBitAnd_Eval_Empty(t *testing.T) {
    98  	assert := require.New(t)
    99  	ctx := sql.NewEmptyContext()
   100  
   101  	m := NewBitAnd(expression.NewGetField(0, types.Int32, "field", true))
   102  	b, _ := m.NewBuffer()
   103  
   104  	v, err := b.Eval(ctx)
   105  	assert.NoError(err)
   106  	assert.Equal(^uint64(0), v)
   107  }
   108  
   109  func TestBitOr_String(t *testing.T) {
   110  	assert := require.New(t)
   111  	m := NewBitOr(expression.NewGetField(0, types.Int32, "field", true))
   112  	assert.Equal("BITOR(field)", m.String())
   113  }
   114  
   115  func TestBitOr_Eval_Int(t *testing.T) {
   116  	assert := require.New(t)
   117  	ctx := sql.NewEmptyContext()
   118  
   119  	m := NewBitOr(expression.NewGetField(0, types.Int64, "field", true))
   120  	b, _ := m.NewBuffer()
   121  
   122  	b.Update(ctx, sql.NewRow(1))
   123  	b.Update(ctx, sql.NewRow(2))
   124  	b.Update(ctx, sql.NewRow(4))
   125  
   126  	v, err := b.Eval(ctx)
   127  	assert.NoError(err)
   128  	assert.Equal(uint64(7), v)
   129  }
   130  
   131  func TestBitOr_Eval_Float64(t *testing.T) {
   132  	assert := require.New(t)
   133  	ctx := sql.NewEmptyContext()
   134  
   135  	m := NewBitOr(expression.NewGetField(0, types.Float64, "field", true))
   136  	b, _ := m.NewBuffer()
   137  
   138  	b.Update(ctx, sql.NewRow(1.123123))
   139  	b.Update(ctx, sql.NewRow(2.3452345))
   140  	b.Update(ctx, sql.NewRow(4.1123123))
   141  
   142  	v, err := b.Eval(ctx)
   143  	assert.NoError(err)
   144  	assert.Equal(uint64(7), v)
   145  }
   146  
   147  func TestBitOr_Eval_Text(t *testing.T) {
   148  	assert := require.New(t)
   149  	ctx := sql.NewEmptyContext()
   150  
   151  	m := NewBitOr(expression.NewGetField(0, types.Text, "field", true))
   152  	b, _ := m.NewBuffer()
   153  
   154  	b.Update(ctx, sql.NewRow("a"))
   155  	b.Update(ctx, sql.NewRow("A"))
   156  	b.Update(ctx, sql.NewRow("b"))
   157  
   158  	v, err := b.Eval(ctx)
   159  	assert.NoError(err)
   160  	assert.Equal(uint64(0), v)
   161  }
   162  
   163  func TestBitOr_Eval_NULL(t *testing.T) {
   164  	assert := require.New(t)
   165  	ctx := sql.NewEmptyContext()
   166  
   167  	m := NewBitOr(expression.NewGetField(0, types.Int32, "field", true))
   168  	b, _ := m.NewBuffer()
   169  
   170  	b.Update(ctx, sql.NewRow(nil))
   171  	b.Update(ctx, sql.NewRow(nil))
   172  	b.Update(ctx, sql.NewRow(nil))
   173  
   174  	v, err := b.Eval(ctx)
   175  	assert.NoError(err)
   176  	assert.Equal(uint64(0), v)
   177  }
   178  
   179  func TestBitOr_Eval_Empty(t *testing.T) {
   180  	assert := require.New(t)
   181  	ctx := sql.NewEmptyContext()
   182  
   183  	m := NewBitOr(expression.NewGetField(0, types.Int32, "field", true))
   184  	b, _ := m.NewBuffer()
   185  
   186  	v, err := b.Eval(ctx)
   187  	assert.NoError(err)
   188  	assert.Equal(uint64(0), v)
   189  }
   190  
   191  func TestBitXor_String(t *testing.T) {
   192  	assert := require.New(t)
   193  	m := NewBitXor(expression.NewGetField(0, types.Int32, "field", true))
   194  	assert.Equal("BITXOR(field)", m.String())
   195  }
   196  
   197  func TestBitXor_Eval_Int(t *testing.T) {
   198  	assert := require.New(t)
   199  	ctx := sql.NewEmptyContext()
   200  
   201  	m := NewBitXor(expression.NewGetField(0, types.Int64, "field", true))
   202  	b, _ := m.NewBuffer()
   203  
   204  	b.Update(ctx, sql.NewRow(1))
   205  	b.Update(ctx, sql.NewRow(2))
   206  	b.Update(ctx, sql.NewRow(5))
   207  
   208  	v, err := b.Eval(ctx)
   209  	assert.NoError(err)
   210  	assert.Equal(uint64(6), v)
   211  }
   212  
   213  func TestBitXor_Eval_Float64(t *testing.T) {
   214  	assert := require.New(t)
   215  	ctx := sql.NewEmptyContext()
   216  
   217  	m := NewBitXor(expression.NewGetField(0, types.Float64, "field", true))
   218  	b, _ := m.NewBuffer()
   219  
   220  	b.Update(ctx, sql.NewRow(1.123123))
   221  	b.Update(ctx, sql.NewRow(2.3452345))
   222  	b.Update(ctx, sql.NewRow(5.1123123))
   223  
   224  	v, err := b.Eval(ctx)
   225  	assert.NoError(err)
   226  	assert.Equal(uint64(6), v)
   227  }
   228  
   229  func TestBitXor_Eval_Text(t *testing.T) {
   230  	assert := require.New(t)
   231  	ctx := sql.NewEmptyContext()
   232  
   233  	m := NewBitXor(expression.NewGetField(0, types.Text, "field", true))
   234  	b, _ := m.NewBuffer()
   235  
   236  	b.Update(ctx, sql.NewRow("a"))
   237  	b.Update(ctx, sql.NewRow("A"))
   238  	b.Update(ctx, sql.NewRow("b"))
   239  
   240  	v, err := b.Eval(ctx)
   241  	assert.NoError(err)
   242  	assert.Equal(uint64(0), v)
   243  }
   244  
   245  func TestBitXor_Eval_NULL(t *testing.T) {
   246  	assert := require.New(t)
   247  	ctx := sql.NewEmptyContext()
   248  
   249  	m := NewBitXor(expression.NewGetField(0, types.Int32, "field", true))
   250  	b, _ := m.NewBuffer()
   251  
   252  	b.Update(ctx, sql.NewRow(nil))
   253  	b.Update(ctx, sql.NewRow(nil))
   254  	b.Update(ctx, sql.NewRow(nil))
   255  
   256  	v, err := b.Eval(ctx)
   257  	assert.NoError(err)
   258  	assert.Equal(uint64(0), v)
   259  }
   260  
   261  func TestBitXor_Eval_Empty(t *testing.T) {
   262  	assert := require.New(t)
   263  	ctx := sql.NewEmptyContext()
   264  
   265  	m := NewBitXor(expression.NewGetField(0, types.Int32, "field", true))
   266  	b, _ := m.NewBuffer()
   267  
   268  	v, err := b.Eval(ctx)
   269  	assert.NoError(err)
   270  	assert.Equal(uint64(0), v)
   271  }