github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/builtin/unary/date_test.go (about) 1 // Copyright 2021 - 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 TestDate(t *testing.T) { 27 convey.Convey("DateCase", t, func() { 28 type kase struct { 29 s string 30 want string 31 } 32 33 kases := []kase{ 34 { 35 s: "2004-04-03", 36 want: "2004-04-03", 37 }, 38 { 39 s: "2021-10-03", 40 want: "2021-10-03", 41 }, 42 { 43 s: "2020-08-23", 44 want: "2020-08-23", 45 }, 46 } 47 48 var inStrs []string 49 var wantStr []string 50 for _, k := range kases { 51 inStrs = append(inStrs, k.s) 52 wantStr = append(wantStr, k.want) 53 } 54 55 inVector := testutil.MakeDateVector(inStrs, nil) 56 wantVec := testutil.MakeDateVector(wantStr, nil) 57 proc := testutil.NewProc() 58 res, err := DateToDate([]*vector.Vector{inVector}, proc) 59 convey.So(err, convey.ShouldBeNil) 60 compare := testutil.CompareVectors(wantVec, res) 61 convey.So(compare, convey.ShouldBeTrue) 62 }) 63 64 convey.Convey("ScalarDateCase", t, func() { 65 type kase struct { 66 s string 67 want string 68 } 69 70 k := kase{ 71 s: "2020-08-23", 72 want: "2020-08-23", 73 } 74 75 inVector := testutil.MakeScalarDate(k.s, 10) 76 wantVec := testutil.MakeScalarDate(k.want, 10) 77 proc := testutil.NewProc() 78 res, err := DateToDate([]*vector.Vector{inVector}, proc) 79 convey.So(err, convey.ShouldBeNil) 80 compare := testutil.CompareVectors(wantVec, res) 81 convey.So(compare, convey.ShouldBeTrue) 82 }) 83 84 convey.Convey("DateTimeCase", t, func() { 85 type kase struct { 86 s string 87 want string 88 } 89 90 kases := []kase{ 91 { 92 s: "2004-04-03 13:11:10", 93 want: "2004-04-03", 94 }, 95 { 96 s: "2021-10-03 15:24:18", 97 want: "2021-10-03", 98 }, 99 { 100 s: "2020-08-23 21:53:09", 101 want: "2020-08-23", 102 }, 103 } 104 105 var inStrs []string 106 var wantStrs []string 107 for _, k := range kases { 108 inStrs = append(inStrs, k.s) 109 wantStrs = append(wantStrs, k.want) 110 } 111 112 inVector := testutil.MakeDateTimeVector(inStrs, nil) 113 wantVec := testutil.MakeDateVector(wantStrs, nil) 114 proc := testutil.NewProc() 115 res, err := DatetimeToDate([]*vector.Vector{inVector}, proc) 116 convey.So(err, convey.ShouldBeNil) 117 compare := testutil.CompareVectors(wantVec, res) 118 convey.So(compare, convey.ShouldBeTrue) 119 }) 120 121 convey.Convey("ScalarDateTimeCase", t, func() { 122 type kase struct { 123 s string 124 want string 125 } 126 127 k := kase{ 128 s: "2021-10-03 15:24:18", 129 want: "2021-10-03", 130 } 131 132 inVector := testutil.MakeScalarDateTime(k.s, 10) 133 wantVec := testutil.MakeScalarDate(k.want, 10) 134 proc := testutil.NewProc() 135 res, err := DatetimeToDate([]*vector.Vector{inVector}, proc) 136 convey.So(err, convey.ShouldBeNil) 137 compare := testutil.CompareVectors(wantVec, res) 138 convey.So(compare, convey.ShouldBeTrue) 139 }) 140 141 convey.Convey("TestDateScalarNull", t, func() { 142 vecs := []*vector.Vector{testutil.MakeScalarNull(types.T_date, 10)} 143 proc := testutil.NewProc() 144 wantVector := testutil.MakeScalarNull(types.T_date, 10) 145 vec, err := DateToDate(vecs, proc) 146 convey.So(err, convey.ShouldBeNil) 147 ret := testutil.CompareVectors(wantVector, vec) 148 convey.So(ret, convey.ShouldBeTrue) 149 }) 150 151 convey.Convey("TestDatetimeScalarNull", t, func() { 152 vecs := []*vector.Vector{testutil.MakeScalarNull(types.T_datetime, 10)} 153 proc := testutil.NewProc() 154 wantVector := testutil.MakeScalarNull(types.T_date, 10) 155 vec, err := DatetimeToDate(vecs, proc) 156 convey.So(err, convey.ShouldBeNil) 157 ret := testutil.CompareVectors(wantVector, vec) 158 convey.So(ret, convey.ShouldBeTrue) 159 }) 160 }