github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/builtin/unary/oct.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/common/moerr"
    19  	"github.com/matrixorigin/matrixone/pkg/container/types"
    20  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    21  	"github.com/matrixorigin/matrixone/pkg/vectorize/oct"
    22  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    23  	"golang.org/x/exp/constraints"
    24  )
    25  
    26  func Oct[T constraints.Unsigned | constraints.Signed](vectors []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
    27  	inputVector := vectors[0]
    28  	resultType := types.Type{Oid: types.T_decimal128, Size: 16}
    29  	inputValues := vector.MustTCols[T](inputVector)
    30  	if inputVector.IsScalar() {
    31  		if inputVector.ConstVectorIsNull() {
    32  			return proc.AllocScalarNullVector(resultType), nil
    33  		}
    34  		resultVector := vector.NewConst(resultType, 1)
    35  		resultValues := make([]types.Decimal128, 1)
    36  		col, err := oct.Oct(inputValues, resultValues)
    37  		if err != nil {
    38  			return nil, err
    39  		}
    40  		vector.SetCol(resultVector, col)
    41  		return resultVector, nil
    42  	} else {
    43  		resultVector, err := proc.AllocVectorOfRows(resultType, int64(len(inputValues)), inputVector.Nsp)
    44  		if err != nil {
    45  			return nil, err
    46  		}
    47  		resultValues := vector.MustTCols[types.Decimal128](resultVector)
    48  		_, err = oct.Oct(inputValues, resultValues)
    49  		if err != nil {
    50  			return nil, err
    51  		}
    52  		return resultVector, nil
    53  	}
    54  }
    55  
    56  func OctFloat[T constraints.Float](vectors []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
    57  	inputVector := vectors[0]
    58  	resultType := types.Type{Oid: types.T_decimal128, Size: 16}
    59  	inputValues := vector.MustTCols[T](inputVector)
    60  	if inputVector.IsScalar() {
    61  		if inputVector.ConstVectorIsNull() {
    62  			return proc.AllocScalarNullVector(resultType), nil
    63  		}
    64  		resultVector := vector.NewConst(resultType, 1)
    65  		resultValues := make([]types.Decimal128, 1)
    66  		col, err := oct.OctFloat(inputValues, resultValues)
    67  		if err != nil {
    68  			return nil, moerr.NewInternalError(proc.Ctx, "the input value is out of integer range")
    69  		}
    70  		vector.SetCol(resultVector, col)
    71  		return resultVector, nil
    72  	} else {
    73  		resultVector, err := proc.AllocVectorOfRows(resultType, int64(len(inputValues)), inputVector.Nsp)
    74  		if err != nil {
    75  			return nil, err
    76  		}
    77  		resultValues := vector.MustTCols[types.Decimal128](resultVector)
    78  		_, err = oct.OctFloat(inputValues, resultValues)
    79  		if err != nil {
    80  			return nil, moerr.NewInternalError(proc.Ctx, "the input value is out of integer range")
    81  		}
    82  		return resultVector, nil
    83  	}
    84  }