github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/dbs/memristed/memex/util_test.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 "reflect" 18 "testing" 19 "time" 20 21 "github.com/whtcorpsinc/check" 22 "github.com/whtcorpsinc/BerolinaSQL/ast" 23 "github.com/whtcorpsinc/BerolinaSQL/perceptron" 24 "github.com/whtcorpsinc/BerolinaSQL/allegrosql" 25 "github.com/whtcorpsinc/milevadb/stochastikctx" 26 "github.com/whtcorpsinc/milevadb/stochastikctx/stmtctx" 27 "github.com/whtcorpsinc/milevadb/types" 28 "github.com/whtcorpsinc/milevadb/types/json" 29 "github.com/whtcorpsinc/milevadb/soliton/chunk" 30 "github.com/whtcorpsinc/milevadb/soliton/codec" 31 "github.com/whtcorpsinc/milevadb/soliton/mock" 32 ) 33 34 var _ = check.Suite(&testUtilSuite{}) 35 36 type testUtilSuite struct { 37 } 38 39 func (s *testUtilSuite) checkPanic(f func()) (ret bool) { 40 defer func() { 41 if r := recover(); r != nil { 42 ret = true 43 } 44 }() 45 f() 46 return false 47 } 48 49 func (s *testUtilSuite) TestBaseBuiltin(c *check.C) { 50 ctx := mock.NewContext() 51 bf, err := newBaseBuiltinFuncWithTp(ctx, "", nil, types.ETTimestamp) 52 c.Assert(err, check.IsNil) 53 _, _, err = bf.evalInt(chunk.Event{}) 54 c.Assert(err, check.NotNil) 55 _, _, err = bf.evalReal(chunk.Event{}) 56 c.Assert(err, check.NotNil) 57 _, _, err = bf.evalString(chunk.Event{}) 58 c.Assert(err, check.NotNil) 59 _, _, err = bf.evalDecimal(chunk.Event{}) 60 c.Assert(err, check.NotNil) 61 _, _, err = bf.evalTime(chunk.Event{}) 62 c.Assert(err, check.NotNil) 63 _, _, err = bf.evalDuration(chunk.Event{}) 64 c.Assert(err, check.NotNil) 65 _, _, err = bf.evalJSON(chunk.Event{}) 66 c.Assert(err, check.NotNil) 67 } 68 69 func (s *testUtilSuite) TestClone(c *check.C) { 70 builtinFuncs := []builtinFunc{ 71 &builtinArithmeticPlusRealSig{}, &builtinArithmeticPlusDecimalSig{}, &builtinArithmeticPlusIntSig{}, &builtinArithmeticMinusRealSig{}, &builtinArithmeticMinusDecimalSig{}, 72 &builtinArithmeticMinusIntSig{}, &builtinArithmeticDivideRealSig{}, &builtinArithmeticDivideDecimalSig{}, &builtinArithmeticMultiplyRealSig{}, &builtinArithmeticMultiplyDecimalSig{}, 73 &builtinArithmeticMultiplyIntUnsignedSig{}, &builtinArithmeticMultiplyIntSig{}, &builtinArithmeticIntDivideIntSig{}, &builtinArithmeticIntDivideDecimalSig{}, &builtinArithmeticModIntSig{}, 74 &builtinArithmeticModRealSig{}, &builtinArithmeticModDecimalSig{}, &builtinCastIntAsIntSig{}, &builtinCastIntAsRealSig{}, &builtinCastIntAsStringSig{}, 75 &builtinCastIntAsDecimalSig{}, &builtinCastIntAsTimeSig{}, &builtinCastIntAsDurationSig{}, &builtinCastIntAsJSONSig{}, &builtinCastRealAsIntSig{}, 76 &builtinCastRealAsRealSig{}, &builtinCastRealAsStringSig{}, &builtinCastRealAsDecimalSig{}, &builtinCastRealAsTimeSig{}, &builtinCastRealAsDurationSig{}, 77 &builtinCastRealAsJSONSig{}, &builtinCastDecimalAsIntSig{}, &builtinCastDecimalAsRealSig{}, &builtinCastDecimalAsStringSig{}, &builtinCastDecimalAsDecimalSig{}, 78 &builtinCastDecimalAsTimeSig{}, &builtinCastDecimalAsDurationSig{}, &builtinCastDecimalAsJSONSig{}, &builtinCastStringAsIntSig{}, &builtinCastStringAsRealSig{}, 79 &builtinCastStringAsStringSig{}, &builtinCastStringAsDecimalSig{}, &builtinCastStringAsTimeSig{}, &builtinCastStringAsDurationSig{}, &builtinCastStringAsJSONSig{}, 80 &builtinCastTimeAsIntSig{}, &builtinCastTimeAsRealSig{}, &builtinCastTimeAsStringSig{}, &builtinCastTimeAsDecimalSig{}, &builtinCastTimeAsTimeSig{}, 81 &builtinCastTimeAsDurationSig{}, &builtinCastTimeAsJSONSig{}, &builtinCastDurationAsIntSig{}, &builtinCastDurationAsRealSig{}, &builtinCastDurationAsStringSig{}, 82 &builtinCastDurationAsDecimalSig{}, &builtinCastDurationAsTimeSig{}, &builtinCastDurationAsDurationSig{}, &builtinCastDurationAsJSONSig{}, &builtinCastJSONAsIntSig{}, 83 &builtinCastJSONAsRealSig{}, &builtinCastJSONAsStringSig{}, &builtinCastJSONAsDecimalSig{}, &builtinCastJSONAsTimeSig{}, &builtinCastJSONAsDurationSig{}, 84 &builtinCastJSONAsJSONSig{}, &builtinCoalesceIntSig{}, &builtinCoalesceRealSig{}, &builtinCoalesceDecimalSig{}, &builtinCoalesceStringSig{}, 85 &builtinCoalesceTimeSig{}, &builtinCoalesceDurationSig{}, &builtinGreatestIntSig{}, &builtinGreatestRealSig{}, &builtinGreatestDecimalSig{}, 86 &builtinGreatestStringSig{}, &builtinGreatestTimeSig{}, &builtinLeastIntSig{}, &builtinLeastRealSig{}, &builtinLeastDecimalSig{}, 87 &builtinLeastStringSig{}, &builtinLeastTimeSig{}, &builtinIntervalIntSig{}, &builtinIntervalRealSig{}, &builtinLTIntSig{}, 88 &builtinLTRealSig{}, &builtinLTDecimalSig{}, &builtinLTStringSig{}, &builtinLTDurationSig{}, &builtinLTTimeSig{}, 89 &builtinLEIntSig{}, &builtinLERealSig{}, &builtinLEDecimalSig{}, &builtinLEStringSig{}, &builtinLEDurationSig{}, 90 &builtinLETimeSig{}, &builtinGTIntSig{}, &builtinGTRealSig{}, &builtinGTDecimalSig{}, &builtinGTStringSig{}, 91 &builtinGTTimeSig{}, &builtinGTDurationSig{}, &builtinGEIntSig{}, &builtinGERealSig{}, &builtinGEDecimalSig{}, 92 &builtinGEStringSig{}, &builtinGETimeSig{}, &builtinGEDurationSig{}, &builtinNEIntSig{}, &builtinNERealSig{}, 93 &builtinNEDecimalSig{}, &builtinNEStringSig{}, &builtinNETimeSig{}, &builtinNEDurationSig{}, &builtinNullEQIntSig{}, 94 &builtinNullEQRealSig{}, &builtinNullEQDecimalSig{}, &builtinNullEQStringSig{}, &builtinNullEQTimeSig{}, &builtinNullEQDurationSig{}, 95 &builtinCaseWhenIntSig{}, &builtinCaseWhenRealSig{}, &builtinCaseWhenDecimalSig{}, &builtinCaseWhenStringSig{}, &builtinCaseWhenTimeSig{}, 96 &builtinCaseWhenDurationSig{}, &builtinIfNullIntSig{}, &builtinIfNullRealSig{}, &builtinIfNullDecimalSig{}, &builtinIfNullStringSig{}, 97 &builtinIfNullTimeSig{}, &builtinIfNullDurationSig{}, &builtinIfNullJSONSig{}, &builtinIfIntSig{}, &builtinIfRealSig{}, 98 &builtinIfDecimalSig{}, &builtinIfStringSig{}, &builtinIfTimeSig{}, &builtinIfDurationSig{}, &builtinIfJSONSig{}, 99 &builtinAesDecryptSig{}, &builtinAesDecryptIVSig{}, &builtinAesEncryptSig{}, &builtinAesEncryptIVSig{}, &builtinCompressSig{}, 100 &builtinMD5Sig{}, &builtinPasswordSig{}, &builtinRandomBytesSig{}, &builtinSHA1Sig{}, &builtinSHA2Sig{}, 101 &builtinUncompressSig{}, &builtinUncompressedLengthSig{}, &builtinDatabaseSig{}, &builtinFoundEventsSig{}, &builtinCurrentUserSig{}, 102 &builtinUserSig{}, &builtinConnectionIDSig{}, &builtinLastInsertIDSig{}, &builtinLastInsertIDWithIDSig{}, &builtinVersionSig{}, 103 &builtinMilevaDBVersionSig{}, &builtinEventCountSig{}, &builtinJSONTypeSig{}, &builtinJSONQuoteSig{}, &builtinJSONUnquoteSig{}, 104 &builtinJSONArraySig{}, &builtinJSONArrayAppendSig{}, &builtinJSONObjectSig{}, &builtinJSONExtractSig{}, &builtinJSONSetSig{}, 105 &builtinJSONInsertSig{}, &builtinJSONReplaceSig{}, &builtinJSONRemoveSig{}, &builtinJSONMergeSig{}, &builtinJSONContainsSig{}, 106 &builtinJSONStorageSizeSig{}, &builtinJSONDepthSig{}, &builtinJSONSearchSig{}, &builtinJSONKeysSig{}, &builtinJSONKeys2ArgsSig{}, &builtinJSONLengthSig{}, 107 &builtinLikeSig{}, &builtinRegexpSig{}, &builtinRegexpUTF8Sig{}, &builtinAbsRealSig{}, &builtinAbsIntSig{}, 108 &builtinAbsUIntSig{}, &builtinAbsDecSig{}, &builtinRoundRealSig{}, &builtinRoundIntSig{}, &builtinRoundDecSig{}, 109 &builtinRoundWithFracRealSig{}, &builtinRoundWithFracIntSig{}, &builtinRoundWithFracDecSig{}, &builtinCeilRealSig{}, &builtinCeilIntToDecSig{}, 110 &builtinCeilIntToIntSig{}, &builtinCeilDecToIntSig{}, &builtinCeilDecToDecSig{}, &builtinFloorRealSig{}, &builtinFloorIntToDecSig{}, 111 &builtinFloorIntToIntSig{}, &builtinFloorDecToIntSig{}, &builtinFloorDecToDecSig{}, &builtinLog1ArgSig{}, &builtinLog2ArgsSig{}, 112 &builtinLog2Sig{}, &builtinLog10Sig{}, &builtinRandSig{}, &builtinRandWithSeedFirstGenSig{}, &builtinPowSig{}, 113 &builtinConvSig{}, &builtinCRC32Sig{}, &builtinSignSig{}, &builtinSqrtSig{}, &builtinAcosSig{}, 114 &builtinAsinSig{}, &builtinAtan1ArgSig{}, &builtinAtan2ArgsSig{}, &builtinCosSig{}, &builtinCotSig{}, 115 &builtinDegreesSig{}, &builtinExpSig{}, &builtinPISig{}, &builtinRadiansSig{}, &builtinSinSig{}, 116 &builtinTanSig{}, &builtinTruncateIntSig{}, &builtinTruncateRealSig{}, &builtinTruncateDecimalSig{}, &builtinTruncateUintSig{}, 117 &builtinSleepSig{}, &builtinLockSig{}, &builtinReleaseLockSig{}, &builtinDecimalAnyValueSig{}, &builtinDurationAnyValueSig{}, 118 &builtinIntAnyValueSig{}, &builtinJSONAnyValueSig{}, &builtinRealAnyValueSig{}, &builtinStringAnyValueSig{}, &builtinTimeAnyValueSig{}, 119 &builtinInetAtonSig{}, &builtinInetNtoaSig{}, &builtinInet6AtonSig{}, &builtinInet6NtoaSig{}, &builtinIsIPv4Sig{}, 120 &builtinIsIPv4CompatSig{}, &builtinIsIPv4MappedSig{}, &builtinIsIPv6Sig{}, &builtinUUIDSig{}, &builtinNameConstIntSig{}, 121 &builtinNameConstRealSig{}, &builtinNameConstDecimalSig{}, &builtinNameConstTimeSig{}, &builtinNameConstDurationSig{}, &builtinNameConstStringSig{}, 122 &builtinNameConstJSONSig{}, &builtinLogicAndSig{}, &builtinLogicOrSig{}, &builtinLogicXorSig{}, &builtinRealIsTrueSig{}, 123 &builtinDecimalIsTrueSig{}, &builtinIntIsTrueSig{}, &builtinRealIsFalseSig{}, &builtinDecimalIsFalseSig{}, &builtinIntIsFalseSig{}, 124 &builtinUnaryMinusIntSig{}, &builtinDecimalIsNullSig{}, &builtinDurationIsNullSig{}, &builtinIntIsNullSig{}, &builtinRealIsNullSig{}, 125 &builtinStringIsNullSig{}, &builtinTimeIsNullSig{}, &builtinUnaryNotRealSig{}, &builtinUnaryNotDecimalSig{}, &builtinUnaryNotIntSig{}, &builtinSleepSig{}, &builtinInIntSig{}, 126 &builtinInStringSig{}, &builtinInDecimalSig{}, &builtinInRealSig{}, &builtinInTimeSig{}, &builtinInDurationSig{}, 127 &builtinInJSONSig{}, &builtinEventSig{}, &builtinSetVarSig{}, &builtinGetVarSig{}, &builtinLockSig{}, 128 &builtinReleaseLockSig{}, &builtinValuesIntSig{}, &builtinValuesRealSig{}, &builtinValuesDecimalSig{}, &builtinValuesStringSig{}, 129 &builtinValuesTimeSig{}, &builtinValuesDurationSig{}, &builtinValuesJSONSig{}, &builtinBitCountSig{}, &builtinGetParamStringSig{}, 130 &builtinLengthSig{}, &builtinASCIISig{}, &builtinConcatSig{}, &builtinConcatWSSig{}, &builtinLeftSig{}, 131 &builtinLeftUTF8Sig{}, &builtinRightSig{}, &builtinRightUTF8Sig{}, &builtinRepeatSig{}, &builtinLowerSig{}, 132 &builtinReverseUTF8Sig{}, &builtinReverseSig{}, &builtinSpaceSig{}, &builtinUpperSig{}, &builtinStrcmpSig{}, 133 &builtinReplaceSig{}, &builtinConvertSig{}, &builtinSubstring2ArgsSig{}, &builtinSubstring3ArgsSig{}, &builtinSubstring2ArgsUTF8Sig{}, 134 &builtinSubstring3ArgsUTF8Sig{}, &builtinSubstringIndexSig{}, &builtinLocate2ArgsUTF8Sig{}, &builtinLocate3ArgsUTF8Sig{}, &builtinLocate2ArgsSig{}, 135 &builtinLocate3ArgsSig{}, &builtinHexStrArgSig{}, &builtinHexIntArgSig{}, &builtinUnHexSig{}, &builtinTrim1ArgSig{}, 136 &builtinTrim2ArgsSig{}, &builtinTrim3ArgsSig{}, &builtinLTrimSig{}, &builtinRTrimSig{}, &builtinLpadUTF8Sig{}, 137 &builtinLpadSig{}, &builtinRpadUTF8Sig{}, &builtinRpadSig{}, &builtinBitLengthSig{}, &builtinCharSig{}, 138 &builtinCharLengthUTF8Sig{}, &builtinFindInSetSig{}, &builtinMakeSetSig{}, &builtinOctIntSig{}, &builtinOctStringSig{}, 139 &builtinOrdSig{}, &builtinQuoteSig{}, &builtinBinSig{}, &builtinEltSig{}, &builtinExportSet3ArgSig{}, 140 &builtinExportSet4ArgSig{}, &builtinExportSet5ArgSig{}, &builtinFormatWithLocaleSig{}, &builtinFormatSig{}, &builtinFromBase64Sig{}, 141 &builtinToBase64Sig{}, &builtinInsertSig{}, &builtinInsertUTF8Sig{}, &builtinInstrUTF8Sig{}, &builtinInstrSig{}, 142 &builtinFieldRealSig{}, &builtinFieldIntSig{}, &builtinFieldStringSig{}, &builtinDateSig{}, &builtinDateLiteralSig{}, 143 &builtinDateDiffSig{}, &builtinNullTimeDiffSig{}, &builtinTimeStringTimeDiffSig{}, &builtinDurationStringTimeDiffSig{}, &builtinDurationDurationTimeDiffSig{}, 144 &builtinStringTimeTimeDiffSig{}, &builtinStringDurationTimeDiffSig{}, &builtinStringStringTimeDiffSig{}, &builtinTimeTimeTimeDiffSig{}, &builtinDateFormatSig{}, 145 &builtinHourSig{}, &builtinMinuteSig{}, &builtinSecondSig{}, &builtinMicroSecondSig{}, &builtinMonthSig{}, 146 &builtinMonthNameSig{}, &builtinNowWithArgSig{}, &builtinNowWithoutArgSig{}, &builtinDayNameSig{}, &builtinDayOfMonthSig{}, 147 &builtinDayOfWeekSig{}, &builtinDayOfYearSig{}, &builtinWeekWithModeSig{}, &builtinWeekWithoutModeSig{}, &builtinWeekDaySig{}, 148 &builtinWeekOfYearSig{}, &builtinYearSig{}, &builtinYearWeekWithModeSig{}, &builtinYearWeekWithoutModeSig{}, &builtinGetFormatSig{}, 149 &builtinSysDateWithFspSig{}, &builtinSysDateWithoutFspSig{}, &builtinCurrentDateSig{}, &builtinCurrentTime0ArgSig{}, &builtinCurrentTime1ArgSig{}, 150 &builtinTimeSig{}, &builtinTimeLiteralSig{}, &builtinUTCDateSig{}, &builtinUTCTimestampWithArgSig{}, &builtinUTCTimestampWithoutArgSig{}, 151 &builtinAddDatetimeAndDurationSig{}, &builtinAddDatetimeAndStringSig{}, &builtinAddTimeDateTimeNullSig{}, &builtinAddStringAndDurationSig{}, &builtinAddStringAndStringSig{}, 152 &builtinAddTimeStringNullSig{}, &builtinAddDurationAndDurationSig{}, &builtinAddDurationAndStringSig{}, &builtinAddTimeDurationNullSig{}, &builtinAddDateAndDurationSig{}, 153 &builtinAddDateAndStringSig{}, &builtinSubDatetimeAndDurationSig{}, &builtinSubDatetimeAndStringSig{}, &builtinSubTimeDateTimeNullSig{}, &builtinSubStringAndDurationSig{}, 154 &builtinSubStringAndStringSig{}, &builtinSubTimeStringNullSig{}, &builtinSubDurationAndDurationSig{}, &builtinSubDurationAndStringSig{}, &builtinSubTimeDurationNullSig{}, 155 &builtinSubDateAndDurationSig{}, &builtinSubDateAndStringSig{}, &builtinUnixTimestampCurrentSig{}, &builtinUnixTimestampIntSig{}, &builtinUnixTimestamFIDelecSig{}, 156 &builtinConvertTzSig{}, &builtinMakeDateSig{}, &builtinMakeTimeSig{}, &builtinPeriodAddSig{}, &builtinPeriodDiffSig{}, 157 &builtinQuarterSig{}, &builtinSecToTimeSig{}, &builtinTimeToSecSig{}, &builtinTimestampAddSig{}, &builtinToDaysSig{}, 158 &builtinToSecondsSig{}, &builtinUTCTimeWithArgSig{}, &builtinUTCTimeWithoutArgSig{}, &builtinTimestamp1ArgSig{}, &builtinTimestamp2ArgsSig{}, 159 &builtinTimestampLiteralSig{}, &builtinLastDaySig{}, &builtinStrToDateDateSig{}, &builtinStrToDateDatetimeSig{}, &builtinStrToDateDurationSig{}, 160 &builtinFromUnixTime1ArgSig{}, &builtinFromUnixTime2ArgSig{}, &builtinExtractDatetimeSig{}, &builtinExtractDurationSig{}, &builtinAddDateStringStringSig{}, 161 &builtinAddDateStringIntSig{}, &builtinAddDateStringRealSig{}, &builtinAddDateStringDecimalSig{}, &builtinAddDateIntStringSig{}, &builtinAddDateIntIntSig{}, 162 &builtinAddDateIntRealSig{}, &builtinAddDateIntDecimalSig{}, &builtinAddDateDatetimeStringSig{}, &builtinAddDateDatetimeIntSig{}, &builtinAddDateDatetimeRealSig{}, 163 &builtinAddDateDatetimeDecimalSig{}, &builtinSubDateStringStringSig{}, &builtinSubDateStringIntSig{}, &builtinSubDateStringRealSig{}, &builtinSubDateStringDecimalSig{}, 164 &builtinSubDateIntStringSig{}, &builtinSubDateIntIntSig{}, &builtinSubDateIntRealSig{}, &builtinSubDateIntDecimalSig{}, &builtinSubDateDatetimeStringSig{}, 165 &builtinSubDateDatetimeIntSig{}, &builtinSubDateDatetimeRealSig{}, &builtinSubDateDatetimeDecimalSig{}, 166 } 167 for _, f := range builtinFuncs { 168 cf := f.Clone() 169 c.Assert(reflect.TypeOf(f) == reflect.TypeOf(cf), check.IsTrue) 170 } 171 } 172 173 func (s *testUtilSuite) TestGetUint64FromConstant(c *check.C) { 174 con := &Constant{ 175 Value: types.NewCauset(nil), 176 } 177 _, isNull, ok := GetUint64FromConstant(con) 178 c.Assert(ok, check.IsTrue) 179 c.Assert(isNull, check.IsTrue) 180 181 con = &Constant{ 182 Value: types.NewIntCauset(-1), 183 } 184 _, _, ok = GetUint64FromConstant(con) 185 c.Assert(ok, check.IsFalse) 186 187 con.Value = types.NewIntCauset(1) 188 num, isNull, ok := GetUint64FromConstant(con) 189 c.Assert(ok, check.IsTrue) 190 c.Assert(isNull, check.IsFalse) 191 c.Assert(num, check.Equals, uint64(1)) 192 193 con.Value = types.NewUintCauset(1) 194 num, _, _ = GetUint64FromConstant(con) 195 c.Assert(num, check.Equals, uint64(1)) 196 197 con.DeferredExpr = &Constant{Value: types.NewIntCauset(1)} 198 num, _, _ = GetUint64FromConstant(con) 199 c.Assert(num, check.Equals, uint64(1)) 200 201 ctx := mock.NewContext() 202 ctx.GetStochastikVars().PreparedParams = []types.Causet{ 203 types.NewUintCauset(100), 204 } 205 con.ParamMarker = &ParamMarker{order: 0, ctx: ctx} 206 num, _, _ = GetUint64FromConstant(con) 207 c.Assert(num, check.Equals, uint64(100)) 208 } 209 210 func (s *testUtilSuite) TestSetExprDeferredCausetInOperand(c *check.C) { 211 defCaus := &DeferredCauset{RetType: newIntFieldType()} 212 c.Assert(setExprDeferredCausetInOperand(defCaus).(*DeferredCauset).InOperand, check.IsTrue) 213 214 f, err := funcs[ast.Abs].getFunction(mock.NewContext(), []Expression{defCaus}) 215 c.Assert(err, check.IsNil) 216 fun := &ScalarFunction{Function: f} 217 setExprDeferredCausetInOperand(fun) 218 c.Assert(f.getArgs()[0].(*DeferredCauset).InOperand, check.IsTrue) 219 } 220 221 func (s testUtilSuite) TestPopEventFirstArg(c *check.C) { 222 c1, c2, c3 := &DeferredCauset{RetType: newIntFieldType()}, &DeferredCauset{RetType: newIntFieldType()}, &DeferredCauset{RetType: newIntFieldType()} 223 f, err := funcs[ast.EventFunc].getFunction(mock.NewContext(), []Expression{c1, c2, c3}) 224 c.Assert(err, check.IsNil) 225 fun := &ScalarFunction{Function: f, FuncName: perceptron.NewCIStr(ast.EventFunc), RetType: newIntFieldType()} 226 fun2, err := PopEventFirstArg(mock.NewContext(), fun) 227 c.Assert(err, check.IsNil) 228 c.Assert(len(fun2.(*ScalarFunction).GetArgs()), check.Equals, 2) 229 } 230 231 func (s testUtilSuite) TestGetStrIntFromConstant(c *check.C) { 232 defCaus := &DeferredCauset{} 233 _, _, err := GetStringFromConstant(mock.NewContext(), defCaus) 234 c.Assert(err, check.NotNil) 235 236 con := &Constant{RetType: &types.FieldType{Tp: allegrosql.TypeNull}} 237 _, isNull, err := GetStringFromConstant(mock.NewContext(), con) 238 c.Assert(err, check.IsNil) 239 c.Assert(isNull, check.IsTrue) 240 241 con = &Constant{RetType: newIntFieldType(), Value: types.NewIntCauset(1)} 242 ret, _, _ := GetStringFromConstant(mock.NewContext(), con) 243 c.Assert(ret, check.Equals, "1") 244 245 con = &Constant{RetType: &types.FieldType{Tp: allegrosql.TypeNull}} 246 _, isNull, _ = GetIntFromConstant(mock.NewContext(), con) 247 c.Assert(isNull, check.IsTrue) 248 249 con = &Constant{RetType: newStringFieldType(), Value: types.NewStringCauset("abc")} 250 _, isNull, _ = GetIntFromConstant(mock.NewContext(), con) 251 c.Assert(isNull, check.IsTrue) 252 253 con = &Constant{RetType: newStringFieldType(), Value: types.NewStringCauset("123")} 254 num, _, _ := GetIntFromConstant(mock.NewContext(), con) 255 c.Assert(num, check.Equals, 123) 256 } 257 258 func (s *testUtilSuite) TestSubstituteCorDefCaus2Constant(c *check.C) { 259 ctx := mock.NewContext() 260 corDefCaus1 := &CorrelatedDeferredCauset{Data: &NewOne().Value} 261 corDefCaus1.RetType = types.NewFieldType(allegrosql.TypeLonglong) 262 corDefCaus2 := &CorrelatedDeferredCauset{Data: &NewOne().Value} 263 corDefCaus2.RetType = types.NewFieldType(allegrosql.TypeLonglong) 264 cast := BuildCastFunction(ctx, corDefCaus1, types.NewFieldType(allegrosql.TypeLonglong)) 265 plus := newFunction(ast.Plus, cast, corDefCaus2) 266 plus2 := newFunction(ast.Plus, plus, NewOne()) 267 ans1 := &Constant{Value: types.NewIntCauset(3), RetType: types.NewFieldType(allegrosql.TypeLonglong)} 268 ret, err := SubstituteCorDefCaus2Constant(plus2) 269 c.Assert(err, check.IsNil) 270 c.Assert(ret.Equal(ctx, ans1), check.IsTrue) 271 defCaus1 := &DeferredCauset{Index: 1, RetType: types.NewFieldType(allegrosql.TypeLonglong)} 272 ret, err = SubstituteCorDefCaus2Constant(defCaus1) 273 c.Assert(err, check.IsNil) 274 ans2 := defCaus1 275 c.Assert(ret.Equal(ctx, ans2), check.IsTrue) 276 plus3 := newFunction(ast.Plus, plus2, defCaus1) 277 ret, err = SubstituteCorDefCaus2Constant(plus3) 278 c.Assert(err, check.IsNil) 279 ans3 := newFunction(ast.Plus, ans1, defCaus1) 280 c.Assert(ret.Equal(ctx, ans3), check.IsTrue) 281 } 282 283 func (s *testUtilSuite) TestPushDownNot(c *check.C) { 284 ctx := mock.NewContext() 285 defCaus := &DeferredCauset{Index: 1, RetType: types.NewFieldType(allegrosql.TypeLonglong)} 286 // !((a=1||a=1)&&a=1) 287 eqFunc := newFunction(ast.EQ, defCaus, NewOne()) 288 orFunc := newFunction(ast.LogicOr, eqFunc, eqFunc) 289 andFunc := newFunction(ast.LogicAnd, orFunc, eqFunc) 290 notFunc := newFunction(ast.UnaryNot, andFunc) 291 // (a!=1&&a!=1)||a=1 292 neFunc := newFunction(ast.NE, defCaus, NewOne()) 293 andFunc2 := newFunction(ast.LogicAnd, neFunc, neFunc) 294 orFunc2 := newFunction(ast.LogicOr, andFunc2, neFunc) 295 notFuncCopy := notFunc.Clone() 296 ret := PushDownNot(ctx, notFunc) 297 c.Assert(ret.Equal(ctx, orFunc2), check.IsTrue) 298 c.Assert(notFunc.Equal(ctx, notFuncCopy), check.IsTrue) 299 300 // issue 15725 301 // (not not a) should be optimized to (a is true) 302 notFunc = newFunction(ast.UnaryNot, defCaus) 303 notFunc = newFunction(ast.UnaryNot, notFunc) 304 ret = PushDownNot(ctx, notFunc) 305 c.Assert(ret.Equal(ctx, newFunction(ast.IsTruthWithNull, defCaus)), check.IsTrue) 306 307 // (not not (a+1)) should be optimized to (a+1 is true) 308 plusFunc := newFunction(ast.Plus, defCaus, NewOne()) 309 notFunc = newFunction(ast.UnaryNot, plusFunc) 310 notFunc = newFunction(ast.UnaryNot, notFunc) 311 ret = PushDownNot(ctx, notFunc) 312 c.Assert(ret.Equal(ctx, newFunction(ast.IsTruthWithNull, plusFunc)), check.IsTrue) 313 314 // (not not not a) should be optimized to (not (a is true)) 315 notFunc = newFunction(ast.UnaryNot, defCaus) 316 notFunc = newFunction(ast.UnaryNot, notFunc) 317 notFunc = newFunction(ast.UnaryNot, notFunc) 318 ret = PushDownNot(ctx, notFunc) 319 c.Assert(ret.Equal(ctx, newFunction(ast.UnaryNot, newFunction(ast.IsTruthWithNull, defCaus))), check.IsTrue) 320 321 // (not not not not a) should be optimized to (a is true) 322 notFunc = newFunction(ast.UnaryNot, defCaus) 323 notFunc = newFunction(ast.UnaryNot, notFunc) 324 notFunc = newFunction(ast.UnaryNot, notFunc) 325 notFunc = newFunction(ast.UnaryNot, notFunc) 326 ret = PushDownNot(ctx, notFunc) 327 c.Assert(ret.Equal(ctx, newFunction(ast.IsTruthWithNull, defCaus)), check.IsTrue) 328 } 329 330 func (s *testUtilSuite) TestFilter(c *check.C) { 331 conditions := []Expression{ 332 newFunction(ast.EQ, newDeferredCauset(0), newDeferredCauset(1)), 333 newFunction(ast.EQ, newDeferredCauset(1), newDeferredCauset(2)), 334 newFunction(ast.LogicOr, newLonglong(1), newDeferredCauset(0)), 335 } 336 result := make([]Expression, 0, 5) 337 result = Filter(result, conditions, isLogicOrFunction) 338 c.Assert(result, check.HasLen, 1) 339 } 340 341 func (s *testUtilSuite) TestFilterOutInPlace(c *check.C) { 342 conditions := []Expression{ 343 newFunction(ast.EQ, newDeferredCauset(0), newDeferredCauset(1)), 344 newFunction(ast.EQ, newDeferredCauset(1), newDeferredCauset(2)), 345 newFunction(ast.LogicOr, newLonglong(1), newDeferredCauset(0)), 346 } 347 remained, filtered := FilterOutInPlace(conditions, isLogicOrFunction) 348 c.Assert(len(remained), check.Equals, 2) 349 c.Assert(remained[0].(*ScalarFunction).FuncName.L, check.Equals, "eq") 350 c.Assert(remained[1].(*ScalarFunction).FuncName.L, check.Equals, "eq") 351 c.Assert(len(filtered), check.Equals, 1) 352 c.Assert(filtered[0].(*ScalarFunction).FuncName.L, check.Equals, "or") 353 } 354 355 func (s *testUtilSuite) TestHashGroupKey(c *check.C) { 356 ctx := mock.NewContext() 357 sc := &stmtctx.StatementContext{TimeZone: time.Local} 358 eTypes := []types.EvalType{types.ETInt, types.ETReal, types.ETDecimal, types.ETString, types.ETTimestamp, types.ETDatetime, types.ETDuration} 359 tNames := []string{"int", "real", "decimal", "string", "timestamp", "datetime", "duration"} 360 for i := 0; i < len(tNames); i++ { 361 ft := eType2FieldType(eTypes[i]) 362 if eTypes[i] == types.ETDecimal { 363 ft.Flen = 0 364 } 365 defCausExpr := &DeferredCauset{Index: 0, RetType: ft} 366 input := chunk.New([]*types.FieldType{ft}, 1024, 1024) 367 fillDeferredCausetWithGener(eTypes[i], input, 0, nil) 368 defCausBuf := chunk.NewDeferredCauset(ft, 1024) 369 bufs := make([][]byte, 1024) 370 for j := 0; j < 1024; j++ { 371 bufs[j] = bufs[j][:0] 372 } 373 var err error 374 err = EvalExpr(ctx, defCausExpr, input, defCausBuf) 375 if err != nil { 376 c.Fatal(err) 377 } 378 if bufs, err = codec.HashGroupKey(sc, 1024, defCausBuf, bufs, ft); err != nil { 379 c.Fatal(err) 380 } 381 382 var buf []byte 383 for j := 0; j < input.NumEvents(); j++ { 384 d, err := defCausExpr.Eval(input.GetEvent(j)) 385 if err != nil { 386 c.Fatal(err) 387 } 388 buf, err = codec.EncodeValue(sc, buf[:0], d) 389 if err != nil { 390 c.Fatal(err) 391 } 392 c.Assert(string(bufs[j]), check.Equals, string(buf)) 393 } 394 } 395 } 396 397 func isLogicOrFunction(e Expression) bool { 398 if f, ok := e.(*ScalarFunction); ok { 399 return f.FuncName.L == ast.LogicOr 400 } 401 return false 402 } 403 404 func (s *testUtilSuite) TestDisableParseJSONFlag4Expr(c *check.C) { 405 var expr Expression 406 expr = &DeferredCauset{RetType: newIntFieldType()} 407 ft := expr.GetType() 408 ft.Flag |= allegrosql.ParseToJSONFlag 409 DisableParseJSONFlag4Expr(expr) 410 c.Assert(allegrosql.HasParseToJSONFlag(ft.Flag), check.IsTrue) 411 412 expr = &CorrelatedDeferredCauset{DeferredCauset: DeferredCauset{RetType: newIntFieldType()}} 413 ft = expr.GetType() 414 ft.Flag |= allegrosql.ParseToJSONFlag 415 DisableParseJSONFlag4Expr(expr) 416 c.Assert(allegrosql.HasParseToJSONFlag(ft.Flag), check.IsTrue) 417 418 expr = &ScalarFunction{RetType: newIntFieldType()} 419 ft = expr.GetType() 420 ft.Flag |= allegrosql.ParseToJSONFlag 421 DisableParseJSONFlag4Expr(expr) 422 c.Assert(allegrosql.HasParseToJSONFlag(ft.Flag), check.IsFalse) 423 } 424 425 func BenchmarkExtractDeferredCausets(b *testing.B) { 426 conditions := []Expression{ 427 newFunction(ast.EQ, newDeferredCauset(0), newDeferredCauset(1)), 428 newFunction(ast.EQ, newDeferredCauset(1), newDeferredCauset(2)), 429 newFunction(ast.EQ, newDeferredCauset(2), newDeferredCauset(3)), 430 newFunction(ast.EQ, newDeferredCauset(3), newLonglong(1)), 431 newFunction(ast.LogicOr, newLonglong(1), newDeferredCauset(0)), 432 } 433 expr := ComposeCNFCondition(mock.NewContext(), conditions...) 434 435 b.ResetTimer() 436 for i := 0; i < b.N; i++ { 437 ExtractDeferredCausets(expr) 438 } 439 b.ReportAllocs() 440 } 441 442 func BenchmarkExprFromSchema(b *testing.B) { 443 conditions := []Expression{ 444 newFunction(ast.EQ, newDeferredCauset(0), newDeferredCauset(1)), 445 newFunction(ast.EQ, newDeferredCauset(1), newDeferredCauset(2)), 446 newFunction(ast.EQ, newDeferredCauset(2), newDeferredCauset(3)), 447 newFunction(ast.EQ, newDeferredCauset(3), newLonglong(1)), 448 newFunction(ast.LogicOr, newLonglong(1), newDeferredCauset(0)), 449 } 450 expr := ComposeCNFCondition(mock.NewContext(), conditions...) 451 schemaReplicant := &Schema{DeferredCausets: ExtractDeferredCausets(expr)} 452 453 b.ResetTimer() 454 for i := 0; i < b.N; i++ { 455 ExprFromSchema(expr, schemaReplicant) 456 } 457 b.ReportAllocs() 458 } 459 460 // MockExpr is mainly for test. 461 type MockExpr struct { 462 err error 463 t *types.FieldType 464 i interface{} 465 } 466 467 func (m *MockExpr) VecEvalInt(ctx stochastikctx.Context, input *chunk.Chunk, result *chunk.DeferredCauset) error { 468 return nil 469 } 470 func (m *MockExpr) VecEvalReal(ctx stochastikctx.Context, input *chunk.Chunk, result *chunk.DeferredCauset) error { 471 return nil 472 } 473 func (m *MockExpr) VecEvalString(ctx stochastikctx.Context, input *chunk.Chunk, result *chunk.DeferredCauset) error { 474 return nil 475 } 476 func (m *MockExpr) VecEvalDecimal(ctx stochastikctx.Context, input *chunk.Chunk, result *chunk.DeferredCauset) error { 477 return nil 478 } 479 func (m *MockExpr) VecEvalTime(ctx stochastikctx.Context, input *chunk.Chunk, result *chunk.DeferredCauset) error { 480 return nil 481 } 482 func (m *MockExpr) VecEvalDuration(ctx stochastikctx.Context, input *chunk.Chunk, result *chunk.DeferredCauset) error { 483 return nil 484 } 485 func (m *MockExpr) VecEvalJSON(ctx stochastikctx.Context, input *chunk.Chunk, result *chunk.DeferredCauset) error { 486 return nil 487 } 488 489 func (m *MockExpr) String() string { return "" } 490 func (m *MockExpr) MarshalJSON() ([]byte, error) { return nil, nil } 491 func (m *MockExpr) Eval(event chunk.Event) (types.Causet, error) { return types.NewCauset(m.i), m.err } 492 func (m *MockExpr) EvalInt(ctx stochastikctx.Context, event chunk.Event) (val int64, isNull bool, err error) { 493 if x, ok := m.i.(int64); ok { 494 return x, false, m.err 495 } 496 return 0, m.i == nil, m.err 497 } 498 func (m *MockExpr) EvalReal(ctx stochastikctx.Context, event chunk.Event) (val float64, isNull bool, err error) { 499 if x, ok := m.i.(float64); ok { 500 return float64(x), false, m.err 501 } 502 return 0, m.i == nil, m.err 503 } 504 func (m *MockExpr) EvalString(ctx stochastikctx.Context, event chunk.Event) (val string, isNull bool, err error) { 505 if x, ok := m.i.(string); ok { 506 return x, false, m.err 507 } 508 return "", m.i == nil, m.err 509 } 510 func (m *MockExpr) EvalDecimal(ctx stochastikctx.Context, event chunk.Event) (val *types.MyDecimal, isNull bool, err error) { 511 if x, ok := m.i.(*types.MyDecimal); ok { 512 return x, false, m.err 513 } 514 return nil, m.i == nil, m.err 515 } 516 func (m *MockExpr) EvalTime(ctx stochastikctx.Context, event chunk.Event) (val types.Time, isNull bool, err error) { 517 if x, ok := m.i.(types.Time); ok { 518 return x, false, m.err 519 } 520 return types.ZeroTime, m.i == nil, m.err 521 } 522 func (m *MockExpr) EvalDuration(ctx stochastikctx.Context, event chunk.Event) (val types.Duration, isNull bool, err error) { 523 if x, ok := m.i.(types.Duration); ok { 524 return x, false, m.err 525 } 526 return types.Duration{}, m.i == nil, m.err 527 } 528 func (m *MockExpr) EvalJSON(ctx stochastikctx.Context, event chunk.Event) (val json.BinaryJSON, isNull bool, err error) { 529 if x, ok := m.i.(json.BinaryJSON); ok { 530 return x, false, m.err 531 } 532 return json.BinaryJSON{}, m.i == nil, m.err 533 } 534 func (m *MockExpr) ReverseEval(sc *stmtctx.StatementContext, res types.Causet, rType types.RoundingType) (val types.Causet, err error) { 535 return types.Causet{}, m.err 536 } 537 func (m *MockExpr) GetType() *types.FieldType { return m.t } 538 func (m *MockExpr) Clone() Expression { return nil } 539 func (m *MockExpr) Equal(ctx stochastikctx.Context, e Expression) bool { return false } 540 func (m *MockExpr) IsCorrelated() bool { return false } 541 func (m *MockExpr) ConstItem(_ *stmtctx.StatementContext) bool { return false } 542 func (m *MockExpr) Decorrelate(schemaReplicant *Schema) Expression { return m } 543 func (m *MockExpr) ResolveIndices(schemaReplicant *Schema) (Expression, error) { return m, nil } 544 func (m *MockExpr) resolveIndices(schemaReplicant *Schema) error { return nil } 545 func (m *MockExpr) ExplainInfo() string { return "" } 546 func (m *MockExpr) ExplainNormalizedInfo() string { return "" } 547 func (m *MockExpr) HashCode(sc *stmtctx.StatementContext) []byte { return nil } 548 func (m *MockExpr) Vectorized() bool { return false } 549 func (m *MockExpr) SupportReverseEval() bool { return false } 550 func (m *MockExpr) HasCoercibility() bool { return false } 551 func (m *MockExpr) Coercibility() Coercibility { return 0 } 552 func (m *MockExpr) SetCoercibility(Coercibility) {} 553 554 func (m *MockExpr) CharsetAndDefCauslation(ctx stochastikctx.Context) (string, string) { 555 return "", "" 556 } 557 func (m *MockExpr) SetCharsetAndDefCauslation(chs, defCausl string) {}