github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/builtin/multi/date_sub.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 multi 16 17 import ( 18 "github.com/matrixorigin/matrixone/pkg/container/types" 19 "github.com/matrixorigin/matrixone/pkg/container/vector" 20 "github.com/matrixorigin/matrixone/pkg/vectorize/date_sub" 21 "github.com/matrixorigin/matrixone/pkg/vm/process" 22 ) 23 24 func DateSub(vectors []*vector.Vector, proc *process.Process) (*vector.Vector, error) { 25 firstVector := vectors[0] 26 secondVector := vectors[1] 27 firstValues := vector.MustTCols[types.Date](vectors[0]) 28 secondValues := vector.MustTCols[int64](vectors[1]) 29 thirdValues := vector.MustTCols[int64](vectors[2]) 30 31 resultType := types.Type{Oid: types.T_date, Size: 4} 32 if firstVector.IsScalar() && secondVector.IsScalar() { 33 if firstVector.IsScalarNull() || secondVector.IsScalarNull() { 34 return proc.AllocScalarNullVector(resultType), nil 35 } 36 resultVector := proc.AllocScalarVector(resultType) 37 rs := make([]types.Date, 1) 38 res, err := date_sub.DateSub(firstValues, secondValues, thirdValues, firstVector.Nsp, secondVector.Nsp, resultVector.Nsp, rs) 39 vector.SetCol(resultVector, res) 40 return resultVector, err 41 } else { 42 var maxLen int 43 if len(firstValues) > len(secondValues) { 44 maxLen = len(firstValues) 45 } else { 46 maxLen = len(secondValues) 47 } 48 resultVector, err := proc.AllocVectorOfRows(resultType, int64(maxLen), nil) 49 if err != nil { 50 return nil, err 51 } 52 resultValues := vector.MustTCols[types.Date](resultVector) 53 _, err = date_sub.DateSub(firstValues, secondValues, thirdValues, firstVector.Nsp, secondVector.Nsp, resultVector.Nsp, resultValues) 54 return resultVector, err 55 } 56 } 57 58 func DatetimeSub(vectors []*vector.Vector, proc *process.Process) (*vector.Vector, error) { 59 firstVector := vectors[0] 60 secondVector := vectors[1] 61 firstValues := vector.MustTCols[types.Datetime](vectors[0]) 62 secondValues := vector.MustTCols[int64](vectors[1]) 63 thirdValues := vector.MustTCols[int64](vectors[2]) 64 65 precision := firstVector.Typ.Precision 66 switch types.IntervalType(thirdValues[0]) { 67 case types.MicroSecond: 68 precision = 6 69 } 70 71 resultType := types.Type{Oid: types.T_datetime, Precision: precision, Size: 8} 72 if firstVector.IsScalar() && secondVector.IsScalar() { 73 if firstVector.IsScalarNull() || secondVector.IsScalarNull() { 74 return proc.AllocScalarNullVector(resultType), nil 75 } 76 resultVector := proc.AllocScalarVector(resultType) 77 rs := make([]types.Datetime, 1) 78 res, err := date_sub.DatetimeSub(firstValues, secondValues, thirdValues, firstVector.Nsp, secondVector.Nsp, resultVector.Nsp, rs) 79 vector.SetCol(resultVector, res) 80 return resultVector, err 81 } else { 82 var maxLen int 83 if len(firstValues) > len(secondValues) { 84 maxLen = len(firstValues) 85 } else { 86 maxLen = len(secondValues) 87 } 88 resultVector, err := proc.AllocVectorOfRows(resultType, int64(maxLen), nil) 89 if err != nil { 90 return nil, err 91 } 92 resultValues := vector.GetFixedVectorValues[types.Datetime](resultVector) 93 _, err = date_sub.DatetimeSub(firstValues, secondValues, thirdValues, firstVector.Nsp, secondVector.Nsp, resultVector.Nsp, resultValues) 94 return resultVector, err 95 } 96 } 97 98 func DateStringSub(vectors []*vector.Vector, proc *process.Process) (*vector.Vector, error) { 99 firstVector := vectors[0] 100 secondVector := vectors[1] 101 firstValues := vector.MustStrCols(vectors[0]) 102 secondValues := vector.MustTCols[int64](vectors[1]) 103 thirdValues := vector.MustTCols[int64](vectors[2]) 104 resultType := types.Type{Oid: types.T_datetime, Precision: 6, Size: 8} 105 106 if firstVector.IsScalar() && secondVector.IsScalar() { 107 if firstVector.IsScalarNull() || secondVector.IsScalarNull() { 108 return proc.AllocScalarNullVector(resultType), nil 109 } 110 resultVector := proc.AllocScalarVector(resultType) 111 rs := make([]types.Datetime, 1) 112 res, err := date_sub.DateStringSub(firstValues, secondValues, thirdValues, firstVector.Nsp, secondVector.Nsp, resultVector.Nsp, rs) 113 vector.SetCol(resultVector, res) 114 return resultVector, err 115 } else { 116 var maxLen int 117 if len(firstValues) > len(secondValues) { 118 maxLen = len(firstValues) 119 } else { 120 maxLen = len(secondValues) 121 } 122 resultVector, err := proc.AllocVectorOfRows(resultType, int64(maxLen), nil) 123 if err != nil { 124 return nil, err 125 } 126 resultValues := vector.MustTCols[types.Datetime](resultVector) 127 _, err = date_sub.DateStringSub(firstValues, secondValues, thirdValues, firstVector.Nsp, secondVector.Nsp, resultVector.Nsp, resultValues) 128 return resultVector, err 129 } 130 } 131 132 func TimeStampSub(vectors []*vector.Vector, proc *process.Process) (*vector.Vector, error) { 133 firstVector := vectors[0] 134 secondVector := vectors[1] 135 firstValues := vector.MustTCols[types.Timestamp](vectors[0]) 136 secondValues := vector.MustTCols[int64](vectors[1]) 137 thirdValues := vector.MustTCols[int64](vectors[2]) 138 139 precision := firstVector.Typ.Precision 140 switch types.IntervalType(thirdValues[0]) { 141 case types.MicroSecond: 142 precision = 6 143 } 144 145 resultType := types.Type{Oid: types.T_timestamp, Precision: precision, Size: 8} 146 if firstVector.IsScalar() && secondVector.IsScalar() { 147 if firstVector.IsScalarNull() || secondVector.IsScalarNull() { 148 return proc.AllocScalarNullVector(resultType), nil 149 } 150 resultVector := proc.AllocScalarVector(resultType) 151 rs := make([]types.Timestamp, 1) 152 res, err := date_sub.TimestampSub(proc.SessionInfo.TimeZone, firstValues, secondValues, thirdValues, firstVector.Nsp, secondVector.Nsp, resultVector.Nsp, rs) 153 vector.SetCol(resultVector, res) 154 return resultVector, err 155 } else { 156 var maxLen int 157 if len(firstValues) > len(secondValues) { 158 maxLen = len(firstValues) 159 } else { 160 maxLen = len(secondValues) 161 } 162 resultVector, err := proc.AllocVectorOfRows(resultType, int64(maxLen), firstVector.Nsp) 163 if err != nil { 164 return nil, err 165 } 166 resultValues := vector.MustTCols[types.Timestamp](resultVector) 167 _, err = date_sub.TimestampSub(proc.SessionInfo.TimeZone, firstValues, secondValues, thirdValues, firstVector.Nsp, secondVector.Nsp, resultVector.Nsp, resultValues) 168 return resultVector, err 169 } 170 }