vitess.io/vitess@v0.16.2/go/vt/sqlparser/ast_funcs_test.go (about)

     1  /*
     2  Copyright 2022 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package sqlparser
    18  
    19  import (
    20  	"fmt"
    21  	"testing"
    22  
    23  	"github.com/stretchr/testify/assert"
    24  	"github.com/stretchr/testify/require"
    25  
    26  	"vitess.io/vitess/go/sqltypes"
    27  	querypb "vitess.io/vitess/go/vt/proto/query"
    28  )
    29  
    30  func TestAddQueryHint(t *testing.T) {
    31  	tcs := []struct {
    32  		comments  Comments
    33  		queryHint string
    34  		expected  Comments
    35  		err       string
    36  	}{
    37  		{
    38  			comments:  Comments{},
    39  			queryHint: "",
    40  			expected:  nil,
    41  		},
    42  		{
    43  			comments:  Comments{},
    44  			queryHint: "SET_VAR(aa)",
    45  			expected:  Comments{"/*+ SET_VAR(aa) */"},
    46  		},
    47  		{
    48  			comments:  Comments{"/* toto */"},
    49  			queryHint: "SET_VAR(aa)",
    50  			expected:  Comments{"/*+ SET_VAR(aa) */", "/* toto */"},
    51  		},
    52  		{
    53  			comments:  Comments{"/* toto */", "/*+ SET_VAR(bb) */"},
    54  			queryHint: "SET_VAR(aa)",
    55  			expected:  Comments{"/*+ SET_VAR(bb) SET_VAR(aa) */", "/* toto */"},
    56  		},
    57  		{
    58  			comments:  Comments{"/* toto */", "/*+ SET_VAR(bb) "},
    59  			queryHint: "SET_VAR(aa)",
    60  			err:       "Query hint comment is malformed",
    61  		},
    62  		{
    63  			comments:  Comments{"/* toto */", "/*+ SET_VAR(bb) */", "/*+ SET_VAR(cc) */"},
    64  			queryHint: "SET_VAR(aa)",
    65  			err:       "Must have only one query hint",
    66  		},
    67  		{
    68  			comments:  Comments{"/*+ SET_VAR(bb) */"},
    69  			queryHint: "SET_VAR(bb)",
    70  			expected:  Comments{"/*+ SET_VAR(bb) */"},
    71  		},
    72  	}
    73  
    74  	for i, tc := range tcs {
    75  		comments := tc.comments.Parsed()
    76  		t.Run(fmt.Sprintf("%d %s", i, String(comments)), func(t *testing.T) {
    77  			got, err := comments.AddQueryHint(tc.queryHint)
    78  			if tc.err != "" {
    79  				require.EqualError(t, err, tc.err)
    80  			} else {
    81  				require.NoError(t, err)
    82  				assert.Equal(t, tc.expected, got)
    83  			}
    84  		})
    85  	}
    86  }
    87  
    88  func TestSQLTypeToQueryType(t *testing.T) {
    89  	tcs := []struct {
    90  		input    string
    91  		unsigned bool
    92  		output   querypb.Type
    93  	}{
    94  		{
    95  			input:    "tinyint",
    96  			unsigned: true,
    97  			output:   sqltypes.Uint8,
    98  		},
    99  		{
   100  			input:    "tinyint",
   101  			unsigned: false,
   102  			output:   sqltypes.Int8,
   103  		},
   104  		{
   105  			input:  "double",
   106  			output: sqltypes.Float64,
   107  		},
   108  		{
   109  			input:  "float8",
   110  			output: sqltypes.Float64,
   111  		},
   112  		{
   113  			input:  "float",
   114  			output: sqltypes.Float32,
   115  		},
   116  		{
   117  			input:  "float4",
   118  			output: sqltypes.Float32,
   119  		},
   120  		{
   121  			input:  "decimal",
   122  			output: sqltypes.Decimal,
   123  		},
   124  	}
   125  
   126  	for _, tc := range tcs {
   127  		name := tc.input
   128  		if tc.unsigned {
   129  			name += " unsigned"
   130  		}
   131  		t.Run(name, func(t *testing.T) {
   132  			got := SQLTypeToQueryType(tc.input, tc.unsigned)
   133  			require.Equal(t, tc.output, got)
   134  		})
   135  	}
   136  }