github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/tuple_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 expression
    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/types"
    24  )
    25  
    26  func TestTuple(t *testing.T) {
    27  	require := require.New(t)
    28  
    29  	tup := NewTuple(
    30  		NewLiteral(int64(1), types.Int64),
    31  		NewLiteral(float64(3.14), types.Float64),
    32  		NewLiteral("foo", types.LongText),
    33  	)
    34  
    35  	ctx := sql.NewEmptyContext()
    36  
    37  	require.False(tup.IsNullable())
    38  	require.True(tup.Resolved())
    39  	require.Equal(types.CreateTuple(types.Int64, types.Float64, types.LongText), tup.Type())
    40  
    41  	result, err := tup.Eval(ctx, nil)
    42  	require.NoError(err)
    43  	require.Equal([]interface{}{int64(1), float64(3.14), "foo"}, result)
    44  
    45  	tup = NewTuple(
    46  		NewGetField(0, types.LongText, "text", true),
    47  	)
    48  
    49  	require.True(tup.IsNullable())
    50  	require.True(tup.Resolved())
    51  	require.Equal(types.LongText, tup.Type())
    52  
    53  	result, err = tup.Eval(ctx, sql.NewRow("foo"))
    54  	require.NoError(err)
    55  	require.Equal("foo", result)
    56  
    57  	tup = NewTuple(
    58  		NewGetField(0, types.LongText, "text", true),
    59  		NewLiteral("bar", types.LongText),
    60  	)
    61  
    62  	require.False(tup.IsNullable())
    63  	require.True(tup.Resolved())
    64  	require.Equal(types.CreateTuple(types.LongText, types.LongText), tup.Type())
    65  
    66  	result, err = tup.Eval(ctx, sql.NewRow("foo"))
    67  	require.NoError(err)
    68  	require.Equal([]interface{}{"foo", "bar"}, result)
    69  
    70  	tup = NewTuple(
    71  		NewUnresolvedColumn("bar"),
    72  		NewLiteral("bar", types.LongText),
    73  	)
    74  
    75  	require.False(tup.Resolved())
    76  	require.False(tup.IsNullable())
    77  }