github.com/matrixorigin/matrixone@v1.2.0/pkg/testutil/util_make.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 testutil
    16  
    17  import (
    18  	"github.com/matrixorigin/matrixone/pkg/common/mpool"
    19  	"github.com/matrixorigin/matrixone/pkg/container/nulls"
    20  	"github.com/matrixorigin/matrixone/pkg/container/types"
    21  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    22  )
    23  
    24  // All vectors generated by the Make Function, their memory is not allocated through the memory pool
    25  // if you want to generate a vector in memory pool, use NewFunction to instead of MakeFunction.
    26  var (
    27  	TestUtilMp = mpool.MustNewZeroNoFixed()
    28  
    29  	MakeInt64Vector = func(values []int64, nsp []uint64) *vector.Vector {
    30  		return makeVector(values, nsp, int64Type)
    31  	}
    32  
    33  	MakeInt32Vector = func(values []int32, nsp []uint64) *vector.Vector {
    34  		return makeVector(values, nsp, int32Type)
    35  	}
    36  
    37  	MakeInt16Vector = func(values []int16, nsp []uint64) *vector.Vector {
    38  		return makeVector(values, nsp, int16Type)
    39  	}
    40  
    41  	MakeRowIdVector = func(values []types.Rowid, nsp []uint64) *vector.Vector {
    42  		return makeVector(values, nsp, rowIdType)
    43  	}
    44  
    45  	MakeInt8Vector = func(values []int8, nsp []uint64) *vector.Vector {
    46  		return makeVector(values, nsp, int8Type)
    47  	}
    48  
    49  	MakeUint16Vector = func(values []uint16, nsp []uint64) *vector.Vector {
    50  		return makeVector(values, nsp, uint16Type)
    51  	}
    52  
    53  	MakeVarcharVector = func(values []string, nsp []uint64) *vector.Vector {
    54  		return makeStringVector(values, nsp, varcharType)
    55  	}
    56  
    57  	MakeTextVector = func(values []string, nsp []uint64) *vector.Vector {
    58  		return makeStringVector(values, nsp, textType)
    59  	}
    60  )
    61  
    62  // functions to make a scalar vector for test.
    63  var (
    64  	MakeScalarNull = func(typ types.T, length int) *vector.Vector {
    65  		return vector.NewConstNull(typ.ToType(), length, NewProc().Mp())
    66  	}
    67  	MakeScalarInt64 = func(v int64, length int) *vector.Vector {
    68  		return makeScalar(v, length, int64Type)
    69  	}
    70  
    71  	MakeScalarVarchar = func(value string, length int) *vector.Vector {
    72  		return makeScalarString(value, length, varcharType)
    73  	}
    74  )
    75  
    76  func makeVector[T types.FixedSizeT](values []T, nsp []uint64, typ types.Type) *vector.Vector {
    77  	vec := vector.NewVec(typ)
    78  	err := vector.AppendFixedList(vec, values, nil, TestUtilMp)
    79  	vec.SetNulls(nulls.Build(len(values), nsp...))
    80  
    81  	if err != nil {
    82  		panic(err)
    83  	}
    84  	return vec
    85  }
    86  
    87  func makeStringVector(values []string, nsp []uint64, typ types.Type) *vector.Vector {
    88  	var err error
    89  
    90  	vec := vector.NewVec(typ)
    91  	if nsp == nil {
    92  		err = vector.AppendStringList(vec, values, nil, TestUtilMp)
    93  	} else {
    94  		err = vector.AppendStringList(vec, values, nil, TestUtilMp)
    95  		vec.SetNulls(nulls.Build(len(values), nsp...))
    96  	}
    97  	if err != nil {
    98  		panic(err)
    99  	}
   100  
   101  	return vec
   102  }
   103  
   104  func makeScalar[T types.FixedSizeT](value T, length int, typ types.Type) *vector.Vector {
   105  	v, err := vector.NewConstFixed(typ, value, length, TestUtilMp)
   106  	if err != nil {
   107  		panic(err)
   108  	}
   109  	return v
   110  }
   111  
   112  func makeScalarString(value string, length int, typ types.Type) *vector.Vector {
   113  	v, err := vector.NewConstBytes(typ, []byte(value), length, TestUtilMp)
   114  	if err != nil {
   115  		panic(err)
   116  	}
   117  	return v
   118  }