1 Star 0 Fork 0

zhoujin826/tidb

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
builtin_cast.go 52.30 KB
一键复制 编辑 原始数据 按行查看 历史
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753
// Copyright 2017 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
// We implement 6 CastAsXXFunctionClass for `cast` built-in functions.
// XX means the return type of the `cast` built-in functions.
// XX contains the following 6 types:
// Int, Decimal, Real, String, Time, Duration.
// We implement 6 CastYYAsXXSig built-in function signatures for every CastAsXXFunctionClass.
// builtinCastXXAsYYSig takes a argument of type XX and returns a value of type YY.
package expression
import (
"math"
"strconv"
"strings"
"github.com/juju/errors"
"github.com/pingcap/tidb/ast"
"github.com/pingcap/tidb/model"
"github.com/pingcap/tidb/mysql"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/terror"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/types/json"
"github.com/pingcap/tidb/util/charset"
tipb "github.com/pingcap/tipb/go-tipb"
)
var (
_ functionClass = &castAsIntFunctionClass{}
_ functionClass = &castAsRealFunctionClass{}
_ functionClass = &castAsStringFunctionClass{}
_ functionClass = &castAsDecimalFunctionClass{}
_ functionClass = &castAsTimeFunctionClass{}
_ functionClass = &castAsDurationFunctionClass{}
_ functionClass = &castAsJSONFunctionClass{}
)
var (
_ builtinFunc = &builtinCastIntAsIntSig{}
_ builtinFunc = &builtinCastIntAsRealSig{}
_ builtinFunc = &builtinCastIntAsStringSig{}
_ builtinFunc = &builtinCastIntAsDecimalSig{}
_ builtinFunc = &builtinCastIntAsTimeSig{}
_ builtinFunc = &builtinCastIntAsDurationSig{}
_ builtinFunc = &builtinCastIntAsJSONSig{}
_ builtinFunc = &builtinCastRealAsIntSig{}
_ builtinFunc = &builtinCastRealAsRealSig{}
_ builtinFunc = &builtinCastRealAsStringSig{}
_ builtinFunc = &builtinCastRealAsDecimalSig{}
_ builtinFunc = &builtinCastRealAsTimeSig{}
_ builtinFunc = &builtinCastRealAsDurationSig{}
_ builtinFunc = &builtinCastRealAsJSONSig{}
_ builtinFunc = &builtinCastDecimalAsIntSig{}
_ builtinFunc = &builtinCastDecimalAsRealSig{}
_ builtinFunc = &builtinCastDecimalAsStringSig{}
_ builtinFunc = &builtinCastDecimalAsDecimalSig{}
_ builtinFunc = &builtinCastDecimalAsTimeSig{}
_ builtinFunc = &builtinCastDecimalAsDurationSig{}
_ builtinFunc = &builtinCastDecimalAsJSONSig{}
_ builtinFunc = &builtinCastStringAsIntSig{}
_ builtinFunc = &builtinCastStringAsRealSig{}
_ builtinFunc = &builtinCastStringAsStringSig{}
_ builtinFunc = &builtinCastStringAsDecimalSig{}
_ builtinFunc = &builtinCastStringAsTimeSig{}
_ builtinFunc = &builtinCastStringAsDurationSig{}
_ builtinFunc = &builtinCastStringAsJSONSig{}
_ builtinFunc = &builtinCastTimeAsIntSig{}
_ builtinFunc = &builtinCastTimeAsRealSig{}
_ builtinFunc = &builtinCastTimeAsStringSig{}
_ builtinFunc = &builtinCastTimeAsDecimalSig{}
_ builtinFunc = &builtinCastTimeAsTimeSig{}
_ builtinFunc = &builtinCastTimeAsDurationSig{}
_ builtinFunc = &builtinCastTimeAsJSONSig{}
_ builtinFunc = &builtinCastDurationAsIntSig{}
_ builtinFunc = &builtinCastDurationAsRealSig{}
_ builtinFunc = &builtinCastDurationAsStringSig{}
_ builtinFunc = &builtinCastDurationAsDecimalSig{}
_ builtinFunc = &builtinCastDurationAsTimeSig{}
_ builtinFunc = &builtinCastDurationAsDurationSig{}
_ builtinFunc = &builtinCastDurationAsJSONSig{}
_ builtinFunc = &builtinCastJSONAsIntSig{}
_ builtinFunc = &builtinCastJSONAsRealSig{}
_ builtinFunc = &builtinCastJSONAsStringSig{}
_ builtinFunc = &builtinCastJSONAsDecimalSig{}
_ builtinFunc = &builtinCastJSONAsTimeSig{}
_ builtinFunc = &builtinCastJSONAsDurationSig{}
_ builtinFunc = &builtinCastJSONAsJSONSig{}
)
type castAsIntFunctionClass struct {
baseFunctionClass
tp *types.FieldType
}
func (c *castAsIntFunctionClass) getFunction(ctx sessionctx.Context, args []Expression) (sig builtinFunc, err error) {
if err := c.verifyArgs(args); err != nil {
return nil, errors.Trace(err)
}
bf := newBaseBuiltinFunc(ctx, args)
bf.tp = c.tp
if args[0].GetType().Hybrid() || IsBinaryLiteral(args[0]) {
sig = &builtinCastIntAsIntSig{bf}
sig.setPbCode(tipb.ScalarFuncSig_CastIntAsInt)
return sig, nil
}
argTp := args[0].GetType().EvalType()
switch argTp {
case types.ETInt:
sig = &builtinCastIntAsIntSig{bf}
sig.setPbCode(tipb.ScalarFuncSig_CastIntAsInt)
case types.ETReal:
sig = &builtinCastRealAsIntSig{bf}
sig.setPbCode(tipb.ScalarFuncSig_CastRealAsInt)
case types.ETDecimal:
sig = &builtinCastDecimalAsIntSig{bf}
sig.setPbCode(tipb.ScalarFuncSig_CastDecimalAsInt)
case types.ETDatetime, types.ETTimestamp:
sig = &builtinCastTimeAsIntSig{bf}
sig.setPbCode(tipb.ScalarFuncSig_CastTimeAsInt)
case types.ETDuration:
sig = &builtinCastDurationAsIntSig{bf}
sig.setPbCode(tipb.ScalarFuncSig_CastDurationAsInt)
case types.ETJson:
sig = &builtinCastJSONAsIntSig{bf}
sig.setPbCode(tipb.ScalarFuncSig_CastJsonAsInt)
case types.ETString:
sig = &builtinCastStringAsIntSig{bf}
sig.setPbCode(tipb.ScalarFuncSig_CastStringAsInt)
default:
panic("unsupported types.EvalType in castAsIntFunctionClass")
}
return sig, nil
}
type castAsRealFunctionClass struct {
baseFunctionClass
tp *types.FieldType
}
func (c *castAsRealFunctionClass) getFunction(ctx sessionctx.Context, args []Expression) (sig builtinFunc, err error) {
if err := c.verifyArgs(args); err != nil {
return nil, errors.Trace(err)
}
bf := newBaseBuiltinFunc(ctx, args)
bf.tp = c.tp
if IsBinaryLiteral(args[0]) {
sig = &builtinCastRealAsRealSig{bf}
sig.setPbCode(tipb.ScalarFuncSig_CastRealAsReal)
return sig, nil
}
var argTp types.EvalType
if args[0].GetType().Hybrid() {
argTp = types.ETInt
} else {
argTp = args[0].GetType().EvalType()
}
switch argTp {
case types.ETInt:
sig = &builtinCastIntAsRealSig{bf}
sig.setPbCode(tipb.ScalarFuncSig_CastIntAsReal)
case types.ETReal:
sig = &builtinCastRealAsRealSig{bf}
sig.setPbCode(tipb.ScalarFuncSig_CastRealAsReal)
case types.ETDecimal:
sig = &builtinCastDecimalAsRealSig{bf}
sig.setPbCode(tipb.ScalarFuncSig_CastDecimalAsReal)
case types.ETDatetime, types.ETTimestamp:
sig = &builtinCastTimeAsRealSig{bf}
sig.setPbCode(tipb.ScalarFuncSig_CastTimeAsReal)
case types.ETDuration:
sig = &builtinCastDurationAsRealSig{bf}
sig.setPbCode(tipb.ScalarFuncSig_CastDurationAsReal)
case types.ETJson:
sig = &builtinCastJSONAsRealSig{bf}
sig.setPbCode(tipb.ScalarFuncSig_CastJsonAsReal)
case types.ETString:
sig = &builtinCastStringAsRealSig{bf}
sig.setPbCode(tipb.ScalarFuncSig_CastStringAsReal)
default:
panic("unsupported types.EvalType in castAsRealFunctionClass")
}
return sig, nil
}
type castAsDecimalFunctionClass struct {
baseFunctionClass
tp *types.FieldType
}
func (c *castAsDecimalFunctionClass) getFunction(ctx sessionctx.Context, args []Expression) (sig builtinFunc, err error) {
if err := c.verifyArgs(args); err != nil {
return nil, errors.Trace(err)
}
bf := newBaseBuiltinFunc(ctx, args)
bf.tp = c.tp
if IsBinaryLiteral(args[0]) {
sig = &builtinCastDecimalAsDecimalSig{bf}
sig.setPbCode(tipb.ScalarFuncSig_CastDecimalAsDecimal)
return sig, nil
}
var argTp types.EvalType
if args[0].GetType().Hybrid() {
argTp = types.ETInt
} else {
argTp = args[0].GetType().EvalType()
}
switch argTp {
case types.ETInt:
sig = &builtinCastIntAsDecimalSig{bf}
sig.setPbCode(tipb.ScalarFuncSig_CastIntAsDecimal)
case types.ETReal:
sig = &builtinCastRealAsDecimalSig{bf}
sig.setPbCode(tipb.ScalarFuncSig_CastRealAsDecimal)
case types.ETDecimal:
sig = &builtinCastDecimalAsDecimalSig{bf}
sig.setPbCode(tipb.ScalarFuncSig_CastDecimalAsDecimal)
case types.ETDatetime, types.ETTimestamp:
sig = &builtinCastTimeAsDecimalSig{bf}
sig.setPbCode(tipb.ScalarFuncSig_CastTimeAsDecimal)
case types.ETDuration:
sig = &builtinCastDurationAsDecimalSig{bf}
sig.setPbCode(tipb.ScalarFuncSig_CastDurationAsDecimal)
case types.ETJson:
sig = &builtinCastJSONAsDecimalSig{bf}
sig.setPbCode(tipb.ScalarFuncSig_CastJsonAsDecimal)
case types.ETString:
sig = &builtinCastStringAsDecimalSig{bf}
sig.setPbCode(tipb.ScalarFuncSig_CastStringAsDecimal)
default:
panic("unsupported types.EvalType in castAsDecimalFunctionClass")
}
return sig, nil
}
type castAsStringFunctionClass struct {
baseFunctionClass
tp *types.FieldType
}
func (c *castAsStringFunctionClass) getFunction(ctx sessionctx.Context, args []Expression) (sig builtinFunc, err error) {
if err := c.verifyArgs(args); err != nil {
return nil, errors.Trace(err)
}
bf := newBaseBuiltinFunc(ctx, args)
bf.tp = c.tp
if args[0].GetType().Hybrid() || IsBinaryLiteral(args[0]) {
sig = &builtinCastStringAsStringSig{bf}
sig.setPbCode(tipb.ScalarFuncSig_CastStringAsString)
return sig, nil
}
argTp := args[0].GetType().EvalType()
switch argTp {
case types.ETInt:
sig = &builtinCastIntAsStringSig{bf}
sig.setPbCode(tipb.ScalarFuncSig_CastIntAsString)
case types.ETReal:
sig = &builtinCastRealAsStringSig{bf}
sig.setPbCode(tipb.ScalarFuncSig_CastRealAsString)
case types.ETDecimal:
sig = &builtinCastDecimalAsStringSig{bf}
sig.setPbCode(tipb.ScalarFuncSig_CastDecimalAsString)
case types.ETDatetime, types.ETTimestamp:
sig = &builtinCastTimeAsStringSig{bf}
sig.setPbCode(tipb.ScalarFuncSig_CastTimeAsString)
case types.ETDuration:
sig = &builtinCastDurationAsStringSig{bf}
sig.setPbCode(tipb.ScalarFuncSig_CastDurationAsString)
case types.ETJson:
sig = &builtinCastJSONAsStringSig{bf}
sig.setPbCode(tipb.ScalarFuncSig_CastJsonAsString)
case types.ETString:
sig = &builtinCastStringAsStringSig{bf}
sig.setPbCode(tipb.ScalarFuncSig_CastStringAsString)
default:
panic("unsupported types.EvalType in castAsStringFunctionClass")
}
return sig, nil
}
type castAsTimeFunctionClass struct {
baseFunctionClass
tp *types.FieldType
}
func (c *castAsTimeFunctionClass) getFunction(ctx sessionctx.Context, args []Expression) (sig builtinFunc, err error) {
if err := c.verifyArgs(args); err != nil {
return nil, errors.Trace(err)
}
bf := newBaseBuiltinFunc(ctx, args)
bf.tp = c.tp
argTp := args[0].GetType().EvalType()
switch argTp {
case types.ETInt:
sig = &builtinCastIntAsTimeSig{bf}
sig.setPbCode(tipb.ScalarFuncSig_CastIntAsTime)
case types.ETReal:
sig = &builtinCastRealAsTimeSig{bf}
sig.setPbCode(tipb.ScalarFuncSig_CastRealAsTime)
case types.ETDecimal:
sig = &builtinCastDecimalAsTimeSig{bf}
sig.setPbCode(tipb.ScalarFuncSig_CastDecimalAsTime)
case types.ETDatetime, types.ETTimestamp:
sig = &builtinCastTimeAsTimeSig{bf}
sig.setPbCode(tipb.ScalarFuncSig_CastTimeAsTime)
case types.ETDuration:
sig = &builtinCastDurationAsTimeSig{bf}
sig.setPbCode(tipb.ScalarFuncSig_CastDurationAsTime)
case types.ETJson:
sig = &builtinCastJSONAsTimeSig{bf}
sig.setPbCode(tipb.ScalarFuncSig_CastJsonAsTime)
case types.ETString:
sig = &builtinCastStringAsTimeSig{bf}
sig.setPbCode(tipb.ScalarFuncSig_CastStringAsTime)
default:
panic("unsupported types.EvalType in castAsTimeFunctionClass")
}
return sig, nil
}
type castAsDurationFunctionClass struct {
baseFunctionClass
tp *types.FieldType
}
func (c *castAsDurationFunctionClass) getFunction(ctx sessionctx.Context, args []Expression) (sig builtinFunc, err error) {
if err := c.verifyArgs(args); err != nil {
return nil, errors.Trace(err)
}
bf := newBaseBuiltinFunc(ctx, args)
bf.tp = c.tp
argTp := args[0].GetType().EvalType()
switch argTp {
case types.ETInt:
sig = &builtinCastIntAsDurationSig{bf}
sig.setPbCode(tipb.ScalarFuncSig_CastIntAsDuration)
case types.ETReal:
sig = &builtinCastRealAsDurationSig{bf}
sig.setPbCode(tipb.ScalarFuncSig_CastRealAsDuration)
case types.ETDecimal:
sig = &builtinCastDecimalAsDurationSig{bf}
sig.setPbCode(tipb.ScalarFuncSig_CastDecimalAsDuration)
case types.ETDatetime, types.ETTimestamp:
sig = &builtinCastTimeAsDurationSig{bf}
sig.setPbCode(tipb.ScalarFuncSig_CastTimeAsDuration)
case types.ETDuration:
sig = &builtinCastDurationAsDurationSig{bf}
sig.setPbCode(tipb.ScalarFuncSig_CastDurationAsDuration)
case types.ETJson:
sig = &builtinCastJSONAsDurationSig{bf}
sig.setPbCode(tipb.ScalarFuncSig_CastJsonAsDuration)
case types.ETString:
sig = &builtinCastStringAsDurationSig{bf}
sig.setPbCode(tipb.ScalarFuncSig_CastStringAsDuration)
default:
panic("unsupported types.EvalType in castAsDurationFunctionClass")
}
return sig, nil
}
type castAsJSONFunctionClass struct {
baseFunctionClass
tp *types.FieldType
}
func (c *castAsJSONFunctionClass) getFunction(ctx sessionctx.Context, args []Expression) (sig builtinFunc, err error) {
if err := c.verifyArgs(args); err != nil {
return nil, errors.Trace(err)
}
bf := newBaseBuiltinFunc(ctx, args)
bf.tp = c.tp
argTp := args[0].GetType().EvalType()
switch argTp {
case types.ETInt:
sig = &builtinCastIntAsJSONSig{bf}
sig.setPbCode(tipb.ScalarFuncSig_CastIntAsJson)
case types.ETReal:
sig = &builtinCastRealAsJSONSig{bf}
sig.setPbCode(tipb.ScalarFuncSig_CastRealAsJson)
case types.ETDecimal:
sig = &builtinCastDecimalAsJSONSig{bf}
sig.setPbCode(tipb.ScalarFuncSig_CastDecimalAsJson)
case types.ETDatetime, types.ETTimestamp:
sig = &builtinCastTimeAsJSONSig{bf}
sig.setPbCode(tipb.ScalarFuncSig_CastTimeAsJson)
case types.ETDuration:
sig = &builtinCastDurationAsJSONSig{bf}
sig.setPbCode(tipb.ScalarFuncSig_CastDurationAsJson)
case types.ETJson:
sig = &builtinCastJSONAsJSONSig{bf}
sig.setPbCode(tipb.ScalarFuncSig_CastJsonAsJson)
case types.ETString:
sig = &builtinCastStringAsJSONSig{bf}
sig.getRetTp().Flag |= mysql.ParseToJSONFlag
sig.setPbCode(tipb.ScalarFuncSig_CastStringAsJson)
default:
panic("unsupported types.EvalType in castAsJSONFunctionClass")
}
return sig, nil
}
type builtinCastIntAsIntSig struct {
baseBuiltinFunc
}
func (b *builtinCastIntAsIntSig) Clone() builtinFunc {
newSig := &builtinCastIntAsIntSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
func (b *builtinCastIntAsIntSig) evalInt(row types.Row) (res int64, isNull bool, err error) {
return b.args[0].EvalInt(b.ctx, row)
}
type builtinCastIntAsRealSig struct {
baseBuiltinFunc
}
func (b *builtinCastIntAsRealSig) Clone() builtinFunc {
newSig := &builtinCastIntAsRealSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
func (b *builtinCastIntAsRealSig) evalReal(row types.Row) (res float64, isNull bool, err error) {
val, isNull, err := b.args[0].EvalInt(b.ctx, row)
if isNull || err != nil {
return res, isNull, errors.Trace(err)
}
if !mysql.HasUnsignedFlag(b.args[0].GetType().Flag) {
res = float64(val)
} else {
var uVal uint64
uVal, err = types.ConvertIntToUint(val, types.UnsignedUpperBound[mysql.TypeLonglong], mysql.TypeLonglong)
res = float64(uVal)
}
return res, false, errors.Trace(err)
}
type builtinCastIntAsDecimalSig struct {
baseBuiltinFunc
}
func (b *builtinCastIntAsDecimalSig) Clone() builtinFunc {
newSig := &builtinCastIntAsDecimalSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
func (b *builtinCastIntAsDecimalSig) evalDecimal(row types.Row) (res *types.MyDecimal, isNull bool, err error) {
val, isNull, err := b.args[0].EvalInt(b.ctx, row)
if isNull || err != nil {
return res, isNull, errors.Trace(err)
}
if !mysql.HasUnsignedFlag(b.args[0].GetType().Flag) {
res = types.NewDecFromInt(val)
} else {
var uVal uint64
uVal, err = types.ConvertIntToUint(val, types.UnsignedUpperBound[mysql.TypeLonglong], mysql.TypeLonglong)
if err != nil {
return res, false, errors.Trace(err)
}
res = types.NewDecFromUint(uVal)
}
res, err = types.ProduceDecWithSpecifiedTp(res, b.tp, b.ctx.GetSessionVars().StmtCtx)
return res, isNull, errors.Trace(err)
}
type builtinCastIntAsStringSig struct {
baseBuiltinFunc
}
func (b *builtinCastIntAsStringSig) Clone() builtinFunc {
newSig := &builtinCastIntAsStringSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
func (b *builtinCastIntAsStringSig) evalString(row types.Row) (res string, isNull bool, err error) {
val, isNull, err := b.args[0].EvalInt(b.ctx, row)
if isNull || err != nil {
return res, isNull, errors.Trace(err)
}
if !mysql.HasUnsignedFlag(b.args[0].GetType().Flag) {
res = strconv.FormatInt(val, 10)
} else {
var uVal uint64
uVal, err = types.ConvertIntToUint(val, types.UnsignedUpperBound[mysql.TypeLonglong], mysql.TypeLonglong)
if err != nil {
return res, false, errors.Trace(err)
}
res = strconv.FormatUint(uVal, 10)
}
res, err = types.ProduceStrWithSpecifiedTp(res, b.tp, b.ctx.GetSessionVars().StmtCtx)
return res, false, errors.Trace(err)
}
type builtinCastIntAsTimeSig struct {
baseBuiltinFunc
}
func (b *builtinCastIntAsTimeSig) Clone() builtinFunc {
newSig := &builtinCastIntAsTimeSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
func (b *builtinCastIntAsTimeSig) evalTime(row types.Row) (res types.Time, isNull bool, err error) {
val, isNull, err := b.args[0].EvalInt(b.ctx, row)
if isNull || err != nil {
return res, isNull, errors.Trace(err)
}
res, err = types.ParseTimeFromNum(b.ctx.GetSessionVars().StmtCtx, val, b.tp.Tp, b.tp.Decimal)
if err != nil {
return res, true, errors.Trace(err)
}
if b.tp.Tp == mysql.TypeDate {
// Truncate hh:mm:ss part if the type is Date.
res.Time = types.FromDate(res.Time.Year(), res.Time.Month(), res.Time.Day(), 0, 0, 0, 0)
}
return res, false, nil
}
type builtinCastIntAsDurationSig struct {
baseBuiltinFunc
}
func (b *builtinCastIntAsDurationSig) Clone() builtinFunc {
newSig := &builtinCastIntAsDurationSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
func (b *builtinCastIntAsDurationSig) evalDuration(row types.Row) (res types.Duration, isNull bool, err error) {
val, isNull, err := b.args[0].EvalInt(b.ctx, row)
if isNull || err != nil {
return res, isNull, errors.Trace(err)
}
dur, err := types.NumberToDuration(val, b.tp.Decimal)
if err != nil {
if types.ErrOverflow.Equal(err) {
err = b.ctx.GetSessionVars().StmtCtx.HandleOverflow(err, err)
}
return res, true, errors.Trace(err)
}
return dur, false, errors.Trace(err)
}
type builtinCastIntAsJSONSig struct {
baseBuiltinFunc
}
func (b *builtinCastIntAsJSONSig) Clone() builtinFunc {
newSig := &builtinCastIntAsJSONSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
func (b *builtinCastIntAsJSONSig) evalJSON(row types.Row) (res json.BinaryJSON, isNull bool, err error) {
val, isNull, err := b.args[0].EvalInt(b.ctx, row)
if isNull || err != nil {
return res, isNull, errors.Trace(err)
}
if mysql.HasIsBooleanFlag(b.args[0].GetType().Flag) {
res = json.CreateBinary(val != 0)
} else if mysql.HasUnsignedFlag(b.args[0].GetType().Flag) {
res = json.CreateBinary(uint64(val))
} else {
res = json.CreateBinary(val)
}
return res, false, nil
}
type builtinCastRealAsJSONSig struct {
baseBuiltinFunc
}
func (b *builtinCastRealAsJSONSig) Clone() builtinFunc {
newSig := &builtinCastRealAsJSONSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
func (b *builtinCastRealAsJSONSig) evalJSON(row types.Row) (res json.BinaryJSON, isNull bool, err error) {
val, isNull, err := b.args[0].EvalReal(b.ctx, row)
// FIXME: `select json_type(cast(1111.11 as json))` should return `DECIMAL`, we return `DOUBLE` now.
return json.CreateBinary(val), isNull, errors.Trace(err)
}
type builtinCastDecimalAsJSONSig struct {
baseBuiltinFunc
}
func (b *builtinCastDecimalAsJSONSig) Clone() builtinFunc {
newSig := &builtinCastDecimalAsJSONSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
func (b *builtinCastDecimalAsJSONSig) evalJSON(row types.Row) (json.BinaryJSON, bool, error) {
val, isNull, err := b.args[0].EvalDecimal(b.ctx, row)
if isNull || err != nil {
return json.BinaryJSON{}, true, errors.Trace(err)
}
// FIXME: `select json_type(cast(1111.11 as json))` should return `DECIMAL`, we return `DOUBLE` now.
f64, err := val.ToFloat64()
if err != nil {
return json.BinaryJSON{}, true, errors.Trace(err)
}
return json.CreateBinary(f64), isNull, errors.Trace(err)
}
type builtinCastStringAsJSONSig struct {
baseBuiltinFunc
}
func (b *builtinCastStringAsJSONSig) Clone() builtinFunc {
newSig := &builtinCastStringAsJSONSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
func (b *builtinCastStringAsJSONSig) evalJSON(row types.Row) (res json.BinaryJSON, isNull bool, err error) {
val, isNull, err := b.args[0].EvalString(b.ctx, row)
if isNull || err != nil {
return res, isNull, errors.Trace(err)
}
if mysql.HasParseToJSONFlag(b.tp.Flag) {
res, err = json.ParseBinaryFromString(val)
} else {
res = json.CreateBinary(val)
}
return res, false, errors.Trace(err)
}
type builtinCastDurationAsJSONSig struct {
baseBuiltinFunc
}
func (b *builtinCastDurationAsJSONSig) Clone() builtinFunc {
newSig := &builtinCastDurationAsJSONSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
func (b *builtinCastDurationAsJSONSig) evalJSON(row types.Row) (res json.BinaryJSON, isNull bool, err error) {
val, isNull, err := b.args[0].EvalDuration(b.ctx, row)
if isNull || err != nil {
return res, isNull, errors.Trace(err)
}
val.Fsp = types.MaxFsp
return json.CreateBinary(val.String()), false, nil
}
type builtinCastTimeAsJSONSig struct {
baseBuiltinFunc
}
func (b *builtinCastTimeAsJSONSig) Clone() builtinFunc {
newSig := &builtinCastTimeAsJSONSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
func (b *builtinCastTimeAsJSONSig) evalJSON(row types.Row) (res json.BinaryJSON, isNull bool, err error) {
val, isNull, err := b.args[0].EvalTime(b.ctx, row)
if isNull || err != nil {
return res, isNull, errors.Trace(err)
}
if val.Type == mysql.TypeDatetime || val.Type == mysql.TypeTimestamp {
val.Fsp = types.MaxFsp
}
return json.CreateBinary(val.String()), false, nil
}
type builtinCastRealAsRealSig struct {
baseBuiltinFunc
}
func (b *builtinCastRealAsRealSig) Clone() builtinFunc {
newSig := &builtinCastRealAsRealSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
func (b *builtinCastRealAsRealSig) evalReal(row types.Row) (res float64, isNull bool, err error) {
return b.args[0].EvalReal(b.ctx, row)
}
type builtinCastRealAsIntSig struct {
baseBuiltinFunc
}
func (b *builtinCastRealAsIntSig) Clone() builtinFunc {
newSig := &builtinCastRealAsIntSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
func (b *builtinCastRealAsIntSig) evalInt(row types.Row) (res int64, isNull bool, err error) {
val, isNull, err := b.args[0].EvalReal(b.ctx, row)
if isNull || err != nil {
return res, isNull, errors.Trace(err)
}
if !mysql.HasUnsignedFlag(b.tp.Flag) {
res, err = types.ConvertFloatToInt(val, types.SignedLowerBound[mysql.TypeLonglong], types.SignedUpperBound[mysql.TypeLonglong], mysql.TypeDouble)
} else {
var uintVal uint64
uintVal, err = types.ConvertFloatToUint(val, types.UnsignedUpperBound[mysql.TypeLonglong], mysql.TypeDouble)
res = int64(uintVal)
}
return res, isNull, errors.Trace(err)
}
type builtinCastRealAsDecimalSig struct {
baseBuiltinFunc
}
func (b *builtinCastRealAsDecimalSig) Clone() builtinFunc {
newSig := &builtinCastRealAsDecimalSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
func (b *builtinCastRealAsDecimalSig) evalDecimal(row types.Row) (res *types.MyDecimal, isNull bool, err error) {
val, isNull, err := b.args[0].EvalReal(b.ctx, row)
if isNull || err != nil {
return res, isNull, errors.Trace(err)
}
res = new(types.MyDecimal)
err = res.FromFloat64(val)
if err != nil {
return res, false, errors.Trace(err)
}
res, err = types.ProduceDecWithSpecifiedTp(res, b.tp, b.ctx.GetSessionVars().StmtCtx)
return res, false, errors.Trace(err)
}
type builtinCastRealAsStringSig struct {
baseBuiltinFunc
}
func (b *builtinCastRealAsStringSig) Clone() builtinFunc {
newSig := &builtinCastRealAsStringSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
func (b *builtinCastRealAsStringSig) evalString(row types.Row) (res string, isNull bool, err error) {
val, isNull, err := b.args[0].EvalReal(b.ctx, row)
if isNull || err != nil {
return res, isNull, errors.Trace(err)
}
res, err = types.ProduceStrWithSpecifiedTp(strconv.FormatFloat(val, 'f', -1, 64), b.tp, b.ctx.GetSessionVars().StmtCtx)
return res, isNull, errors.Trace(err)
}
type builtinCastRealAsTimeSig struct {
baseBuiltinFunc
}
func (b *builtinCastRealAsTimeSig) Clone() builtinFunc {
newSig := &builtinCastRealAsTimeSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
func (b *builtinCastRealAsTimeSig) evalTime(row types.Row) (types.Time, bool, error) {
val, isNull, err := b.args[0].EvalReal(b.ctx, row)
if isNull || err != nil {
return types.Time{}, true, errors.Trace(err)
}
sc := b.ctx.GetSessionVars().StmtCtx
res, err := types.ParseTime(sc, strconv.FormatFloat(val, 'f', -1, 64), b.tp.Tp, b.tp.Decimal)
if err != nil {
return types.Time{}, true, errors.Trace(err)
}
if b.tp.Tp == mysql.TypeDate {
// Truncate hh:mm:ss part if the type is Date.
res.Time = types.FromDate(res.Time.Year(), res.Time.Month(), res.Time.Day(), 0, 0, 0, 0)
}
return res, false, nil
}
type builtinCastRealAsDurationSig struct {
baseBuiltinFunc
}
func (b *builtinCastRealAsDurationSig) Clone() builtinFunc {
newSig := &builtinCastRealAsDurationSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
func (b *builtinCastRealAsDurationSig) evalDuration(row types.Row) (res types.Duration, isNull bool, err error) {
val, isNull, err := b.args[0].EvalReal(b.ctx, row)
if isNull || err != nil {
return res, isNull, errors.Trace(err)
}
res, err = types.ParseDuration(strconv.FormatFloat(val, 'f', -1, 64), b.tp.Decimal)
return res, false, errors.Trace(err)
}
type builtinCastDecimalAsDecimalSig struct {
baseBuiltinFunc
}
func (b *builtinCastDecimalAsDecimalSig) Clone() builtinFunc {
newSig := &builtinCastDecimalAsDecimalSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
func (b *builtinCastDecimalAsDecimalSig) evalDecimal(row types.Row) (res *types.MyDecimal, isNull bool, err error) {
evalDecimal, isNull, err := b.args[0].EvalDecimal(b.ctx, row)
if isNull || err != nil {
return res, isNull, errors.Trace(err)
}
res = &types.MyDecimal{}
*res = *evalDecimal
sc := b.ctx.GetSessionVars().StmtCtx
res, err = types.ProduceDecWithSpecifiedTp(res, b.tp, sc)
return res, false, errors.Trace(err)
}
type builtinCastDecimalAsIntSig struct {
baseBuiltinFunc
}
func (b *builtinCastDecimalAsIntSig) Clone() builtinFunc {
newSig := &builtinCastDecimalAsIntSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
func (b *builtinCastDecimalAsIntSig) evalInt(row types.Row) (res int64, isNull bool, err error) {
val, isNull, err := b.args[0].EvalDecimal(b.ctx, row)
if isNull || err != nil {
return res, isNull, errors.Trace(err)
}
// Round is needed for both unsigned and signed.
var to types.MyDecimal
err = val.Round(&to, 0, types.ModeHalfEven)
if err != nil {
return 0, true, errors.Trace(err)
}
if mysql.HasUnsignedFlag(b.tp.Flag) {
var uintRes uint64
uintRes, err = to.ToUint()
res = int64(uintRes)
} else {
res, err = to.ToInt()
}
if types.ErrOverflow.Equal(err) {
warnErr := types.ErrTruncatedWrongVal.GenByArgs("DECIMAL", val)
err = b.ctx.GetSessionVars().StmtCtx.HandleOverflow(err, warnErr)
}
return res, false, errors.Trace(err)
}
type builtinCastDecimalAsStringSig struct {
baseBuiltinFunc
}
func (b *builtinCastDecimalAsStringSig) Clone() builtinFunc {
newSig := &builtinCastDecimalAsStringSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
func (b *builtinCastDecimalAsStringSig) evalString(row types.Row) (res string, isNull bool, err error) {
val, isNull, err := b.args[0].EvalDecimal(b.ctx, row)
if isNull || err != nil {
return res, isNull, errors.Trace(err)
}
sc := b.ctx.GetSessionVars().StmtCtx
res, err = types.ProduceStrWithSpecifiedTp(string(val.ToString()), b.tp, sc)
return res, false, errors.Trace(err)
}
type builtinCastDecimalAsRealSig struct {
baseBuiltinFunc
}
func (b *builtinCastDecimalAsRealSig) Clone() builtinFunc {
newSig := &builtinCastDecimalAsRealSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
func (b *builtinCastDecimalAsRealSig) evalReal(row types.Row) (res float64, isNull bool, err error) {
val, isNull, err := b.args[0].EvalDecimal(b.ctx, row)
if isNull || err != nil {
return res, isNull, errors.Trace(err)
}
res, err = val.ToFloat64()
return res, false, errors.Trace(err)
}
type builtinCastDecimalAsTimeSig struct {
baseBuiltinFunc
}
func (b *builtinCastDecimalAsTimeSig) Clone() builtinFunc {
newSig := &builtinCastDecimalAsTimeSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
func (b *builtinCastDecimalAsTimeSig) evalTime(row types.Row) (res types.Time, isNull bool, err error) {
val, isNull, err := b.args[0].EvalDecimal(b.ctx, row)
if isNull || err != nil {
return res, isNull, errors.Trace(err)
}
sc := b.ctx.GetSessionVars().StmtCtx
res, err = types.ParseTime(sc, string(val.ToString()), b.tp.Tp, b.tp.Decimal)
if err != nil {
return res, false, errors.Trace(err)
}
if b.tp.Tp == mysql.TypeDate {
// Truncate hh:mm:ss part if the type is Date.
res.Time = types.FromDate(res.Time.Year(), res.Time.Month(), res.Time.Day(), 0, 0, 0, 0)
}
return res, false, errors.Trace(err)
}
type builtinCastDecimalAsDurationSig struct {
baseBuiltinFunc
}
func (b *builtinCastDecimalAsDurationSig) Clone() builtinFunc {
newSig := &builtinCastDecimalAsDurationSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
func (b *builtinCastDecimalAsDurationSig) evalDuration(row types.Row) (res types.Duration, isNull bool, err error) {
val, isNull, err := b.args[0].EvalDecimal(b.ctx, row)
if isNull || err != nil {
return res, false, errors.Trace(err)
}
res, err = types.ParseDuration(string(val.ToString()), b.tp.Decimal)
if types.ErrTruncatedWrongVal.Equal(err) {
err = b.ctx.GetSessionVars().StmtCtx.HandleTruncate(err)
// ZeroDuration of error ErrTruncatedWrongVal needs to be considered NULL.
if res == types.ZeroDuration {
return res, true, errors.Trace(err)
}
}
return res, false, errors.Trace(err)
}
type builtinCastStringAsStringSig struct {
baseBuiltinFunc
}
func (b *builtinCastStringAsStringSig) Clone() builtinFunc {
newSig := &builtinCastStringAsStringSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
func (b *builtinCastStringAsStringSig) evalString(row types.Row) (res string, isNull bool, err error) {
res, isNull, err = b.args[0].EvalString(b.ctx, row)
if isNull || err != nil {
return res, isNull, errors.Trace(err)
}
sc := b.ctx.GetSessionVars().StmtCtx
res, err = types.ProduceStrWithSpecifiedTp(res, b.tp, sc)
return res, false, errors.Trace(err)
}
type builtinCastStringAsIntSig struct {
baseBuiltinFunc
}
func (b *builtinCastStringAsIntSig) Clone() builtinFunc {
newSig := &builtinCastStringAsIntSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
// handleOverflow handles the overflow caused by cast string as int,
// see https://dev.mysql.com/doc/refman/5.7/en/out-of-range-and-overflow.html.
// When an out-of-range value is assigned to an integer column, MySQL stores the value representing the corresponding endpoint of the column data type range. If it is in select statement, it will return the
// endpoint value with a warning.
func (b *builtinCastStringAsIntSig) handleOverflow(origRes int64, origStr string, origErr error, isNegative bool) (res int64, err error) {
res, err = origRes, origErr
if err == nil {
return
}
sc := b.ctx.GetSessionVars().StmtCtx
if sc.InSelectStmt && types.ErrOverflow.Equal(origErr) {
if isNegative {
res = math.MinInt64
} else {
uval := uint64(math.MaxUint64)
res = int64(uval)
}
warnErr := types.ErrTruncatedWrongVal.GenByArgs("INTEGER", origStr)
err = sc.HandleOverflow(origErr, warnErr)
}
return
}
func (b *builtinCastStringAsIntSig) evalInt(row types.Row) (res int64, isNull bool, err error) {
if b.args[0].GetType().Hybrid() || IsBinaryLiteral(b.args[0]) {
return b.args[0].EvalInt(b.ctx, row)
}
val, isNull, err := b.args[0].EvalString(b.ctx, row)
if isNull || err != nil {
return res, isNull, errors.Trace(err)
}
val = strings.TrimSpace(val)
isNegative := false
if len(val) > 1 && val[0] == '-' { // negative number
isNegative = true
}
var ures uint64
sc := b.ctx.GetSessionVars().StmtCtx
if isNegative {
res, err = types.StrToInt(sc, val)
if err == nil {
// If overflow, don't append this warnings
sc.AppendWarning(types.ErrCastNegIntAsUnsigned)
}
} else {
ures, err = types.StrToUint(sc, val)
res = int64(ures)
if err == nil && !mysql.HasUnsignedFlag(b.tp.Flag) && ures > uint64(math.MaxInt64) {
sc.AppendWarning(types.ErrCastAsSignedOverflow)
}
}
res, err = b.handleOverflow(res, val, err, isNegative)
return res, false, errors.Trace(err)
}
type builtinCastStringAsRealSig struct {
baseBuiltinFunc
}
func (b *builtinCastStringAsRealSig) Clone() builtinFunc {
newSig := &builtinCastStringAsRealSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
func (b *builtinCastStringAsRealSig) evalReal(row types.Row) (res float64, isNull bool, err error) {
if IsBinaryLiteral(b.args[0]) {
return b.args[0].EvalReal(b.ctx, row)
}
val, isNull, err := b.args[0].EvalString(b.ctx, row)
if isNull || err != nil {
return res, isNull, errors.Trace(err)
}
sc := b.ctx.GetSessionVars().StmtCtx
res, err = types.StrToFloat(sc, val)
if err != nil {
return 0, false, errors.Trace(err)
}
res, err = types.ProduceFloatWithSpecifiedTp(res, b.tp, sc)
return res, false, errors.Trace(err)
}
type builtinCastStringAsDecimalSig struct {
baseBuiltinFunc
}
func (b *builtinCastStringAsDecimalSig) Clone() builtinFunc {
newSig := &builtinCastStringAsDecimalSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
func (b *builtinCastStringAsDecimalSig) evalDecimal(row types.Row) (res *types.MyDecimal, isNull bool, err error) {
if IsBinaryLiteral(b.args[0]) {
return b.args[0].EvalDecimal(b.ctx, row)
}
val, isNull, err := b.args[0].EvalString(b.ctx, row)
if isNull || err != nil {
return res, isNull, errors.Trace(err)
}
res = new(types.MyDecimal)
sc := b.ctx.GetSessionVars().StmtCtx
err = sc.HandleTruncate(res.FromString([]byte(val)))
if err != nil {
return res, false, errors.Trace(err)
}
res, err = types.ProduceDecWithSpecifiedTp(res, b.tp, sc)
return res, false, errors.Trace(err)
}
type builtinCastStringAsTimeSig struct {
baseBuiltinFunc
}
func (b *builtinCastStringAsTimeSig) Clone() builtinFunc {
newSig := &builtinCastStringAsTimeSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
func (b *builtinCastStringAsTimeSig) evalTime(row types.Row) (res types.Time, isNull bool, err error) {
val, isNull, err := b.args[0].EvalString(b.ctx, row)
if isNull || err != nil {
return res, isNull, errors.Trace(err)
}
sc := b.ctx.GetSessionVars().StmtCtx
res, err = types.ParseTime(sc, val, b.tp.Tp, b.tp.Decimal)
if err != nil {
return res, false, errors.Trace(err)
}
if b.tp.Tp == mysql.TypeDate {
// Truncate hh:mm:ss part if the type is Date.
res.Time = types.FromDate(res.Time.Year(), res.Time.Month(), res.Time.Day(), 0, 0, 0, 0)
}
return res, false, errors.Trace(err)
}
type builtinCastStringAsDurationSig struct {
baseBuiltinFunc
}
func (b *builtinCastStringAsDurationSig) Clone() builtinFunc {
newSig := &builtinCastStringAsDurationSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
func (b *builtinCastStringAsDurationSig) evalDuration(row types.Row) (res types.Duration, isNull bool, err error) {
val, isNull, err := b.args[0].EvalString(b.ctx, row)
if isNull || err != nil {
return res, isNull, errors.Trace(err)
}
res, err = types.ParseDuration(val, b.tp.Decimal)
if types.ErrTruncatedWrongVal.Equal(err) {
sc := b.ctx.GetSessionVars().StmtCtx
err = sc.HandleTruncate(err)
// ZeroDuration of error ErrTruncatedWrongVal needs to be considered NULL.
if res == types.ZeroDuration {
return res, true, errors.Trace(err)
}
}
return res, false, errors.Trace(err)
}
type builtinCastTimeAsTimeSig struct {
baseBuiltinFunc
}
func (b *builtinCastTimeAsTimeSig) Clone() builtinFunc {
newSig := &builtinCastTimeAsTimeSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
func (b *builtinCastTimeAsTimeSig) evalTime(row types.Row) (res types.Time, isNull bool, err error) {
res, isNull, err = b.args[0].EvalTime(b.ctx, row)
if isNull || err != nil {
return res, isNull, errors.Trace(err)
}
sc := b.ctx.GetSessionVars().StmtCtx
if res, err = res.Convert(sc, b.tp.Tp); err != nil {
return res, true, errors.Trace(err)
}
res, err = res.RoundFrac(sc, b.tp.Decimal)
if b.tp.Tp == mysql.TypeDate {
// Truncate hh:mm:ss part if the type is Date.
res.Time = types.FromDate(res.Time.Year(), res.Time.Month(), res.Time.Day(), 0, 0, 0, 0)
res.Type = b.tp.Tp
}
return res, false, errors.Trace(err)
}
type builtinCastTimeAsIntSig struct {
baseBuiltinFunc
}
func (b *builtinCastTimeAsIntSig) Clone() builtinFunc {
newSig := &builtinCastTimeAsIntSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
func (b *builtinCastTimeAsIntSig) evalInt(row types.Row) (res int64, isNull bool, err error) {
val, isNull, err := b.args[0].EvalTime(b.ctx, row)
if isNull || err != nil {
return res, isNull, errors.Trace(err)
}
sc := b.ctx.GetSessionVars().StmtCtx
t, err := val.RoundFrac(sc, types.DefaultFsp)
if err != nil {
return res, false, errors.Trace(err)
}
res, err = t.ToNumber().ToInt()
return res, false, errors.Trace(err)
}
type builtinCastTimeAsRealSig struct {
baseBuiltinFunc
}
func (b *builtinCastTimeAsRealSig) Clone() builtinFunc {
newSig := &builtinCastTimeAsRealSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
func (b *builtinCastTimeAsRealSig) evalReal(row types.Row) (res float64, isNull bool, err error) {
val, isNull, err := b.args[0].EvalTime(b.ctx, row)
if isNull || err != nil {
return res, isNull, errors.Trace(err)
}
res, err = val.ToNumber().ToFloat64()
return res, false, errors.Trace(err)
}
type builtinCastTimeAsDecimalSig struct {
baseBuiltinFunc
}
func (b *builtinCastTimeAsDecimalSig) Clone() builtinFunc {
newSig := &builtinCastTimeAsDecimalSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
func (b *builtinCastTimeAsDecimalSig) evalDecimal(row types.Row) (res *types.MyDecimal, isNull bool, err error) {
val, isNull, err := b.args[0].EvalTime(b.ctx, row)
if isNull || err != nil {
return res, isNull, errors.Trace(err)
}
sc := b.ctx.GetSessionVars().StmtCtx
res, err = types.ProduceDecWithSpecifiedTp(val.ToNumber(), b.tp, sc)
return res, false, errors.Trace(err)
}
type builtinCastTimeAsStringSig struct {
baseBuiltinFunc
}
func (b *builtinCastTimeAsStringSig) Clone() builtinFunc {
newSig := &builtinCastTimeAsStringSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
func (b *builtinCastTimeAsStringSig) evalString(row types.Row) (res string, isNull bool, err error) {
val, isNull, err := b.args[0].EvalTime(b.ctx, row)
if isNull || err != nil {
return res, isNull, errors.Trace(err)
}
sc := b.ctx.GetSessionVars().StmtCtx
res, err = types.ProduceStrWithSpecifiedTp(val.String(), b.tp, sc)
return res, false, errors.Trace(err)
}
type builtinCastTimeAsDurationSig struct {
baseBuiltinFunc
}
func (b *builtinCastTimeAsDurationSig) Clone() builtinFunc {
newSig := &builtinCastTimeAsDurationSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
func (b *builtinCastTimeAsDurationSig) evalDuration(row types.Row) (res types.Duration, isNull bool, err error) {
val, isNull, err := b.args[0].EvalTime(b.ctx, row)
if isNull || err != nil {
return res, isNull, errors.Trace(err)
}
res, err = val.ConvertToDuration()
if err != nil {
return res, false, errors.Trace(err)
}
res, err = res.RoundFrac(b.tp.Decimal)
return res, false, errors.Trace(err)
}
type builtinCastDurationAsDurationSig struct {
baseBuiltinFunc
}
func (b *builtinCastDurationAsDurationSig) Clone() builtinFunc {
newSig := &builtinCastDurationAsDurationSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
func (b *builtinCastDurationAsDurationSig) evalDuration(row types.Row) (res types.Duration, isNull bool, err error) {
res, isNull, err = b.args[0].EvalDuration(b.ctx, row)
if isNull || err != nil {
return res, isNull, errors.Trace(err)
}
res, err = res.RoundFrac(b.tp.Decimal)
return res, false, errors.Trace(err)
}
type builtinCastDurationAsIntSig struct {
baseBuiltinFunc
}
func (b *builtinCastDurationAsIntSig) Clone() builtinFunc {
newSig := &builtinCastDurationAsIntSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
func (b *builtinCastDurationAsIntSig) evalInt(row types.Row) (res int64, isNull bool, err error) {
val, isNull, err := b.args[0].EvalDuration(b.ctx, row)
if isNull || err != nil {
return res, isNull, errors.Trace(err)
}
dur, err := val.RoundFrac(types.DefaultFsp)
if err != nil {
return res, false, errors.Trace(err)
}
res, err = dur.ToNumber().ToInt()
return res, false, errors.Trace(err)
}
type builtinCastDurationAsRealSig struct {
baseBuiltinFunc
}
func (b *builtinCastDurationAsRealSig) Clone() builtinFunc {
newSig := &builtinCastDurationAsRealSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
func (b *builtinCastDurationAsRealSig) evalReal(row types.Row) (res float64, isNull bool, err error) {
val, isNull, err := b.args[0].EvalDuration(b.ctx, row)
if isNull || err != nil {
return res, isNull, errors.Trace(err)
}
res, err = val.ToNumber().ToFloat64()
return res, false, errors.Trace(err)
}
type builtinCastDurationAsDecimalSig struct {
baseBuiltinFunc
}
func (b *builtinCastDurationAsDecimalSig) Clone() builtinFunc {
newSig := &builtinCastDurationAsDecimalSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
func (b *builtinCastDurationAsDecimalSig) evalDecimal(row types.Row) (res *types.MyDecimal, isNull bool, err error) {
val, isNull, err := b.args[0].EvalDuration(b.ctx, row)
if isNull || err != nil {
return res, isNull, errors.Trace(err)
}
sc := b.ctx.GetSessionVars().StmtCtx
res, err = types.ProduceDecWithSpecifiedTp(val.ToNumber(), b.tp, sc)
return res, false, errors.Trace(err)
}
type builtinCastDurationAsStringSig struct {
baseBuiltinFunc
}
func (b *builtinCastDurationAsStringSig) Clone() builtinFunc {
newSig := &builtinCastDurationAsStringSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
func (b *builtinCastDurationAsStringSig) evalString(row types.Row) (res string, isNull bool, err error) {
val, isNull, err := b.args[0].EvalDuration(b.ctx, row)
if isNull || err != nil {
return res, isNull, errors.Trace(err)
}
sc := b.ctx.GetSessionVars().StmtCtx
res, err = types.ProduceStrWithSpecifiedTp(val.String(), b.tp, sc)
return res, false, errors.Trace(err)
}
type builtinCastDurationAsTimeSig struct {
baseBuiltinFunc
}
func (b *builtinCastDurationAsTimeSig) Clone() builtinFunc {
newSig := &builtinCastDurationAsTimeSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
func (b *builtinCastDurationAsTimeSig) evalTime(row types.Row) (res types.Time, isNull bool, err error) {
val, isNull, err := b.args[0].EvalDuration(b.ctx, row)
if isNull || err != nil {
return res, isNull, errors.Trace(err)
}
sc := b.ctx.GetSessionVars().StmtCtx
res, err = val.ConvertToTime(sc, b.tp.Tp)
if err != nil {
return res, false, errors.Trace(err)
}
res, err = res.RoundFrac(sc, b.tp.Decimal)
return res, false, errors.Trace(err)
}
type builtinCastJSONAsJSONSig struct {
baseBuiltinFunc
}
func (b *builtinCastJSONAsJSONSig) Clone() builtinFunc {
newSig := &builtinCastJSONAsJSONSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
func (b *builtinCastJSONAsJSONSig) evalJSON(row types.Row) (res json.BinaryJSON, isNull bool, err error) {
return b.args[0].EvalJSON(b.ctx, row)
}
type builtinCastJSONAsIntSig struct {
baseBuiltinFunc
}
func (b *builtinCastJSONAsIntSig) Clone() builtinFunc {
newSig := &builtinCastJSONAsIntSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
func (b *builtinCastJSONAsIntSig) evalInt(row types.Row) (res int64, isNull bool, err error) {
val, isNull, err := b.args[0].EvalJSON(b.ctx, row)
if isNull || err != nil {
return res, isNull, errors.Trace(err)
}
sc := b.ctx.GetSessionVars().StmtCtx
res, err = types.ConvertJSONToInt(sc, val, mysql.HasUnsignedFlag(b.tp.Flag))
return
}
type builtinCastJSONAsRealSig struct {
baseBuiltinFunc
}
func (b *builtinCastJSONAsRealSig) Clone() builtinFunc {
newSig := &builtinCastJSONAsRealSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
func (b *builtinCastJSONAsRealSig) evalReal(row types.Row) (res float64, isNull bool, err error) {
val, isNull, err := b.args[0].EvalJSON(b.ctx, row)
if isNull || err != nil {
return res, isNull, errors.Trace(err)
}
sc := b.ctx.GetSessionVars().StmtCtx
res, err = types.ConvertJSONToFloat(sc, val)
return
}
type builtinCastJSONAsDecimalSig struct {
baseBuiltinFunc
}
func (b *builtinCastJSONAsDecimalSig) Clone() builtinFunc {
newSig := &builtinCastJSONAsDecimalSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
func (b *builtinCastJSONAsDecimalSig) evalDecimal(row types.Row) (res *types.MyDecimal, isNull bool, err error) {
val, isNull, err := b.args[0].EvalJSON(b.ctx, row)
if isNull || err != nil {
return res, isNull, errors.Trace(err)
}
sc := b.ctx.GetSessionVars().StmtCtx
res, err = types.ConvertJSONToDecimal(sc, val)
if err != nil {
return res, false, errors.Trace(err)
}
res, err = types.ProduceDecWithSpecifiedTp(res, b.tp, sc)
return res, false, errors.Trace(err)
}
type builtinCastJSONAsStringSig struct {
baseBuiltinFunc
}
func (b *builtinCastJSONAsStringSig) Clone() builtinFunc {
newSig := &builtinCastJSONAsStringSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
func (b *builtinCastJSONAsStringSig) evalString(row types.Row) (res string, isNull bool, err error) {
val, isNull, err := b.args[0].EvalJSON(b.ctx, row)
if isNull || err != nil {
return res, isNull, errors.Trace(err)
}
return val.String(), false, nil
}
type builtinCastJSONAsTimeSig struct {
baseBuiltinFunc
}
func (b *builtinCastJSONAsTimeSig) Clone() builtinFunc {
newSig := &builtinCastJSONAsTimeSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
func (b *builtinCastJSONAsTimeSig) evalTime(row types.Row) (res types.Time, isNull bool, err error) {
val, isNull, err := b.args[0].EvalJSON(b.ctx, row)
if isNull || err != nil {
return res, isNull, errors.Trace(err)
}
s, err := val.Unquote()
if err != nil {
return res, false, errors.Trace(err)
}
sc := b.ctx.GetSessionVars().StmtCtx
res, err = types.ParseTime(sc, s, b.tp.Tp, b.tp.Decimal)
if err != nil {
return res, false, errors.Trace(err)
}
if b.tp.Tp == mysql.TypeDate {
// Truncate hh:mm:ss part if the type is Date.
res.Time = types.FromDate(res.Time.Year(), res.Time.Month(), res.Time.Day(), 0, 0, 0, 0)
}
return
}
type builtinCastJSONAsDurationSig struct {
baseBuiltinFunc
}
func (b *builtinCastJSONAsDurationSig) Clone() builtinFunc {
newSig := &builtinCastJSONAsDurationSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
func (b *builtinCastJSONAsDurationSig) evalDuration(row types.Row) (res types.Duration, isNull bool, err error) {
val, isNull, err := b.args[0].EvalJSON(b.ctx, row)
if isNull || err != nil {
return res, isNull, errors.Trace(err)
}
s, err := val.Unquote()
if err != nil {
return res, false, errors.Trace(err)
}
res, err = types.ParseDuration(s, b.tp.Decimal)
if types.ErrTruncatedWrongVal.Equal(err) {
sc := b.ctx.GetSessionVars().StmtCtx
err = sc.HandleTruncate(err)
}
return
}
// BuildCastFunction builds a CAST ScalarFunction from the Expression.
func BuildCastFunction(ctx sessionctx.Context, expr Expression, tp *types.FieldType) (res Expression) {
var fc functionClass
switch tp.EvalType() {
case types.ETInt:
fc = &castAsIntFunctionClass{baseFunctionClass{ast.Cast, 1, 1}, tp}
case types.ETDecimal:
fc = &castAsDecimalFunctionClass{baseFunctionClass{ast.Cast, 1, 1}, tp}
case types.ETReal:
fc = &castAsRealFunctionClass{baseFunctionClass{ast.Cast, 1, 1}, tp}
case types.ETDatetime, types.ETTimestamp:
fc = &castAsTimeFunctionClass{baseFunctionClass{ast.Cast, 1, 1}, tp}
case types.ETDuration:
fc = &castAsDurationFunctionClass{baseFunctionClass{ast.Cast, 1, 1}, tp}
case types.ETJson:
fc = &castAsJSONFunctionClass{baseFunctionClass{ast.Cast, 1, 1}, tp}
case types.ETString:
fc = &castAsStringFunctionClass{baseFunctionClass{ast.Cast, 1, 1}, tp}
}
f, err := fc.getFunction(ctx, []Expression{expr})
terror.Log(errors.Trace(err))
res = &ScalarFunction{
FuncName: model.NewCIStr(ast.Cast),
RetType: tp,
Function: f,
}
// We do not fold CAST if the eval type of this scalar function is ETJson
// since we may reset the flag of the field type of CastAsJson later which would
// affect the evaluation of it.
if tp.EvalType() != types.ETJson {
res = FoldConstant(res)
}
return res
}
// WrapWithCastAsInt wraps `expr` with `cast` if the return type
// of expr is not type int,
// otherwise, returns `expr` directly.
func WrapWithCastAsInt(ctx sessionctx.Context, expr Expression) Expression {
if expr.GetType().EvalType() == types.ETInt {
return expr
}
tp := types.NewFieldType(mysql.TypeLonglong)
tp.Flen, tp.Decimal = expr.GetType().Flen, 0
types.SetBinChsClnFlag(tp)
return BuildCastFunction(ctx, expr, tp)
}
// WrapWithCastAsReal wraps `expr` with `cast` if the return type
// of expr is not type real,
// otherwise, returns `expr` directly.
func WrapWithCastAsReal(ctx sessionctx.Context, expr Expression) Expression {
if expr.GetType().EvalType() == types.ETReal {
return expr
}
tp := types.NewFieldType(mysql.TypeDouble)
tp.Flen, tp.Decimal = mysql.MaxRealWidth, types.UnspecifiedLength
types.SetBinChsClnFlag(tp)
return BuildCastFunction(ctx, expr, tp)
}
// WrapWithCastAsDecimal wraps `expr` with `cast` if the return type
// of expr is not type decimal,
// otherwise, returns `expr` directly.
func WrapWithCastAsDecimal(ctx sessionctx.Context, expr Expression) Expression {
if expr.GetType().EvalType() == types.ETDecimal {
return expr
}
tp := types.NewFieldType(mysql.TypeNewDecimal)
tp.Flen, tp.Decimal = expr.GetType().Flen, types.UnspecifiedLength
types.SetBinChsClnFlag(tp)
return BuildCastFunction(ctx, expr, tp)
}
// WrapWithCastAsString wraps `expr` with `cast` if the return type
// of expr is not type string,
// otherwise, returns `expr` directly.
func WrapWithCastAsString(ctx sessionctx.Context, expr Expression) Expression {
if expr.GetType().EvalType() == types.ETString {
return expr
}
argLen := expr.GetType().Flen
if expr.GetType().EvalType() == types.ETInt {
argLen = mysql.MaxIntWidth
}
tp := types.NewFieldType(mysql.TypeVarString)
tp.Charset, tp.Collate = charset.CharsetUTF8, charset.CollationUTF8
tp.Flen, tp.Decimal = argLen, types.UnspecifiedLength
return BuildCastFunction(ctx, expr, tp)
}
// WrapWithCastAsTime wraps `expr` with `cast` if the return type
// of expr is not same as type of the specified `tp` ,
// otherwise, returns `expr` directly.
func WrapWithCastAsTime(ctx sessionctx.Context, expr Expression, tp *types.FieldType) Expression {
exprTp := expr.GetType().Tp
if tp.Tp == exprTp {
return expr
} else if (exprTp == mysql.TypeDate || exprTp == mysql.TypeTimestamp) && tp.Tp == mysql.TypeDatetime {
return expr
}
switch x := expr.GetType(); x.Tp {
case mysql.TypeDatetime, mysql.TypeTimestamp, mysql.TypeDate, mysql.TypeDuration:
tp.Decimal = x.Decimal
default:
tp.Decimal = types.MaxFsp
}
switch tp.Tp {
case mysql.TypeDate:
tp.Flen = mysql.MaxDateWidth
case mysql.TypeDatetime, mysql.TypeTimestamp:
tp.Flen = mysql.MaxDatetimeWidthNoFsp
if tp.Decimal > 0 {
tp.Flen = tp.Flen + 1 + tp.Decimal
}
}
types.SetBinChsClnFlag(tp)
return BuildCastFunction(ctx, expr, tp)
}
// WrapWithCastAsDuration wraps `expr` with `cast` if the return type
// of expr is not type duration,
// otherwise, returns `expr` directly.
func WrapWithCastAsDuration(ctx sessionctx.Context, expr Expression) Expression {
if expr.GetType().Tp == mysql.TypeDuration {
return expr
}
tp := types.NewFieldType(mysql.TypeDuration)
switch x := expr.GetType(); x.Tp {
case mysql.TypeDatetime, mysql.TypeTimestamp, mysql.TypeDate:
tp.Decimal = x.Decimal
default:
tp.Decimal = types.MaxFsp
}
tp.Flen = mysql.MaxDurationWidthNoFsp
if tp.Decimal > 0 {
tp.Flen = tp.Flen + 1 + tp.Decimal
}
return BuildCastFunction(ctx, expr, tp)
}
// WrapWithCastAsJSON wraps `expr` with `cast` if the return type
// of expr is not type json,
// otherwise, returns `expr` directly.
func WrapWithCastAsJSON(ctx sessionctx.Context, expr Expression) Expression {
if expr.GetType().Tp == mysql.TypeJSON && !mysql.HasParseToJSONFlag(expr.GetType().Flag) {
return expr
}
tp := &types.FieldType{
Tp: mysql.TypeJSON,
Flen: 12582912, // FIXME: Here the Flen is not trusted.
Decimal: 0,
Charset: charset.CharsetUTF8,
Collate: charset.CollationUTF8,
Flag: mysql.BinaryFlag,
}
return BuildCastFunction(ctx, expr, tp)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/zhoujin826/tidb.git
git@gitee.com:zhoujin826/tidb.git
zhoujin826
tidb
tidb
v2.0.9

搜索帮助