github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/plan/function/arithmetic.go (about) 1 // Copyright 2021 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 function 16 17 import ( 18 "math" 19 20 "github.com/matrixorigin/matrixone/pkg/container/nulls" 21 "github.com/matrixorigin/matrixone/pkg/container/types" 22 "github.com/matrixorigin/matrixone/pkg/container/vector" 23 "github.com/matrixorigin/matrixone/pkg/vectorize/moarray" 24 "github.com/matrixorigin/matrixone/pkg/vm/process" 25 ) 26 27 func plusOperatorSupportsVectorScalar(typ1, typ2 types.Type) bool { 28 29 if (typ1.Oid.IsArrayRelate() && typ2.IsNumeric()) || // Vec + Scalar 30 (typ1.IsNumeric() && typ2.Oid.IsArrayRelate()) { // Scalar + Vec 31 return true 32 } 33 return false 34 } 35 36 func plusOperatorSupports(typ1, typ2 types.Type) bool { 37 if typ1.Oid != typ2.Oid { 38 return false 39 } 40 switch typ1.Oid { 41 case types.T_uint8, types.T_uint16, types.T_uint32, types.T_uint64, types.T_bit: 42 case types.T_int8, types.T_int16, types.T_int32, types.T_int64: 43 case types.T_float32, types.T_float64: 44 case types.T_decimal64, types.T_decimal128: 45 case types.T_array_float32, types.T_array_float64: 46 default: 47 return false 48 } 49 return true 50 } 51 52 func minusOperatorSupportsVectorScalar(typ1, typ2 types.Type) bool { 53 if typ1.Oid.IsArrayRelate() && typ2.IsNumeric() { // Vec - Scalar 54 return true 55 } 56 return false 57 } 58 59 func minusOperatorSupports(typ1, typ2 types.Type) bool { 60 if typ1.Oid != typ2.Oid { 61 return false 62 } 63 switch typ1.Oid { 64 case types.T_uint8, types.T_uint16, types.T_uint32, types.T_uint64, types.T_bit: 65 case types.T_int8, types.T_int16, types.T_int32, types.T_int64: 66 case types.T_float32, types.T_float64: 67 case types.T_decimal64, types.T_decimal128: 68 case types.T_date, types.T_datetime: 69 case types.T_array_float32, types.T_array_float64: 70 default: 71 return false 72 } 73 return true 74 } 75 func multiOperatorSupportsVectorScalar(typ1, typ2 types.Type) bool { 76 if (typ1.Oid.IsArrayRelate() && typ2.IsNumeric()) || // Vec * Scalar 77 (typ1.IsNumeric() && typ2.Oid.IsArrayRelate()) { // Scalar * Vec 78 return true 79 } 80 return false 81 } 82 func multiOperatorSupports(typ1, typ2 types.Type) bool { 83 if typ1.Oid != typ2.Oid { 84 return false 85 } 86 switch typ1.Oid { 87 case types.T_uint8, types.T_uint16, types.T_uint32, types.T_uint64, types.T_bit: 88 case types.T_int8, types.T_int16, types.T_int32, types.T_int64: 89 case types.T_float32, types.T_float64: 90 case types.T_decimal64, types.T_decimal128: 91 case types.T_array_float32, types.T_array_float64: 92 default: 93 return false 94 } 95 return true 96 } 97 98 func divOperatorSupportsVectorScalar(typ1, typ2 types.Type) bool { 99 if typ1.Oid.IsArrayRelate() && typ2.IsNumeric() { // Vec / Scalar 100 return true 101 } 102 return false 103 } 104 105 func divOperatorSupports(typ1, typ2 types.Type) bool { 106 if typ1.Oid != typ2.Oid { 107 return false 108 } 109 switch typ1.Oid { 110 case types.T_float32, types.T_float64: 111 case types.T_decimal64, types.T_decimal128: 112 case types.T_array_float32, types.T_array_float64: 113 default: 114 return false 115 } 116 return true 117 } 118 119 func integerDivOperatorSupports(typ1, typ2 types.Type) bool { 120 if typ1.Oid != typ2.Oid { 121 return false 122 } 123 switch typ1.Oid { 124 case types.T_float32, types.T_float64: 125 default: 126 return false 127 } 128 return true 129 } 130 131 func modOperatorSupports(typ1, typ2 types.Type) bool { 132 if typ1.Oid != typ2.Oid { 133 return false 134 } 135 switch typ1.Oid { 136 case types.T_uint8, types.T_uint16, types.T_uint32, types.T_uint64, types.T_bit: 137 case types.T_int8, types.T_int16, types.T_int32, types.T_int64: 138 case types.T_float32, types.T_float64: 139 case types.T_decimal128, types.T_decimal64: 140 default: 141 return false 142 } 143 return true 144 } 145 146 func vectorScalarOp[T types.RealNumbers](ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int, op string) (err error) { 147 rs := vector.MustFunctionResult[types.Varlena](result) 148 vs := vector.GenerateFunctionStrParameter(ivecs[0]) 149 num := vector.GenerateFunctionFixedTypeParameter[T](ivecs[1]) 150 151 for i := uint64(0); i < uint64(length); i++ { 152 vec, null1 := vs.GetStrValue(i) 153 sca, null2 := num.GetValue(i) 154 155 if null1 || null2 { 156 if err = rs.AppendBytes(nil, true); err != nil { 157 return err 158 } 159 } else { 160 out, err := moarray.ScalarOp[T](types.BytesToArray[T](vec), op, float64(sca)) 161 if err != nil { 162 return err 163 } 164 165 if err = rs.AppendBytes(types.ArrayToBytes[T](out), false); err != nil { 166 return err 167 } 168 } 169 } 170 return nil 171 } 172 173 func plusFnVectorScalar(parameters []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 174 vectorIdx, scalarIdx := 0, 1 175 if parameters[1].GetType().Oid.IsArrayRelate() { 176 vectorIdx, scalarIdx = 1, 0 177 } 178 179 vectorAndScalarParams := []*vector.Vector{parameters[vectorIdx], parameters[scalarIdx]} 180 if parameters[vectorIdx].GetType().Oid == types.T_array_float32 { 181 return vectorScalarOp[float32](vectorAndScalarParams, result, proc, length, "+") 182 } else { 183 return vectorScalarOp[float64](vectorAndScalarParams, result, proc, length, "+") 184 } 185 } 186 187 func plusFn(parameters []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 188 paramType := parameters[0].GetType() 189 switch paramType.Oid { 190 case types.T_bit: 191 return opBinaryFixedFixedToFixed[uint64, uint64, uint64](parameters, result, proc, length, func(v1, v2 uint64) uint64 { 192 return v1 + v2 193 }) 194 case types.T_uint8: 195 return opBinaryFixedFixedToFixed[uint8, uint8, uint8](parameters, result, proc, length, func(v1, v2 uint8) uint8 { 196 return v1 + v2 197 }) 198 case types.T_uint16: 199 return opBinaryFixedFixedToFixed[uint16, uint16, uint16](parameters, result, proc, length, func(v1, v2 uint16) uint16 { 200 return v1 + v2 201 }) 202 case types.T_uint32: 203 return opBinaryFixedFixedToFixed[uint32, uint32, uint32](parameters, result, proc, length, func(v1, v2 uint32) uint32 { 204 return v1 + v2 205 }) 206 case types.T_uint64: 207 return opBinaryFixedFixedToFixed[uint64, uint64, uint64](parameters, result, proc, length, func(v1, v2 uint64) uint64 { 208 return v1 + v2 209 }) 210 case types.T_int8: 211 return opBinaryFixedFixedToFixed[int8, int8, int8](parameters, result, proc, length, func(v1, v2 int8) int8 { 212 return v1 + v2 213 }) 214 case types.T_int16: 215 return opBinaryFixedFixedToFixed[int16, int16, int16](parameters, result, proc, length, func(v1, v2 int16) int16 { 216 return v1 + v2 217 }) 218 case types.T_int32: 219 return opBinaryFixedFixedToFixed[int32, int32, int32](parameters, result, proc, length, func(v1, v2 int32) int32 { 220 return v1 + v2 221 }) 222 case types.T_int64: 223 return opBinaryFixedFixedToFixed[int64, int64, int64](parameters, result, proc, length, func(v1, v2 int64) int64 { 224 return v1 + v2 225 }) 226 case types.T_float32: 227 return opBinaryFixedFixedToFixed[float32, float32, float32](parameters, result, proc, length, func(v1, v2 float32) float32 { 228 return v1 + v2 229 }) 230 case types.T_float64: 231 return opBinaryFixedFixedToFixed[float64, float64, float64](parameters, result, proc, length, func(v1, v2 float64) float64 { 232 return v1 + v2 233 }) 234 case types.T_decimal64: 235 return decimalArith[types.Decimal64](parameters, result, proc, length, func(v1, v2 types.Decimal64, scale1, scale2 int32) (types.Decimal64, error) { 236 r, _, err := v1.Add(v2, scale1, scale2) 237 return r, err 238 }) 239 case types.T_decimal128: 240 return decimal128ArithArray(parameters, result, proc, length, decimal128AddArray) 241 242 case types.T_array_float32: 243 return opBinaryBytesBytesToBytesWithErrorCheck(parameters, result, proc, length, plusFnArray[float32]) 244 case types.T_array_float64: 245 return opBinaryBytesBytesToBytesWithErrorCheck(parameters, result, proc, length, plusFnArray[float64]) 246 } 247 panic("unreached code") 248 } 249 250 func minusFnVectorScalar(parameters []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 251 vectorIdx, scalarIdx := 0, 1 252 vectorAndScalarParams := []*vector.Vector{parameters[vectorIdx], parameters[scalarIdx]} 253 if parameters[vectorIdx].GetType().Oid == types.T_array_float32 { 254 return vectorScalarOp[float32](vectorAndScalarParams, result, proc, length, "-") 255 } else { 256 return vectorScalarOp[float64](vectorAndScalarParams, result, proc, length, "-") 257 } 258 } 259 260 func minusFn(parameters []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 261 paramType := parameters[0].GetType() 262 switch paramType.Oid { 263 case types.T_bit: 264 return opBinaryFixedFixedToFixed[uint64, uint64, uint64](parameters, result, proc, length, func(v1, v2 uint64) uint64 { 265 return v1 - v2 266 }) 267 case types.T_uint8: 268 return opBinaryFixedFixedToFixed[uint8, uint8, uint8](parameters, result, proc, length, func(v1, v2 uint8) uint8 { 269 return v1 - v2 270 }) 271 case types.T_uint16: 272 return opBinaryFixedFixedToFixed[uint16, uint16, uint16](parameters, result, proc, length, func(v1, v2 uint16) uint16 { 273 return v1 - v2 274 }) 275 case types.T_uint32: 276 return opBinaryFixedFixedToFixed[uint32, uint32, uint32](parameters, result, proc, length, func(v1, v2 uint32) uint32 { 277 return v1 - v2 278 }) 279 case types.T_uint64: 280 return opBinaryFixedFixedToFixed[uint64, uint64, uint64](parameters, result, proc, length, func(v1, v2 uint64) uint64 { 281 return v1 - v2 282 }) 283 case types.T_int8: 284 return opBinaryFixedFixedToFixed[int8, int8, int8](parameters, result, proc, length, func(v1, v2 int8) int8 { 285 return v1 - v2 286 }) 287 case types.T_int16: 288 return opBinaryFixedFixedToFixed[int16, int16, int16](parameters, result, proc, length, func(v1, v2 int16) int16 { 289 return v1 - v2 290 }) 291 case types.T_int32: 292 return opBinaryFixedFixedToFixed[int32, int32, int32](parameters, result, proc, length, func(v1, v2 int32) int32 { 293 return v1 - v2 294 }) 295 case types.T_int64: 296 return opBinaryFixedFixedToFixed[int64, int64, int64](parameters, result, proc, length, func(v1, v2 int64) int64 { 297 return v1 - v2 298 }) 299 case types.T_float32: 300 return opBinaryFixedFixedToFixed[float32, float32, float32](parameters, result, proc, length, func(v1, v2 float32) float32 { 301 return v1 - v2 302 }) 303 case types.T_float64: 304 return opBinaryFixedFixedToFixed[float64, float64, float64](parameters, result, proc, length, func(v1, v2 float64) float64 { 305 return v1 - v2 306 }) 307 case types.T_decimal64: 308 return decimalArith[types.Decimal64](parameters, result, proc, length, func(v1, v2 types.Decimal64, scale1, scale2 int32) (types.Decimal64, error) { 309 r, _, err := v1.Sub(v2, scale1, scale2) 310 return r, err 311 }) 312 case types.T_decimal128: 313 return decimal128ArithArray(parameters, result, proc, length, decimal128SubArray) 314 315 case types.T_date: 316 return opBinaryFixedFixedToFixed[types.Date, types.Date, int64](parameters, result, proc, length, func(v1, v2 types.Date) int64 { 317 return int64(v1 - v2) 318 }) 319 case types.T_datetime: 320 return opBinaryFixedFixedToFixed[types.Datetime, types.Datetime, int64](parameters, result, proc, length, func(v1, v2 types.Datetime) int64 { 321 return v1.DatetimeMinusWithSecond(v2) 322 }) 323 case types.T_array_float32: 324 return opBinaryBytesBytesToBytesWithErrorCheck(parameters, result, proc, length, minusFnArray[float32]) 325 case types.T_array_float64: 326 return opBinaryBytesBytesToBytesWithErrorCheck(parameters, result, proc, length, minusFnArray[float64]) 327 } 328 panic("unreached code") 329 } 330 331 func multiFnVectorScalar(parameters []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 332 vectorIdx, scalarIdx := 0, 1 333 if parameters[1].GetType().Oid.IsArrayRelate() { 334 vectorIdx, scalarIdx = 1, 0 335 } 336 337 vectorAndScalarParams := []*vector.Vector{parameters[vectorIdx], parameters[scalarIdx]} 338 if parameters[vectorIdx].GetType().Oid == types.T_array_float32 { 339 return vectorScalarOp[float32](vectorAndScalarParams, result, proc, length, "*") 340 } else { 341 return vectorScalarOp[float64](vectorAndScalarParams, result, proc, length, "*") 342 } 343 } 344 345 func multiFn(parameters []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 346 paramType := parameters[0].GetType() 347 switch paramType.Oid { 348 case types.T_bit: 349 return opBinaryFixedFixedToFixed[uint64, uint64, uint64](parameters, result, proc, length, func(v1, v2 uint64) uint64 { 350 return v1 * v2 351 }) 352 case types.T_uint8: 353 return opBinaryFixedFixedToFixed[uint8, uint8, uint8](parameters, result, proc, length, func(v1, v2 uint8) uint8 { 354 return v1 * v2 355 }) 356 case types.T_uint16: 357 return opBinaryFixedFixedToFixed[uint16, uint16, uint16](parameters, result, proc, length, func(v1, v2 uint16) uint16 { 358 return v1 * v2 359 }) 360 case types.T_uint32: 361 return opBinaryFixedFixedToFixed[uint32, uint32, uint32](parameters, result, proc, length, func(v1, v2 uint32) uint32 { 362 return v1 * v2 363 }) 364 case types.T_uint64: 365 return opBinaryFixedFixedToFixed[uint64, uint64, uint64](parameters, result, proc, length, func(v1, v2 uint64) uint64 { 366 return v1 * v2 367 }) 368 case types.T_int8: 369 return opBinaryFixedFixedToFixed[int8, int8, int8](parameters, result, proc, length, func(v1, v2 int8) int8 { 370 return v1 * v2 371 }) 372 case types.T_int16: 373 return opBinaryFixedFixedToFixed[int16, int16, int16](parameters, result, proc, length, func(v1, v2 int16) int16 { 374 return v1 * v2 375 }) 376 case types.T_int32: 377 return opBinaryFixedFixedToFixed[int32, int32, int32](parameters, result, proc, length, func(v1, v2 int32) int32 { 378 return v1 * v2 379 }) 380 case types.T_int64: 381 return opBinaryFixedFixedToFixed[int64, int64, int64](parameters, result, proc, length, func(v1, v2 int64) int64 { 382 return v1 * v2 383 }) 384 case types.T_float32: 385 return opBinaryFixedFixedToFixed[float32, float32, float32](parameters, result, proc, length, func(v1, v2 float32) float32 { 386 return v1 * v2 387 }) 388 case types.T_float64: 389 return opBinaryFixedFixedToFixed[float64, float64, float64](parameters, result, proc, length, func(v1, v2 float64) float64 { 390 return v1 * v2 391 }) 392 case types.T_decimal64: 393 return decimalArith2(parameters, result, proc, length, func(x, y types.Decimal128, scale1, scale2 int32) (types.Decimal128, error) { 394 rt, _, err := x.Mul(y, scale1, scale2) 395 return rt, err 396 }) 397 case types.T_decimal128: 398 return decimal128ArithArray(parameters, result, proc, length, decimal128MultiArray) 399 400 case types.T_array_float32: 401 return opBinaryBytesBytesToBytesWithErrorCheck(parameters, result, proc, length, multiFnArray[float32]) 402 case types.T_array_float64: 403 return opBinaryBytesBytesToBytesWithErrorCheck(parameters, result, proc, length, multiFnArray[float64]) 404 } 405 panic("unreached code") 406 } 407 408 func divFnVectorScalar(parameters []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 409 vectorIdx, scalarIdx := 0, 1 410 vectorAndScalarParams := []*vector.Vector{parameters[vectorIdx], parameters[scalarIdx]} 411 if parameters[vectorIdx].GetType().Oid == types.T_array_float32 { 412 return vectorScalarOp[float32](vectorAndScalarParams, result, proc, length, "/") 413 } else { 414 return vectorScalarOp[float64](vectorAndScalarParams, result, proc, length, "/") 415 } 416 } 417 418 func divFn(parameters []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 419 paramType := parameters[0].GetType() 420 switch paramType.Oid { 421 case types.T_float32: 422 return specialTemplateForDivFunction[float32, float32](parameters, result, proc, length, func(v1, v2 float32) float32 { 423 return v1 / v2 424 }) 425 case types.T_float64: 426 return specialTemplateForDivFunction[float64, float64](parameters, result, proc, length, func(v1, v2 float64) float64 { 427 return v1 / v2 428 }) 429 case types.T_decimal64: 430 return decimalArith2(parameters, result, proc, length, func(x, y types.Decimal128, scale1, scale2 int32) (types.Decimal128, error) { 431 rt, _, err := x.Div(y, scale1, scale2) 432 return rt, err 433 }) 434 case types.T_decimal128: 435 return decimalArith[types.Decimal128](parameters, result, proc, length, func(v1, v2 types.Decimal128, scale1, scale2 int32) (types.Decimal128, error) { 436 r, _, err := v1.Div(v2, scale1, scale2) 437 return r, err 438 }) 439 case types.T_array_float32: 440 return opBinaryBytesBytesToBytesWithErrorCheck(parameters, result, proc, length, divFnArray[float32]) 441 case types.T_array_float64: 442 return opBinaryBytesBytesToBytesWithErrorCheck(parameters, result, proc, length, divFnArray[float64]) 443 } 444 panic("unreached code") 445 } 446 447 func integerDivFn(parameters []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 448 paramType := parameters[0].GetType() 449 if paramType.Oid == types.T_float32 { 450 return specialTemplateForDivFunction[float32, int64](parameters, result, proc, length, func(v1, v2 float32) int64 { 451 return int64(v1 / v2) 452 }) 453 } 454 if paramType.Oid == types.T_float64 { 455 return specialTemplateForDivFunction[float64, int64](parameters, result, proc, length, func(v1, v2 float64) int64 { 456 return int64(v1 / v2) 457 }) 458 } 459 panic("unreached code") 460 } 461 462 func modFn(parameters []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 463 paramType := parameters[0].GetType() 464 switch paramType.Oid { 465 case types.T_bit: 466 return specialTemplateForModFunction[uint64](parameters, result, proc, length, func(v1, v2 uint64) uint64 { 467 return v1 % v2 468 }) 469 case types.T_uint8: 470 return specialTemplateForModFunction[uint8](parameters, result, proc, length, func(v1, v2 uint8) uint8 { 471 return v1 % v2 472 }) 473 case types.T_uint16: 474 return specialTemplateForModFunction[uint16](parameters, result, proc, length, func(v1, v2 uint16) uint16 { 475 return v1 % v2 476 }) 477 case types.T_uint32: 478 return specialTemplateForModFunction[uint32](parameters, result, proc, length, func(v1, v2 uint32) uint32 { 479 return v1 % v2 480 }) 481 case types.T_uint64: 482 return specialTemplateForModFunction[uint64](parameters, result, proc, length, func(v1, v2 uint64) uint64 { 483 return v1 % v2 484 }) 485 case types.T_int8: 486 return specialTemplateForModFunction[int8](parameters, result, proc, length, func(v1, v2 int8) int8 { 487 return v1 % v2 488 }) 489 case types.T_int16: 490 return specialTemplateForModFunction[int16](parameters, result, proc, length, func(v1, v2 int16) int16 { 491 return v1 % v2 492 }) 493 case types.T_int32: 494 return specialTemplateForModFunction[int32](parameters, result, proc, length, func(v1, v2 int32) int32 { 495 return v1 % v2 496 }) 497 case types.T_int64: 498 return specialTemplateForModFunction[int64](parameters, result, proc, length, func(v1, v2 int64) int64 { 499 return v1 % v2 500 }) 501 case types.T_float32: 502 return specialTemplateForModFunction[float32](parameters, result, proc, length, func(v1, v2 float32) float32 { 503 return float32(math.Mod(float64(v1), float64(v2))) 504 }) 505 case types.T_float64: 506 return specialTemplateForModFunction[float64](parameters, result, proc, length, func(v1, v2 float64) float64 { 507 return math.Mod(v1, v2) 508 }) 509 case types.T_decimal64: 510 return decimalArith[types.Decimal64](parameters, result, proc, length, func(v1, v2 types.Decimal64, scale1, scale2 int32) (types.Decimal64, error) { 511 r, _, err := v1.Mod(v2, scale1, scale2) 512 return r, err 513 }) 514 case types.T_decimal128: 515 return decimalArith[types.Decimal128](parameters, result, proc, length, func(v1, v2 types.Decimal128, scale1, scale2 int32) (types.Decimal128, error) { 516 r, _, err := v1.Mod(v2, scale1, scale2) 517 return r, err 518 }) 519 } 520 panic("unreached code") 521 } 522 523 func plusFnArray[T types.RealNumbers](v1, v2 []byte) ([]byte, error) { 524 525 _v1 := types.BytesToArray[T](v1) 526 _v2 := types.BytesToArray[T](v2) 527 528 r, err := moarray.Add(_v1, _v2) 529 if err != nil { 530 return nil, err 531 } 532 533 return types.ArrayToBytes[T](r), nil 534 } 535 536 func minusFnArray[T types.RealNumbers](v1, v2 []byte) ([]byte, error) { 537 538 _v1 := types.BytesToArray[T](v1) 539 _v2 := types.BytesToArray[T](v2) 540 541 r, err := moarray.Subtract(_v1, _v2) 542 if err != nil { 543 return nil, err 544 } 545 546 return types.ArrayToBytes[T](r), nil 547 } 548 549 func multiFnArray[T types.RealNumbers](v1, v2 []byte) ([]byte, error) { 550 551 _v1 := types.BytesToArray[T](v1) 552 _v2 := types.BytesToArray[T](v2) 553 554 r, err := moarray.Multiply(_v1, _v2) 555 if err != nil { 556 return nil, err 557 } 558 559 return types.ArrayToBytes[T](r), nil 560 } 561 562 func divFnArray[T types.RealNumbers](v1, v2 []byte) ([]byte, error) { 563 564 _v1 := types.BytesToArray[T](v1) 565 _v2 := types.BytesToArray[T](v2) 566 567 r, err := moarray.Divide(_v1, _v2) 568 if err != nil { 569 return nil, err 570 } 571 572 return types.ArrayToBytes[T](r), nil 573 } 574 575 func decimal128ScaleArray(v, rs []types.Decimal128, len int, n int32) error { 576 for i := 0; i < len; i++ { 577 rs[i] = v[i] 578 err := rs[i].ScaleInplace(n) 579 if err != nil { 580 return err 581 } 582 } 583 return nil 584 } 585 586 func decimal128ScaleArrayWithNulls(v, rs []types.Decimal128, len int, n int32, null1, null2 *nulls.Nulls) error { 587 for i := 0; i < len; i++ { 588 if null1.Contains(uint64(i)) || null2.Contains(uint64(i)) { 589 continue 590 } 591 rs[i] = v[i] 592 err := rs[i].ScaleInplace(n) 593 if err != nil { 594 return err 595 } 596 } 597 return nil 598 } 599 600 func decimal128AddArray(v1, v2, rs []types.Decimal128, scale1, scale2 int32, null1, null2 *nulls.Nulls) error { 601 len1 := len(v1) 602 len2 := len(v2) 603 var err error 604 if null1.IsEmpty() && null2.IsEmpty() { 605 if len1 == len2 { 606 // all vector, or all constant 607 if scale1 > scale2 { 608 err = decimal128ScaleArray(v2, rs, len2, scale1-scale2) 609 if err != nil { 610 return err 611 } 612 for i := 0; i < len1; i++ { 613 err = rs[i].AddInplace(&v1[i]) 614 if err != nil { 615 return err 616 } 617 } 618 } else if scale1 < scale2 { 619 err = decimal128ScaleArray(v1, rs, len1, scale2-scale1) 620 if err != nil { 621 return err 622 } 623 for i := 0; i < len1; i++ { 624 err = rs[i].AddInplace(&v2[i]) 625 if err != nil { 626 return err 627 } 628 } 629 } else { 630 for i := 0; i < len1; i++ { 631 rs[i] = v1[i] 632 err = rs[i].AddInplace(&v2[i]) 633 if err != nil { 634 return err 635 } 636 } 637 } 638 } else { 639 if len1 == 1 { 640 // v1 constant, v2 vector 641 if scale1 > scale2 { 642 err = decimal128ScaleArray(v2, rs, len2, scale1-scale2) 643 if err != nil { 644 return err 645 } 646 for i := 0; i < len2; i++ { 647 err = rs[i].AddInplace(&v1[0]) 648 if err != nil { 649 return err 650 } 651 } 652 } else if scale1 < scale2 { 653 err = decimal128ScaleArray(v1, rs, len1, scale2-scale1) 654 if err != nil { 655 return err 656 } 657 tmp := rs[0] 658 for i := 0; i < len2; i++ { 659 rs[i] = tmp 660 err = rs[i].AddInplace(&v2[i]) 661 if err != nil { 662 return err 663 } 664 } 665 } else { 666 tmp := v1[0] 667 for i := 0; i < len2; i++ { 668 rs[i] = tmp 669 err = rs[i].AddInplace(&v2[i]) 670 if err != nil { 671 return err 672 } 673 } 674 } 675 } else { 676 // v1 vector, v2 constant 677 if scale1 > scale2 { 678 err = decimal128ScaleArray(v2, rs, len2, scale1-scale2) 679 if err != nil { 680 return err 681 } 682 tmp := rs[0] 683 for i := 0; i < len1; i++ { 684 rs[i] = tmp 685 err = rs[i].AddInplace(&v1[i]) 686 if err != nil { 687 return err 688 } 689 } 690 } else if scale1 < scale2 { 691 err = decimal128ScaleArray(v1, rs, len1, scale2-scale1) 692 if err != nil { 693 return err 694 } 695 for i := 0; i < len1; i++ { 696 err = rs[i].AddInplace(&v2[0]) 697 if err != nil { 698 return err 699 } 700 } 701 } else { 702 tmp := v2[0] 703 for i := 0; i < len1; i++ { 704 rs[i] = tmp 705 err = rs[i].AddInplace(&v1[i]) 706 if err != nil { 707 return err 708 } 709 } 710 } 711 } 712 } 713 } else { 714 if len1 == len2 { 715 // all vector, or all constant 716 if scale1 > scale2 { 717 err = decimal128ScaleArrayWithNulls(v2, rs, len2, scale1-scale2, null1, null2) 718 if err != nil { 719 return err 720 } 721 for i := 0; i < len1; i++ { 722 if null1.Contains(uint64(i)) || null2.Contains(uint64(i)) { 723 continue 724 } 725 err = rs[i].AddInplace(&v1[i]) 726 if err != nil { 727 return err 728 } 729 } 730 } else if scale1 < scale2 { 731 err = decimal128ScaleArrayWithNulls(v1, rs, len1, scale2-scale1, null1, null2) 732 if err != nil { 733 return err 734 } 735 for i := 0; i < len1; i++ { 736 if null1.Contains(uint64(i)) || null2.Contains(uint64(i)) { 737 continue 738 } 739 err = rs[i].AddInplace(&v2[i]) 740 if err != nil { 741 return err 742 } 743 } 744 } else { 745 for i := 0; i < len1; i++ { 746 if null1.Contains(uint64(i)) || null2.Contains(uint64(i)) { 747 continue 748 } 749 rs[i] = v1[i] 750 err = rs[i].AddInplace(&v2[i]) 751 if err != nil { 752 return err 753 } 754 } 755 } 756 } else { 757 if len1 == 1 { 758 // v1 constant, v2 vector 759 if null1.Contains(0) { 760 return nil 761 } 762 if scale1 > scale2 { 763 err = decimal128ScaleArrayWithNulls(v2, rs, len2, scale1-scale2, null1, null2) 764 if err != nil { 765 return err 766 } 767 for i := 0; i < len2; i++ { 768 if null2.Contains(uint64(i)) { 769 continue 770 } 771 err = rs[i].AddInplace(&v1[0]) 772 if err != nil { 773 return err 774 } 775 } 776 } else if scale1 < scale2 { 777 err = decimal128ScaleArray(v1, rs, len1, scale2-scale1) 778 if err != nil { 779 return err 780 } 781 tmp := rs[0] 782 for i := 0; i < len2; i++ { 783 if null2.Contains(uint64(i)) { 784 continue 785 } 786 rs[i] = tmp 787 err = rs[i].AddInplace(&v2[i]) 788 if err != nil { 789 return err 790 } 791 } 792 } else { 793 tmp := v1[0] 794 for i := 0; i < len2; i++ { 795 if null2.Contains(uint64(i)) { 796 continue 797 } 798 rs[i] = tmp 799 err = rs[i].AddInplace(&v2[i]) 800 if err != nil { 801 return err 802 } 803 } 804 } 805 } else { 806 // v1 vector, v2 constant 807 if null2.Contains(0) { 808 return nil 809 } 810 if scale1 > scale2 { 811 err = decimal128ScaleArray(v2, rs, len2, scale1-scale2) 812 if err != nil { 813 return err 814 } 815 tmp := rs[0] 816 for i := 0; i < len1; i++ { 817 if null1.Contains(uint64(i)) { 818 continue 819 } 820 rs[i] = tmp 821 err = rs[i].AddInplace(&v1[i]) 822 if err != nil { 823 return err 824 } 825 } 826 } else if scale1 < scale2 { 827 err = decimal128ScaleArrayWithNulls(v1, rs, len1, scale2-scale1, null1, null2) 828 if err != nil { 829 return err 830 } 831 for i := 0; i < len1; i++ { 832 if null1.Contains(uint64(i)) { 833 continue 834 } 835 err = rs[i].AddInplace(&v2[0]) 836 if err != nil { 837 return err 838 } 839 } 840 } else { 841 tmp := v2[0] 842 for i := 0; i < len1; i++ { 843 if null1.Contains(uint64(i)) { 844 continue 845 } 846 rs[i] = tmp 847 err = rs[i].AddInplace(&v1[i]) 848 if err != nil { 849 return err 850 } 851 } 852 } 853 } 854 } 855 } 856 857 return nil 858 } 859 860 func decimal128SubArray(v1, v2, rs []types.Decimal128, scale1, scale2 int32, null1, null2 *nulls.Nulls) error { 861 len1 := len(v1) 862 len2 := len(v2) 863 var err error 864 865 if null1.IsEmpty() && null2.IsEmpty() { 866 if len1 == len2 { 867 // all vector, or all constant 868 if scale1 > scale2 { 869 err = decimal128ScaleArray(v2, rs, len2, scale1-scale2) 870 if err != nil { 871 return err 872 } 873 for i := 0; i < len1; i++ { 874 rs[i].MinusInplace() 875 err = rs[i].AddInplace(&v1[i]) 876 if err != nil { 877 return err 878 } 879 } 880 } else if scale1 < scale2 { 881 err = decimal128ScaleArray(v1, rs, len1, scale2-scale1) 882 if err != nil { 883 return err 884 } 885 for i := 0; i < len1; i++ { 886 rs[i].MinusInplace() 887 err = rs[i].AddInplace(&v2[i]) 888 if err != nil { 889 return err 890 } 891 rs[i].MinusInplace() 892 } 893 } else { 894 for i := 0; i < len1; i++ { 895 rs[i] = v2[i] 896 rs[i].MinusInplace() 897 err = rs[i].AddInplace(&v1[i]) 898 if err != nil { 899 return err 900 } 901 } 902 } 903 } else { 904 if len1 == 1 { 905 // v1 constant, v2 vector 906 if scale1 > scale2 { 907 err = decimal128ScaleArray(v2, rs, len2, scale1-scale2) 908 if err != nil { 909 return err 910 } 911 for i := 0; i < len2; i++ { 912 rs[i].MinusInplace() 913 err = rs[i].AddInplace(&v1[0]) 914 if err != nil { 915 return err 916 } 917 } 918 } else if scale1 < scale2 { 919 err = decimal128ScaleArray(v1, rs, len1, scale2-scale1) 920 if err != nil { 921 return err 922 } 923 tmp := rs[0] 924 tmp.MinusInplace() 925 for i := 0; i < len2; i++ { 926 rs[i] = tmp 927 err = rs[i].AddInplace(&v2[i]) 928 rs[i].MinusInplace() 929 if err != nil { 930 return err 931 } 932 } 933 } else { 934 tmp := v1[0] 935 tmp.MinusInplace() 936 for i := 0; i < len2; i++ { 937 rs[i] = tmp 938 err = rs[i].AddInplace(&v2[i]) 939 rs[i].MinusInplace() 940 if err != nil { 941 return err 942 } 943 } 944 } 945 } else { 946 // v1 vector, v2 constant 947 if scale1 > scale2 { 948 err = decimal128ScaleArray(v2, rs, len2, scale1-scale2) 949 if err != nil { 950 return err 951 } 952 tmp := rs[0] 953 tmp.MinusInplace() 954 for i := 0; i < len1; i++ { 955 rs[i] = tmp 956 err = rs[i].AddInplace(&v1[i]) 957 if err != nil { 958 return err 959 } 960 } 961 } else if scale1 < scale2 { 962 err = decimal128ScaleArray(v1, rs, len1, scale2-scale1) 963 if err != nil { 964 return err 965 } 966 for i := 0; i < len1; i++ { 967 rs[i].MinusInplace() 968 err = rs[i].AddInplace(&v2[0]) 969 if err != nil { 970 return err 971 } 972 rs[i].MinusInplace() 973 } 974 } else { 975 tmp := v2[0] 976 tmp.MinusInplace() 977 for i := 0; i < len1; i++ { 978 rs[i] = tmp 979 err = rs[i].AddInplace(&v1[i]) 980 if err != nil { 981 return err 982 } 983 } 984 } 985 } 986 } 987 } else { 988 if len1 == len2 { 989 // all vector, or all constant 990 if scale1 > scale2 { 991 err = decimal128ScaleArrayWithNulls(v2, rs, len2, scale1-scale2, null1, null2) 992 if err != nil { 993 return err 994 } 995 for i := 0; i < len1; i++ { 996 if null1.Contains(uint64(i)) || null2.Contains(uint64(i)) { 997 continue 998 } 999 rs[i].MinusInplace() 1000 err = rs[i].AddInplace(&v1[i]) 1001 if err != nil { 1002 return err 1003 } 1004 } 1005 } else if scale1 < scale2 { 1006 err = decimal128ScaleArrayWithNulls(v1, rs, len1, scale2-scale1, null1, null2) 1007 if err != nil { 1008 return err 1009 } 1010 for i := 0; i < len1; i++ { 1011 if null1.Contains(uint64(i)) || null2.Contains(uint64(i)) { 1012 continue 1013 } 1014 rs[i].MinusInplace() 1015 err = rs[i].AddInplace(&v2[i]) 1016 if err != nil { 1017 return err 1018 } 1019 rs[i].MinusInplace() 1020 } 1021 } else { 1022 for i := 0; i < len1; i++ { 1023 if null1.Contains(uint64(i)) || null2.Contains(uint64(i)) { 1024 continue 1025 } 1026 rs[i] = v2[i] 1027 rs[i].MinusInplace() 1028 err = rs[i].AddInplace(&v1[i]) 1029 if err != nil { 1030 return err 1031 } 1032 } 1033 } 1034 } else { 1035 if len1 == 1 { 1036 // v1 constant, v2 vector 1037 if null1.Contains(0) { 1038 return nil 1039 } 1040 if scale1 > scale2 { 1041 err = decimal128ScaleArrayWithNulls(v2, rs, len2, scale1-scale2, null1, null2) 1042 if err != nil { 1043 return err 1044 } 1045 for i := 0; i < len2; i++ { 1046 if null2.Contains(uint64(i)) { 1047 continue 1048 } 1049 rs[i].MinusInplace() 1050 err = rs[i].AddInplace(&v1[0]) 1051 if err != nil { 1052 return err 1053 } 1054 } 1055 } else if scale1 < scale2 { 1056 err = decimal128ScaleArray(v1, rs, len1, scale2-scale1) 1057 if err != nil { 1058 return err 1059 } 1060 tmp := rs[0] 1061 tmp.MinusInplace() 1062 for i := 0; i < len2; i++ { 1063 if null2.Contains(uint64(i)) { 1064 continue 1065 } 1066 rs[i] = tmp 1067 err = rs[i].AddInplace(&v2[i]) 1068 rs[i].MinusInplace() 1069 if err != nil { 1070 return err 1071 } 1072 } 1073 } else { 1074 tmp := v1[0] 1075 tmp.MinusInplace() 1076 for i := 0; i < len2; i++ { 1077 if null2.Contains(uint64(i)) { 1078 continue 1079 } 1080 rs[i] = tmp 1081 err = rs[i].AddInplace(&v2[i]) 1082 rs[i].MinusInplace() 1083 if err != nil { 1084 return err 1085 } 1086 } 1087 } 1088 } else { 1089 // v1 vector, v2 constant 1090 if null2.Contains(0) { 1091 return nil 1092 } 1093 if scale1 > scale2 { 1094 err = decimal128ScaleArray(v2, rs, len2, scale1-scale2) 1095 if err != nil { 1096 return err 1097 } 1098 tmp := rs[0] 1099 tmp.MinusInplace() 1100 for i := 0; i < len1; i++ { 1101 if null1.Contains(uint64(i)) { 1102 continue 1103 } 1104 rs[i] = tmp 1105 err = rs[i].AddInplace(&v1[i]) 1106 if err != nil { 1107 return err 1108 } 1109 } 1110 } else if scale1 < scale2 { 1111 err = decimal128ScaleArrayWithNulls(v1, rs, len1, scale2-scale1, null1, null2) 1112 if err != nil { 1113 return err 1114 } 1115 for i := 0; i < len1; i++ { 1116 if null1.Contains(uint64(i)) { 1117 continue 1118 } 1119 rs[i].MinusInplace() 1120 err = rs[i].AddInplace(&v2[0]) 1121 if err != nil { 1122 return err 1123 } 1124 rs[i].MinusInplace() 1125 } 1126 } else { 1127 tmp := v2[0] 1128 tmp.MinusInplace() 1129 for i := 0; i < len1; i++ { 1130 if null1.Contains(uint64(i)) { 1131 continue 1132 } 1133 rs[i] = tmp 1134 err = rs[i].AddInplace(&v1[i]) 1135 if err != nil { 1136 return err 1137 } 1138 } 1139 } 1140 } 1141 } 1142 } 1143 return nil 1144 } 1145 1146 func decimal128MultiArray(v1, v2, rs []types.Decimal128, scale1, scale2 int32, null1, null2 *nulls.Nulls) error { 1147 len1 := len(v1) 1148 len2 := len(v2) 1149 var err error 1150 1151 if null1.IsEmpty() && null2.IsEmpty() { 1152 var scale int32 = 12 1153 if scale1 > scale { 1154 scale = scale1 1155 } 1156 if scale2 > scale { 1157 scale = scale2 1158 } 1159 if scale1+scale2 < scale { 1160 scale = scale1 + scale2 1161 } 1162 scale = scale - scale1 - scale2 1163 1164 if len1 == len2 { 1165 for i := 0; i < len1; i++ { 1166 rs[i] = v1[i] 1167 err = rs[i].MulInplace(&v2[i], scale, scale1, scale2) 1168 if err != nil { 1169 return err 1170 } 1171 } 1172 } else { 1173 if len1 == 1 { 1174 for i := 0; i < len2; i++ { 1175 rs[i] = v1[0] 1176 err = rs[i].MulInplace(&v2[i], scale, scale1, scale2) 1177 if err != nil { 1178 return err 1179 } 1180 } 1181 } else { 1182 for i := 0; i < len1; i++ { 1183 rs[i] = v1[i] 1184 err = rs[i].MulInplace(&v2[0], scale, scale1, scale2) 1185 if err != nil { 1186 return err 1187 } 1188 } 1189 } 1190 } 1191 } else { 1192 var scale int32 = 12 1193 if scale1 > scale { 1194 scale = scale1 1195 } 1196 if scale2 > scale { 1197 scale = scale2 1198 } 1199 if scale1+scale2 < scale { 1200 scale = scale1 + scale2 1201 } 1202 scale = scale - scale1 - scale2 1203 1204 if len1 == len2 { 1205 for i := 0; i < len1; i++ { 1206 if null1.Contains(uint64(i)) || null2.Contains(uint64(i)) { 1207 continue 1208 } 1209 rs[i] = v1[i] 1210 err = rs[i].MulInplace(&v2[i], scale, scale1, scale2) 1211 if err != nil { 1212 return err 1213 } 1214 } 1215 } else { 1216 if len1 == 1 { 1217 if null1.Contains(0) { 1218 return nil 1219 } 1220 for i := 0; i < len2; i++ { 1221 if null2.Contains(uint64(i)) { 1222 continue 1223 } 1224 rs[i] = v1[0] 1225 err = rs[i].MulInplace(&v2[i], scale, scale1, scale2) 1226 if err != nil { 1227 return err 1228 } 1229 } 1230 } else { 1231 if null2.Contains(0) { 1232 return nil 1233 } 1234 for i := 0; i < len1; i++ { 1235 if null1.Contains(uint64(i)) { 1236 continue 1237 } 1238 rs[i] = v1[i] 1239 err = rs[i].MulInplace(&v2[0], scale, scale1, scale2) 1240 if err != nil { 1241 return err 1242 } 1243 } 1244 } 1245 } 1246 } 1247 return nil 1248 }