github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/builtin/multi/format.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 multi
    16  
    17  import (
    18  	"github.com/matrixorigin/matrixone/pkg/container/nulls"
    19  	"github.com/matrixorigin/matrixone/pkg/container/types"
    20  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    21  	"github.com/matrixorigin/matrixone/pkg/vectorize/format"
    22  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    23  )
    24  
    25  func Format(vecs []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
    26  	var paramNum = len(vecs)
    27  	if vecs[0].IsScalarNull() || vecs[1].IsScalarNull() {
    28  		return proc.AllocScalarNullVector(vecs[0].Typ), nil
    29  	}
    30  
    31  	//get the first arg number
    32  	numberCols := vector.MustStrCols(vecs[0])
    33  	//get the second arg precision
    34  	precisionCols := vector.MustStrCols(vecs[1])
    35  	//get the third arg locale
    36  	var localeCols []string
    37  	if paramNum == 2 || vecs[2].IsScalarNull() {
    38  		localeCols = []string{"en_US"}
    39  	} else {
    40  		localeCols = vector.MustStrCols(vecs[2])
    41  	}
    42  
    43  	//calcute rows
    44  	rowCount := vector.Length(vecs[0])
    45  
    46  	var resultVec *vector.Vector = nil
    47  	resultValues := make([]string, rowCount)
    48  	resultNsp := nulls.NewWithSize(rowCount)
    49  
    50  	// set null row
    51  	nulls.Or(vecs[0].Nsp, vecs[1].Nsp, resultNsp)
    52  
    53  	var constVectors []bool
    54  	if paramNum == 2 || vecs[2].IsScalarNull() {
    55  		constVectors = []bool{vecs[0].IsScalar(), vecs[1].IsScalar(), true}
    56  	} else {
    57  		constVectors = []bool{vecs[0].IsScalar(), vecs[1].IsScalar(), vecs[2].IsScalar()}
    58  	}
    59  
    60  	//get result values
    61  	err := format.Format(numberCols, precisionCols, localeCols, rowCount, constVectors, resultValues)
    62  	if err != nil {
    63  		return nil, err
    64  	}
    65  	resultVec = vector.NewWithStrings(types.T_varchar.ToType(), resultValues, resultNsp, proc.Mp())
    66  
    67  	return resultVec, nil
    68  }