github.com/matrixorigin/matrixone@v0.7.0/pkg/vectorize/unixtimestamp/unixtimestamp.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 unixtimestamp
    16  
    17  import (
    18  	"github.com/matrixorigin/matrixone/pkg/container/types"
    19  )
    20  
    21  var (
    22  	UnixTimestampToInt        func([]types.Timestamp, []int64) []int64
    23  	UnixTimestampToFloat      func([]types.Timestamp, []float64) []float64
    24  	UnixTimestampToDecimal128 func([]types.Timestamp, []types.Decimal128) ([]types.Decimal128, error)
    25  )
    26  
    27  func init() {
    28  	UnixTimestampToInt = unixTimestampToInt
    29  	UnixTimestampToFloat = unixTimestampToFloat
    30  	UnixTimestampToDecimal128 = unixTimestampToDecimal128
    31  }
    32  
    33  func unixTimestampToInt(xs []types.Timestamp, rs []int64) []int64 {
    34  	for i := range xs {
    35  		rs[i] = xs[i].Unix()
    36  	}
    37  	return rs
    38  }
    39  
    40  func unixTimestampToFloat(xs []types.Timestamp, rs []float64) []float64 {
    41  	for i := range xs {
    42  		rs[i] = xs[i].UnixToFloat()
    43  	}
    44  	return rs
    45  }
    46  
    47  func unixTimestampToDecimal128(xs []types.Timestamp, rs []types.Decimal128) ([]types.Decimal128, error) {
    48  	for i := range xs {
    49  		res, err := xs[i].UnixToDecimal128()
    50  		if err != nil {
    51  			return nil, err
    52  		}
    53  		rs[i] = res
    54  	}
    55  	return rs, nil
    56  }