github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/builtin/multi/date_sub_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 multi 16 17 import ( 18 "testing" 19 20 "github.com/matrixorigin/matrixone/pkg/common/moerr" 21 "github.com/matrixorigin/matrixone/pkg/container/types" 22 "github.com/matrixorigin/matrixone/pkg/container/vector" 23 "github.com/matrixorigin/matrixone/pkg/testutil" 24 "github.com/matrixorigin/matrixone/pkg/vm/process" 25 "github.com/stretchr/testify/require" 26 ) 27 28 func TestDateSub(t *testing.T) { 29 cases := []struct { 30 name string 31 vecs []*vector.Vector 32 proc *process.Process 33 want string 34 }{ 35 { 36 name: "TEST01", 37 vecs: makeDateSubVectors("2022-01-02", true, 1, types.Day), 38 proc: testutil.NewProc(), 39 want: "2022-01-01", 40 }, 41 } 42 43 for _, c := range cases { 44 t.Run(c.name, func(t *testing.T) { 45 date, err := DateSub(c.vecs, c.proc) 46 if err != nil { 47 t.Fatal(err) 48 } 49 require.Equal(t, c.want, date.Col.([]types.Date)[0].String()) 50 }) 51 } 52 53 } 54 55 func TestDatetimeSub(t *testing.T) { 56 cases := []struct { 57 name string 58 vecs []*vector.Vector 59 proc *process.Process 60 want string 61 }{ 62 { 63 name: "TEST01", 64 vecs: makeDatetimeSubVectors("2022-01-02 00:00:00", true, 1, types.Day), 65 proc: testutil.NewProc(), 66 want: "2022-01-01 00:00:00", 67 }, 68 } 69 70 for _, c := range cases { 71 t.Run(c.name, func(t *testing.T) { 72 date, err := DatetimeSub(c.vecs, c.proc) 73 if err != nil { 74 t.Fatal(err) 75 } 76 require.Equal(t, c.want, date.Col.([]types.Datetime)[0].String()) 77 }) 78 } 79 80 } 81 82 func TestDateStringSub(t *testing.T) { 83 cases := []struct { 84 name string 85 vecs []*vector.Vector 86 proc *process.Process 87 want string 88 err uint16 89 }{ 90 { 91 name: "TEST01", 92 vecs: makeDateStringSubVectors("2022-01-02", true, 1, types.Day), 93 proc: testutil.NewProc(), 94 want: "2022-01-01 00:00:00", 95 err: 0, 96 }, 97 { 98 name: "TEST02", 99 vecs: makeDateStringSubVectors("2022-01-02 00:00:00", true, 1, types.Day), 100 proc: testutil.NewProc(), 101 want: "2022-01-01 00:00:00", 102 err: 0, 103 }, 104 { 105 name: "TEST03", 106 vecs: makeDateStringSubVectors("2022-01-01", true, 1, types.Second), 107 proc: testutil.NewProc(), 108 want: "2021-12-31 23:59:59", 109 err: 0, 110 }, 111 { 112 name: "TEST04", 113 vecs: makeDateStringSubVectors("xxxx", true, 1, types.Second), 114 proc: testutil.NewProc(), 115 want: "0001-01-01 00:00:00", 116 err: moerr.ErrInvalidInput, 117 }, 118 } 119 120 for _, c := range cases { 121 t.Run(c.name, func(t *testing.T) { 122 date, err := DateStringSub(c.vecs, c.proc) 123 require.Equal(t, c.want, date.Col.([]types.Datetime)[0].String()) 124 require.True(t, moerr.IsMoErrCode(err, c.err)) 125 }) 126 } 127 128 } 129 130 func makeDateSubVectors(str string, isConst bool, num int64, unit types.IntervalType) []*vector.Vector { 131 vec := make([]*vector.Vector, 3) 132 133 date, _ := types.ParseDateCast(str) 134 135 vec[0] = vector.NewConstFixed(types.T_date.ToType(), 1, date, testutil.TestUtilMp) 136 vec[1] = vector.NewConstFixed(types.T_int64.ToType(), 1, num, testutil.TestUtilMp) 137 vec[2] = vector.NewConstFixed(types.T_int64.ToType(), 1, int64(unit), testutil.TestUtilMp) 138 return vec 139 } 140 141 func makeDatetimeSubVectors(str string, isConst bool, num int64, unit types.IntervalType) []*vector.Vector { 142 vec := make([]*vector.Vector, 3) 143 144 datetime, _ := types.ParseDatetime(str, 0) 145 146 vec[0] = vector.NewConstFixed(types.T_datetime.ToType(), 1, datetime, testutil.TestUtilMp) 147 vec[1] = vector.NewConstFixed(types.T_int64.ToType(), 1, num, testutil.TestUtilMp) 148 vec[2] = vector.NewConstFixed(types.T_int64.ToType(), 1, int64(unit), testutil.TestUtilMp) 149 return vec 150 } 151 152 func makeDateStringSubVectors(str string, isConst bool, num int64, unit types.IntervalType) []*vector.Vector { 153 vec := make([]*vector.Vector, 3) 154 vec[0] = vector.NewConstString(types.T_varchar.ToType(), 1, str, testutil.TestUtilMp) 155 vec[1] = vector.NewConstFixed(types.T_int64.ToType(), 1, num, testutil.TestUtilMp) 156 vec[2] = vector.NewConstFixed(types.T_int64.ToType(), 1, int64(unit), testutil.TestUtilMp) 157 return vec 158 }