github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/tae/tables/functions.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 tables
    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/engine/tae/catalog"
    23  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/common"
    24  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/compute"
    25  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/containers"
    26  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/iface/txnif"
    27  )
    28  
    29  var dedupNABlkFunctions = map[types.T]any{
    30  	types.T_bool:       dedupNABlkFuncFactory(compute.CompareBool),
    31  	types.T_bit:        dedupNABlkOrderedFunc[uint64],
    32  	types.T_int8:       dedupNABlkOrderedFunc[int8],
    33  	types.T_int16:      dedupNABlkOrderedFunc[int16],
    34  	types.T_int32:      dedupNABlkOrderedFunc[int32],
    35  	types.T_int64:      dedupNABlkOrderedFunc[int64],
    36  	types.T_uint8:      dedupNABlkOrderedFunc[uint8],
    37  	types.T_uint16:     dedupNABlkOrderedFunc[uint16],
    38  	types.T_uint32:     dedupNABlkOrderedFunc[uint32],
    39  	types.T_uint64:     dedupNABlkOrderedFunc[uint64],
    40  	types.T_float32:    dedupNABlkOrderedFunc[float32],
    41  	types.T_float64:    dedupNABlkOrderedFunc[float64],
    42  	types.T_timestamp:  dedupNABlkOrderedFunc[types.Timestamp],
    43  	types.T_date:       dedupNABlkOrderedFunc[types.Date],
    44  	types.T_time:       dedupNABlkOrderedFunc[types.Time],
    45  	types.T_datetime:   dedupNABlkOrderedFunc[types.Datetime],
    46  	types.T_enum:       dedupNABlkOrderedFunc[types.Enum],
    47  	types.T_decimal64:  dedupNABlkFuncFactory(types.CompareDecimal64),
    48  	types.T_decimal128: dedupNABlkFuncFactory(types.CompareDecimal128),
    49  	types.T_decimal256: dedupNABlkFuncFactory(types.CompareDecimal256),
    50  	types.T_TS:         dedupNABlkFuncFactory(types.CompareTSTSAligned),
    51  	types.T_Rowid:      dedupNABlkFuncFactory(types.CompareRowidRowidAligned),
    52  	types.T_Blockid:    dedupNABlkFuncFactory(types.CompareBlockidBlockidAligned),
    53  	types.T_uuid:       dedupNABlkFuncFactory(types.CompareUuid),
    54  
    55  	types.T_char:      dedupNABlkBytesFunc,
    56  	types.T_varchar:   dedupNABlkBytesFunc,
    57  	types.T_blob:      dedupNABlkBytesFunc,
    58  	types.T_binary:    dedupNABlkBytesFunc,
    59  	types.T_varbinary: dedupNABlkBytesFunc,
    60  	types.T_json:      dedupNABlkBytesFunc,
    61  	types.T_text:      dedupNABlkBytesFunc,
    62  
    63  	types.T_array_float32: dedupNABlkBytesFunc,
    64  	types.T_array_float64: dedupNABlkBytesFunc,
    65  }
    66  
    67  var dedupAlkFunctions = map[types.T]any{
    68  	types.T_bool:       dedupABlkFuncFactory(compute.CompareBool),
    69  	types.T_bit:        dedupABlkFuncFactory(compute.CompareOrdered[uint64]),
    70  	types.T_int8:       dedupABlkFuncFactory(compute.CompareOrdered[int8]),
    71  	types.T_int16:      dedupABlkFuncFactory(compute.CompareOrdered[int16]),
    72  	types.T_int32:      dedupABlkFuncFactory(compute.CompareOrdered[int32]),
    73  	types.T_int64:      dedupABlkFuncFactory(compute.CompareOrdered[int64]),
    74  	types.T_uint8:      dedupABlkFuncFactory(compute.CompareOrdered[uint8]),
    75  	types.T_uint16:     dedupABlkFuncFactory(compute.CompareOrdered[uint16]),
    76  	types.T_uint32:     dedupABlkFuncFactory(compute.CompareOrdered[uint32]),
    77  	types.T_uint64:     dedupABlkFuncFactory(compute.CompareOrdered[uint64]),
    78  	types.T_float32:    dedupABlkFuncFactory(compute.CompareOrdered[float32]),
    79  	types.T_float64:    dedupABlkFuncFactory(compute.CompareOrdered[float64]),
    80  	types.T_timestamp:  dedupABlkFuncFactory(compute.CompareOrdered[types.Timestamp]),
    81  	types.T_date:       dedupABlkFuncFactory(compute.CompareOrdered[types.Date]),
    82  	types.T_time:       dedupABlkFuncFactory(compute.CompareOrdered[types.Time]),
    83  	types.T_datetime:   dedupABlkFuncFactory(compute.CompareOrdered[types.Datetime]),
    84  	types.T_enum:       dedupABlkFuncFactory(compute.CompareOrdered[types.Enum]),
    85  	types.T_decimal64:  dedupABlkFuncFactory(types.CompareDecimal64),
    86  	types.T_decimal128: dedupABlkFuncFactory(types.CompareDecimal128),
    87  	types.T_decimal256: dedupABlkFuncFactory(types.CompareDecimal256),
    88  	types.T_TS:         dedupABlkFuncFactory(types.CompareTSTSAligned),
    89  	types.T_Rowid:      dedupABlkFuncFactory(types.CompareRowidRowidAligned),
    90  	types.T_Blockid:    dedupABlkFuncFactory(types.CompareBlockidBlockidAligned),
    91  	types.T_uuid:       dedupABlkFuncFactory(types.CompareUuid),
    92  
    93  	types.T_char:      dedupABlkBytesFunc,
    94  	types.T_varchar:   dedupABlkBytesFunc,
    95  	types.T_blob:      dedupABlkBytesFunc,
    96  	types.T_binary:    dedupABlkBytesFunc,
    97  	types.T_varbinary: dedupABlkBytesFunc,
    98  	types.T_json:      dedupABlkBytesFunc,
    99  	types.T_text:      dedupABlkBytesFunc,
   100  
   101  	types.T_array_float32: dedupABlkBytesFunc,
   102  	types.T_array_float64: dedupABlkBytesFunc,
   103  }
   104  
   105  func parseNADedeupArgs(args ...any) (vec *vector.Vector, mask *nulls.Bitmap, def *catalog.ColDef) {
   106  	vec = args[0].(containers.Vector).GetDownstreamVector()
   107  	if args[1] != nil {
   108  		mask = args[1].(*nulls.Bitmap)
   109  	}
   110  	if args[2] != nil {
   111  		def = args[2].(*catalog.ColDef)
   112  	}
   113  	return
   114  }
   115  
   116  func parseADedeupArgs(args ...any) (
   117  	vec containers.Vector, mask *nulls.Bitmap, def *catalog.ColDef,
   118  	scan func(uint16) (containers.Vector, error),
   119  	txn txnif.TxnReader,
   120  ) {
   121  	vec = args[0].(containers.Vector)
   122  	if args[1] != nil {
   123  		mask = args[1].(*nulls.Bitmap)
   124  	}
   125  	if args[2] != nil {
   126  		def = args[2].(*catalog.ColDef)
   127  	}
   128  	if args[3] != nil {
   129  		scan = args[3].(func(uint16) (containers.Vector, error))
   130  	}
   131  	if args[4] != nil {
   132  		txn = args[4].(txnif.TxnReader)
   133  	}
   134  	return
   135  }
   136  
   137  func dedupNABlkFuncFactory[T any](comp func(T, T) int) func(args ...any) func(T, bool, int) error {
   138  	return func(args ...any) func(T, bool, int) error {
   139  		vec, mask, def := parseNADedeupArgs(args...)
   140  		vs := vector.MustFixedCol[T](vec)
   141  		return func(v T, _ bool, row int) (err error) {
   142  			// logutil.Infof("row=%d,v=%v", row, v)
   143  			if _, existed := compute.GetOffsetWithFunc(
   144  				vs,
   145  				v,
   146  				comp,
   147  				mask,
   148  			); existed {
   149  				entry := common.TypeStringValue(*vec.GetType(), any(v), false)
   150  				return moerr.NewDuplicateEntryNoCtx(entry, def.Name)
   151  			}
   152  			return
   153  		}
   154  	}
   155  }
   156  
   157  func dedupNABlkBytesFunc(args ...any) func([]byte, bool, int) error {
   158  	vec, mask, def := parseNADedeupArgs(args...)
   159  	return func(v []byte, _ bool, row int) (err error) {
   160  		// logutil.Infof("row=%d,v=%v", row, v)
   161  		if _, existed := compute.GetOffsetOfBytes(
   162  			vec,
   163  			v,
   164  			mask,
   165  		); existed {
   166  			entry := common.TypeStringValue(*vec.GetType(), any(v), false)
   167  			return moerr.NewDuplicateEntryNoCtx(entry, def.Name)
   168  		}
   169  		return
   170  	}
   171  }
   172  
   173  func dedupNABlkOrderedFunc[T types.OrderedT](args ...any) func(T, bool, int) error {
   174  	vec, mask, def := parseNADedeupArgs(args...)
   175  	vs := vector.MustFixedCol[T](vec)
   176  	return func(v T, _ bool, row int) (err error) {
   177  		// logutil.Infof("row=%d,v=%v", row, v)
   178  		if _, existed := compute.GetOffsetOfOrdered(
   179  			vs,
   180  			v,
   181  			mask,
   182  		); existed {
   183  			entry := common.TypeStringValue(*vec.GetType(), any(v), false)
   184  			return moerr.NewDuplicateEntryNoCtx(entry, def.Name)
   185  		}
   186  		return
   187  	}
   188  }
   189  
   190  func dedupABlkBytesFunc(args ...any) func([]byte, bool, int) error {
   191  	vec, mask, def, scan, txn := parseADedeupArgs(args...)
   192  	return func(v1 []byte, _ bool, _ int) error {
   193  		var tsVec containers.Vector
   194  		defer func() {
   195  			if tsVec != nil {
   196  				tsVec.Close()
   197  				tsVec = nil
   198  			}
   199  		}()
   200  		return containers.ForeachWindowVarlen(
   201  			vec.GetDownstreamVector(),
   202  			0,
   203  			vec.Length(),
   204  			func(v2 []byte, _ bool, row int) (err error) {
   205  				// logutil.Infof("row=%d,v1=%v,v2=%v", row, v1, v2)
   206  				if mask.Contains(uint64(row)) {
   207  					return
   208  				}
   209  				if compute.CompareBytes(v1, v2) != 0 {
   210  					return
   211  				}
   212  				if tsVec == nil {
   213  					if tsVec, err = scan(0); err != nil {
   214  						return
   215  					}
   216  				}
   217  				commitTS := tsVec.Get(row).(types.TS)
   218  				startTS := txn.GetStartTS()
   219  				if commitTS.Greater(&startTS) {
   220  					return txnif.ErrTxnWWConflict
   221  				}
   222  				entry := common.TypeStringValue(*vec.GetType(), any(v1), false)
   223  				return moerr.NewDuplicateEntryNoCtx(entry, def.Name)
   224  			}, nil, nil)
   225  	}
   226  }
   227  
   228  func dedupABlkFuncFactory[T types.FixedSizeT](comp func(T, T) int) func(args ...any) func(T, bool, int) error {
   229  	return func(args ...any) func(T, bool, int) error {
   230  		vec, mask, def, scan, txn := parseADedeupArgs(args...)
   231  		return func(v1 T, _ bool, _ int) error {
   232  			var tsVec containers.Vector
   233  			defer func() {
   234  				if tsVec != nil {
   235  					tsVec.Close()
   236  					tsVec = nil
   237  				}
   238  			}()
   239  			return containers.ForeachWindowFixed(
   240  				vec.GetDownstreamVector(),
   241  				0,
   242  				vec.Length(),
   243  				func(v2 T, _ bool, row int) (err error) {
   244  					if mask.Contains(uint64(row)) {
   245  						return
   246  					}
   247  					if comp(v1, v2) != 0 {
   248  						return
   249  					}
   250  					if tsVec == nil {
   251  						if tsVec, err = scan(0); err != nil {
   252  							return
   253  						}
   254  					}
   255  					commitTS := tsVec.Get(row).(types.TS)
   256  					startTS := txn.GetStartTS()
   257  					if commitTS.Greater(&startTS) {
   258  						return txnif.ErrTxnWWConflict
   259  					}
   260  					entry := common.TypeStringValue(*vec.GetType(), any(v1), false)
   261  					return moerr.NewDuplicateEntryNoCtx(entry, def.Name)
   262  				}, nil, nil)
   263  		}
   264  	}
   265  }
   266  
   267  func dedupNABlkClosure(
   268  	vec containers.Vector,
   269  	txn txnif.TxnReader,
   270  	mask *nulls.Bitmap,
   271  	def *catalog.ColDef) func(any, bool, int) error {
   272  	return func(v any, _ bool, _ int) (err error) {
   273  		if _, existed := compute.GetOffsetByVal(vec, v, mask); existed {
   274  			entry := common.TypeStringValue(*vec.GetType(), v, false)
   275  			return moerr.NewDuplicateEntryNoCtx(entry, def.Name)
   276  		}
   277  		return nil
   278  	}
   279  }
   280  
   281  func dedupABlkClosureFactory(
   282  	scan func() (containers.Vector, error),
   283  ) func(
   284  	containers.Vector,
   285  	txnif.TxnReader,
   286  	*nulls.Bitmap,
   287  	*catalog.ColDef,
   288  ) func(any, bool, int) error {
   289  	return func(vec containers.Vector, txn txnif.TxnReader, mask *nulls.Bitmap, def *catalog.ColDef) func(any, bool, int) error {
   290  		return func(v1 any, _ bool, _ int) (err error) {
   291  			var tsVec containers.Vector
   292  			defer func() {
   293  				if tsVec != nil {
   294  					tsVec.Close()
   295  					tsVec = nil
   296  				}
   297  			}()
   298  			return vec.Foreach(func(v2 any, _ bool, row int) (err error) {
   299  				// logutil.Infof("%v, %v, %d", v1, v2, row)
   300  				if mask.Contains(uint64(row)) {
   301  					return
   302  				}
   303  				if compute.CompareGeneric(v1, v2, vec.GetType().Oid) != 0 {
   304  					return
   305  				}
   306  				if tsVec == nil {
   307  					tsVec, err = scan()
   308  					if err != nil {
   309  						return
   310  					}
   311  				}
   312  				commitTS := tsVec.Get(row).(types.TS)
   313  				startTS := txn.GetStartTS()
   314  				if commitTS.Greater(&startTS) {
   315  					return txnif.ErrTxnWWConflict
   316  				}
   317  				entry := common.TypeStringValue(*vec.GetType(), v1, false)
   318  				return moerr.NewDuplicateEntryNoCtx(entry, def.Name)
   319  			}, nil)
   320  		}
   321  	}
   322  }