github.com/dolthub/go-mysql-server@v0.18.0/sql/rowexec/showvariables_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 rowexec
    16  
    17  import (
    18  	"io"
    19  	"testing"
    20  
    21  	"github.com/stretchr/testify/assert"
    22  	"github.com/stretchr/testify/require"
    23  
    24  	"github.com/dolthub/go-mysql-server/sql"
    25  	"github.com/dolthub/go-mysql-server/sql/expression"
    26  	"github.com/dolthub/go-mysql-server/sql/plan"
    27  	"github.com/dolthub/go-mysql-server/sql/types"
    28  )
    29  
    30  func TestShowVariables(t *testing.T) {
    31  	require := require.New(t)
    32  
    33  	ctx := sql.NewEmptyContext()
    34  	sv := plan.NewShowVariables(nil, false)
    35  	require.True(sv.Resolved())
    36  
    37  	it, err := DefaultBuilder.Build(ctx, sv, nil)
    38  	require.NoError(err)
    39  
    40  	vars := ctx.GetAllSessionVariables()
    41  	for row, err := it.Next(ctx); err == nil; row, err = it.Next(ctx) {
    42  		key := row[0].(string)
    43  		val := row[1]
    44  
    45  		t.Logf("key: %s\tval: %v\n", key, val)
    46  
    47  		require.Equal(vars[key], val)
    48  		delete(vars, key)
    49  	}
    50  	if err != io.EOF {
    51  		require.NoError(err)
    52  	}
    53  	require.NoError(it.Close(ctx))
    54  	require.Equal(0, len(vars))
    55  }
    56  
    57  func TestShowVariablesWithLike(t *testing.T) {
    58  	sv := plan.NewShowVariables(expression.NewLike(
    59  		expression.NewGetField(0, types.LongText, "", false),
    60  		expression.NewLiteral("%t_into_buffer_size", types.LongText),
    61  		nil,
    62  	), false)
    63  	require.True(t, sv.Resolved())
    64  
    65  	context := sql.NewEmptyContext()
    66  	err := context.SetSessionVariable(context, "select_into_buffer_size", int64(8192))
    67  	require.NoError(t, err)
    68  
    69  	it, err := DefaultBuilder.Build(context, sv, nil)
    70  	require.NoError(t, err)
    71  
    72  	rows, err := sql.RowIterToRows(context, it)
    73  	require.NoError(t, err)
    74  
    75  	expectedRows := []sql.Row{
    76  		{"select_into_buffer_size", int64(8192)},
    77  	}
    78  
    79  	assert.Equal(t, expectedRows, rows)
    80  
    81  	// GLOBAL variable should not change
    82  	sv2 := plan.NewShowVariables(expression.NewLike(
    83  		expression.NewGetField(0, types.LongText, "", false),
    84  		expression.NewLiteral("%t_into_buffer_size", types.LongText),
    85  		nil,
    86  	), true)
    87  	require.True(t, sv2.Resolved())
    88  
    89  	it2, err := DefaultBuilder.Build(context, sv2, nil)
    90  	require.NoError(t, err)
    91  
    92  	rows2, err := sql.RowIterToRows(context, it2)
    93  	require.NoError(t, err)
    94  
    95  	expectedRows2 := []sql.Row{
    96  		{"select_into_buffer_size", int64(131072)},
    97  	}
    98  
    99  	assert.Equal(t, expectedRows2, rows2)
   100  }
   101  
   102  func TestShowVariablesWithWhere(t *testing.T) {
   103  	filter := expression.NewEquals(expression.NewGetField(0, types.Text, "variable_name", true), expression.NewLiteral("select_into_buffer_size", types.Text))
   104  	sv := plan.NewShowVariables(filter, false)
   105  	require.True(t, sv.Resolved())
   106  
   107  	context := sql.NewEmptyContext()
   108  	err := context.SetSessionVariable(context, "select_into_buffer_size", int64(8192))
   109  	require.NoError(t, err)
   110  
   111  	it, err := DefaultBuilder.Build(context, sv, nil)
   112  	require.NoError(t, err)
   113  
   114  	rows, err := sql.RowIterToRows(context, it)
   115  	require.NoError(t, err)
   116  
   117  	expectedRows := []sql.Row{
   118  		{"select_into_buffer_size", int64(8192)},
   119  	}
   120  
   121  	assert.Equal(t, expectedRows, rows)
   122  }