github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/builtin/unary/year_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 "testing" 19 20 "github.com/matrixorigin/matrixone/pkg/container/types" 21 "github.com/matrixorigin/matrixone/pkg/container/vector" 22 "github.com/matrixorigin/matrixone/pkg/testutil" 23 "github.com/smartystreets/goconvey/convey" 24 ) 25 26 func TestYearFunction(t *testing.T) { 27 convey.Convey("DateToYearCase", t, func() { 28 type kase struct { 29 s string 30 want int64 31 } 32 33 kases := []kase{ 34 { 35 s: "2004-04-03", 36 want: 2004, 37 }, 38 { 39 s: "2014-08-03", 40 want: 2014, 41 }, 42 { 43 s: "2008-01-03", 44 want: 2008, 45 }, 46 } 47 48 var inStrs []string 49 var wantint64 []int64 50 for _, k := range kases { 51 inStrs = append(inStrs, k.s) 52 wantint64 = append(wantint64, k.want) 53 } 54 55 inVector := testutil.MakeDateVector(inStrs, nil) 56 wantVector := testutil.MakeInt64Vector(wantint64, nil) 57 proc := testutil.NewProc() 58 res, err := DateToYear([]*vector.Vector{inVector}, proc) 59 convey.So(err, convey.ShouldBeNil) 60 compare := testutil.CompareVectors(wantVector, res) 61 convey.So(compare, convey.ShouldBeTrue) 62 }) 63 64 convey.Convey("DateToYearCaseScalar", t, func() { 65 type kase struct { 66 s string 67 want int64 68 } 69 70 k := kase{ 71 s: "2004-04-03", 72 want: 2004, 73 } 74 75 inVector := testutil.MakeScalarDate(k.s, 10) 76 wantVector := testutil.MakeScalarInt64(k.want, 10) 77 proc := testutil.NewProc() 78 res, err := DateToYear([]*vector.Vector{inVector}, proc) 79 convey.So(err, convey.ShouldBeNil) 80 compare := testutil.CompareVectors(wantVector, res) 81 convey.So(compare, convey.ShouldBeTrue) 82 }) 83 84 convey.Convey("DateToYearCaseScalarNull", t, func() { 85 inVector := testutil.MakeScalarNull(types.T_date, 10) 86 wantVector := testutil.MakeScalarNull(types.T_int64, 10) 87 proc := testutil.NewProc() 88 res, err := DateToYear([]*vector.Vector{inVector}, proc) 89 convey.So(err, convey.ShouldBeNil) 90 compare := testutil.CompareVectors(wantVector, res) 91 convey.So(compare, convey.ShouldBeTrue) 92 }) 93 94 convey.Convey("DatetimeToYearCase", t, func() { 95 type kase struct { 96 s string 97 want int64 98 } 99 100 kases := []kase{ 101 { 102 s: "2004-04-03 13:11:10", 103 want: 2004, 104 }, 105 { 106 s: "1999-08-05 11:01:02", 107 want: 1999, 108 }, 109 { 110 s: "2004-01-03 23:15:08", 111 want: 2004, 112 }, 113 } 114 115 var inStrs []string 116 var wantInt64 []int64 117 for _, k := range kases { 118 inStrs = append(inStrs, k.s) 119 wantInt64 = append(wantInt64, k.want) 120 } 121 inVector := testutil.MakeDateTimeVector(inStrs, nil) 122 wantVector := testutil.MakeInt64Vector(wantInt64, nil) 123 proc := testutil.NewProc() 124 res, err := DatetimeToYear([]*vector.Vector{inVector}, proc) 125 convey.So(err, convey.ShouldBeNil) 126 compare := testutil.CompareVectors(wantVector, res) 127 convey.So(compare, convey.ShouldBeTrue) 128 }) 129 130 convey.Convey("DatetimeToYearCaseScalar", t, func() { 131 type kase struct { 132 s string 133 want int64 134 } 135 136 k := kase{ 137 s: "2004-01-03 23:15:08", 138 want: 2004, 139 } 140 141 inVector := testutil.MakeScalarDateTime(k.s, 10) 142 wantVector := testutil.MakeScalarInt64(k.want, 10) 143 proc := testutil.NewProc() 144 res, err := DatetimeToYear([]*vector.Vector{inVector}, proc) 145 convey.So(err, convey.ShouldBeNil) 146 compare := testutil.CompareVectors(wantVector, res) 147 convey.So(compare, convey.ShouldBeTrue) 148 }) 149 150 convey.Convey("DatetimeToYearCaseScalarNull", t, func() { 151 inVector := testutil.MakeScalarNull(types.T_datetime, 10) 152 wantVector := testutil.MakeScalarNull(types.T_int64, 10) 153 proc := testutil.NewProc() 154 res, err := DatetimeToYear([]*vector.Vector{inVector}, proc) 155 convey.So(err, convey.ShouldBeNil) 156 compare := testutil.CompareVectors(wantVector, res) 157 convey.So(compare, convey.ShouldBeTrue) 158 }) 159 160 convey.Convey("DateStringToYearCase", t, func() { 161 type kase struct { 162 s string 163 want int64 164 } 165 166 kases := []kase{ 167 { 168 s: "2014-04-03", 169 want: 2014, 170 }, 171 { 172 s: "2009-11-03", 173 want: 2009, 174 }, 175 { 176 s: "2012-07-03", 177 want: 2012, 178 }, 179 { 180 s: "2012-02-03 18:23:15", 181 want: 2012, 182 }, 183 } 184 185 var inStrs []string 186 var wantInt64 []int64 187 for _, k := range kases { 188 inStrs = append(inStrs, k.s) 189 wantInt64 = append(wantInt64, k.want) 190 } 191 192 inVector := testutil.MakeVarcharVector(inStrs, nil) 193 wantVector := testutil.MakeInt64Vector(wantInt64, nil) 194 proc := testutil.NewProc() 195 res, err := DateStringToYear([]*vector.Vector{inVector}, proc) 196 convey.So(err, convey.ShouldBeNil) 197 compare := testutil.CompareVectors(wantVector, res) 198 convey.So(compare, convey.ShouldBeTrue) 199 }) 200 201 convey.Convey("DateStringToYearCaseScalar", t, func() { 202 type kase struct { 203 s string 204 want int64 205 } 206 207 k := kase{ 208 s: "2004-01-03 23:15:08", 209 want: 2004, 210 } 211 212 inVector := testutil.MakeScalarChar(k.s, 10) 213 wantVector := testutil.MakeScalarInt64(k.want, 10) 214 proc := testutil.NewProc() 215 res, err := DateStringToYear([]*vector.Vector{inVector}, proc) 216 convey.So(err, convey.ShouldBeNil) 217 compare := testutil.CompareVectors(wantVector, res) 218 convey.So(compare, convey.ShouldBeTrue) 219 }) 220 221 convey.Convey("DateStringToYearCaseScalarNull", t, func() { 222 inVector := testutil.MakeScalarNull(types.T_char, 10) 223 wantVector := testutil.MakeScalarNull(types.T_int64, 10) 224 proc := testutil.NewProc() 225 res, err := DateStringToYear([]*vector.Vector{inVector}, proc) 226 convey.So(err, convey.ShouldBeNil) 227 compare := testutil.CompareVectors(wantVector, res) 228 convey.So(compare, convey.ShouldBeTrue) 229 }) 230 }