github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/builtin/unary/hex.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  	"encoding/hex"
    19  	"fmt"
    20  
    21  	"github.com/matrixorigin/matrixone/pkg/container/types"
    22  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    23  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    24  )
    25  
    26  func HexString(vectors []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
    27  	inputVector := vectors[0]
    28  	resultType := types.New(types.T_varchar, types.MaxVarcharLen, 0, 0)
    29  	inputValues := vector.MustStrCols(inputVector)
    30  	if inputVector.IsScalar() {
    31  		if inputVector.ConstVectorIsNull() {
    32  			return proc.AllocScalarNullVector(resultType), nil
    33  		}
    34  		resultValues := make([]string, 1)
    35  		HexEncodeString(inputValues, resultValues)
    36  		return vector.NewConstString(resultType, inputVector.Length(), resultValues[0], proc.Mp()), nil
    37  	} else {
    38  		resultValues := make([]string, len(inputValues))
    39  		HexEncodeString(inputValues, resultValues)
    40  		return vector.NewWithStrings(resultType, resultValues, inputVector.Nsp, proc.Mp()), nil
    41  	}
    42  }
    43  
    44  func HexInt64(vectors []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
    45  	inputVector := vectors[0]
    46  	resultType := types.New(types.T_varchar, types.MaxVarcharLen, 0, 0)
    47  	inputValues := vector.MustTCols[int64](inputVector)
    48  	if inputVector.IsScalar() {
    49  		if inputVector.ConstVectorIsNull() {
    50  			return proc.AllocScalarNullVector(resultType), nil
    51  		}
    52  		resultValues := make([]string, 1)
    53  		HexEncodeInt64(inputValues, resultValues)
    54  		return vector.NewConstString(resultType, inputVector.Length(), resultValues[0], proc.Mp()), nil
    55  	} else {
    56  		resultValues := make([]string, len(inputValues))
    57  		HexEncodeInt64(inputValues, resultValues)
    58  		return vector.NewWithStrings(resultType, resultValues, inputVector.Nsp, proc.Mp()), nil
    59  	}
    60  }
    61  
    62  func HexEncodeString(xs []string, rs []string) []string {
    63  	for i, str := range xs {
    64  		dst := hex.EncodeToString([]byte(str))
    65  		rs[i] = dst
    66  	}
    67  	return rs
    68  }
    69  
    70  func HexEncodeInt64(xs []int64, rs []string) []string {
    71  	for i, str := range xs {
    72  		rs[i] = fmt.Sprintf("%X", str)
    73  	}
    74  	return rs
    75  }