github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/builtin/multi/unix_timestamp.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 package multi 15 16 import ( 17 "time" 18 19 "github.com/matrixorigin/matrixone/pkg/container/nulls" 20 "github.com/matrixorigin/matrixone/pkg/container/types" 21 "github.com/matrixorigin/matrixone/pkg/container/vector" 22 "github.com/matrixorigin/matrixone/pkg/vectorize/unixtimestamp" 23 "github.com/matrixorigin/matrixone/pkg/vm/process" 24 ) 25 26 func UnixTimestamp(lv []*vector.Vector, proc *process.Process) (*vector.Vector, error) { 27 if len(lv) == 0 { 28 rs := make([]int64, 1) 29 unixtimestamp.UnixTimestampToInt([]types.Timestamp{types.CurrentTimestamp()}, rs) 30 return vector.NewConstFixed(types.T_int64.ToType(), 1, rs[0], proc.Mp()), nil 31 } 32 33 inVec := lv[0] 34 size := types.T(types.T_int64).TypeLen() 35 if inVec.IsScalarNull() { 36 return proc.AllocScalarNullVector(types.Type{Oid: types.T_int64, Size: int32(size)}), nil 37 } 38 times := vector.MustTCols[types.Timestamp](inVec) 39 40 if inVec.IsScalar() { 41 rs := make([]int64, 1) 42 unixtimestamp.UnixTimestampToInt(times, rs) 43 if rs[0] >= 0 { 44 return vector.NewConstFixed(types.T_int64.ToType(), inVec.Length(), rs[0], proc.Mp()), nil 45 } else { 46 return proc.AllocScalarNullVector(types.Type{Oid: types.T_int64, Size: int32(size)}), nil 47 } 48 } 49 50 vec, err := proc.AllocVectorOfRows(types.T_int64.ToType(), int64(len(times)), inVec.Nsp) 51 if err != nil { 52 return nil, err 53 } 54 rs := vector.MustTCols[int64](vec) 55 for i := 0; i < len(times); i++ { 56 // XXX This is simply wrong. We should raise error. 57 if times[i] < 0 { 58 nulls.Add(vec.Nsp, uint64(i)) 59 } 60 } 61 unixtimestamp.UnixTimestampToInt(times, rs) 62 for i, r := range rs { 63 if r < 0 { 64 nulls.Add(vec.Nsp, uint64(i)) 65 } 66 } 67 return vec, nil 68 } 69 70 func UnixTimestampVarcharToInt64(lv []*vector.Vector, proc *process.Process) (*vector.Vector, error) { 71 inVec := lv[0] 72 resultType := types.T_int64.ToType() 73 74 if inVec.IsScalarNull() { 75 return proc.AllocScalarNullVector(resultType), nil 76 } 77 78 if inVec.IsScalar() { 79 tms := make([]types.Timestamp, 1) 80 rs := make([]int64, 1) 81 tms[0] = MustTimestamp(proc.SessionInfo.TimeZone, inVec.GetString(0)) 82 unixtimestamp.UnixTimestampToInt(tms, rs) 83 if rs[0] >= 0 { 84 return vector.NewConstFixed(resultType, inVec.Length(), rs[0], proc.Mp()), nil 85 } else { 86 return proc.AllocScalarNullVector(resultType), nil 87 } 88 } 89 90 vlen := inVec.Length() 91 times := make([]types.Timestamp, vlen) 92 for i := 0; i < vlen; i++ { 93 times[i] = MustTimestamp(proc.SessionInfo.TimeZone, inVec.GetString(int64(i))) 94 } 95 vec, err := proc.AllocVectorOfRows(resultType, int64(vlen), inVec.Nsp) 96 if err != nil { 97 return nil, err 98 } 99 rs := vector.MustTCols[int64](vec) 100 unixtimestamp.UnixTimestampToInt(times, rs) 101 for i, r := range rs { 102 if r < 0 { 103 nulls.Add(vec.Nsp, uint64(i)) 104 } 105 } 106 return vec, nil 107 } 108 109 func UnixTimestampVarcharToFloat64(lv []*vector.Vector, proc *process.Process) (*vector.Vector, error) { 110 inVec := lv[0] 111 resultType := types.T_float64.ToType() 112 if inVec.IsScalarNull() { 113 return proc.AllocScalarNullVector(resultType), nil 114 } 115 116 if inVec.IsScalar() { 117 tms := make([]types.Timestamp, 1) 118 rs := make([]float64, 1) 119 tms[0] = MustTimestamp(proc.SessionInfo.TimeZone, inVec.GetString(0)) 120 unixtimestamp.UnixTimestampToFloat(tms, rs) 121 if rs[0] >= 0 { 122 return vector.NewConstFixed(resultType, inVec.Length(), rs[0], proc.Mp()), nil 123 } else { 124 return proc.AllocScalarNullVector(resultType), nil 125 } 126 } 127 128 vlen := inVec.Length() 129 times := make([]types.Timestamp, vlen) 130 for i := 0; i < vlen; i++ { 131 times[i] = MustTimestamp(proc.SessionInfo.TimeZone, inVec.GetString(int64(i))) 132 } 133 vec, err := proc.AllocVectorOfRows(resultType, int64(vlen), inVec.Nsp) 134 if err != nil { 135 return nil, err 136 } 137 rs := vector.MustTCols[float64](vec) 138 unixtimestamp.UnixTimestampToFloat(times, rs) 139 for i, r := range rs { 140 if r < 0 { 141 nulls.Add(vec.Nsp, uint64(i)) 142 } 143 } 144 return vec, nil 145 } 146 147 var ( 148 Decimal128Zero = types.Decimal128_FromInt32(0) 149 ) 150 151 func UnixTimestampVarcharToDecimal128(lv []*vector.Vector, proc *process.Process) (*vector.Vector, error) { 152 inVec := lv[0] 153 resultType := types.Type{Oid: types.T_decimal128, Size: 16, Scale: 6} 154 if inVec.IsScalarNull() { 155 return proc.AllocScalarNullVector(resultType), nil 156 } 157 158 if inVec.IsScalar() { 159 tms := make([]types.Timestamp, 1) 160 rs := make([]types.Decimal128, 1) 161 tms[0] = MustTimestamp(proc.SessionInfo.TimeZone, inVec.GetString(0)) 162 unixtimestamp.UnixTimestampToDecimal128(tms, rs) 163 if rs[0].Ge(Decimal128Zero) { 164 return vector.NewConstFixed(resultType, inVec.Length(), rs[0], proc.Mp()), nil 165 } else { 166 return proc.AllocScalarNullVector(resultType), nil 167 } 168 } 169 170 vlen := inVec.Length() 171 times := make([]types.Timestamp, vlen) 172 for i := 0; i < vlen; i++ { 173 times[i] = MustTimestamp(proc.SessionInfo.TimeZone, inVec.GetString(int64(i))) 174 } 175 vec, err := proc.AllocVectorOfRows(resultType, int64(vlen), inVec.Nsp) 176 if err != nil { 177 return nil, err 178 } 179 rs := vector.MustTCols[types.Decimal128](vec) 180 unixtimestamp.UnixTimestampToDecimal128(times, rs) 181 for i, r := range rs { 182 if r.Lt(Decimal128Zero) { 183 nulls.Add(vec.Nsp, uint64(i)) 184 } 185 } 186 return vec, nil 187 } 188 189 func MustTimestamp(loc *time.Location, s string) types.Timestamp { 190 ts, err := types.ParseTimestamp(loc, s, 6) 191 if err != nil { 192 ts = 0 193 } 194 return ts 195 }