github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/builtin/unary/time_test.go (about) 1 // Copyright 2022 Matrix Origin 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package unary 16 17 import ( 18 "strconv" 19 "testing" 20 21 "github.com/matrixorigin/matrixone/pkg/common/moerr" 22 "github.com/matrixorigin/matrixone/pkg/container/types" 23 "github.com/matrixorigin/matrixone/pkg/container/vector" 24 "github.com/matrixorigin/matrixone/pkg/testutil" 25 "github.com/matrixorigin/matrixone/pkg/vm/process" 26 "github.com/stretchr/testify/require" 27 ) 28 29 func TestTime(t *testing.T) { 30 cases := []struct { 31 name string 32 input string 33 precise int32 34 isConst bool 35 testTyp types.Type 36 vecs []*vector.Vector 37 proc *process.Process 38 want []types.Time 39 }{ 40 //============================== Date ============================== 41 { 42 name: "TimeTest-FromDate01", 43 input: "2022-01-01", 44 precise: 0, 45 isConst: false, 46 testTyp: types.T_date.ToType(), 47 proc: testutil.NewProc(), 48 want: []types.Time{types.TimeFromClock(false, 0, 0, 0, 0)}, 49 }, 50 { 51 name: "TimeTest-FromDate02", 52 input: "20110101", 53 precise: 0, 54 isConst: false, 55 testTyp: types.T_date.ToType(), 56 proc: testutil.NewProc(), 57 want: []types.Time{types.TimeFromClock(false, 0, 0, 0, 0)}, 58 }, 59 60 //============================== Datetime ============================== 61 { 62 name: "TimeTest-FromDatetime01", 63 input: "2022-01-01 16:22:44", 64 precise: 0, 65 isConst: false, 66 testTyp: types.T_datetime.ToType(), 67 proc: testutil.NewProc(), 68 want: []types.Time{types.TimeFromClock(false, 16, 22, 44, 0)}, 69 }, 70 { 71 name: "TimeTest-FromDatetime02", 72 input: "2022-01-01 16:22:44.123456", 73 precise: 4, 74 isConst: false, 75 testTyp: types.T_datetime.ToType(), 76 proc: testutil.NewProc(), 77 want: []types.Time{types.TimeFromClock(false, 16, 22, 44, 123500)}, 78 }, 79 80 //============================== DateString ============================== 81 // precise is default 6 when input 82 { 83 name: "TimeTest-FromDateString01", 84 input: "20110101112233", 85 precise: 6, 86 isConst: false, 87 testTyp: types.T_varchar.ToType(), 88 proc: testutil.NewProc(), 89 want: []types.Time{types.TimeFromClock(false, 2011010111, 22, 33, 0)}, 90 }, 91 { 92 name: "TimeTest-FromDateString02", 93 input: "2022-01-01 16:22:44.1235", 94 precise: 6, 95 isConst: false, 96 testTyp: types.T_varchar.ToType(), 97 proc: testutil.NewProc(), 98 want: []types.Time{types.TimeFromClock(false, 16, 22, 44, 123500)}, 99 }, 100 { 101 name: "TimeTest-FromDateString03", 102 input: "2022-01-01 16:22:44", 103 precise: 6, 104 isConst: false, 105 testTyp: types.T_varchar.ToType(), 106 proc: testutil.NewProc(), 107 want: []types.Time{types.TimeFromClock(false, 16, 22, 44, 0)}, 108 }, 109 { 110 name: "TimeTest-FromDateString04", 111 input: "-112233", 112 precise: 6, 113 isConst: false, 114 testTyp: types.T_varchar.ToType(), 115 proc: testutil.NewProc(), 116 want: []types.Time{types.TimeFromClock(true, 11, 22, 33, 0)}, 117 }, 118 { 119 name: "TimeTest-FromDateString05", 120 input: "-233.123", 121 precise: 6, 122 isConst: false, 123 testTyp: types.T_varchar.ToType(), 124 proc: testutil.NewProc(), 125 want: []types.Time{types.TimeFromClock(true, 0, 2, 33, 123000)}, 126 }, 127 //============================== Int64 ============================== 128 { 129 name: "TimeTest-FromInt64-01", 130 input: "112233", 131 precise: 0, 132 isConst: false, 133 testTyp: types.T_int64.ToType(), 134 proc: testutil.NewProc(), 135 want: []types.Time{types.TimeFromClock(false, 11, 22, 33, 0)}, 136 }, 137 { 138 name: "TimeTest-FromInt64-02", 139 input: "20221212112233", 140 precise: 0, 141 isConst: false, 142 testTyp: types.T_int64.ToType(), 143 proc: testutil.NewProc(), 144 want: []types.Time{types.TimeFromClock(false, 2022121211, 22, 33, 0)}, 145 }, 146 { 147 name: "TimeTest-FromInt64-03", 148 input: "-20221212112233", 149 precise: 0, 150 isConst: false, 151 testTyp: types.T_int64.ToType(), 152 proc: testutil.NewProc(), 153 want: []types.Time{types.TimeFromClock(true, 2022121211, 22, 33, 0)}, 154 }, 155 //============================== Decimal128 ============================== 156 { 157 name: "TimeTest-FromDecimal128-01", 158 input: "20221212112233.4444", 159 precise: 3, 160 isConst: false, 161 testTyp: types.T_decimal128.ToType(), 162 proc: testutil.NewProc(), 163 want: []types.Time{types.TimeFromClock(false, 2022121211, 22, 33, 444000)}, 164 }, 165 { 166 name: "TimeTest-FromDecimal128-02", 167 input: "20221212112233.4446", 168 precise: 3, 169 isConst: false, 170 testTyp: types.T_decimal128.ToType(), 171 proc: testutil.NewProc(), 172 want: []types.Time{types.TimeFromClock(false, 2022121211, 22, 33, 445000)}, 173 }, 174 { 175 name: "TimeTest-FromDecimal128-03", 176 input: "-20221212112233.4444", 177 precise: 3, 178 isConst: false, 179 testTyp: types.T_decimal128.ToType(), 180 proc: testutil.NewProc(), 181 want: []types.Time{types.TimeFromClock(true, 2022121211, 22, 33, 444000)}, 182 }, 183 { 184 name: "TimeTest-FromDecimal128-04", 185 input: "-20221212112233.4446", 186 precise: 3, 187 isConst: false, 188 testTyp: types.T_decimal128.ToType(), 189 proc: testutil.NewProc(), 190 want: []types.Time{types.TimeFromClock(true, 2022121211, 22, 33, 445000)}, 191 }, 192 } 193 194 for _, c := range cases { 195 t.Run(c.name, func(t *testing.T) { 196 vec, err1 := makeVectorForTimeTest(c.input, c.precise, c.isConst, c.testTyp, c.proc) 197 require.Equal(t, uint16(0), err1) 198 199 var result *vector.Vector 200 var err error 201 switch c.testTyp.Oid { 202 case types.T_int64: 203 result, err = Int64ToTime(vec, c.proc) 204 case types.T_decimal128: 205 result, err = Decimal128ToTime(vec, c.proc) 206 case types.T_date: 207 result, err = DateToTime(vec, c.proc) 208 case types.T_datetime: 209 result, err = DatetimeToTime(vec, c.proc) 210 case types.T_char, types.T_varchar: 211 result, err = DateStringToTime(vec, c.proc) 212 } 213 require.NoError(t, err) 214 require.Equal(t, c.want, result.Col.([]types.Time)) 215 }) 216 } 217 218 } 219 220 func makeVectorForTimeTest(str string, precision int32, isConst bool, typ types.Type, proc *process.Process) ([]*vector.Vector, uint16) { 221 vec := make([]*vector.Vector, 1) 222 if isConst { 223 switch typ.Oid { 224 case types.T_int64: 225 data, err := strconv.Atoi(str) 226 if err != nil { 227 return nil, moerr.ErrInvalidInput 228 } 229 vec[0] = vector.NewConstFixed(types.T_int64.ToType(), 1, data, testutil.TestUtilMp) 230 case types.T_decimal128: 231 data, err := types.ParseStringToDecimal128(str, 34, precision, false) 232 if err != nil { 233 return nil, moerr.ErrInvalidInput 234 } 235 vec[0] = vector.NewConstFixed(types.T_decimal128.ToType(), 1, data, testutil.TestUtilMp) 236 case types.T_date: 237 data, err := types.ParseDateCast(str) 238 if err != nil { 239 return nil, moerr.ErrInvalidInput 240 } 241 vec[0] = vector.NewConstFixed(types.T_date.ToType(), 1, data, testutil.TestUtilMp) 242 case types.T_datetime: 243 data, err := types.ParseDatetime(str, precision) 244 if err != nil { 245 return nil, moerr.ErrInvalidInput 246 } 247 vec[0] = vector.NewConstFixed(types.T_datetime.ToType(), 1, data, testutil.TestUtilMp) 248 vec[0].Typ.Precision = precision 249 case types.T_char, types.T_varchar: 250 vec[0] = vector.NewConstString(types.Type{Oid: types.T_varchar, Size: 26}, 1, str, testutil.TestUtilMp) 251 } 252 } else { 253 input := make([]string, 0) 254 input = append(input, str) 255 switch typ.Oid { 256 case types.T_int64: 257 input := make([]int64, 0) 258 tmp, err := strconv.Atoi(str) 259 if err != nil { 260 return nil, moerr.ErrInvalidInput 261 } 262 input = append(input, int64(tmp)) 263 vec[0] = testutil.MakeInt64Vector(input, nil) 264 case types.T_decimal128: 265 input := make([]types.Decimal128, 0) 266 tmp, err := types.ParseStringToDecimal128(str, 34, precision, false) 267 if err != nil { 268 return nil, moerr.ErrInvalidInput 269 } 270 input = append(input, tmp) 271 vec[0] = vector.NewWithFixed(typ, input, nil, testutil.TestUtilMp) 272 273 case types.T_date: 274 vec[0] = testutil.MakeDateVector(input, nil) 275 case types.T_datetime: 276 vec[0] = testutil.MakeDateTimeVector(input, nil) 277 vec[0].Typ.Precision = precision 278 case types.T_char: 279 vec[0] = testutil.MakeCharVector(input, nil) 280 case types.T_varchar: 281 vec[0] = testutil.MakeVarcharVector(input, nil) 282 default: 283 return vec, moerr.ErrInvalidInput 284 } 285 } 286 return vec, 0 287 }