github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/builtin/unary/week_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/matrixorigin/matrixone/pkg/vm/process" 24 "github.com/stretchr/testify/require" 25 ) 26 27 func TestDateToWeekFunc(t *testing.T) { 28 procs := testutil.NewProc() 29 cases := []struct { 30 name string 31 proc *process.Process 32 inputstr []string 33 inputNsp []uint64 34 expected []uint8 35 isScalar bool 36 }{ 37 { 38 name: "Date to week test - first and last week", 39 proc: procs, 40 inputstr: []string{"2003-12-30", "2004-01-02", "2004-12-31", "2005-01-01"}, 41 expected: []uint8{1, 1, 53, 53}, 42 isScalar: false, 43 }, 44 { 45 name: "Date to week test - normal", 46 proc: procs, 47 inputstr: []string{"2001-02-16", "2012-06-18", "2015-09-25", "2022-12-05"}, 48 expected: []uint8{7, 25, 39, 49}, 49 isScalar: false, 50 }, 51 { 52 name: "Date to week test - scalar", 53 proc: procs, 54 inputstr: []string{"2003-12-30"}, 55 expected: []uint8{1}, 56 isScalar: true, 57 }, 58 { 59 name: "Date to week test - null", 60 proc: procs, 61 expected: []uint8{0}, 62 isScalar: true, 63 }, 64 } 65 66 for _, c := range cases { 67 t.Run(c.name, func(t *testing.T) { 68 vecs := make([]*vector.Vector, 1) 69 if c.inputstr != nil { 70 vecs[0] = testutil.MakeDateVector(c.inputstr, c.inputNsp) 71 if c.isScalar { 72 vecs[0].MakeScalar(1) 73 } 74 } else { 75 vecs[0] = testutil.MakeScalarNull(types.T_date, 0) 76 } 77 78 result, err := DateToWeek(vecs, c.proc) 79 if err != nil { 80 t.Fatal(err) 81 } 82 col := result.Col.([]uint8) 83 require.Equal(t, c.expected, col) 84 require.Equal(t, c.isScalar, result.IsScalar()) 85 }) 86 } 87 } 88 89 func TestDatetimeToWeekFunc(t *testing.T) { 90 procs := testutil.NewProc() 91 cases := []struct { 92 name string 93 proc *process.Process 94 inputstr []string 95 inputNsp []uint64 96 expected []uint8 97 isScalar bool 98 }{ 99 { 100 name: "Datetime to week test - first and last week", 101 proc: procs, 102 inputstr: []string{"2003-12-30 13:11:10", "2004-01-02 19:22:10", "2004-12-31 00:00:00", "2005-01-01 04:05:06"}, 103 expected: []uint8{1, 1, 53, 53}, 104 isScalar: false, 105 }, 106 { 107 name: "Datetime to week test - normal", 108 proc: procs, 109 inputstr: []string{"2001-02-16 13:11:10", "2012-06-18 19:22:10", "2015-09-25 00:00:00", "2022-12-05 04:05:06"}, 110 expected: []uint8{7, 25, 39, 49}, 111 isScalar: false, 112 }, 113 { 114 name: "Datetime to week test - scalar", 115 proc: procs, 116 inputstr: []string{"2003-12-30 00:00:00"}, 117 expected: []uint8{1}, 118 isScalar: true, 119 }, 120 { 121 name: "Datetime to week test - null", 122 proc: procs, 123 expected: []uint8{0}, 124 isScalar: true, 125 }, 126 } 127 128 for _, c := range cases { 129 t.Run(c.name, func(t *testing.T) { 130 vecs := make([]*vector.Vector, 1) 131 if c.inputstr != nil { 132 vecs[0] = testutil.MakeDateTimeVector(c.inputstr, c.inputNsp) 133 if c.isScalar { 134 vecs[0].MakeScalar(1) 135 } 136 } else { 137 vecs[0] = testutil.MakeScalarNull(types.T_datetime, 0) 138 } 139 140 result, err := DatetimeToWeek(vecs, c.proc) 141 if err != nil { 142 t.Fatal(err) 143 } 144 col := result.Col.([]uint8) 145 require.Equal(t, c.expected, col) 146 require.Equal(t, c.isScalar, result.IsScalar()) 147 }) 148 } 149 }