github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/builtin/unary/date.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 "github.com/matrixorigin/matrixone/pkg/container/types" 19 "github.com/matrixorigin/matrixone/pkg/container/vector" 20 "github.com/matrixorigin/matrixone/pkg/vectorize/date" 21 "github.com/matrixorigin/matrixone/pkg/vm/process" 22 ) 23 24 func DateToDate(vectors []*vector.Vector, proc *process.Process) (*vector.Vector, error) { 25 // XXX Why do we ever need this one? Isn't this a noop? 26 inputVector := vectors[0] 27 resultType := types.Type{Oid: types.T_date, Size: 4} 28 inputValues := vector.MustTCols[types.Date](inputVector) 29 if inputVector.IsScalar() { 30 if inputVector.ConstVectorIsNull() { 31 return proc.AllocScalarNullVector(resultType), nil 32 } 33 resultVector := vector.NewConst(resultType, 1) 34 resultValues := make([]types.Date, 1) 35 copy(resultValues, inputValues) 36 vector.SetCol(resultVector, resultValues) 37 return resultVector, nil 38 } else { 39 resultVector, err := proc.AllocVectorOfRows(resultType, int64(len(inputValues)), inputVector.Nsp) 40 if err != nil { 41 return nil, err 42 } 43 resultValues := vector.MustTCols[types.Date](resultVector) 44 copy(resultValues, inputValues) 45 return resultVector, nil 46 } 47 } 48 49 func DatetimeToDate(vectors []*vector.Vector, proc *process.Process) (*vector.Vector, error) { 50 inputVector := vectors[0] 51 resultType := types.Type{Oid: types.T_date, Size: 4} 52 inputValues := vector.MustTCols[types.Datetime](inputVector) 53 if inputVector.IsScalar() { 54 if inputVector.ConstVectorIsNull() { 55 return proc.AllocScalarNullVector(resultType), nil 56 } 57 resultVector := vector.NewConst(resultType, 1) 58 resultValues := make([]types.Date, 1) 59 vector.SetCol(resultVector, date.DatetimeToDate(inputValues, resultValues)) 60 return resultVector, nil 61 } else { 62 resultVector, err := proc.AllocVectorOfRows(resultType, int64(len(inputValues)), inputVector.Nsp) 63 if err != nil { 64 return nil, err 65 } 66 resultValues := vector.MustTCols[types.Date](resultVector) 67 date.DatetimeToDate(inputValues, resultValues) 68 return resultVector, nil 69 } 70 } 71 72 func TimeToDate(vectors []*vector.Vector, proc *process.Process) (*vector.Vector, error) { 73 inputVector := vectors[0] 74 resultType := types.Type{Oid: types.T_date, Size: 4} 75 inputValues := vector.MustTCols[types.Time](inputVector) 76 if inputVector.IsScalar() { 77 if inputVector.ConstVectorIsNull() { 78 return proc.AllocScalarNullVector(resultType), nil 79 } 80 resultVector := vector.NewConst(resultType, 1) 81 resultValues := make([]types.Date, 1) 82 vector.SetCol(resultVector, date.TimeToDate(inputValues, resultValues)) 83 return resultVector, nil 84 } else { 85 resultVector, err := proc.AllocVectorOfRows(resultType, int64(len(inputValues)), inputVector.Nsp) 86 if err != nil { 87 return nil, err 88 } 89 resultValues := vector.MustTCols[types.Date](resultVector) 90 date.TimeToDate(inputValues, resultValues) 91 return resultVector, nil 92 } 93 } 94 95 func DateStringToDate(vectors []*vector.Vector, proc *process.Process) (*vector.Vector, error) { 96 inputVector := vectors[0] 97 resultType := types.Type{Oid: types.T_date, Size: 4} 98 inputValues := vector.MustStrCols(inputVector) 99 100 if inputVector.IsScalar() { 101 if inputVector.ConstVectorIsNull() { 102 return proc.AllocScalarNullVector(resultType), nil 103 } 104 resultVector := vector.NewConst(resultType, 1) 105 resultValues := make([]types.Date, 1) 106 result, err := date.DateStringToDate(inputValues, resultValues) 107 vector.SetCol(resultVector, result) 108 return resultVector, err 109 } else { 110 resultVector, err := proc.AllocVectorOfRows(resultType, int64(len(inputValues)), inputVector.Nsp) 111 if err != nil { 112 return nil, err 113 } 114 resultValues := vector.MustTCols[types.Date](resultVector) 115 _, err = date.DateStringToDate(inputValues, resultValues) 116 return resultVector, err 117 } 118 } 119 120 func TimesToDate(vectors []*vector.Vector, proc *process.Process) (*vector.Vector, error) { 121 inputVector := vectors[0] 122 resultType := types.Type{Oid: types.T_date, Size: 4} 123 inputValues := vector.MustStrCols(inputVector) 124 125 if inputVector.IsScalar() { 126 if inputVector.ConstVectorIsNull() { 127 return proc.AllocScalarNullVector(resultType), nil 128 } 129 resultVector := vector.NewConst(resultType, 1) 130 resultValues := make([]types.Date, 1) 131 result, err := date.DateStringToDate(inputValues, resultValues) 132 vector.SetCol(resultVector, result) 133 return resultVector, err 134 } else { 135 resultVector, err := proc.AllocVectorOfRows(resultType, int64(len(inputValues)), inputVector.Nsp) 136 if err != nil { 137 return nil, err 138 } 139 resultValues := vector.MustTCols[types.Date](resultVector) 140 _, err = date.DateStringToDate(inputValues, resultValues) 141 return resultVector, err 142 } 143 }