github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/builtin/multi/serial.go (about)

     1  // Copyright 2021 - 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/common/moerr"
    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  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    23  )
    24  
    25  // Serial have a similar function named SerialWithCompacted in the index_util
    26  // Serial func is used by users, the function make true when input vec have ten
    27  // rows, the output vec is ten rows, when the vectors have null value, the output
    28  // vec will set the row null
    29  // for example:
    30  // input vec is [[1, 1, 1], [2, 2, null], [3, 3, 3]]
    31  // result vec is [serial(1, 2, 3), serial(1, 2, 3), null]
    32  func Serial(vectors []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
    33  	for _, v := range vectors {
    34  		if v.IsScalar() {
    35  			return nil, moerr.NewConstraintViolation(proc.Ctx, "serial function don't support constant value")
    36  		}
    37  	}
    38  	return SerialWithSomeCols(vectors, proc)
    39  }
    40  
    41  func SerialWithSomeCols(vectors []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
    42  	length := vector.Length(vectors[0])
    43  	vct := types.T_varchar.ToType()
    44  	val := make([][]byte, 0, length)
    45  	ps := types.NewPackerArray(length, proc.Mp())
    46  	bitMap := new(nulls.Nulls)
    47  
    48  	for _, v := range vectors {
    49  		switch v.Typ.Oid {
    50  		case types.T_bool:
    51  			s := vector.MustTCols[bool](v)
    52  			for i, b := range s {
    53  				if nulls.Contains(v.Nsp, uint64(i)) {
    54  					nulls.Add(bitMap, uint64(i))
    55  				} else {
    56  					ps[i].EncodeBool(b)
    57  				}
    58  			}
    59  		case types.T_int8:
    60  			s := vector.MustTCols[int8](v)
    61  			for i, b := range s {
    62  				if nulls.Contains(v.Nsp, uint64(i)) {
    63  					nulls.Add(bitMap, uint64(i))
    64  				} else {
    65  					ps[i].EncodeInt8(b)
    66  				}
    67  			}
    68  		case types.T_int16:
    69  			s := vector.MustTCols[int16](v)
    70  			for i, b := range s {
    71  				if nulls.Contains(v.Nsp, uint64(i)) {
    72  					nulls.Add(bitMap, uint64(i))
    73  				} else {
    74  					ps[i].EncodeInt16(b)
    75  				}
    76  			}
    77  		case types.T_int32:
    78  			s := vector.MustTCols[int32](v)
    79  			for i, b := range s {
    80  				if nulls.Contains(v.Nsp, uint64(i)) {
    81  					nulls.Add(bitMap, uint64(i))
    82  				} else {
    83  					ps[i].EncodeInt32(b)
    84  				}
    85  			}
    86  		case types.T_int64:
    87  			s := vector.MustTCols[int64](v)
    88  			for i, b := range s {
    89  				if nulls.Contains(v.Nsp, uint64(i)) {
    90  					nulls.Add(bitMap, uint64(i))
    91  				} else {
    92  					ps[i].EncodeInt64(b)
    93  				}
    94  			}
    95  		case types.T_uint8:
    96  			s := vector.MustTCols[uint8](v)
    97  			for i, b := range s {
    98  				if nulls.Contains(v.Nsp, uint64(i)) {
    99  					nulls.Add(bitMap, uint64(i))
   100  				} else {
   101  					ps[i].EncodeUint8(b)
   102  				}
   103  			}
   104  		case types.T_uint16:
   105  			s := vector.MustTCols[uint16](v)
   106  			for i, b := range s {
   107  				if nulls.Contains(v.Nsp, uint64(i)) {
   108  					nulls.Add(bitMap, uint64(i))
   109  				} else {
   110  					ps[i].EncodeUint16(b)
   111  				}
   112  			}
   113  		case types.T_uint32:
   114  			s := vector.MustTCols[uint32](v)
   115  			for i, b := range s {
   116  				if nulls.Contains(v.Nsp, uint64(i)) {
   117  					nulls.Add(bitMap, uint64(i))
   118  				} else {
   119  					ps[i].EncodeUint32(b)
   120  				}
   121  			}
   122  		case types.T_uint64:
   123  			s := vector.MustTCols[uint64](v)
   124  			for i, b := range s {
   125  				if nulls.Contains(v.Nsp, uint64(i)) {
   126  					nulls.Add(bitMap, uint64(i))
   127  				} else {
   128  					ps[i].EncodeUint64(b)
   129  				}
   130  			}
   131  		case types.T_float32:
   132  			s := vector.MustTCols[float32](v)
   133  			for i, b := range s {
   134  				if nulls.Contains(v.Nsp, uint64(i)) {
   135  					nulls.Add(bitMap, uint64(i))
   136  				} else {
   137  					ps[i].EncodeFloat32(b)
   138  				}
   139  			}
   140  		case types.T_float64:
   141  			s := vector.MustTCols[float64](v)
   142  			for i, b := range s {
   143  				if nulls.Contains(v.Nsp, uint64(i)) {
   144  					nulls.Add(bitMap, uint64(i))
   145  				} else {
   146  					ps[i].EncodeFloat64(b)
   147  				}
   148  			}
   149  		case types.T_date:
   150  			s := vector.MustTCols[types.Date](v)
   151  			for i, b := range s {
   152  				if nulls.Contains(v.Nsp, uint64(i)) {
   153  					nulls.Add(bitMap, uint64(i))
   154  				} else {
   155  					ps[i].EncodeDate(b)
   156  				}
   157  			}
   158  		case types.T_time:
   159  			s := vector.MustTCols[types.Time](v)
   160  			for i, b := range s {
   161  				if nulls.Contains(v.Nsp, uint64(i)) {
   162  					nulls.Add(bitMap, uint64(i))
   163  				} else {
   164  					ps[i].EncodeTime(b)
   165  				}
   166  			}
   167  		case types.T_datetime:
   168  			s := vector.MustTCols[types.Datetime](v)
   169  			for i, b := range s {
   170  				if nulls.Contains(v.Nsp, uint64(i)) {
   171  					nulls.Add(bitMap, uint64(i))
   172  				} else {
   173  					ps[i].EncodeDatetime(b)
   174  				}
   175  			}
   176  		case types.T_timestamp:
   177  			s := vector.MustTCols[types.Timestamp](v)
   178  			for i, b := range s {
   179  				if nulls.Contains(v.Nsp, uint64(i)) {
   180  					nulls.Add(bitMap, uint64(i))
   181  				} else {
   182  					ps[i].EncodeTimestamp(b)
   183  				}
   184  			}
   185  		case types.T_decimal64:
   186  			s := vector.MustTCols[types.Decimal64](v)
   187  			for i, b := range s {
   188  				if nulls.Contains(v.Nsp, uint64(i)) {
   189  					nulls.Add(bitMap, uint64(i))
   190  				} else {
   191  					ps[i].EncodeDecimal64(b)
   192  				}
   193  			}
   194  		case types.T_decimal128:
   195  			s := vector.MustTCols[types.Decimal128](v)
   196  			for i, b := range s {
   197  				if nulls.Contains(v.Nsp, uint64(i)) {
   198  					nulls.Add(bitMap, uint64(i))
   199  				} else {
   200  					ps[i].EncodeDecimal128(b)
   201  				}
   202  			}
   203  		case types.T_json, types.T_char, types.T_varchar, types.T_blob, types.T_text:
   204  			vs := vector.GetStrVectorValues(v)
   205  			for i := range vs {
   206  				if nulls.Contains(v.Nsp, uint64(i)) {
   207  					nulls.Add(bitMap, uint64(i))
   208  				} else {
   209  					ps[i].EncodeStringType([]byte(vs[i]))
   210  				}
   211  			}
   212  		}
   213  	}
   214  
   215  	for i := range ps {
   216  		val = append(val, ps[i].GetBuf())
   217  	}
   218  
   219  	vec := vector.NewWithBytes(vct, val, bitMap, proc.Mp())
   220  
   221  	for _, p := range ps {
   222  		p.FreeMem()
   223  	}
   224  	return vec, nil
   225  }