github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/colexec/sample/poolData.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 sample
    16  
    17  import (
    18  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    19  	"github.com/matrixorigin/matrixone/pkg/common/mpool"
    20  	"github.com/matrixorigin/matrixone/pkg/container/batch"
    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  type poolData struct {
    27  	// validBatch stores the valid rows.
    28  	validBatch *batch.Batch
    29  
    30  	// invalidBatch stores the invalid rows.
    31  	// in fact, we only store one invalid row.
    32  	invalidBatch *batch.Batch
    33  }
    34  
    35  func (pd *poolData) appendValidRow(proc *process.Process, mp *mpool.MPool, bat *batch.Batch, offset int, length int) error {
    36  	if pd.validBatch == nil {
    37  		pd.validBatch = batch.NewWithSize(len(bat.Vecs))
    38  		for i := range pd.validBatch.Vecs {
    39  			pd.validBatch.Vecs[i] = proc.GetVector(*bat.Vecs[i].GetType())
    40  		}
    41  	}
    42  
    43  	for i := range pd.validBatch.Vecs {
    44  		if err := pd.validBatch.Vecs[i].UnionBatch(bat.Vecs[i], int64(offset), length, nil, mp); err != nil {
    45  			return err
    46  		}
    47  	}
    48  	pd.validBatch.AddRowCount(length)
    49  	return nil
    50  }
    51  
    52  func (pd *poolData) appendInvalidRow(proc *process.Process, mp *mpool.MPool, bat *batch.Batch, row int) error {
    53  	if pd.invalidBatch != nil {
    54  		return nil
    55  	}
    56  	pd.invalidBatch = batch.NewWithSize(len(bat.Vecs))
    57  	for i := range pd.invalidBatch.Vecs {
    58  		pd.invalidBatch.Vecs[i] = proc.GetVector(*bat.Vecs[i].GetType())
    59  	}
    60  
    61  	for i := range pd.invalidBatch.Vecs {
    62  		if err := pd.invalidBatch.Vecs[i].UnionBatch(bat.Vecs[i], int64(row), 1, nil, mp); err != nil {
    63  			return err
    64  		}
    65  	}
    66  	pd.invalidBatch.SetRowCount(1)
    67  	return nil
    68  }
    69  
    70  func (pd *poolData) replaceValidRow(mp *mpool.MPool, bat *batch.Batch, row1, row2 int) (err error) {
    71  	var right int
    72  	for i, vec := range bat.Vecs {
    73  		right = row2
    74  		if vec.IsConst() {
    75  			right = 0
    76  		}
    77  		if f := replaceMethods[vec.GetType().Oid]; f != nil {
    78  			err = f(pd.validBatch.Vecs[i], vec, row1, right, mp)
    79  		} else {
    80  			return moerr.NewInternalErrorNoCtx("unsupported type for sample pool.")
    81  		}
    82  		if err != nil {
    83  			return err
    84  		}
    85  	}
    86  	return nil
    87  }
    88  
    89  // flush returns the result of poolData and set the source pointer to be nil.
    90  // priority: validBatch > invalidBatch.
    91  func (pd *poolData) flush() (bat *batch.Batch) {
    92  	if pd.validBatch != nil {
    93  		bat = pd.validBatch
    94  		pd.validBatch = nil
    95  	} else {
    96  		bat = pd.invalidBatch
    97  		pd.invalidBatch = nil
    98  	}
    99  	return bat
   100  }
   101  
   102  func (pd *poolData) clean(mp *mpool.MPool) {
   103  	if pd.validBatch != nil {
   104  		pd.validBatch.Clean(mp)
   105  	}
   106  	if pd.invalidBatch != nil {
   107  		pd.invalidBatch.Clean(mp)
   108  	}
   109  }
   110  
   111  type replaceFunc func(toVec, fromVec *vector.Vector, row1, row2 int, mp *mpool.MPool) error
   112  
   113  var replaceMethods []replaceFunc
   114  
   115  func init() {
   116  	replaceMethods = make([]replaceFunc, 256)
   117  	replaceMethods[types.T_bit] = func(toVec, fromVec *vector.Vector, row1, row2 int, mp *mpool.MPool) error {
   118  		return vector.SetFixedAt[uint64](toVec, row1, vector.GetFixedAt[uint64](fromVec, row2))
   119  	}
   120  	replaceMethods[types.T_int8] = func(toVec, fromVec *vector.Vector, row1, row2 int, mp *mpool.MPool) error {
   121  		return vector.SetFixedAt[int8](toVec, row1, vector.GetFixedAt[int8](fromVec, row2))
   122  	}
   123  	replaceMethods[types.T_int16] = func(toVec, fromVec *vector.Vector, row1, row2 int, mp *mpool.MPool) error {
   124  		return vector.SetFixedAt[int16](toVec, row1, vector.GetFixedAt[int16](fromVec, row2))
   125  	}
   126  	replaceMethods[types.T_int32] = func(toVec, fromVec *vector.Vector, row1, row2 int, mp *mpool.MPool) error {
   127  		return vector.SetFixedAt[int32](toVec, row1, vector.GetFixedAt[int32](fromVec, row2))
   128  	}
   129  	replaceMethods[types.T_int64] = func(toVec, fromVec *vector.Vector, row1, row2 int, mp *mpool.MPool) error {
   130  		return vector.SetFixedAt[int64](toVec, row1, vector.GetFixedAt[int64](fromVec, row2))
   131  	}
   132  	replaceMethods[types.T_uint8] = func(toVec, fromVec *vector.Vector, row1, row2 int, mp *mpool.MPool) error {
   133  		return vector.SetFixedAt[uint8](toVec, row1, vector.GetFixedAt[uint8](fromVec, row2))
   134  	}
   135  	replaceMethods[types.T_uint16] = func(toVec, fromVec *vector.Vector, row1, row2 int, mp *mpool.MPool) error {
   136  		return vector.SetFixedAt[uint16](toVec, row1, vector.GetFixedAt[uint16](fromVec, row2))
   137  	}
   138  	replaceMethods[types.T_uint32] = func(toVec, fromVec *vector.Vector, row1, row2 int, mp *mpool.MPool) error {
   139  		return vector.SetFixedAt[uint32](toVec, row1, vector.GetFixedAt[uint32](fromVec, row2))
   140  	}
   141  	replaceMethods[types.T_uint64] = func(toVec, fromVec *vector.Vector, row1, row2 int, mp *mpool.MPool) error {
   142  		return vector.SetFixedAt[uint64](toVec, row1, vector.GetFixedAt[uint64](fromVec, row2))
   143  	}
   144  	replaceMethods[types.T_float32] = func(toVec, fromVec *vector.Vector, row1, row2 int, mp *mpool.MPool) error {
   145  		return vector.SetFixedAt[float32](toVec, row1, vector.GetFixedAt[float32](fromVec, row2))
   146  	}
   147  	replaceMethods[types.T_float64] = func(toVec, fromVec *vector.Vector, row1, row2 int, mp *mpool.MPool) error {
   148  		return vector.SetFixedAt[float64](toVec, row1, vector.GetFixedAt[float64](fromVec, row2))
   149  	}
   150  	replaceMethods[types.T_date] = func(toVec, fromVec *vector.Vector, row1, row2 int, mp *mpool.MPool) error {
   151  		return vector.SetFixedAt[types.Date](toVec, row1, vector.GetFixedAt[types.Date](fromVec, row2))
   152  	}
   153  	replaceMethods[types.T_datetime] = func(toVec, fromVec *vector.Vector, row1, row2 int, mp *mpool.MPool) error {
   154  		return vector.SetFixedAt[types.Datetime](toVec, row1, vector.GetFixedAt[types.Datetime](fromVec, row2))
   155  	}
   156  	replaceMethods[types.T_timestamp] = func(toVec, fromVec *vector.Vector, row1, row2 int, mp *mpool.MPool) error {
   157  		return vector.SetFixedAt[types.Timestamp](toVec, row1, vector.GetFixedAt[types.Timestamp](fromVec, row2))
   158  	}
   159  	replaceMethods[types.T_time] = func(toVec, fromVec *vector.Vector, row1, row2 int, mp *mpool.MPool) error {
   160  		return vector.SetFixedAt[types.Time](toVec, row1, vector.GetFixedAt[types.Time](fromVec, row2))
   161  	}
   162  	replaceMethods[types.T_enum] = func(toVec, fromVec *vector.Vector, row1, row2 int, mp *mpool.MPool) error {
   163  		return vector.SetFixedAt[types.Enum](toVec, row1, vector.GetFixedAt[types.Enum](fromVec, row2))
   164  	}
   165  	replaceMethods[types.T_decimal64] = func(toVec, fromVec *vector.Vector, row1, row2 int, mp *mpool.MPool) error {
   166  		return vector.SetFixedAt[types.Decimal64](toVec, row1, vector.GetFixedAt[types.Decimal64](fromVec, row2))
   167  	}
   168  	replaceMethods[types.T_decimal128] = func(toVec, fromVec *vector.Vector, row1, row2 int, mp *mpool.MPool) error {
   169  		return vector.SetFixedAt[types.Decimal128](toVec, row1, vector.GetFixedAt[types.Decimal128](fromVec, row2))
   170  	}
   171  	replaceMethods[types.T_TS] = func(toVec, fromVec *vector.Vector, row1, row2 int, mp *mpool.MPool) error {
   172  		return vector.SetFixedAt[types.TS](toVec, row1, vector.GetFixedAt[types.TS](fromVec, row2))
   173  	}
   174  	replaceMethods[types.T_Rowid] = func(toVec, fromVec *vector.Vector, row1, row2 int, mp *mpool.MPool) error {
   175  		return vector.SetFixedAt[types.Rowid](toVec, row1, vector.GetFixedAt[types.Rowid](fromVec, row2))
   176  	}
   177  
   178  	for _, oid := range []types.T{types.T_char, types.T_varchar, types.T_binary, types.T_varbinary,
   179  		types.T_json, types.T_blob, types.T_text,
   180  		types.T_array_float32, types.T_array_float64} {
   181  		replaceMethods[oid] = func(toVec, fromVec *vector.Vector, row1, row2 int, mp *mpool.MPool) error {
   182  			return vector.SetBytesAt(toVec, row1, fromVec.GetBytesAt(row2), mp)
   183  		}
   184  	}
   185  }