github.com/dolthub/go-mysql-server@v0.18.0/sql/planbuilder/parsecolumndefault_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 planbuilder
    16  
    17  import (
    18  	"reflect"
    19  
    20  	"github.com/dolthub/go-mysql-server/sql"
    21  )
    22  
    23  // TODO use planbuilder
    24  //func TestStringToColumnDefaultValue(t *testing.T) {
    25  //	tests := []struct {
    26  //		exprStr      string
    27  //		expectedExpr sql.Expression
    28  //	}{
    29  //		{
    30  //			"2",
    31  //			NewColumnDefaultValue(
    32  //				expression.NewLiteral(int8(2), types.Int8),
    33  //				nil,
    34  //				true,
    35  //				false,
    36  //				true,
    37  //			),
    38  //		},
    39  //		{
    40  //			"(2)",
    41  //			NewColumnDefaultValue(
    42  //				expression.NewLiteral(int8(2), types.Int8),
    43  //				nil,
    44  //				false,
    45  //				true,
    46  //				true,
    47  //			),
    48  //		},
    49  //		{
    50  //			"(RAND() + 5)",
    51  //			NewColumnDefaultValue(
    52  //				expression.NewArithmetic(
    53  //					expression.NewUnresolvedFunction("rand", false, nil),
    54  //					expression.NewLiteral(int8(5), types.Int8),
    55  //					"+",
    56  //				),
    57  //				nil,
    58  //				false,
    59  //				true,
    60  //				true,
    61  //			),
    62  //		},
    63  //		{
    64  //			"(GREATEST(RAND(), RAND()))",
    65  //			NewColumnDefaultValue(
    66  //				expression.NewUnresolvedFunction("greatest", false, nil,
    67  //					expression.NewUnresolvedFunction("rand", false, nil),
    68  //					expression.NewUnresolvedFunction("rand", false, nil),
    69  //				),
    70  //				nil,
    71  //				false,
    72  //				true,
    73  //				true,
    74  //			),
    75  //		},
    76  //	}
    77  //
    78  //	for _, test := range tests {
    79  //		t.Run(test.exprStr, func(t *testing.T) {
    80  //			res, err := StringToColumnDefaultValue(sql.NewEmptyContext(), test.exprStr)
    81  //			if test.expectedExpr == nil {
    82  //				assert.Error(t, err)
    83  //			} else {
    84  //				require.NoError(t, err)
    85  //				assert.Equal(t, test.expectedExpr, res)
    86  //			}
    87  //		})
    88  //	}
    89  //}
    90  
    91  // must executes functions of the form "func(args...) (sql.Expression, error)" and panics on errors
    92  func must(f interface{}, args ...interface{}) sql.Expression {
    93  	fType := reflect.TypeOf(f)
    94  	if fType.Kind() != reflect.Func ||
    95  		fType.NumOut() != 2 ||
    96  		!fType.Out(0).AssignableTo(reflect.TypeOf((*sql.Expression)(nil)).Elem()) ||
    97  		!fType.Out(1).AssignableTo(reflect.TypeOf((*error)(nil)).Elem()) {
    98  		panic("invalid function given")
    99  	}
   100  	// we let reflection ensure that the arguments match
   101  	argVals := make([]reflect.Value, len(args))
   102  	for i, arg := range args {
   103  		argVals[i] = reflect.ValueOf(arg)
   104  	}
   105  	fVal := reflect.ValueOf(f)
   106  	out := fVal.Call(argVals)
   107  	err, _ := out[1].Interface().(error)
   108  	if err != nil {
   109  		panic("must err is nil")
   110  	}
   111  	return out[0].Interface().(sql.Expression)
   112  }
   113  
   114  func NewColumnDefaultValue(expr sql.Expression, outType sql.Type, isLiteral, isParenthesized, mayReturnNil bool) *sql.ColumnDefaultValue {
   115  	cdv, err := sql.NewColumnDefaultValue(expr, outType, isLiteral, isParenthesized, mayReturnNil)
   116  	if err != nil {
   117  		panic(err)
   118  	}
   119  	return cdv
   120  }