github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/operator/op_bit_test.go (about) 1 // Copyright 2022 Matrix Origin 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 operator 16 17 import ( 18 "fmt" 19 "testing" 20 21 "github.com/matrixorigin/matrixone/pkg/container/types" 22 "github.com/matrixorigin/matrixone/pkg/container/vector" 23 "github.com/matrixorigin/matrixone/pkg/testutil" 24 "github.com/stretchr/testify/require" 25 ) 26 27 func TestOpXorGeneral(t *testing.T) { 28 testCases := []arg{ 29 { 30 info: "1 ^ 2", proc: testutil.NewProc(), 31 vs: []*vector.Vector{ 32 testutil.MakeScalarInt64(1, 1), 33 testutil.MakeScalarInt64(2, 1), 34 }, 35 match: true, 36 err: false, 37 expect: testutil.MakeScalarInt64(3, 1), 38 }, 39 40 { 41 info: "-1 ^ 2", proc: testutil.NewProc(), 42 vs: []*vector.Vector{ 43 testutil.MakeScalarInt64(-1, 1), 44 testutil.MakeScalarInt64(2, 1), 45 }, 46 match: true, 47 err: false, 48 expect: testutil.MakeScalarInt64(-3, 1), 49 }, 50 51 { 52 info: "null ^ 2", proc: testutil.NewProc(), 53 vs: []*vector.Vector{ 54 testutil.MakeScalarNull(types.T_int64, 1), 55 testutil.MakeScalarInt64(2, 1), 56 }, 57 match: true, 58 err: false, 59 expect: testutil.MakeScalarNull(types.T_int64, 1), 60 }, 61 62 { 63 info: "a ^ 2", proc: testutil.NewProc(), 64 vs: []*vector.Vector{ 65 testutil.MakeInt64Vector([]int64{-1, 0, 3, 0}, []uint64{1, 3}), 66 testutil.MakeScalarInt64(2, 1), 67 }, 68 match: true, 69 err: false, 70 expect: testutil.MakeInt64Vector([]int64{-3, 0, 1, 0}, []uint64{1, 3}), 71 }, 72 73 { 74 info: "a ^ b", proc: testutil.NewProc(), 75 vs: []*vector.Vector{ 76 testutil.MakeInt64Vector([]int64{-1, 0, 3, 0}, []uint64{1, 3}), 77 testutil.MakeInt64Vector([]int64{2, 3, 2, 4}, []uint64{1, 3}), 78 }, 79 match: true, 80 err: false, 81 expect: testutil.MakeInt64Vector([]int64{-3, 0, 1, 0}, []uint64{1, 3}), 82 }, 83 } 84 85 for i, tc := range testCases { 86 t.Run(tc.info, func(t *testing.T) { 87 got, ergot := OpBitXorFun[int64](tc.vs, tc.proc) 88 if tc.err { 89 require.Errorf(t, ergot, fmt.Sprintf("case '%d' expected error, but no error happens", i)) 90 } else { 91 require.NoError(t, ergot) 92 require.True(t, testutil.CompareVectors(tc.expect, got), "got vector is different with expected") 93 } 94 }) 95 } 96 } 97 98 func TestOpOrGeneral(t *testing.T) { 99 testCases := []arg{ 100 { 101 info: "1 | 2", proc: testutil.NewProc(), 102 vs: []*vector.Vector{ 103 testutil.MakeScalarInt64(1, 1), 104 testutil.MakeScalarInt64(2, 1), 105 }, 106 match: true, 107 err: false, 108 expect: testutil.MakeScalarInt64(3, 1), 109 }, 110 111 { 112 info: "-1 | 2", proc: testutil.NewProc(), 113 vs: []*vector.Vector{ 114 testutil.MakeScalarInt64(-1, 1), 115 testutil.MakeScalarInt64(2, 1), 116 }, 117 match: true, 118 err: false, 119 expect: testutil.MakeScalarInt64(-1, 1), 120 }, 121 122 { 123 info: "null | 2", proc: testutil.NewProc(), 124 vs: []*vector.Vector{ 125 testutil.MakeScalarNull(types.T_int64, 1), 126 testutil.MakeScalarInt64(2, 1), 127 }, 128 match: true, 129 err: false, 130 expect: testutil.MakeScalarNull(types.T_int64, 1), 131 }, 132 133 { 134 info: "a | 2", proc: testutil.NewProc(), 135 vs: []*vector.Vector{ 136 testutil.MakeInt64Vector([]int64{1, 0, 3, 0}, []uint64{1, 3}), 137 testutil.MakeScalarInt64(2, 1), 138 }, 139 match: true, 140 err: false, 141 expect: testutil.MakeInt64Vector([]int64{3, 0, 3, 0}, []uint64{1, 3}), 142 }, 143 144 { 145 info: "a | b", proc: testutil.NewProc(), 146 vs: []*vector.Vector{ 147 testutil.MakeInt64Vector([]int64{1, 0, 3, 0}, []uint64{1, 3}), 148 testutil.MakeInt64Vector([]int64{2, 3, 2, 4}, []uint64{1, 3}), 149 }, 150 match: true, 151 err: false, 152 expect: testutil.MakeInt64Vector([]int64{3, 0, 3, 0}, []uint64{1, 3}), 153 }, 154 } 155 156 for i, tc := range testCases { 157 t.Run(tc.info, func(t *testing.T) { 158 got, ergot := OpBitOrFun[int64](tc.vs, tc.proc) 159 if tc.err { 160 require.Errorf(t, ergot, fmt.Sprintf("case '%d' expected error, but no error happens", i)) 161 } else { 162 require.NoError(t, ergot) 163 require.True(t, testutil.CompareVectors(tc.expect, got), "got vector is different with expected") 164 } 165 }) 166 } 167 } 168 169 func TestOpAndGeneral(t *testing.T) { 170 testCases := []arg{ 171 { 172 info: "1 & 2", proc: testutil.NewProc(), 173 vs: []*vector.Vector{ 174 testutil.MakeScalarInt64(1, 1), 175 testutil.MakeScalarInt64(2, 1), 176 }, 177 match: true, 178 err: false, 179 expect: testutil.MakeScalarInt64(0, 1), 180 }, 181 182 { 183 info: "-1 & 2", proc: testutil.NewProc(), 184 vs: []*vector.Vector{ 185 testutil.MakeScalarInt64(-1, 1), 186 testutil.MakeScalarInt64(2, 1), 187 }, 188 match: true, 189 err: false, 190 expect: testutil.MakeScalarInt64(2, 1), 191 }, 192 193 { 194 info: "null & 2", proc: testutil.NewProc(), 195 vs: []*vector.Vector{ 196 testutil.MakeScalarNull(types.T_int64, 1), 197 testutil.MakeScalarInt64(2, 1), 198 }, 199 match: true, 200 err: false, 201 expect: testutil.MakeScalarNull(types.T_int64, 1), 202 }, 203 204 { 205 info: "a & 2", proc: testutil.NewProc(), 206 vs: []*vector.Vector{ 207 testutil.MakeInt64Vector([]int64{1, 0, 3, 0}, []uint64{1, 3}), 208 testutil.MakeScalarInt64(2, 1), 209 }, 210 match: true, 211 err: false, 212 expect: testutil.MakeInt64Vector([]int64{0, 0, 2, 0}, []uint64{1, 3}), 213 }, 214 215 { 216 info: "a & b", proc: testutil.NewProc(), 217 vs: []*vector.Vector{ 218 testutil.MakeInt64Vector([]int64{1, 0, 3, 0}, []uint64{1, 3}), 219 testutil.MakeInt64Vector([]int64{2, 3, 2, 4}, []uint64{1, 3}), 220 }, 221 match: true, 222 err: false, 223 expect: testutil.MakeInt64Vector([]int64{0, 0, 2, 0}, []uint64{1, 3}), 224 }, 225 } 226 227 for i, tc := range testCases { 228 t.Run(tc.info, func(t *testing.T) { 229 got, ergot := OpBitAndFun[int64](tc.vs, tc.proc) 230 if tc.err { 231 require.Errorf(t, ergot, fmt.Sprintf("case '%d' expected error, but no error happens", i)) 232 } else { 233 require.NoError(t, ergot) 234 require.True(t, testutil.CompareVectors(tc.expect, got), "got vector is different with expected") 235 } 236 }) 237 } 238 } 239 240 func TestOpRightShiftGeneral(t *testing.T) { 241 testCases := []arg{ 242 { 243 info: "1024 >> 2", proc: testutil.NewProc(), 244 vs: []*vector.Vector{ 245 testutil.MakeScalarInt64(1024, 1), 246 testutil.MakeScalarInt64(2, 1), 247 }, 248 match: true, 249 err: false, 250 expect: testutil.MakeScalarInt64(256, 1), 251 }, 252 253 { 254 info: "-5 >> 2", proc: testutil.NewProc(), 255 vs: []*vector.Vector{ 256 testutil.MakeScalarInt64(-5, 1), 257 testutil.MakeScalarInt64(2, 1), 258 }, 259 match: true, 260 err: false, 261 expect: testutil.MakeScalarInt64(-2, 1), 262 }, 263 264 { 265 info: "2 >> -2", proc: testutil.NewProc(), 266 vs: []*vector.Vector{ 267 testutil.MakeScalarInt64(2, 1), 268 testutil.MakeScalarInt64(-2, 1), 269 }, 270 match: true, 271 err: false, 272 expect: testutil.MakeScalarInt64(0, 1), 273 }, 274 275 { 276 info: "null >> 2", proc: testutil.NewProc(), 277 vs: []*vector.Vector{ 278 testutil.MakeScalarNull(types.T_any, 1), 279 testutil.MakeScalarInt64(2, 1), 280 }, 281 match: true, 282 err: false, 283 expect: testutil.MakeScalarNull(types.T_any, 1), 284 }, 285 286 { 287 info: "a >> 2", proc: testutil.NewProc(), 288 vs: []*vector.Vector{ 289 testutil.MakeInt64Vector([]int64{-5, 0, 1024, 0}, []uint64{1, 3}), 290 testutil.MakeScalarInt64(2, 1), 291 }, 292 match: true, 293 err: false, 294 expect: testutil.MakeInt64Vector([]int64{-2, 0, 256, 0}, []uint64{1, 3}), 295 }, 296 297 { 298 info: "a >> b", proc: testutil.NewProc(), 299 vs: []*vector.Vector{ 300 testutil.MakeInt64Vector([]int64{-5, 0, 1024, 0}, []uint64{1, 3}), 301 testutil.MakeInt64Vector([]int64{2, 3, 2, 4}, []uint64{1, 3}), 302 }, 303 match: true, 304 err: false, 305 expect: testutil.MakeInt64Vector([]int64{-2, 0, 256, 0}, []uint64{1, 3}), 306 }, 307 } 308 309 for i, tc := range testCases { 310 t.Run(tc.info, func(t *testing.T) { 311 got, ergot := OpBitRightShiftFun[int64](tc.vs, tc.proc) 312 if tc.err { 313 require.Errorf(t, ergot, fmt.Sprintf("case '%d' expected error, but no error happens", i)) 314 } else { 315 require.NoError(t, ergot) 316 require.True(t, testutil.CompareVectors(tc.expect, got), "got vector is different with expected") 317 } 318 }) 319 } 320 } 321 322 func TestOpLeftShiftGeneral(t *testing.T) { 323 testCases := []arg{ 324 { 325 info: "1 << 2", proc: testutil.NewProc(), 326 vs: []*vector.Vector{ 327 testutil.MakeScalarInt64(1, 1), 328 testutil.MakeScalarInt64(2, 1), 329 }, 330 match: true, 331 err: false, 332 expect: testutil.MakeScalarInt64(4, 1), 333 }, 334 335 { 336 info: "-1 << 2", proc: testutil.NewProc(), 337 vs: []*vector.Vector{ 338 testutil.MakeScalarInt64(-1, 1), 339 testutil.MakeScalarInt64(2, 1), 340 }, 341 match: true, 342 err: false, 343 expect: testutil.MakeScalarInt64(-4, 1), 344 }, 345 346 { 347 info: "2 << -2", proc: testutil.NewProc(), 348 vs: []*vector.Vector{ 349 testutil.MakeScalarInt64(2, 1), 350 testutil.MakeScalarInt64(-2, 1), 351 }, 352 match: true, 353 err: false, 354 expect: testutil.MakeScalarInt64(0, 1), 355 }, 356 357 { 358 info: "null << 2", proc: testutil.NewProc(), 359 vs: []*vector.Vector{ 360 testutil.MakeScalarNull(types.T_any, 1), 361 testutil.MakeScalarInt64(2, 1), 362 }, 363 match: true, 364 err: false, 365 expect: testutil.MakeScalarNull(types.T_any, 1), 366 }, 367 368 { 369 info: "a << 2", proc: testutil.NewProc(), 370 vs: []*vector.Vector{ 371 testutil.MakeInt64Vector([]int64{-1, 0, 1, 0}, []uint64{1, 3}), 372 testutil.MakeScalarInt64(2, 1), 373 }, 374 match: true, 375 err: false, 376 expect: testutil.MakeInt64Vector([]int64{-4, 0, 4, 0}, []uint64{1, 3}), 377 }, 378 379 { 380 info: "a << b", proc: testutil.NewProc(), 381 vs: []*vector.Vector{ 382 testutil.MakeInt64Vector([]int64{-1, 0, 1, 0}, []uint64{1, 3}), 383 testutil.MakeInt64Vector([]int64{2, 3, 2, 4}, []uint64{1, 3}), 384 }, 385 match: true, 386 err: false, 387 expect: testutil.MakeInt64Vector([]int64{-4, 0, 4, 0}, []uint64{1, 3}), 388 }, 389 } 390 391 for i, tc := range testCases { 392 t.Run(tc.info, func(t *testing.T) { 393 got, ergot := OpBitLeftShiftFun[int64](tc.vs, tc.proc) 394 if tc.err { 395 require.Errorf(t, ergot, fmt.Sprintf("case '%d' expected error, but no error happens", i)) 396 } else { 397 require.NoError(t, ergot) 398 require.True(t, testutil.CompareVectors(tc.expect, got), "got vector is different with expected") 399 } 400 }) 401 } 402 }