github.com/matrixorigin/matrixone@v1.2.0/pkg/container/vector/functionTools.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 vector
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"github.com/matrixorigin/matrixone/pkg/common/bitmap"
    21  	"github.com/matrixorigin/matrixone/pkg/common/mpool"
    22  	"github.com/matrixorigin/matrixone/pkg/container/types"
    23  )
    24  
    25  // FunctionParameterWrapper is generated from a vector.
    26  // It hides the relevant details of vector (like scalar and contain null or not.)
    27  // and provides a series of methods to get values.
    28  type FunctionParameterWrapper[T types.FixedSizeT] interface {
    29  	// GetType will return the type info of wrapped parameter.
    30  	GetType() types.Type
    31  
    32  	// GetSourceVector return the source vector.
    33  	GetSourceVector() *Vector
    34  
    35  	// GetValue return the Idx th value and if it's null or not.
    36  	// watch that, if str type, GetValue will return the []types.Varlena directly.
    37  	GetValue(idx uint64) (T, bool)
    38  
    39  	// GetStrValue return the Idx th string value and if it's null or not.
    40  	//TODO: Later rename it to GetBytes as it makes more sense.
    41  	GetStrValue(idx uint64) ([]byte, bool)
    42  
    43  	// UnSafeGetAllValue return all the values.
    44  	// please use it carefully because we didn't check the null situation.
    45  	UnSafeGetAllValue() []T
    46  
    47  	WithAnyNullValue() bool
    48  }
    49  
    50  var _ FunctionParameterWrapper[int64] = &FunctionParameterNormal[int64]{}
    51  var _ FunctionParameterWrapper[int64] = &FunctionParameterWithoutNull[int64]{}
    52  var _ FunctionParameterWrapper[int64] = &FunctionParameterScalar[int64]{}
    53  var _ FunctionParameterWrapper[int64] = &FunctionParameterScalarNull[int64]{}
    54  var _ FunctionParameterWrapper[types.Varlena] = &FunctionParameterNormalSpecial1[types.Varlena]{}
    55  var _ FunctionParameterWrapper[types.Varlena] = &FunctionParameterWithoutNullSpecial1[types.Varlena]{}
    56  
    57  func GenerateFunctionFixedTypeParameter[T types.FixedSizeTExceptStrType](v *Vector) FunctionParameterWrapper[T] {
    58  	t := v.GetType()
    59  	if v.IsConstNull() {
    60  		return &FunctionParameterScalarNull[T]{
    61  			typ:          *t,
    62  			sourceVector: v,
    63  		}
    64  	}
    65  	cols := MustFixedCol[T](v)
    66  	if v.IsConst() {
    67  		return &FunctionParameterScalar[T]{
    68  			typ:          *t,
    69  			sourceVector: v,
    70  			scalarValue:  cols[0],
    71  		}
    72  	}
    73  	if !v.nsp.IsEmpty() {
    74  		return &FunctionParameterNormal[T]{
    75  			typ:          *t,
    76  			sourceVector: v,
    77  			values:       cols,
    78  			nullMap:      v.GetNulls().GetBitmap(),
    79  		}
    80  	}
    81  	return &FunctionParameterWithoutNull[T]{
    82  		typ:          *t,
    83  		sourceVector: v,
    84  		values:       cols,
    85  	}
    86  }
    87  
    88  func GenerateFunctionStrParameter(v *Vector) FunctionParameterWrapper[types.Varlena] {
    89  	t := v.GetType()
    90  	if v.IsConstNull() {
    91  		return &FunctionParameterScalarNull[types.Varlena]{
    92  			typ:          *t,
    93  			sourceVector: v,
    94  		}
    95  	}
    96  	var cols []types.Varlena
    97  	ToSlice(v, &cols)
    98  	if v.IsConst() {
    99  		return &FunctionParameterScalar[types.Varlena]{
   100  			typ:          *t,
   101  			sourceVector: v,
   102  			scalarValue:  cols[0],
   103  			scalarStr:    cols[0].GetByteSlice(v.area),
   104  		}
   105  	}
   106  
   107  	if !v.nsp.IsEmpty() {
   108  		if len(v.area) == 0 {
   109  			return &FunctionParameterNormalSpecial1[types.Varlena]{
   110  				typ:          *t,
   111  				sourceVector: v,
   112  				strValues:    cols,
   113  				nullMap:      v.GetNulls().GetBitmap(),
   114  			}
   115  		}
   116  		return &FunctionParameterNormal[types.Varlena]{
   117  			typ:          *t,
   118  			sourceVector: v,
   119  			strValues:    cols,
   120  			area:         v.area,
   121  			nullMap:      v.GetNulls().GetBitmap(),
   122  		}
   123  	}
   124  	if len(v.area) == 0 {
   125  		return &FunctionParameterWithoutNullSpecial1[types.Varlena]{
   126  			typ:          *t,
   127  			sourceVector: v,
   128  			strValues:    cols,
   129  		}
   130  	}
   131  	return &FunctionParameterWithoutNull[types.Varlena]{
   132  		typ:          *t,
   133  		sourceVector: v,
   134  		strValues:    cols,
   135  		area:         v.area,
   136  	}
   137  }
   138  
   139  // FunctionParameterNormal is a wrapper of normal vector which
   140  // may contains null value.
   141  type FunctionParameterNormal[T types.FixedSizeT] struct {
   142  	typ          types.Type
   143  	sourceVector *Vector
   144  	values       []T
   145  	strValues    []types.Varlena
   146  	area         []byte
   147  	nullMap      *bitmap.Bitmap
   148  }
   149  
   150  func (p *FunctionParameterNormal[T]) GetType() types.Type {
   151  	return p.typ
   152  }
   153  
   154  func (p *FunctionParameterNormal[T]) GetSourceVector() *Vector {
   155  	return p.sourceVector
   156  }
   157  
   158  func (p *FunctionParameterNormal[T]) GetValue(idx uint64) (value T, isNull bool) {
   159  	if p.nullMap.Contains(idx) {
   160  		return value, true
   161  	}
   162  	return p.values[idx], false
   163  }
   164  
   165  func (p *FunctionParameterNormal[T]) GetStrValue(idx uint64) (value []byte, isNull bool) {
   166  	if p.nullMap.Contains(idx) {
   167  		return nil, true
   168  	}
   169  	return p.strValues[idx].GetByteSlice(p.area), false
   170  }
   171  
   172  func (p *FunctionParameterNormal[T]) UnSafeGetAllValue() []T {
   173  	return p.values
   174  }
   175  
   176  func (p *FunctionParameterNormal[T]) WithAnyNullValue() bool {
   177  	return true
   178  }
   179  
   180  // FunctionParameterNormalSpecial1 is an optimized wrapper of string vector whose
   181  // string width <= types.VarlenaInlineSize
   182  type FunctionParameterNormalSpecial1[T types.FixedSizeT] struct {
   183  	typ          types.Type
   184  	sourceVector *Vector
   185  	strValues    []types.Varlena
   186  	nullMap      *bitmap.Bitmap
   187  }
   188  
   189  func (p *FunctionParameterNormalSpecial1[T]) GetType() types.Type {
   190  	return p.typ
   191  }
   192  
   193  func (p *FunctionParameterNormalSpecial1[T]) GetSourceVector() *Vector {
   194  	return p.sourceVector
   195  }
   196  
   197  func (p *FunctionParameterNormalSpecial1[T]) GetValue(_ uint64) (T, bool) {
   198  	panic("please use GetStrValue method.")
   199  }
   200  
   201  func (p *FunctionParameterNormalSpecial1[T]) GetStrValue(idx uint64) ([]byte, bool) {
   202  	if p.nullMap.Contains(idx) {
   203  		return nil, true
   204  	}
   205  	return p.strValues[idx].ByteSlice(), false
   206  }
   207  
   208  func (p *FunctionParameterNormalSpecial1[T]) UnSafeGetAllValue() []T {
   209  	panic("not implement")
   210  }
   211  
   212  func (p *FunctionParameterNormalSpecial1[T]) WithAnyNullValue() bool {
   213  	return true
   214  }
   215  
   216  // FunctionParameterWithoutNull is a wrapper of normal vector but
   217  // without null value.
   218  type FunctionParameterWithoutNull[T types.FixedSizeT] struct {
   219  	typ          types.Type
   220  	sourceVector *Vector
   221  	values       []T
   222  	strValues    []types.Varlena
   223  	area         []byte
   224  }
   225  
   226  func (p *FunctionParameterWithoutNull[T]) GetType() types.Type {
   227  	return p.typ
   228  }
   229  
   230  func (p *FunctionParameterWithoutNull[T]) GetSourceVector() *Vector {
   231  	return p.sourceVector
   232  }
   233  
   234  func (p *FunctionParameterWithoutNull[T]) GetValue(idx uint64) (T, bool) {
   235  	return p.values[idx], false
   236  }
   237  
   238  func (p *FunctionParameterWithoutNull[T]) GetStrValue(idx uint64) ([]byte, bool) {
   239  	return p.strValues[idx].GetByteSlice(p.area), false
   240  }
   241  
   242  func (p *FunctionParameterWithoutNull[T]) UnSafeGetAllValue() []T {
   243  	return p.values
   244  }
   245  
   246  func (p *FunctionParameterWithoutNull[T]) WithAnyNullValue() bool {
   247  	return false
   248  }
   249  
   250  // FunctionParameterWithoutNullSpecial1 is an optimized wrapper of string vector without null value and
   251  // whose string width <= types.VarlenaInlineSize
   252  type FunctionParameterWithoutNullSpecial1[T types.FixedSizeT] struct {
   253  	typ          types.Type
   254  	sourceVector *Vector
   255  	strValues    []types.Varlena
   256  }
   257  
   258  func (p *FunctionParameterWithoutNullSpecial1[T]) GetType() types.Type {
   259  	return p.typ
   260  }
   261  
   262  func (p *FunctionParameterWithoutNullSpecial1[T]) GetSourceVector() *Vector {
   263  	return p.sourceVector
   264  }
   265  
   266  func (p *FunctionParameterWithoutNullSpecial1[T]) GetValue(_ uint64) (T, bool) {
   267  	panic("please use GetStrValue method.")
   268  }
   269  
   270  func (p *FunctionParameterWithoutNullSpecial1[T]) GetStrValue(idx uint64) ([]byte, bool) {
   271  	return p.strValues[idx].ByteSlice(), false
   272  }
   273  
   274  func (p *FunctionParameterWithoutNullSpecial1[T]) UnSafeGetAllValue() []T {
   275  	panic("not implement")
   276  }
   277  
   278  func (p *FunctionParameterWithoutNullSpecial1[T]) WithAnyNullValue() bool {
   279  	return false
   280  }
   281  
   282  // FunctionParameterScalar is a wrapper of scalar vector.
   283  type FunctionParameterScalar[T types.FixedSizeT] struct {
   284  	typ          types.Type
   285  	sourceVector *Vector
   286  	scalarValue  T
   287  	scalarStr    []byte
   288  }
   289  
   290  func (p *FunctionParameterScalar[T]) GetType() types.Type {
   291  	return p.typ
   292  }
   293  
   294  func (p *FunctionParameterScalar[T]) GetSourceVector() *Vector {
   295  	return p.sourceVector
   296  }
   297  
   298  func (p *FunctionParameterScalar[T]) GetValue(_ uint64) (T, bool) {
   299  	return p.scalarValue, false
   300  }
   301  
   302  func (p *FunctionParameterScalar[T]) GetStrValue(_ uint64) ([]byte, bool) {
   303  	return p.scalarStr, false
   304  }
   305  
   306  func (p *FunctionParameterScalar[T]) UnSafeGetAllValue() []T {
   307  	return []T{p.scalarValue}
   308  }
   309  
   310  func (p *FunctionParameterScalar[T]) WithAnyNullValue() bool {
   311  	return false
   312  }
   313  
   314  // FunctionParameterScalarNull is a wrapper of scalar null vector.
   315  type FunctionParameterScalarNull[T types.FixedSizeT] struct {
   316  	typ          types.Type
   317  	sourceVector *Vector
   318  }
   319  
   320  func (p *FunctionParameterScalarNull[T]) GetType() types.Type {
   321  	return p.typ
   322  }
   323  
   324  func (p *FunctionParameterScalarNull[T]) GetSourceVector() *Vector {
   325  	return p.sourceVector
   326  }
   327  
   328  func (p *FunctionParameterScalarNull[T]) GetValue(_ uint64) (value T, isNull bool) {
   329  	return value, true
   330  }
   331  
   332  func (p *FunctionParameterScalarNull[T]) GetStrValue(_ uint64) ([]byte, bool) {
   333  	return nil, true
   334  }
   335  
   336  func (p *FunctionParameterScalarNull[T]) UnSafeGetAllValue() []T {
   337  	return nil
   338  }
   339  
   340  func (p *FunctionParameterScalarNull[T]) WithAnyNullValue() bool {
   341  	return true
   342  }
   343  
   344  type FunctionResultWrapper interface {
   345  	SetResultVector(vec *Vector)
   346  	GetResultVector() *Vector
   347  	Free()
   348  	PreExtendAndReset(size int) error
   349  }
   350  
   351  var _ FunctionResultWrapper = &FunctionResult[int64]{}
   352  
   353  type FunctionResult[T types.FixedSizeT] struct {
   354  	typ types.Type
   355  	vec *Vector
   356  	mp  *mpool.MPool
   357  
   358  	getVectorMethod func(typ types.Type) *Vector
   359  	putVectorMethod func(vec *Vector)
   360  
   361  	isVarlena bool
   362  	cols      []T
   363  	length    uint64
   364  }
   365  
   366  func MustFunctionResult[T types.FixedSizeT](wrapper FunctionResultWrapper) *FunctionResult[T] {
   367  	if fr, ok := wrapper.(*FunctionResult[T]); ok {
   368  		return fr
   369  	}
   370  	panic("wrong type for FunctionResultWrapper")
   371  }
   372  
   373  func newResultFunc[T types.FixedSizeT](
   374  	v *Vector,
   375  	getVectorMethod func(typ types.Type) *Vector,
   376  	putVectorMethod func(vec *Vector),
   377  	mp *mpool.MPool) *FunctionResult[T] {
   378  
   379  	f := &FunctionResult[T]{
   380  		typ:             *v.GetType(),
   381  		vec:             v,
   382  		mp:              mp,
   383  		getVectorMethod: getVectorMethod,
   384  		putVectorMethod: putVectorMethod,
   385  	}
   386  
   387  	var tempT T
   388  	var s interface{} = &tempT
   389  	if _, ok := s.(*types.Varlena); ok {
   390  		f.isVarlena = true
   391  	}
   392  	return f
   393  }
   394  
   395  func (fr *FunctionResult[T]) PreExtendAndReset(targetSize int) error {
   396  	if fr.vec == nil {
   397  		fr.vec = fr.getVectorMethod(fr.typ)
   398  	}
   399  
   400  	oldLength := fr.vec.Length()
   401  
   402  	if more := targetSize - oldLength; more > 0 {
   403  		if err := fr.vec.PreExtend(more, fr.mp); err != nil {
   404  			return err
   405  		}
   406  	}
   407  	fr.vec.Reset(fr.typ)
   408  
   409  	if !fr.isVarlena {
   410  		fr.length = 0
   411  		fr.vec.SetLength(targetSize)
   412  		if targetSize > oldLength {
   413  			fr.cols = MustFixedCol[T](fr.vec)
   414  		}
   415  	}
   416  	return nil
   417  }
   418  
   419  func (fr *FunctionResult[T]) Append(val T, isnull bool) error {
   420  	if isnull {
   421  		// XXX LOW PERF
   422  		// if we can expand the nulls while appending null first times.
   423  		// or we can append from last to first. can reduce a lot of expansion.
   424  		fr.vec.nsp.Add(fr.length)
   425  	} else {
   426  		fr.cols[fr.length] = val
   427  	}
   428  	fr.length++
   429  	return nil
   430  }
   431  
   432  func (fr *FunctionResult[T]) AppendBytes(val []byte, isnull bool) error {
   433  	if !fr.vec.IsConst() {
   434  		return AppendBytes(fr.vec, val, isnull, fr.mp)
   435  	} else if !isnull {
   436  		return SetConstBytes(fr.vec, val, fr.vec.Length(), fr.mp)
   437  	}
   438  	return nil
   439  }
   440  
   441  func (fr *FunctionResult[T]) AppendMustValue(val T) {
   442  	fr.cols[fr.length] = val
   443  	fr.length++
   444  }
   445  
   446  func (fr *FunctionResult[T]) AppendMustNull() {
   447  	fr.vec.nsp.Add(fr.length)
   448  	fr.length++
   449  }
   450  
   451  func (fr *FunctionResult[T]) AppendMustBytesValue(val []byte) error {
   452  	return AppendBytes(fr.vec, val, false, fr.mp)
   453  }
   454  
   455  func (fr *FunctionResult[T]) AppendMustNullForBytesResult() error {
   456  	var v T
   457  	return appendOneFixed(fr.vec, v, true, fr.mp)
   458  }
   459  
   460  func (fr *FunctionResult[T]) GetType() types.Type {
   461  	return *fr.vec.GetType()
   462  }
   463  
   464  func (fr *FunctionResult[T]) TempSetType(t types.Type) {
   465  	fr.vec.SetType(t)
   466  }
   467  
   468  func (fr *FunctionResult[T]) DupFromParameter(fp FunctionParameterWrapper[T], length int) (err error) {
   469  	for i := uint64(0); i < uint64(length); i++ {
   470  		v, null := fp.GetValue(i)
   471  		if err = fr.Append(v, null); err != nil {
   472  			return err
   473  		}
   474  	}
   475  	return err
   476  }
   477  
   478  func (fr *FunctionResult[T]) SetResultVector(v *Vector) {
   479  	fr.vec = v
   480  }
   481  
   482  func (fr *FunctionResult[T]) GetResultVector() *Vector {
   483  	return fr.vec
   484  }
   485  
   486  func (fr *FunctionResult[T]) ConvertToStrParameter() FunctionParameterWrapper[types.Varlena] {
   487  	return GenerateFunctionStrParameter(fr.vec)
   488  }
   489  
   490  func (fr *FunctionResult[T]) Free() {
   491  	if fr.vec != nil {
   492  		fr.putVectorMethod(fr.vec)
   493  		fr.vec = nil
   494  	}
   495  }
   496  
   497  func NewFunctionResultWrapper(
   498  	getVectorMethod func(typ types.Type) *Vector,
   499  	putVectorMethod func(vec *Vector),
   500  	typ types.Type,
   501  	mp *mpool.MPool) FunctionResultWrapper {
   502  	v := getVectorMethod(typ)
   503  
   504  	switch typ.Oid {
   505  	case types.T_char, types.T_varchar, types.T_blob, types.T_text, types.T_binary, types.T_varbinary,
   506  		types.T_array_float32, types.T_array_float64:
   507  		// IF STRING type.
   508  		return newResultFunc[types.Varlena](v, getVectorMethod, putVectorMethod, mp)
   509  	case types.T_json:
   510  		return newResultFunc[types.Varlena](v, getVectorMethod, putVectorMethod, mp)
   511  	}
   512  
   513  	switch typ.Oid {
   514  	case types.T_bool:
   515  		return newResultFunc[bool](v, getVectorMethod, putVectorMethod, mp)
   516  	case types.T_bit:
   517  		return newResultFunc[uint64](v, getVectorMethod, putVectorMethod, mp)
   518  	case types.T_int8:
   519  		return newResultFunc[int8](v, getVectorMethod, putVectorMethod, mp)
   520  	case types.T_int16:
   521  		return newResultFunc[int16](v, getVectorMethod, putVectorMethod, mp)
   522  	case types.T_int32:
   523  		return newResultFunc[int32](v, getVectorMethod, putVectorMethod, mp)
   524  	case types.T_int64:
   525  		return newResultFunc[int64](v, getVectorMethod, putVectorMethod, mp)
   526  	case types.T_uint8:
   527  		return newResultFunc[uint8](v, getVectorMethod, putVectorMethod, mp)
   528  	case types.T_uint16:
   529  		return newResultFunc[uint16](v, getVectorMethod, putVectorMethod, mp)
   530  	case types.T_uint32:
   531  		return newResultFunc[uint32](v, getVectorMethod, putVectorMethod, mp)
   532  	case types.T_uint64:
   533  		return newResultFunc[uint64](v, getVectorMethod, putVectorMethod, mp)
   534  	case types.T_float32:
   535  		return newResultFunc[float32](v, getVectorMethod, putVectorMethod, mp)
   536  	case types.T_float64:
   537  		return newResultFunc[float64](v, getVectorMethod, putVectorMethod, mp)
   538  	case types.T_date:
   539  		return newResultFunc[types.Date](v, getVectorMethod, putVectorMethod, mp)
   540  	case types.T_datetime:
   541  		return newResultFunc[types.Datetime](v, getVectorMethod, putVectorMethod, mp)
   542  	case types.T_time:
   543  		return newResultFunc[types.Time](v, getVectorMethod, putVectorMethod, mp)
   544  	case types.T_timestamp:
   545  		return newResultFunc[types.Timestamp](v, getVectorMethod, putVectorMethod, mp)
   546  	case types.T_decimal64:
   547  		return newResultFunc[types.Decimal64](v, getVectorMethod, putVectorMethod, mp)
   548  	case types.T_decimal128:
   549  		return newResultFunc[types.Decimal128](v, getVectorMethod, putVectorMethod, mp)
   550  	case types.T_TS:
   551  		return newResultFunc[types.TS](v, getVectorMethod, putVectorMethod, mp)
   552  	case types.T_Rowid:
   553  		return newResultFunc[types.Rowid](v, getVectorMethod, putVectorMethod, mp)
   554  	case types.T_Blockid:
   555  		return newResultFunc[types.Blockid](v, getVectorMethod, putVectorMethod, mp)
   556  	case types.T_uuid:
   557  		return newResultFunc[types.Uuid](v, getVectorMethod, putVectorMethod, mp)
   558  	case types.T_enum:
   559  		return newResultFunc[types.Enum](v, getVectorMethod, putVectorMethod, mp)
   560  	}
   561  	panic(fmt.Sprintf("unexpected type %s for function result", typ))
   562  }