|  | 
|  | 1 | +// Copyright 2024 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 function | 
|  | 16 | + | 
|  | 17 | +import ( | 
|  | 18 | +	"testing" | 
|  | 19 | + | 
|  | 20 | +	"github.com/dolthub/vitess/go/sqltypes" | 
|  | 21 | +	"github.com/stretchr/testify/require" | 
|  | 22 | + | 
|  | 23 | +	"github.com/dolthub/go-mysql-server/sql" | 
|  | 24 | +	"github.com/dolthub/go-mysql-server/sql/expression" | 
|  | 25 | +	"github.com/dolthub/go-mysql-server/sql/types" | 
|  | 26 | +) | 
|  | 27 | + | 
|  | 28 | +func TestGtidSubtract(t *testing.T) { | 
|  | 29 | +	tests := []struct { | 
|  | 30 | +		left, right sql.Expression | 
|  | 31 | +		expected    any | 
|  | 32 | +		error       string | 
|  | 33 | +	}{ | 
|  | 34 | +		// NULL cases | 
|  | 35 | +		{ | 
|  | 36 | +			left:     nil, | 
|  | 37 | +			right:    nil, | 
|  | 38 | +			expected: nil, | 
|  | 39 | +		}, | 
|  | 40 | +		{ | 
|  | 41 | +			left:     newStringLiteral("3E11FA47-71CA-11E1-9E33-C80AA9429562:21-57"), | 
|  | 42 | +			right:    nil, | 
|  | 43 | +			expected: nil, | 
|  | 44 | +		}, | 
|  | 45 | +		{ | 
|  | 46 | +			left:     nil, | 
|  | 47 | +			right:    newStringLiteral("3E11FA47-71CA-11E1-9E33-C80AA9429562:21-57"), | 
|  | 48 | +			expected: nil, | 
|  | 49 | +		}, | 
|  | 50 | +		{ | 
|  | 51 | +			left:     expression.NewLiteral(nil, types.Null), | 
|  | 52 | +			right:    expression.NewLiteral(nil, types.Null), | 
|  | 53 | +			expected: nil, | 
|  | 54 | +		}, | 
|  | 55 | +		{ | 
|  | 56 | +			left:     newStringLiteral("3E11FA47-71CA-11E1-9E33-C80AA9429562:21-57"), | 
|  | 57 | +			right:    expression.NewLiteral(nil, types.Null), | 
|  | 58 | +			expected: nil, | 
|  | 59 | +		}, | 
|  | 60 | +		{ | 
|  | 61 | +			left:     expression.NewLiteral(nil, types.Null), | 
|  | 62 | +			right:    newStringLiteral("3E11FA47-71CA-11E1-9E33-C80AA9429562:21-57"), | 
|  | 63 | +			expected: nil, | 
|  | 64 | +		}, | 
|  | 65 | + | 
|  | 66 | +		// Error cases | 
|  | 67 | +		{ | 
|  | 68 | +			left:  newStringLiteral("3E11FA47-71CA-11E1-9E33-C80AA9429562:21-57"), | 
|  | 69 | +			right: newStringLiteral("not a parseable SID:not a valid interval"), | 
|  | 70 | +			error: "invalid MySQL 5.6 GTID set (\"not a parseable SID:not a valid interval\"): invalid MySQL 5.6 SID \"not a parseable SID\"", | 
|  | 71 | +		}, | 
|  | 72 | +		{ | 
|  | 73 | +			left:  expression.NewLiteral(42, types.Int32), | 
|  | 74 | +			right: expression.NewLiteral(42, types.Int32), | 
|  | 75 | +			error: "invalid type: 42", | 
|  | 76 | +		}, | 
|  | 77 | +		{ | 
|  | 78 | +			left:  newStringLiteral("3E11FA47-71CA-11E1-9E33-C80AA9429562:21-57"), | 
|  | 79 | +			right: expression.NewLiteral(42, types.Int32), | 
|  | 80 | +			error: "invalid type: 42", | 
|  | 81 | +		}, | 
|  | 82 | +		{ | 
|  | 83 | +			left:  expression.NewLiteral(42, types.Int32), | 
|  | 84 | +			right: newStringLiteral("3E11FA47-71CA-11E1-9E33-C80AA9429562:21-57"), | 
|  | 85 | +			error: "invalid type: 42", | 
|  | 86 | +		}, | 
|  | 87 | + | 
|  | 88 | +		// MySQL documentation cases | 
|  | 89 | +		{ | 
|  | 90 | +			left:     newStringLiteral("3E11FA47-71CA-11E1-9E33-C80AA9429562:21-57"), | 
|  | 91 | +			right:    newStringLiteral("3E11FA47-71CA-11E1-9E33-C80AA9429562:21"), | 
|  | 92 | +			expected: "3e11fa47-71ca-11e1-9e33-c80aa9429562:22-57", | 
|  | 93 | +		}, | 
|  | 94 | +		{ | 
|  | 95 | +			left:     newStringLiteral("3E11FA47-71CA-11E1-9E33-C80AA9429562:21-57"), | 
|  | 96 | +			right:    newStringLiteral("3E11FA47-71CA-11E1-9E33-C80AA9429562:20-25"), | 
|  | 97 | +			expected: "3e11fa47-71ca-11e1-9e33-c80aa9429562:26-57", | 
|  | 98 | +		}, | 
|  | 99 | +		{ | 
|  | 100 | +			left:     newStringLiteral("3E11FA47-71CA-11E1-9E33-C80AA9429562:21-57"), | 
|  | 101 | +			right:    newStringLiteral("3E11FA47-71CA-11E1-9E33-C80AA9429562:23-24"), | 
|  | 102 | +			expected: "3e11fa47-71ca-11e1-9e33-c80aa9429562:21-22:25-57", | 
|  | 103 | +		}, | 
|  | 104 | +		{ | 
|  | 105 | +			left:     newStringLiteral("3E11FA47-71CA-11E1-9E33-C80AA9429562:21-57"), | 
|  | 106 | +			right:    newStringLiteral("3E11FA47-71CA-11E1-9E33-C80AA9429562:21-57"), | 
|  | 107 | +			expected: "", | 
|  | 108 | +		}, | 
|  | 109 | + | 
|  | 110 | +		// Additional cases | 
|  | 111 | +		{ | 
|  | 112 | +			left:     newStringLiteral("3E11FA47-71CA-11E1-9E33-C80AA9429562:21-57"), | 
|  | 113 | +			right:    newStringLiteral("3E11FA47-71CA-11E1-9E33-C80AA9429562:20-21"), | 
|  | 114 | +			expected: "3e11fa47-71ca-11e1-9e33-c80aa9429562:22-57", | 
|  | 115 | +		}, | 
|  | 116 | +		{ | 
|  | 117 | +			left:     newStringLiteral("3E11FA47-71CA-11E1-9E33-C80AA9429562:21-57"), | 
|  | 118 | +			right:    newStringLiteral("3E11FA47-71CA-11E1-9E33-C80AA9429562:57-58"), | 
|  | 119 | +			expected: "3e11fa47-71ca-11e1-9e33-c80aa9429562:21-56", | 
|  | 120 | +		}, | 
|  | 121 | +	} | 
|  | 122 | + | 
|  | 123 | +	for _, test := range tests { | 
|  | 124 | +		f := NewGtidSubtract(test.left, test.right) | 
|  | 125 | +		t.Run(f.String(), func(t *testing.T) { | 
|  | 126 | +			res, err := f.Eval(sql.NewEmptyContext(), nil) | 
|  | 127 | +			if test.error != "" { | 
|  | 128 | +				require.Equal(t, test.error, err.Error()) | 
|  | 129 | +			} else { | 
|  | 130 | +				require.NoError(t, err) | 
|  | 131 | +				require.Equal(t, test.expected, res) | 
|  | 132 | +			} | 
|  | 133 | +		}) | 
|  | 134 | +	} | 
|  | 135 | +} | 
|  | 136 | + | 
|  | 137 | +func newStringLiteral(s string) sql.Expression { | 
|  | 138 | +	return expression.NewLiteral(s, types.MustCreateStringWithDefaults(sqltypes.VarChar, 100)) | 
|  | 139 | +} | 
0 commit comments