github.com/XiaoMi/Gaea@v1.2.5/parser/tidb-types/datum_eval.go (about)

     1  // Copyright 2016 PingCAP, 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 types
    15  
    16  import (
    17  	"github.com/cznic/mathutil"
    18  	"github.com/pingcap/errors"
    19  
    20  	"github.com/XiaoMi/Gaea/parser/opcode"
    21  )
    22  
    23  // ComputePlus computes the result of a+b.
    24  func ComputePlus(a, b Datum) (d Datum, err error) {
    25  	switch a.Kind() {
    26  	case KindInt64:
    27  		switch b.Kind() {
    28  		case KindInt64:
    29  			r, err1 := AddInt64(a.GetInt64(), b.GetInt64())
    30  			d.SetInt64(r)
    31  			return d, errors.Trace(err1)
    32  		case KindUint64:
    33  			r, err1 := AddInteger(b.GetUint64(), a.GetInt64())
    34  			d.SetUint64(r)
    35  			return d, errors.Trace(err1)
    36  		}
    37  	case KindUint64:
    38  		switch b.Kind() {
    39  		case KindInt64:
    40  			r, err1 := AddInteger(a.GetUint64(), b.GetInt64())
    41  			d.SetUint64(r)
    42  			return d, errors.Trace(err1)
    43  		case KindUint64:
    44  			r, err1 := AddUint64(a.GetUint64(), b.GetUint64())
    45  			d.SetUint64(r)
    46  			return d, errors.Trace(err1)
    47  		}
    48  	case KindFloat64:
    49  		switch b.Kind() {
    50  		case KindFloat64:
    51  			r := a.GetFloat64() + b.GetFloat64()
    52  			d.SetFloat64(r)
    53  			return d, nil
    54  		}
    55  	case KindMysqlDecimal:
    56  		switch b.Kind() {
    57  		case KindMysqlDecimal:
    58  			r := new(MyDecimal)
    59  			err = DecimalAdd(a.GetMysqlDecimal(), b.GetMysqlDecimal(), r)
    60  			d.SetMysqlDecimal(r)
    61  			d.SetFrac(mathutil.Max(a.Frac(), b.Frac()))
    62  			return d, err
    63  		}
    64  	}
    65  	_, err = InvOp2(a.GetValue(), b.GetValue(), opcode.Plus)
    66  	return d, err
    67  }