github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/builtin/unary/json_quote.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/container/types"
    19  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    20  	"github.com/matrixorigin/matrixone/pkg/vectorize/json_quote"
    21  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    22  )
    23  
    24  func JsonQuote(vecs []*vector.Vector, proc *process.Process) (ret *vector.Vector, err error) {
    25  	vec := vecs[0]
    26  	defer func() {
    27  		if err != nil && ret != nil {
    28  			ret.Free(proc.Mp())
    29  		}
    30  	}()
    31  	resultType := types.T_json.ToType()
    32  	if vec.IsScalarNull() {
    33  		ret = proc.AllocScalarNullVector(resultType)
    34  		return
    35  	}
    36  	vs := vector.MustStrCols(vec)
    37  	if vec.IsScalar() {
    38  		var dt []byte
    39  		dt, err = json_quote.Single(vs[0])
    40  		if err != nil {
    41  			return
    42  		}
    43  		ret = proc.AllocScalarVector(resultType)
    44  		err = vector.SetBytesAt(ret, 0, dt, proc.Mp())
    45  		return
    46  	}
    47  	ret, err = proc.AllocVectorOfRows(resultType, int64(vec.Length()), vec.Nsp)
    48  	if err != nil {
    49  		return
    50  	}
    51  	rs := vector.MustBytesCols(ret)
    52  	rs, err = json_quote.Batch(vs, rs, ret.Nsp)
    53  	if err != nil {
    54  		return
    55  	}
    56  	for i := 0; i < len(rs); i++ {
    57  		err = vector.SetBytesAt(ret, i, rs[i], proc.Mp())
    58  		if err != nil {
    59  			return
    60  		}
    61  	}
    62  	return
    63  }