github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/builtin/multi/floor.go (about)

     1  // Copyright 2021 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  	"strconv"
    19  
    20  	"github.com/matrixorigin/matrixone/pkg/container/types"
    21  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    22  	"github.com/matrixorigin/matrixone/pkg/vectorize/floor"
    23  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    24  )
    25  
    26  // floor function's evaluation for arguments: [uint64]
    27  func FloorUInt64(vecs []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
    28  	return generalMathMulti("floor", vecs, proc, floor.FloorUint64)
    29  }
    30  
    31  // floor function's evaluation for arguments: [int64]
    32  func FloorInt64(vecs []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
    33  	return generalMathMulti("floor", vecs, proc, floor.FloorInt64)
    34  }
    35  
    36  // Parse string to float instead of int.
    37  func FloorStr(vecs []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
    38  	values := vector.MustStrCols(vecs[0])
    39  	floatvector, err := proc.AllocVectorOfRows(types.T_float64.ToType(), 0, nil)
    40  	if err != nil {
    41  		return nil, err
    42  	}
    43  	for _, v := range values {
    44  		float, err := strconv.ParseFloat(v, 64)
    45  		if err != nil {
    46  			return nil, err
    47  		}
    48  		if err := floatvector.Append(float, false, proc.Mp()); err != nil {
    49  			floatvector.Free(proc.Mp())
    50  			return nil, err
    51  		}
    52  	}
    53  	newvecs := make([]*vector.Vector, 0)
    54  	newvecs = append(newvecs, floatvector)
    55  	return FloorFloat64(newvecs, proc)
    56  }
    57  
    58  // floor function's evaluation for arguments: [float64]
    59  func FloorFloat64(vecs []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
    60  	return generalMathMulti("floor", vecs, proc, floor.FloorFloat64)
    61  }
    62  
    63  func FloorDecimal128(vecs []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
    64  	scale := vecs[0].Typ.Scale
    65  	cb := func(vs []types.Decimal128, rs []types.Decimal128, digits int64) []types.Decimal128 {
    66  		return floor.FloorDecimal128(scale, vs, rs, digits)
    67  	}
    68  	return generalMathMulti("floor", vecs, proc, cb)
    69  }