github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/builtin/unary/timestamp.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/timestamp"
    21  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    22  )
    23  
    24  func DateToTimestamp(vectors []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
    25  	inputVector := vectors[0]
    26  	resultType := types.Type{Oid: types.T_timestamp, Precision: 6, Size: 8}
    27  	inputValues := vector.MustTCols[types.Date](inputVector)
    28  	if inputVector.IsScalar() {
    29  		if inputVector.ConstVectorIsNull() {
    30  			return proc.AllocScalarNullVector(resultType), nil
    31  		}
    32  		resultVector := vector.NewConst(resultType, 1)
    33  		resultValues := make([]types.Timestamp, 1)
    34  		vector.SetCol(resultVector, timestamp.DateToTimestamp(proc.SessionInfo.TimeZone, inputValues, resultVector.Nsp, resultValues))
    35  		vector.SetCol(resultVector, resultValues)
    36  		return resultVector, nil
    37  	} else {
    38  		resultVector, err := proc.AllocVectorOfRows(resultType, int64(len(inputValues)), inputVector.Nsp)
    39  		if err != nil {
    40  			return nil, err
    41  		}
    42  		resultValues := vector.MustTCols[types.Timestamp](resultVector)
    43  		timestamp.DateToTimestamp(proc.SessionInfo.TimeZone, inputValues, resultVector.Nsp, resultValues)
    44  		return resultVector, nil
    45  	}
    46  }
    47  
    48  func DatetimeToTimestamp(vectors []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
    49  	inputVector := vectors[0]
    50  	resultType := types.Type{Oid: types.T_timestamp, Precision: inputVector.Typ.Precision, Size: 8}
    51  	inputValues := vector.MustTCols[types.Datetime](inputVector)
    52  	if inputVector.IsScalar() {
    53  		if inputVector.ConstVectorIsNull() {
    54  			return proc.AllocScalarNullVector(resultType), nil
    55  		}
    56  		resultVector := vector.NewConst(resultType, 1)
    57  		resultValues := make([]types.Timestamp, 1)
    58  		vector.SetCol(resultVector, timestamp.DatetimeToTimestamp(proc.SessionInfo.TimeZone, inputValues, resultVector.Nsp, resultValues))
    59  		return resultVector, nil
    60  	} else {
    61  		resultVector, err := proc.AllocVectorOfRows(resultType, int64(len(inputValues)), inputVector.Nsp)
    62  		if err != nil {
    63  			return nil, err
    64  		}
    65  		resultValues := vector.MustTCols[types.Timestamp](resultVector)
    66  		timestamp.DatetimeToTimestamp(proc.SessionInfo.TimeZone, inputValues, resultVector.Nsp, resultValues)
    67  		return resultVector, nil
    68  	}
    69  }
    70  
    71  func TimestampToTimestamp(vectors []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
    72  	// XXX should this be an Noop?
    73  	inputVector := vectors[0]
    74  	resultType := types.Type{Oid: types.T_timestamp, Precision: inputVector.Typ.Precision, Size: 8}
    75  	inputValues := vector.MustTCols[types.Timestamp](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.Timestamp, 1)
    82  		copy(resultValues, inputValues)
    83  		vector.SetCol(resultVector, resultValues)
    84  		return resultVector, nil
    85  	} else {
    86  		resultVector, err := proc.AllocVectorOfRows(resultType, int64(len(inputValues)), inputVector.Nsp)
    87  		if err != nil {
    88  			return nil, err
    89  		}
    90  		resultValues := vector.MustTCols[types.Timestamp](resultVector)
    91  		copy(resultValues, inputValues)
    92  		return resultVector, nil
    93  	}
    94  }
    95  
    96  func DateStringToTimestamp(vectors []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
    97  	inputVector := vectors[0]
    98  	resultType := types.Type{Oid: types.T_timestamp, Precision: 6, Size: 8}
    99  	inputValues := vector.MustStrCols(inputVector)
   100  
   101  	if inputVector.IsScalar() {
   102  		if inputVector.ConstVectorIsNull() {
   103  			return proc.AllocScalarNullVector(resultType), nil
   104  		}
   105  		resultVector := vector.NewConst(resultType, 1)
   106  		resultValues := make([]types.Timestamp, 1)
   107  		vector.SetCol(resultVector, timestamp.DateStringToTimestamp(proc.SessionInfo.TimeZone, inputValues, resultVector.Nsp, resultValues))
   108  		return resultVector, nil
   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.Timestamp](resultVector)
   115  		timestamp.DateStringToTimestamp(proc.SessionInfo.TimeZone, inputValues, resultVector.Nsp, resultValues)
   116  		return resultVector, nil
   117  	}
   118  }