github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/function/hash_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 function 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/expression" 24 "github.com/dolthub/go-mysql-server/sql/types" 25 ) 26 27 // NOTE: all expected values are pulled from MySQL 8.0 28 29 func TestMD5(t *testing.T) { 30 tests := []struct { 31 val sql.Expression 32 expectedOut string 33 }{ 34 {expression.NewLiteral(int64(1), types.Int64), "c4ca4238a0b923820dcc509a6f75849b"}, 35 {expression.NewLiteral("1", types.Text), "c4ca4238a0b923820dcc509a6f75849b"}, 36 {expression.NewLiteral("abcd", types.Text), "e2fc714c4727ee9395f324cd2e7f331f"}, 37 {expression.NewLiteral(float32(2.5), types.Float32), "8221435bcce913b5c2dc22eaf6cb6590"}, 38 {expression.NewLiteral("2.5", types.Text), "8221435bcce913b5c2dc22eaf6cb6590"}, 39 {NewMD5(expression.NewLiteral(int8(10), types.Int8)), "8d8e353b98d5191d5ceea1aa3eb05d43"}, 40 } 41 42 for _, test := range tests { 43 f := NewMD5(test.val) 44 t.Run(f.String(), func(t *testing.T) { 45 res, err := f.Eval(sql.NewEmptyContext(), nil) 46 require.NoError(t, err) 47 require.Equal(t, test.expectedOut, res) 48 }) 49 } 50 51 // Test nil 52 f := NewMD5(expression.NewLiteral(nil, types.Null)) 53 t.Run(f.String(), func(t *testing.T) { 54 res, err := f.Eval(sql.NewEmptyContext(), nil) 55 require.NoError(t, err) 56 require.Equal(t, nil, res) 57 }) 58 } 59 60 func TestSHA1(t *testing.T) { 61 tests := []struct { 62 val sql.Expression 63 expectedOut string 64 }{ 65 {expression.NewLiteral(int64(1), types.Int64), "356a192b7913b04c54574d18c28d46e6395428ab"}, 66 {expression.NewLiteral("1", types.Text), "356a192b7913b04c54574d18c28d46e6395428ab"}, 67 {expression.NewLiteral("abcd", types.Text), "81fe8bfe87576c3ecb22426f8e57847382917acf"}, 68 {expression.NewLiteral(float32(2.5), types.Float32), "555a5c5c92b230dccab828d90e89ec66847ab9ce"}, 69 {expression.NewLiteral("2.5", types.Text), "555a5c5c92b230dccab828d90e89ec66847ab9ce"}, 70 {NewSHA1(expression.NewLiteral(int8(10), types.Int8)), "f270819294d6d015758421bdcb1202fd353c6f06"}, 71 } 72 73 for _, test := range tests { 74 f := NewSHA1(test.val) 75 t.Run(f.String(), func(t *testing.T) { 76 res, err := f.Eval(sql.NewEmptyContext(), nil) 77 require.NoError(t, err) 78 require.Equal(t, test.expectedOut, res) 79 }) 80 } 81 82 // Test nil 83 f := NewSHA1(expression.NewLiteral(nil, types.Null)) 84 t.Run(f.String(), func(t *testing.T) { 85 res, err := f.Eval(sql.NewEmptyContext(), nil) 86 require.NoError(t, err) 87 require.Equal(t, nil, res) 88 }) 89 } 90 91 func TestSHA2(t *testing.T) { 92 tests := []struct { 93 arg sql.Expression 94 count sql.Expression 95 expectedOut string 96 }{ 97 { 98 expression.NewLiteral(int64(1), types.Int64), 99 expression.NewLiteral(int64(224), types.Int64), 100 "e25388fde8290dc286a6164fa2d97e551b53498dcbf7bc378eb1f178", 101 }, 102 { 103 expression.NewLiteral("1", types.Text), 104 expression.NewLiteral("224", types.Text), 105 "e25388fde8290dc286a6164fa2d97e551b53498dcbf7bc378eb1f178", 106 }, 107 { 108 expression.NewLiteral("abcd", types.Text), 109 expression.NewPlus( 110 expression.NewLiteral(int64(220), types.Int64), 111 expression.NewLiteral(int64(4), types.Int64), 112 ), 113 "a76654d8e3550e9a2d67a0eeb6c67b220e5885eddd3fde135806e601", 114 }, 115 { 116 expression.NewLiteral(float32(2.5), types.Float32), 117 expression.NewLiteral(int64(224), types.Int64), 118 "55b3f1e81821cba451fd6c844db119240fd96b2b34dfcca150b8dfd3", 119 }, 120 { 121 expression.NewLiteral("2.5", types.Text), 122 expression.NewLiteral("224", types.Text), 123 "55b3f1e81821cba451fd6c844db119240fd96b2b34dfcca150b8dfd3", 124 }, 125 { 126 expression.NewLiteral(int64(1), types.Int64), 127 expression.NewLiteral(int64(256), types.Int64), 128 "6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b", 129 }, 130 { 131 expression.NewLiteral("1", types.Text), 132 expression.NewLiteral("256", types.Text), 133 "6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b", 134 }, 135 { 136 expression.NewLiteral("abcd", types.Text), 137 expression.NewPlus( 138 expression.NewLiteral(int64(250), types.Int64), 139 expression.NewLiteral(int64(6), types.Int64), 140 ), 141 "88d4266fd4e6338d13b845fcf289579d209c897823b9217da3e161936f031589", 142 }, 143 { 144 expression.NewLiteral(float32(2.5), types.Float32), 145 expression.NewLiteral(int64(256), types.Int64), 146 "b8736b999909049671d0ea075a42b308a5fbe2df1854899123fe09eb0ee9de61", 147 }, 148 { 149 expression.NewLiteral("2.5", types.Text), 150 expression.NewLiteral("256", types.Text), 151 "b8736b999909049671d0ea075a42b308a5fbe2df1854899123fe09eb0ee9de61", 152 }, 153 { 154 expression.NewLiteral(int64(1), types.Int64), 155 expression.NewLiteral(int64(0), types.Int64), 156 "6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b", 157 }, 158 { 159 expression.NewLiteral("1", types.Text), 160 expression.NewLiteral("0", types.Text), 161 "6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b", 162 }, 163 { 164 expression.NewLiteral("abcd", types.Text), 165 expression.NewLiteral(int64(0), types.Int64), 166 "88d4266fd4e6338d13b845fcf289579d209c897823b9217da3e161936f031589", 167 }, 168 { 169 expression.NewLiteral(float32(2.5), types.Float32), 170 expression.NewLiteral(int64(0), types.Int64), 171 "b8736b999909049671d0ea075a42b308a5fbe2df1854899123fe09eb0ee9de61", 172 }, 173 { 174 expression.NewLiteral("2.5", types.Text), 175 expression.NewLiteral("0", types.Text), 176 "b8736b999909049671d0ea075a42b308a5fbe2df1854899123fe09eb0ee9de61", 177 }, 178 { 179 expression.NewLiteral(int64(1), types.Int64), 180 expression.NewLiteral(int64(384), types.Int64), 181 "47f05d367b0c32e438fb63e6cf4a5f35c2aa2f90dc7543f8a41a0f95ce8a40a313ab5cf36134a2068c4c969cb50db776", 182 }, 183 { 184 expression.NewLiteral("1", types.Text), 185 expression.NewLiteral("384", types.Text), 186 "47f05d367b0c32e438fb63e6cf4a5f35c2aa2f90dc7543f8a41a0f95ce8a40a313ab5cf36134a2068c4c969cb50db776", 187 }, 188 { 189 expression.NewLiteral("abcd", types.Text), 190 expression.NewPlus( 191 expression.NewLiteral(int64(380), types.Int64), 192 expression.NewLiteral(int64(4), types.Int64), 193 ), 194 "1165b3406ff0b52a3d24721f785462ca2276c9f454a116c2b2ba20171a7905ea5a026682eb659c4d5f115c363aa3c79b", 195 }, 196 { 197 expression.NewLiteral(float32(2.5), types.Float32), 198 expression.NewLiteral(int64(384), types.Int64), 199 "36b9f321bf02e6f84ee38bf6189496a9ee02d081d7197289a2b73cd39e8d8dbcd466599fd6af13f0d79e9d1051f061bc", 200 }, 201 { 202 expression.NewLiteral("2.5", types.Text), 203 expression.NewLiteral("384", types.Text), 204 "36b9f321bf02e6f84ee38bf6189496a9ee02d081d7197289a2b73cd39e8d8dbcd466599fd6af13f0d79e9d1051f061bc", 205 }, 206 { 207 expression.NewLiteral(int64(1), types.Int64), 208 expression.NewLiteral(int64(512), types.Int64), 209 "4dff4ea340f0a823f15d3f4f01ab62eae0e5da579ccb851f8db9dfe84c58b2b37b89903a740e1ee172da793a6e79d560e5f7f9bd058a12a280433ed6fa46510a", 210 }, 211 { 212 expression.NewLiteral("1", types.Text), 213 expression.NewLiteral("512", types.Text), 214 "4dff4ea340f0a823f15d3f4f01ab62eae0e5da579ccb851f8db9dfe84c58b2b37b89903a740e1ee172da793a6e79d560e5f7f9bd058a12a280433ed6fa46510a", 215 }, 216 { 217 expression.NewLiteral("abcd", types.Text), 218 expression.NewPlus( 219 expression.NewLiteral(int64(510), types.Int64), 220 expression.NewLiteral(int64(2), types.Int64), 221 ), 222 "d8022f2060ad6efd297ab73dcc5355c9b214054b0d1776a136a669d26a7d3b14f73aa0d0ebff19ee333368f0164b6419a96da49e3e481753e7e96b716bdccb6f", 223 }, 224 { 225 expression.NewLiteral(float32(2.5), types.Float32), 226 expression.NewLiteral(int64(512), types.Int64), 227 "a4281cc49c2503bd0a0876db08ac6280583ebfcee6186c054792d877e7febe63251bfb82616504ed8ee36b146a7d5c6bfcdfcf9c27969a3874bab4544efed501", 228 }, 229 { 230 expression.NewLiteral("2.5", types.Text), 231 expression.NewLiteral("512", types.Text), 232 "a4281cc49c2503bd0a0876db08ac6280583ebfcee6186c054792d877e7febe63251bfb82616504ed8ee36b146a7d5c6bfcdfcf9c27969a3874bab4544efed501", 233 }, 234 } 235 236 for _, test := range tests { 237 f := NewSHA2(test.arg, test.count) 238 t.Run(f.String(), func(t *testing.T) { 239 res, err := f.Eval(sql.NewEmptyContext(), nil) 240 require.NoError(t, err) 241 require.Equal(t, test.expectedOut, res) 242 }) 243 } 244 } 245 246 func TestSHA2Null(t *testing.T) { 247 tests := []struct { 248 arg sql.Expression 249 count sql.Expression 250 }{ 251 { 252 expression.NewLiteral(nil, types.Null), 253 expression.NewLiteral(int64(224), types.Int64), 254 }, 255 { 256 expression.NewLiteral("1", types.Text), 257 expression.NewLiteral(nil, types.Null), 258 }, 259 { 260 expression.NewLiteral(float32(2.5), types.Float32), 261 expression.NewLiteral(int64(7), types.Int64), 262 }, 263 { 264 expression.NewLiteral(float32(2.5), types.Float32), 265 expression.NewLiteral("255", types.Text), 266 }, 267 } 268 269 for _, test := range tests { 270 f := NewSHA2(test.arg, test.count) 271 t.Run(f.String(), func(t *testing.T) { 272 res, err := f.Eval(sql.NewEmptyContext(), nil) 273 require.NoError(t, err) 274 require.Equal(t, nil, res) 275 }) 276 } 277 }