github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/builtin/unary/timestamp_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 "time" 20 21 "github.com/matrixorigin/matrixone/pkg/container/nulls" 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 TestDateToTimestamp(t *testing.T) { 30 cases := []struct { 31 name string 32 vecs []*vector.Vector 33 proc *process.Process 34 want []types.Timestamp 35 }{ 36 { 37 name: "TEST01", 38 vecs: makeDateToTimestampVectors("2022-01-01", true), 39 proc: testutil.NewProc(), 40 want: []types.Timestamp{types.FromClockZone(time.Local, 2022, 1, 1, 0, 0, 0, 0)}, 41 }, 42 } 43 44 for _, c := range cases { 45 t.Run(c.name, func(t *testing.T) { 46 result, err := DateToTimestamp(c.vecs, c.proc) 47 if err != nil { 48 t.Fatal(err) 49 } 50 require.Equal(t, c.want, result.Col.([]types.Timestamp)) 51 }) 52 } 53 54 } 55 56 func TestDatetimeToTimestamp(t *testing.T) { 57 cases := []struct { 58 name string 59 vecs []*vector.Vector 60 proc *process.Process 61 want []types.Timestamp 62 }{ 63 { 64 name: "TEST01", 65 vecs: makeDatetimeToTimestampVectors("2022-01-01 00:00:00", true), 66 proc: testutil.NewProc(), 67 want: []types.Timestamp{types.FromClockZone(time.Local, 2022, 1, 1, 0, 0, 0, 0)}, 68 }, 69 } 70 71 for _, c := range cases { 72 t.Run(c.name, func(t *testing.T) { 73 date, err := DatetimeToTimestamp(c.vecs, c.proc) 74 if err != nil { 75 t.Fatal(err) 76 } 77 require.Equal(t, c.want, date.Col.([]types.Timestamp)) 78 }) 79 } 80 81 } 82 83 func TestDateStringAdd(t *testing.T) { 84 cases := []struct { 85 name string 86 vecs []*vector.Vector 87 proc *process.Process 88 want []types.Timestamp 89 contain bool 90 }{ 91 { 92 name: "TEST01", 93 vecs: makeDateStringToTimestampVectors("2022-01-01", true), 94 proc: testutil.NewProc(), 95 want: []types.Timestamp{types.FromClockZone(time.Local, 2022, 1, 1, 0, 0, 0, 0)}, 96 contain: false, 97 }, 98 { 99 name: "TEST02", 100 vecs: makeDateStringToTimestampVectors("2022-01-01 00:00:00", true), 101 proc: testutil.NewProc(), 102 want: []types.Timestamp{types.FromClockZone(time.Local, 2022, 1, 1, 0, 0, 0, 0)}, 103 contain: false, 104 }, 105 { 106 name: "TEST03", 107 vecs: makeDateStringToTimestampVectors("xxxx", true), 108 proc: testutil.NewProc(), 109 want: []types.Timestamp{types.Timestamp(0)}, 110 contain: true, 111 }, 112 } 113 114 for _, c := range cases { 115 t.Run(c.name, func(t *testing.T) { 116 date, err := DateStringToTimestamp(c.vecs, c.proc) 117 if err != nil { 118 t.Fatal(err) 119 } 120 require.Equal(t, c.want, date.Col.([]types.Timestamp)) 121 require.Equal(t, c.contain, nulls.Contains(date.Nsp, 0)) 122 }) 123 } 124 125 } 126 127 func makeDateToTimestampVectors(str string, isConst bool) []*vector.Vector { 128 vec := make([]*vector.Vector, 1) 129 130 date, _ := types.ParseDateCast(str) 131 132 vec[0] = vector.NewConstFixed(types.T_date.ToType(), 1, date, testutil.TestUtilMp) 133 return vec 134 } 135 136 func makeDatetimeToTimestampVectors(str string, isConst bool) []*vector.Vector { 137 vec := make([]*vector.Vector, 1) 138 139 datetime, _ := types.ParseDatetime(str, 0) 140 vec[0] = vector.NewConstFixed(types.T_datetime.ToType(), 1, datetime, testutil.TestUtilMp) 141 142 return vec 143 } 144 145 func makeDateStringToTimestampVectors(str string, isConst bool) []*vector.Vector { 146 typ := types.Type{Oid: types.T_varchar, Size: 26} 147 vec := make([]*vector.Vector, 1) 148 vec[0] = vector.NewConstString(typ, 1, str, testutil.TestUtilMp) 149 return vec 150 }