github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/builtin/unary/time.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/time"
    21  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    22  )
    23  
    24  func TimeToTime(vectors []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
    25  	inputVector := vectors[0]
    26  	resultType := types.Type{Oid: types.T_time, Size: 8, Precision: inputVector.Typ.Precision}
    27  	inputValues := vector.MustTCols[types.Time](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.Time, 1)
    34  		copy(resultValues, inputValues)
    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.Time](resultVector)
    43  		copy(resultValues, inputValues)
    44  		return resultVector, nil
    45  	}
    46  }
    47  
    48  func DatetimeToTime(vectors []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
    49  	inputVector := vectors[0]
    50  	inputPrecision := inputVector.Typ.Precision
    51  	resultType := types.Type{Oid: types.T_time, Size: 8, Precision: inputPrecision}
    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.Time, 1)
    59  		vector.SetCol(resultVector, time.DatetimeToTime(inputValues, resultValues, inputPrecision))
    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.Time](resultVector)
    67  		time.DatetimeToTime(inputValues, resultValues, inputPrecision)
    68  		return resultVector, nil
    69  	}
    70  }
    71  
    72  func DateToTime(vectors []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
    73  	inputVector := vectors[0]
    74  	resultType := types.Type{Oid: types.T_time, Size: 8}
    75  	inputValues := vector.MustTCols[types.Date](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.Time, 1)
    82  		vector.SetCol(resultVector, time.DateToTime(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.Time](resultVector)
    90  		time.DateToTime(inputValues, resultValues)
    91  		return resultVector, nil
    92  	}
    93  }
    94  
    95  func DateStringToTime(vectors []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
    96  	inputVector := vectors[0]
    97  	resultType := types.Type{Oid: types.T_time, Size: 8}
    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.Time, 1)
   106  		result, err := time.DateStringToTime(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.Time](resultVector)
   115  		_, err = time.DateStringToTime(inputValues, resultValues)
   116  		return resultVector, err
   117  	}
   118  }
   119  
   120  func Int64ToTime(vectors []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
   121  	inputVector := vectors[0]
   122  	resultType := types.Type{Oid: types.T_time, Size: 8}
   123  	inputValues := vector.MustTCols[int64](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.Time, 1)
   131  		result, err := time.Int64ToTime(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.Time](resultVector)
   140  		_, err = time.Int64ToTime(inputValues, resultValues)
   141  		return resultVector, err
   142  	}
   143  }
   144  
   145  func Decimal128ToTime(vectors []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
   146  	inputVector := vectors[0]
   147  	resultType := types.Type{Oid: types.T_time, Size: 8, Precision: inputVector.Typ.Scale}
   148  	inputValues := vector.MustTCols[types.Decimal128](inputVector)
   149  
   150  	if inputVector.IsScalar() {
   151  		if inputVector.ConstVectorIsNull() {
   152  			return proc.AllocScalarNullVector(resultType), nil
   153  		}
   154  		resultVector := vector.NewConst(resultType, 1)
   155  		resultValues := make([]types.Time, 1)
   156  		result, err := time.Decimal128ToTime(inputValues, resultValues)
   157  		vector.SetCol(resultVector, result)
   158  		return resultVector, err
   159  	} else {
   160  		resultVector, err := proc.AllocVectorOfRows(resultType, int64(len(inputValues)), inputVector.Nsp)
   161  		if err != nil {
   162  			return nil, err
   163  		}
   164  		resultValues := vector.MustTCols[types.Time](resultVector)
   165  		_, err = time.Decimal128ToTime(inputValues, resultValues)
   166  		return resultVector, err
   167  	}
   168  }