github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/dbs/memristed/memex/builtin_op.go (about) 1 // Copyright 2020 WHTCORPS INC, 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 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package memex 15 16 import ( 17 "fmt" 18 "math" 19 20 "github.com/whtcorpsinc/errors" 21 "github.com/whtcorpsinc/BerolinaSQL/allegrosql" 22 "github.com/whtcorpsinc/BerolinaSQL/opcode" 23 "github.com/whtcorpsinc/milevadb/stochastikctx" 24 "github.com/whtcorpsinc/milevadb/types" 25 "github.com/whtcorpsinc/milevadb/soliton/chunk" 26 "github.com/whtcorpsinc/fidelpb/go-fidelpb" 27 ) 28 29 var ( 30 _ functionClass = &logicAndFunctionClass{} 31 _ functionClass = &logicOrFunctionClass{} 32 _ functionClass = &logicXorFunctionClass{} 33 _ functionClass = &isTrueOrFalseFunctionClass{} 34 _ functionClass = &unaryMinusFunctionClass{} 35 _ functionClass = &isNullFunctionClass{} 36 _ functionClass = &unaryNotFunctionClass{} 37 ) 38 39 var ( 40 _ builtinFunc = &builtinLogicAndSig{} 41 _ builtinFunc = &builtinLogicOrSig{} 42 _ builtinFunc = &builtinLogicXorSig{} 43 _ builtinFunc = &builtinRealIsTrueSig{} 44 _ builtinFunc = &builtinDecimalIsTrueSig{} 45 _ builtinFunc = &builtinIntIsTrueSig{} 46 _ builtinFunc = &builtinRealIsFalseSig{} 47 _ builtinFunc = &builtinDecimalIsFalseSig{} 48 _ builtinFunc = &builtinIntIsFalseSig{} 49 _ builtinFunc = &builtinUnaryMinusIntSig{} 50 _ builtinFunc = &builtinDecimalIsNullSig{} 51 _ builtinFunc = &builtinDurationIsNullSig{} 52 _ builtinFunc = &builtinIntIsNullSig{} 53 _ builtinFunc = &builtinRealIsNullSig{} 54 _ builtinFunc = &builtinStringIsNullSig{} 55 _ builtinFunc = &builtinTimeIsNullSig{} 56 _ builtinFunc = &builtinUnaryNotRealSig{} 57 _ builtinFunc = &builtinUnaryNotDecimalSig{} 58 _ builtinFunc = &builtinUnaryNotIntSig{} 59 ) 60 61 type logicAndFunctionClass struct { 62 baseFunctionClass 63 } 64 65 func (c *logicAndFunctionClass) getFunction(ctx stochastikctx.Context, args []Expression) (builtinFunc, error) { 66 err := c.verifyArgs(args) 67 if err != nil { 68 return nil, err 69 } 70 args[0], err = wrapWithIsTrue(ctx, true, args[0], false) 71 if err != nil { 72 return nil, errors.Trace(err) 73 } 74 args[1], err = wrapWithIsTrue(ctx, true, args[1], false) 75 if err != nil { 76 return nil, errors.Trace(err) 77 } 78 79 bf, err := newBaseBuiltinFuncWithTp(ctx, c.funcName, args, types.ETInt, types.ETInt, types.ETInt) 80 if err != nil { 81 return nil, err 82 } 83 sig := &builtinLogicAndSig{bf} 84 sig.setPbCode(fidelpb.ScalarFuncSig_LogicalAnd) 85 sig.tp.Flen = 1 86 return sig, nil 87 } 88 89 type builtinLogicAndSig struct { 90 baseBuiltinFunc 91 } 92 93 func (b *builtinLogicAndSig) Clone() builtinFunc { 94 newSig := &builtinLogicAndSig{} 95 newSig.cloneFrom(&b.baseBuiltinFunc) 96 return newSig 97 } 98 99 func (b *builtinLogicAndSig) evalInt(event chunk.Event) (int64, bool, error) { 100 arg0, isNull0, err := b.args[0].EvalInt(b.ctx, event) 101 if err != nil || (!isNull0 && arg0 == 0) { 102 return 0, err != nil, err 103 } 104 arg1, isNull1, err := b.args[1].EvalInt(b.ctx, event) 105 if err != nil || (!isNull1 && arg1 == 0) { 106 return 0, err != nil, err 107 } 108 if isNull0 || isNull1 { 109 return 0, true, nil 110 } 111 return 1, false, nil 112 } 113 114 type logicOrFunctionClass struct { 115 baseFunctionClass 116 } 117 118 func (c *logicOrFunctionClass) getFunction(ctx stochastikctx.Context, args []Expression) (builtinFunc, error) { 119 err := c.verifyArgs(args) 120 if err != nil { 121 return nil, err 122 } 123 args[0], err = wrapWithIsTrue(ctx, true, args[0], false) 124 if err != nil { 125 return nil, errors.Trace(err) 126 } 127 args[1], err = wrapWithIsTrue(ctx, true, args[1], false) 128 if err != nil { 129 return nil, errors.Trace(err) 130 } 131 132 bf, err := newBaseBuiltinFuncWithTp(ctx, c.funcName, args, types.ETInt, types.ETInt, types.ETInt) 133 if err != nil { 134 return nil, err 135 } 136 bf.tp.Flen = 1 137 sig := &builtinLogicOrSig{bf} 138 sig.setPbCode(fidelpb.ScalarFuncSig_LogicalOr) 139 return sig, nil 140 } 141 142 type builtinLogicOrSig struct { 143 baseBuiltinFunc 144 } 145 146 func (b *builtinLogicOrSig) Clone() builtinFunc { 147 newSig := &builtinLogicOrSig{} 148 newSig.cloneFrom(&b.baseBuiltinFunc) 149 return newSig 150 } 151 152 func (b *builtinLogicOrSig) evalInt(event chunk.Event) (int64, bool, error) { 153 arg0, isNull0, err := b.args[0].EvalInt(b.ctx, event) 154 if err != nil { 155 return 0, true, err 156 } 157 if !isNull0 && arg0 != 0 { 158 return 1, false, nil 159 } 160 arg1, isNull1, err := b.args[1].EvalInt(b.ctx, event) 161 if err != nil { 162 return 0, true, err 163 } 164 if !isNull1 && arg1 != 0 { 165 return 1, false, nil 166 } 167 if isNull0 || isNull1 { 168 return 0, true, nil 169 } 170 return 0, false, nil 171 } 172 173 type logicXorFunctionClass struct { 174 baseFunctionClass 175 } 176 177 func (c *logicXorFunctionClass) getFunction(ctx stochastikctx.Context, args []Expression) (builtinFunc, error) { 178 err := c.verifyArgs(args) 179 if err != nil { 180 return nil, err 181 } 182 args[0], err = wrapWithIsTrue(ctx, true, args[0], false) 183 if err != nil { 184 return nil, errors.Trace(err) 185 } 186 args[1], err = wrapWithIsTrue(ctx, true, args[1], false) 187 if err != nil { 188 return nil, errors.Trace(err) 189 } 190 191 bf, err := newBaseBuiltinFuncWithTp(ctx, c.funcName, args, types.ETInt, types.ETInt, types.ETInt) 192 if err != nil { 193 return nil, err 194 } 195 sig := &builtinLogicXorSig{bf} 196 sig.setPbCode(fidelpb.ScalarFuncSig_LogicalXor) 197 sig.tp.Flen = 1 198 return sig, nil 199 } 200 201 type builtinLogicXorSig struct { 202 baseBuiltinFunc 203 } 204 205 func (b *builtinLogicXorSig) Clone() builtinFunc { 206 newSig := &builtinLogicXorSig{} 207 newSig.cloneFrom(&b.baseBuiltinFunc) 208 return newSig 209 } 210 211 func (b *builtinLogicXorSig) evalInt(event chunk.Event) (int64, bool, error) { 212 arg0, isNull, err := b.args[0].EvalInt(b.ctx, event) 213 if isNull || err != nil { 214 return 0, isNull, err 215 } 216 arg1, isNull, err := b.args[1].EvalInt(b.ctx, event) 217 if isNull || err != nil { 218 return 0, isNull, err 219 } 220 if (arg0 != 0 && arg1 != 0) || (arg0 == 0 && arg1 == 0) { 221 return 0, false, nil 222 } 223 return 1, false, nil 224 } 225 226 type bitAndFunctionClass struct { 227 baseFunctionClass 228 } 229 230 func (c *bitAndFunctionClass) getFunction(ctx stochastikctx.Context, args []Expression) (builtinFunc, error) { 231 err := c.verifyArgs(args) 232 if err != nil { 233 return nil, err 234 } 235 bf, err := newBaseBuiltinFuncWithTp(ctx, c.funcName, args, types.ETInt, types.ETInt, types.ETInt) 236 if err != nil { 237 return nil, err 238 } 239 sig := &builtinBitAndSig{bf} 240 sig.setPbCode(fidelpb.ScalarFuncSig_BitAndSig) 241 sig.tp.Flag |= allegrosql.UnsignedFlag 242 return sig, nil 243 } 244 245 type builtinBitAndSig struct { 246 baseBuiltinFunc 247 } 248 249 func (b *builtinBitAndSig) Clone() builtinFunc { 250 newSig := &builtinBitAndSig{} 251 newSig.cloneFrom(&b.baseBuiltinFunc) 252 return newSig 253 } 254 255 func (b *builtinBitAndSig) evalInt(event chunk.Event) (int64, bool, error) { 256 arg0, isNull, err := b.args[0].EvalInt(b.ctx, event) 257 if isNull || err != nil { 258 return 0, isNull, err 259 } 260 arg1, isNull, err := b.args[1].EvalInt(b.ctx, event) 261 if isNull || err != nil { 262 return 0, isNull, err 263 } 264 return arg0 & arg1, false, nil 265 } 266 267 type bitOrFunctionClass struct { 268 baseFunctionClass 269 } 270 271 func (c *bitOrFunctionClass) getFunction(ctx stochastikctx.Context, args []Expression) (builtinFunc, error) { 272 err := c.verifyArgs(args) 273 if err != nil { 274 return nil, err 275 } 276 bf, err := newBaseBuiltinFuncWithTp(ctx, c.funcName, args, types.ETInt, types.ETInt, types.ETInt) 277 if err != nil { 278 return nil, err 279 } 280 sig := &builtinBitOrSig{bf} 281 sig.setPbCode(fidelpb.ScalarFuncSig_BitOrSig) 282 sig.tp.Flag |= allegrosql.UnsignedFlag 283 return sig, nil 284 } 285 286 type builtinBitOrSig struct { 287 baseBuiltinFunc 288 } 289 290 func (b *builtinBitOrSig) Clone() builtinFunc { 291 newSig := &builtinBitOrSig{} 292 newSig.cloneFrom(&b.baseBuiltinFunc) 293 return newSig 294 } 295 296 func (b *builtinBitOrSig) evalInt(event chunk.Event) (int64, bool, error) { 297 arg0, isNull, err := b.args[0].EvalInt(b.ctx, event) 298 if isNull || err != nil { 299 return 0, isNull, err 300 } 301 arg1, isNull, err := b.args[1].EvalInt(b.ctx, event) 302 if isNull || err != nil { 303 return 0, isNull, err 304 } 305 return arg0 | arg1, false, nil 306 } 307 308 type bitXorFunctionClass struct { 309 baseFunctionClass 310 } 311 312 func (c *bitXorFunctionClass) getFunction(ctx stochastikctx.Context, args []Expression) (builtinFunc, error) { 313 err := c.verifyArgs(args) 314 if err != nil { 315 return nil, err 316 } 317 bf, err := newBaseBuiltinFuncWithTp(ctx, c.funcName, args, types.ETInt, types.ETInt, types.ETInt) 318 if err != nil { 319 return nil, err 320 } 321 sig := &builtinBitXorSig{bf} 322 sig.setPbCode(fidelpb.ScalarFuncSig_BitXorSig) 323 sig.tp.Flag |= allegrosql.UnsignedFlag 324 return sig, nil 325 } 326 327 type builtinBitXorSig struct { 328 baseBuiltinFunc 329 } 330 331 func (b *builtinBitXorSig) Clone() builtinFunc { 332 newSig := &builtinBitXorSig{} 333 newSig.cloneFrom(&b.baseBuiltinFunc) 334 return newSig 335 } 336 337 func (b *builtinBitXorSig) evalInt(event chunk.Event) (int64, bool, error) { 338 arg0, isNull, err := b.args[0].EvalInt(b.ctx, event) 339 if isNull || err != nil { 340 return 0, isNull, err 341 } 342 arg1, isNull, err := b.args[1].EvalInt(b.ctx, event) 343 if isNull || err != nil { 344 return 0, isNull, err 345 } 346 return arg0 ^ arg1, false, nil 347 } 348 349 type leftShiftFunctionClass struct { 350 baseFunctionClass 351 } 352 353 func (c *leftShiftFunctionClass) getFunction(ctx stochastikctx.Context, args []Expression) (builtinFunc, error) { 354 err := c.verifyArgs(args) 355 if err != nil { 356 return nil, err 357 } 358 bf, err := newBaseBuiltinFuncWithTp(ctx, c.funcName, args, types.ETInt, types.ETInt, types.ETInt) 359 if err != nil { 360 return nil, err 361 } 362 sig := &builtinLeftShiftSig{bf} 363 sig.setPbCode(fidelpb.ScalarFuncSig_LeftShift) 364 sig.tp.Flag |= allegrosql.UnsignedFlag 365 return sig, nil 366 } 367 368 type builtinLeftShiftSig struct { 369 baseBuiltinFunc 370 } 371 372 func (b *builtinLeftShiftSig) Clone() builtinFunc { 373 newSig := &builtinLeftShiftSig{} 374 newSig.cloneFrom(&b.baseBuiltinFunc) 375 return newSig 376 } 377 378 func (b *builtinLeftShiftSig) evalInt(event chunk.Event) (int64, bool, error) { 379 arg0, isNull, err := b.args[0].EvalInt(b.ctx, event) 380 if isNull || err != nil { 381 return 0, isNull, err 382 } 383 arg1, isNull, err := b.args[1].EvalInt(b.ctx, event) 384 if isNull || err != nil { 385 return 0, isNull, err 386 } 387 return int64(uint64(arg0) << uint64(arg1)), false, nil 388 } 389 390 type rightShiftFunctionClass struct { 391 baseFunctionClass 392 } 393 394 func (c *rightShiftFunctionClass) getFunction(ctx stochastikctx.Context, args []Expression) (builtinFunc, error) { 395 err := c.verifyArgs(args) 396 if err != nil { 397 return nil, err 398 } 399 bf, err := newBaseBuiltinFuncWithTp(ctx, c.funcName, args, types.ETInt, types.ETInt, types.ETInt) 400 if err != nil { 401 return nil, err 402 } 403 sig := &builtinRightShiftSig{bf} 404 sig.setPbCode(fidelpb.ScalarFuncSig_RightShift) 405 sig.tp.Flag |= allegrosql.UnsignedFlag 406 return sig, nil 407 } 408 409 type builtinRightShiftSig struct { 410 baseBuiltinFunc 411 } 412 413 func (b *builtinRightShiftSig) Clone() builtinFunc { 414 newSig := &builtinRightShiftSig{} 415 newSig.cloneFrom(&b.baseBuiltinFunc) 416 return newSig 417 } 418 419 func (b *builtinRightShiftSig) evalInt(event chunk.Event) (int64, bool, error) { 420 arg0, isNull, err := b.args[0].EvalInt(b.ctx, event) 421 if isNull || err != nil { 422 return 0, isNull, err 423 } 424 arg1, isNull, err := b.args[1].EvalInt(b.ctx, event) 425 if isNull || err != nil { 426 return 0, isNull, err 427 } 428 return int64(uint64(arg0) >> uint64(arg1)), false, nil 429 } 430 431 type isTrueOrFalseFunctionClass struct { 432 baseFunctionClass 433 op opcode.Op 434 435 // keepNull indicates how this function treats a null input parameter. 436 // If keepNull is true and the input parameter is null, the function will return null. 437 // If keepNull is false, the null input parameter will be cast to 0. 438 keepNull bool 439 } 440 441 func (c *isTrueOrFalseFunctionClass) getFunction(ctx stochastikctx.Context, args []Expression) (builtinFunc, error) { 442 if err := c.verifyArgs(args); err != nil { 443 return nil, err 444 } 445 446 argTp := args[0].GetType().EvalType() 447 if argTp == types.ETTimestamp || argTp == types.ETDatetime || argTp == types.ETDuration { 448 argTp = types.ETInt 449 } else if argTp == types.ETJson || argTp == types.ETString { 450 argTp = types.ETReal 451 } 452 453 bf, err := newBaseBuiltinFuncWithTp(ctx, c.funcName, args, types.ETInt, argTp) 454 if err != nil { 455 return nil, err 456 } 457 bf.tp.Flen = 1 458 459 var sig builtinFunc 460 switch c.op { 461 case opcode.IsTruth: 462 switch argTp { 463 case types.ETReal: 464 sig = &builtinRealIsTrueSig{bf, c.keepNull} 465 if c.keepNull { 466 sig.setPbCode(fidelpb.ScalarFuncSig_RealIsTrueWithNull) 467 } else { 468 sig.setPbCode(fidelpb.ScalarFuncSig_RealIsTrue) 469 } 470 case types.ETDecimal: 471 sig = &builtinDecimalIsTrueSig{bf, c.keepNull} 472 if c.keepNull { 473 sig.setPbCode(fidelpb.ScalarFuncSig_DecimalIsTrueWithNull) 474 } else { 475 sig.setPbCode(fidelpb.ScalarFuncSig_DecimalIsTrue) 476 } 477 case types.ETInt: 478 sig = &builtinIntIsTrueSig{bf, c.keepNull} 479 if c.keepNull { 480 sig.setPbCode(fidelpb.ScalarFuncSig_IntIsTrueWithNull) 481 } else { 482 sig.setPbCode(fidelpb.ScalarFuncSig_IntIsTrue) 483 } 484 default: 485 return nil, errors.Errorf("unexpected types.EvalType %v", argTp) 486 } 487 case opcode.IsFalsity: 488 switch argTp { 489 case types.ETReal: 490 sig = &builtinRealIsFalseSig{bf, c.keepNull} 491 if c.keepNull { 492 sig.setPbCode(fidelpb.ScalarFuncSig_RealIsFalseWithNull) 493 } else { 494 sig.setPbCode(fidelpb.ScalarFuncSig_RealIsFalse) 495 } 496 case types.ETDecimal: 497 sig = &builtinDecimalIsFalseSig{bf, c.keepNull} 498 if c.keepNull { 499 sig.setPbCode(fidelpb.ScalarFuncSig_DecimalIsFalseWithNull) 500 } else { 501 sig.setPbCode(fidelpb.ScalarFuncSig_DecimalIsFalse) 502 } 503 case types.ETInt: 504 sig = &builtinIntIsFalseSig{bf, c.keepNull} 505 if c.keepNull { 506 sig.setPbCode(fidelpb.ScalarFuncSig_IntIsFalseWithNull) 507 } else { 508 sig.setPbCode(fidelpb.ScalarFuncSig_IntIsFalse) 509 } 510 default: 511 return nil, errors.Errorf("unexpected types.EvalType %v", argTp) 512 } 513 } 514 return sig, nil 515 } 516 517 type builtinRealIsTrueSig struct { 518 baseBuiltinFunc 519 keepNull bool 520 } 521 522 func (b *builtinRealIsTrueSig) Clone() builtinFunc { 523 newSig := &builtinRealIsTrueSig{keepNull: b.keepNull} 524 newSig.cloneFrom(&b.baseBuiltinFunc) 525 return newSig 526 } 527 528 func (b *builtinRealIsTrueSig) evalInt(event chunk.Event) (int64, bool, error) { 529 input, isNull, err := b.args[0].EvalReal(b.ctx, event) 530 if err != nil { 531 return 0, true, err 532 } 533 if b.keepNull && isNull { 534 return 0, true, nil 535 } 536 if isNull || input == 0 { 537 return 0, false, nil 538 } 539 return 1, false, nil 540 } 541 542 type builtinDecimalIsTrueSig struct { 543 baseBuiltinFunc 544 keepNull bool 545 } 546 547 func (b *builtinDecimalIsTrueSig) Clone() builtinFunc { 548 newSig := &builtinDecimalIsTrueSig{keepNull: b.keepNull} 549 newSig.cloneFrom(&b.baseBuiltinFunc) 550 return newSig 551 } 552 553 func (b *builtinDecimalIsTrueSig) evalInt(event chunk.Event) (int64, bool, error) { 554 input, isNull, err := b.args[0].EvalDecimal(b.ctx, event) 555 if err != nil { 556 return 0, true, err 557 } 558 if b.keepNull && isNull { 559 return 0, true, nil 560 } 561 if isNull || input.IsZero() { 562 return 0, false, nil 563 } 564 return 1, false, nil 565 } 566 567 type builtinIntIsTrueSig struct { 568 baseBuiltinFunc 569 keepNull bool 570 } 571 572 func (b *builtinIntIsTrueSig) Clone() builtinFunc { 573 newSig := &builtinIntIsTrueSig{keepNull: b.keepNull} 574 newSig.cloneFrom(&b.baseBuiltinFunc) 575 return newSig 576 } 577 578 func (b *builtinIntIsTrueSig) evalInt(event chunk.Event) (int64, bool, error) { 579 input, isNull, err := b.args[0].EvalInt(b.ctx, event) 580 if err != nil { 581 return 0, true, err 582 } 583 if b.keepNull && isNull { 584 return 0, true, nil 585 } 586 if isNull || input == 0 { 587 return 0, false, nil 588 } 589 return 1, false, nil 590 } 591 592 type builtinRealIsFalseSig struct { 593 baseBuiltinFunc 594 keepNull bool 595 } 596 597 func (b *builtinRealIsFalseSig) Clone() builtinFunc { 598 newSig := &builtinRealIsFalseSig{keepNull: b.keepNull} 599 newSig.cloneFrom(&b.baseBuiltinFunc) 600 return newSig 601 } 602 603 func (b *builtinRealIsFalseSig) evalInt(event chunk.Event) (int64, bool, error) { 604 input, isNull, err := b.args[0].EvalReal(b.ctx, event) 605 if err != nil { 606 return 0, true, err 607 } 608 if b.keepNull && isNull { 609 return 0, true, nil 610 } 611 if isNull || input != 0 { 612 return 0, false, nil 613 } 614 return 1, false, nil 615 } 616 617 type builtinDecimalIsFalseSig struct { 618 baseBuiltinFunc 619 keepNull bool 620 } 621 622 func (b *builtinDecimalIsFalseSig) Clone() builtinFunc { 623 newSig := &builtinDecimalIsFalseSig{keepNull: b.keepNull} 624 newSig.cloneFrom(&b.baseBuiltinFunc) 625 return newSig 626 } 627 628 func (b *builtinDecimalIsFalseSig) evalInt(event chunk.Event) (int64, bool, error) { 629 input, isNull, err := b.args[0].EvalDecimal(b.ctx, event) 630 if err != nil { 631 return 0, true, err 632 } 633 if b.keepNull && isNull { 634 return 0, true, nil 635 } 636 if isNull || !input.IsZero() { 637 return 0, false, nil 638 } 639 return 1, false, nil 640 } 641 642 type builtinIntIsFalseSig struct { 643 baseBuiltinFunc 644 keepNull bool 645 } 646 647 func (b *builtinIntIsFalseSig) Clone() builtinFunc { 648 newSig := &builtinIntIsFalseSig{keepNull: b.keepNull} 649 newSig.cloneFrom(&b.baseBuiltinFunc) 650 return newSig 651 } 652 653 func (b *builtinIntIsFalseSig) evalInt(event chunk.Event) (int64, bool, error) { 654 input, isNull, err := b.args[0].EvalInt(b.ctx, event) 655 if err != nil { 656 return 0, true, err 657 } 658 if b.keepNull && isNull { 659 return 0, true, nil 660 } 661 if isNull || input != 0 { 662 return 0, false, nil 663 } 664 return 1, false, nil 665 } 666 667 type bitNegFunctionClass struct { 668 baseFunctionClass 669 } 670 671 func (c *bitNegFunctionClass) getFunction(ctx stochastikctx.Context, args []Expression) (builtinFunc, error) { 672 if err := c.verifyArgs(args); err != nil { 673 return nil, err 674 } 675 bf, err := newBaseBuiltinFuncWithTp(ctx, c.funcName, args, types.ETInt, types.ETInt) 676 if err != nil { 677 return nil, err 678 } 679 bf.tp.Flag |= allegrosql.UnsignedFlag 680 sig := &builtinBitNegSig{bf} 681 sig.setPbCode(fidelpb.ScalarFuncSig_BitNegSig) 682 return sig, nil 683 } 684 685 type builtinBitNegSig struct { 686 baseBuiltinFunc 687 } 688 689 func (b *builtinBitNegSig) Clone() builtinFunc { 690 newSig := &builtinBitNegSig{} 691 newSig.cloneFrom(&b.baseBuiltinFunc) 692 return newSig 693 } 694 695 func (b *builtinBitNegSig) evalInt(event chunk.Event) (int64, bool, error) { 696 arg, isNull, err := b.args[0].EvalInt(b.ctx, event) 697 if isNull || err != nil { 698 return 0, isNull, err 699 } 700 return ^arg, false, nil 701 } 702 703 type unaryNotFunctionClass struct { 704 baseFunctionClass 705 } 706 707 func (c *unaryNotFunctionClass) getFunction(ctx stochastikctx.Context, args []Expression) (builtinFunc, error) { 708 if err := c.verifyArgs(args); err != nil { 709 return nil, err 710 } 711 712 argTp := args[0].GetType().EvalType() 713 if argTp == types.ETTimestamp || argTp == types.ETDatetime || argTp == types.ETDuration { 714 argTp = types.ETInt 715 } else if argTp == types.ETJson || argTp == types.ETString { 716 argTp = types.ETReal 717 } 718 719 bf, err := newBaseBuiltinFuncWithTp(ctx, c.funcName, args, types.ETInt, argTp) 720 if err != nil { 721 return nil, err 722 } 723 bf.tp.Flen = 1 724 725 var sig builtinFunc 726 switch argTp { 727 case types.ETReal: 728 sig = &builtinUnaryNotRealSig{bf} 729 sig.setPbCode(fidelpb.ScalarFuncSig_UnaryNotReal) 730 case types.ETDecimal: 731 sig = &builtinUnaryNotDecimalSig{bf} 732 sig.setPbCode(fidelpb.ScalarFuncSig_UnaryNotDecimal) 733 case types.ETInt: 734 sig = &builtinUnaryNotIntSig{bf} 735 sig.setPbCode(fidelpb.ScalarFuncSig_UnaryNotInt) 736 default: 737 return nil, errors.Errorf("unexpected types.EvalType %v", argTp) 738 } 739 return sig, nil 740 } 741 742 type builtinUnaryNotRealSig struct { 743 baseBuiltinFunc 744 } 745 746 func (b *builtinUnaryNotRealSig) Clone() builtinFunc { 747 newSig := &builtinUnaryNotRealSig{} 748 newSig.cloneFrom(&b.baseBuiltinFunc) 749 return newSig 750 } 751 752 func (b *builtinUnaryNotRealSig) evalInt(event chunk.Event) (int64, bool, error) { 753 arg, isNull, err := b.args[0].EvalReal(b.ctx, event) 754 if isNull || err != nil { 755 return 0, true, err 756 } 757 if arg == 0 { 758 return 1, false, nil 759 } 760 return 0, false, nil 761 } 762 763 type builtinUnaryNotDecimalSig struct { 764 baseBuiltinFunc 765 } 766 767 func (b *builtinUnaryNotDecimalSig) Clone() builtinFunc { 768 newSig := &builtinUnaryNotDecimalSig{} 769 newSig.cloneFrom(&b.baseBuiltinFunc) 770 return newSig 771 } 772 773 func (b *builtinUnaryNotDecimalSig) evalInt(event chunk.Event) (int64, bool, error) { 774 arg, isNull, err := b.args[0].EvalDecimal(b.ctx, event) 775 if isNull || err != nil { 776 return 0, true, err 777 } 778 if arg.IsZero() { 779 return 1, false, nil 780 } 781 return 0, false, nil 782 } 783 784 type builtinUnaryNotIntSig struct { 785 baseBuiltinFunc 786 } 787 788 func (b *builtinUnaryNotIntSig) Clone() builtinFunc { 789 newSig := &builtinUnaryNotIntSig{} 790 newSig.cloneFrom(&b.baseBuiltinFunc) 791 return newSig 792 } 793 794 func (b *builtinUnaryNotIntSig) evalInt(event chunk.Event) (int64, bool, error) { 795 arg, isNull, err := b.args[0].EvalInt(b.ctx, event) 796 if isNull || err != nil { 797 return 0, true, err 798 } 799 if arg == 0 { 800 return 1, false, nil 801 } 802 return 0, false, nil 803 } 804 805 type unaryMinusFunctionClass struct { 806 baseFunctionClass 807 } 808 809 func (c *unaryMinusFunctionClass) handleIntOverflow(arg *Constant) (overflow bool) { 810 if allegrosql.HasUnsignedFlag(arg.GetType().Flag) { 811 uval := arg.Value.GetUint64() 812 // -math.MinInt64 is 9223372036854775808, so if uval is more than 9223372036854775808, like 813 // 9223372036854775809, -9223372036854775809 is less than math.MinInt64, overflow occurs. 814 if uval > uint64(-math.MinInt64) { 815 return true 816 } 817 } else { 818 val := arg.Value.GetInt64() 819 // The math.MinInt64 is -9223372036854775808, the math.MaxInt64 is 9223372036854775807, 820 // which is less than abs(-9223372036854775808). When val == math.MinInt64, overflow occurs. 821 if val == math.MinInt64 { 822 return true 823 } 824 } 825 return false 826 } 827 828 // typeInfer infers unaryMinus function return type. when the arg is an int constant and overflow, 829 // typerInfer will infers the return type as types.ETDecimal, not types.ETInt. 830 func (c *unaryMinusFunctionClass) typeInfer(argExpr Expression) (types.EvalType, bool) { 831 tp := argExpr.GetType().EvalType() 832 if tp != types.ETInt && tp != types.ETDecimal { 833 tp = types.ETReal 834 } 835 836 overflow := false 837 // TODO: Handle float overflow. 838 if arg, ok := argExpr.(*Constant); ok && tp == types.ETInt { 839 overflow = c.handleIntOverflow(arg) 840 if overflow { 841 tp = types.ETDecimal 842 } 843 } 844 return tp, overflow 845 } 846 847 func (c *unaryMinusFunctionClass) getFunction(ctx stochastikctx.Context, args []Expression) (sig builtinFunc, err error) { 848 if err = c.verifyArgs(args); err != nil { 849 return nil, err 850 } 851 852 argExpr, argExprTp := args[0], args[0].GetType() 853 _, intOverflow := c.typeInfer(argExpr) 854 855 var bf baseBuiltinFunc 856 switch argExprTp.EvalType() { 857 case types.ETInt: 858 if intOverflow { 859 bf, err = newBaseBuiltinFuncWithTp(ctx, c.funcName, args, types.ETDecimal, types.ETDecimal) 860 if err != nil { 861 return nil, err 862 } 863 sig = &builtinUnaryMinusDecimalSig{bf, true} 864 sig.setPbCode(fidelpb.ScalarFuncSig_UnaryMinusDecimal) 865 } else { 866 bf, err = newBaseBuiltinFuncWithTp(ctx, c.funcName, args, types.ETInt, types.ETInt) 867 if err != nil { 868 return nil, err 869 } 870 sig = &builtinUnaryMinusIntSig{bf} 871 sig.setPbCode(fidelpb.ScalarFuncSig_UnaryMinusInt) 872 } 873 bf.tp.Decimal = 0 874 case types.ETDecimal: 875 bf, err = newBaseBuiltinFuncWithTp(ctx, c.funcName, args, types.ETDecimal, types.ETDecimal) 876 if err != nil { 877 return nil, err 878 } 879 bf.tp.Decimal = argExprTp.Decimal 880 sig = &builtinUnaryMinusDecimalSig{bf, false} 881 sig.setPbCode(fidelpb.ScalarFuncSig_UnaryMinusDecimal) 882 case types.ETReal: 883 bf, err = newBaseBuiltinFuncWithTp(ctx, c.funcName, args, types.ETReal, types.ETReal) 884 if err != nil { 885 return nil, err 886 } 887 sig = &builtinUnaryMinusRealSig{bf} 888 sig.setPbCode(fidelpb.ScalarFuncSig_UnaryMinusReal) 889 default: 890 tp := argExpr.GetType().Tp 891 if types.IsTypeTime(tp) || tp == allegrosql.TypeDuration { 892 bf, err = newBaseBuiltinFuncWithTp(ctx, c.funcName, args, types.ETDecimal, types.ETDecimal) 893 if err != nil { 894 return nil, err 895 } 896 sig = &builtinUnaryMinusDecimalSig{bf, false} 897 sig.setPbCode(fidelpb.ScalarFuncSig_UnaryMinusDecimal) 898 } else { 899 bf, err = newBaseBuiltinFuncWithTp(ctx, c.funcName, args, types.ETReal, types.ETReal) 900 if err != nil { 901 return nil, err 902 } 903 sig = &builtinUnaryMinusRealSig{bf} 904 sig.setPbCode(fidelpb.ScalarFuncSig_UnaryMinusReal) 905 } 906 } 907 bf.tp.Flen = argExprTp.Flen + 1 908 return sig, err 909 } 910 911 type builtinUnaryMinusIntSig struct { 912 baseBuiltinFunc 913 } 914 915 func (b *builtinUnaryMinusIntSig) Clone() builtinFunc { 916 newSig := &builtinUnaryMinusIntSig{} 917 newSig.cloneFrom(&b.baseBuiltinFunc) 918 return newSig 919 } 920 921 func (b *builtinUnaryMinusIntSig) evalInt(event chunk.Event) (res int64, isNull bool, err error) { 922 var val int64 923 val, isNull, err = b.args[0].EvalInt(b.ctx, event) 924 if err != nil || isNull { 925 return val, isNull, err 926 } 927 928 if allegrosql.HasUnsignedFlag(b.args[0].GetType().Flag) { 929 uval := uint64(val) 930 if uval > uint64(-math.MinInt64) { 931 return 0, false, types.ErrOverflow.GenWithStackByArgs("BIGINT", fmt.Sprintf("-%v", uval)) 932 } else if uval == uint64(-math.MinInt64) { 933 return math.MinInt64, false, nil 934 } 935 } else if val == math.MinInt64 { 936 return 0, false, types.ErrOverflow.GenWithStackByArgs("BIGINT", fmt.Sprintf("-%v", val)) 937 } 938 return -val, false, nil 939 } 940 941 type builtinUnaryMinusDecimalSig struct { 942 baseBuiltinFunc 943 944 constantArgOverflow bool 945 } 946 947 func (b *builtinUnaryMinusDecimalSig) Clone() builtinFunc { 948 newSig := &builtinUnaryMinusDecimalSig{constantArgOverflow: b.constantArgOverflow} 949 newSig.cloneFrom(&b.baseBuiltinFunc) 950 return newSig 951 } 952 953 func (b *builtinUnaryMinusDecimalSig) evalDecimal(event chunk.Event) (*types.MyDecimal, bool, error) { 954 dec, isNull, err := b.args[0].EvalDecimal(b.ctx, event) 955 if err != nil || isNull { 956 return dec, isNull, err 957 } 958 return types.DecimalNeg(dec), false, nil 959 } 960 961 type builtinUnaryMinusRealSig struct { 962 baseBuiltinFunc 963 } 964 965 func (b *builtinUnaryMinusRealSig) Clone() builtinFunc { 966 newSig := &builtinUnaryMinusRealSig{} 967 newSig.cloneFrom(&b.baseBuiltinFunc) 968 return newSig 969 } 970 971 func (b *builtinUnaryMinusRealSig) evalReal(event chunk.Event) (float64, bool, error) { 972 val, isNull, err := b.args[0].EvalReal(b.ctx, event) 973 return -val, isNull, err 974 } 975 976 type isNullFunctionClass struct { 977 baseFunctionClass 978 } 979 980 func (c *isNullFunctionClass) getFunction(ctx stochastikctx.Context, args []Expression) (builtinFunc, error) { 981 if err := c.verifyArgs(args); err != nil { 982 return nil, err 983 } 984 argTp := args[0].GetType().EvalType() 985 if argTp == types.ETTimestamp { 986 argTp = types.ETDatetime 987 } else if argTp == types.ETJson { 988 argTp = types.ETString 989 } 990 bf, err := newBaseBuiltinFuncWithTp(ctx, c.funcName, args, types.ETInt, argTp) 991 if err != nil { 992 return nil, err 993 } 994 bf.tp.Flen = 1 995 var sig builtinFunc 996 switch argTp { 997 case types.ETInt: 998 sig = &builtinIntIsNullSig{bf} 999 sig.setPbCode(fidelpb.ScalarFuncSig_IntIsNull) 1000 case types.ETDecimal: 1001 sig = &builtinDecimalIsNullSig{bf} 1002 sig.setPbCode(fidelpb.ScalarFuncSig_DecimalIsNull) 1003 case types.ETReal: 1004 sig = &builtinRealIsNullSig{bf} 1005 sig.setPbCode(fidelpb.ScalarFuncSig_RealIsNull) 1006 case types.ETDatetime: 1007 sig = &builtinTimeIsNullSig{bf} 1008 sig.setPbCode(fidelpb.ScalarFuncSig_TimeIsNull) 1009 case types.ETDuration: 1010 sig = &builtinDurationIsNullSig{bf} 1011 sig.setPbCode(fidelpb.ScalarFuncSig_DurationIsNull) 1012 case types.ETString: 1013 sig = &builtinStringIsNullSig{bf} 1014 sig.setPbCode(fidelpb.ScalarFuncSig_StringIsNull) 1015 default: 1016 panic("unexpected types.EvalType") 1017 } 1018 return sig, nil 1019 } 1020 1021 type builtinDecimalIsNullSig struct { 1022 baseBuiltinFunc 1023 } 1024 1025 func (b *builtinDecimalIsNullSig) Clone() builtinFunc { 1026 newSig := &builtinDecimalIsNullSig{} 1027 newSig.cloneFrom(&b.baseBuiltinFunc) 1028 return newSig 1029 } 1030 1031 func evalIsNull(isNull bool, err error) (int64, bool, error) { 1032 if err != nil { 1033 return 0, true, err 1034 } 1035 if isNull { 1036 return 1, false, nil 1037 } 1038 return 0, false, nil 1039 } 1040 1041 func (b *builtinDecimalIsNullSig) evalInt(event chunk.Event) (int64, bool, error) { 1042 _, isNull, err := b.args[0].EvalDecimal(b.ctx, event) 1043 return evalIsNull(isNull, err) 1044 } 1045 1046 type builtinDurationIsNullSig struct { 1047 baseBuiltinFunc 1048 } 1049 1050 func (b *builtinDurationIsNullSig) Clone() builtinFunc { 1051 newSig := &builtinDurationIsNullSig{} 1052 newSig.cloneFrom(&b.baseBuiltinFunc) 1053 return newSig 1054 } 1055 1056 func (b *builtinDurationIsNullSig) evalInt(event chunk.Event) (int64, bool, error) { 1057 _, isNull, err := b.args[0].EvalDuration(b.ctx, event) 1058 return evalIsNull(isNull, err) 1059 } 1060 1061 type builtinIntIsNullSig struct { 1062 baseBuiltinFunc 1063 } 1064 1065 func (b *builtinIntIsNullSig) Clone() builtinFunc { 1066 newSig := &builtinIntIsNullSig{} 1067 newSig.cloneFrom(&b.baseBuiltinFunc) 1068 return newSig 1069 } 1070 1071 func (b *builtinIntIsNullSig) evalInt(event chunk.Event) (int64, bool, error) { 1072 _, isNull, err := b.args[0].EvalInt(b.ctx, event) 1073 return evalIsNull(isNull, err) 1074 } 1075 1076 type builtinRealIsNullSig struct { 1077 baseBuiltinFunc 1078 } 1079 1080 func (b *builtinRealIsNullSig) Clone() builtinFunc { 1081 newSig := &builtinRealIsNullSig{} 1082 newSig.cloneFrom(&b.baseBuiltinFunc) 1083 return newSig 1084 } 1085 1086 func (b *builtinRealIsNullSig) evalInt(event chunk.Event) (int64, bool, error) { 1087 _, isNull, err := b.args[0].EvalReal(b.ctx, event) 1088 return evalIsNull(isNull, err) 1089 } 1090 1091 type builtinStringIsNullSig struct { 1092 baseBuiltinFunc 1093 } 1094 1095 func (b *builtinStringIsNullSig) Clone() builtinFunc { 1096 newSig := &builtinStringIsNullSig{} 1097 newSig.cloneFrom(&b.baseBuiltinFunc) 1098 return newSig 1099 } 1100 1101 func (b *builtinStringIsNullSig) evalInt(event chunk.Event) (int64, bool, error) { 1102 _, isNull, err := b.args[0].EvalString(b.ctx, event) 1103 return evalIsNull(isNull, err) 1104 } 1105 1106 type builtinTimeIsNullSig struct { 1107 baseBuiltinFunc 1108 } 1109 1110 func (b *builtinTimeIsNullSig) Clone() builtinFunc { 1111 newSig := &builtinTimeIsNullSig{} 1112 newSig.cloneFrom(&b.baseBuiltinFunc) 1113 return newSig 1114 } 1115 1116 func (b *builtinTimeIsNullSig) evalInt(event chunk.Event) (int64, bool, error) { 1117 _, isNull, err := b.args[0].EvalTime(b.ctx, event) 1118 return evalIsNull(isNull, err) 1119 }