github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/tae/compute/compute.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 compute
    16  
    17  import (
    18  	"bytes"
    19  
    20  	"github.com/matrixorigin/matrixone/pkg/container/nulls"
    21  	"github.com/matrixorigin/matrixone/pkg/container/types"
    22  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    23  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/containers"
    24  )
    25  
    26  func ShuffleByDeletes(inputDeletes, deletes *nulls.Bitmap) (outDeletes *nulls.Bitmap) {
    27  	if deletes.IsEmpty() || inputDeletes.IsEmpty() {
    28  		return inputDeletes
    29  	}
    30  	delIt := inputDeletes.GetBitmap().Iterator()
    31  	outDeletes = nulls.NewWithSize(1)
    32  	deleteIt := deletes.GetBitmap().Iterator()
    33  	deleteCnt := uint64(0)
    34  	for deleteIt.HasNext() {
    35  		del := deleteIt.Next()
    36  		for delIt.HasNext() {
    37  			row := delIt.PeekNext()
    38  			if row < del {
    39  				outDeletes.Add(row - deleteCnt)
    40  				delIt.Next()
    41  			} else if row == del {
    42  				delIt.Next()
    43  			} else {
    44  				break
    45  			}
    46  		}
    47  		deleteCnt++
    48  	}
    49  	for delIt.HasNext() {
    50  		row := delIt.Next()
    51  		outDeletes.Add(row - deleteCnt)
    52  	}
    53  
    54  	return outDeletes
    55  }
    56  
    57  func GetOffsetMapBeforeApplyDeletes(deletes *nulls.Bitmap) []uint32 {
    58  	if deletes.IsEmpty() {
    59  		return nil
    60  	}
    61  	prev := -1
    62  	mapping := make([]uint32, 0)
    63  	it := deletes.GetBitmap().Iterator()
    64  	for it.HasNext() {
    65  		del := it.Next()
    66  		for i := uint32(prev + 1); i < uint32(del); i++ {
    67  			mapping = append(mapping, i)
    68  		}
    69  		prev = int(del)
    70  	}
    71  	mapping = append(mapping, uint32(prev)+1)
    72  	return mapping
    73  }
    74  
    75  func GetOffsetOfBytes(
    76  	data *vector.Vector,
    77  	val []byte,
    78  	skipmask *nulls.Bitmap,
    79  ) (offset int, exist bool) {
    80  	start, end := 0, data.Length()-1
    81  	var mid int
    82  	for start <= end {
    83  		mid = (start + end) / 2
    84  		res := bytes.Compare(data.GetBytesAt(mid), val)
    85  		if res > 0 {
    86  			end = mid - 1
    87  		} else if res < 0 {
    88  			start = mid + 1
    89  		} else {
    90  			if skipmask != nil && skipmask.Contains(uint64(mid)) {
    91  				return
    92  			}
    93  			offset = mid
    94  			exist = true
    95  			return
    96  		}
    97  	}
    98  	return
    99  
   100  }
   101  
   102  func GetOffsetWithFunc[T any](
   103  	vals []T,
   104  	val T,
   105  	compare func(a, b T) int,
   106  	skipmask *nulls.Bitmap,
   107  ) (offset int, exist bool) {
   108  	start, end := 0, len(vals)-1
   109  	var mid int
   110  	for start <= end {
   111  		mid = (start + end) / 2
   112  		res := compare(vals[mid], val)
   113  		if res > 0 {
   114  			end = mid - 1
   115  		} else if res < 0 {
   116  			start = mid + 1
   117  		} else {
   118  			if skipmask != nil && skipmask.Contains(uint64(mid)) {
   119  				return
   120  			}
   121  			offset = mid
   122  			exist = true
   123  			return
   124  		}
   125  	}
   126  	return
   127  }
   128  
   129  func GetOffsetOfOrdered[T types.OrderedT](column []T, val T, skipmask *nulls.Bitmap) (offset int, exist bool) {
   130  	start, end := 0, len(column)-1
   131  	var mid int
   132  	for start <= end {
   133  		mid = (start + end) / 2
   134  		if column[mid] > val {
   135  			end = mid - 1
   136  		} else if column[mid] < val {
   137  			start = mid + 1
   138  		} else {
   139  			if skipmask != nil && skipmask.Contains(uint64(mid)) {
   140  				return
   141  			}
   142  			offset = mid
   143  			exist = true
   144  			return
   145  		}
   146  	}
   147  	return
   148  }
   149  
   150  func GetOffsetByVal(data containers.Vector, v any, skipmask *nulls.Bitmap) (offset int, exist bool) {
   151  	vec := data.GetDownstreamVector()
   152  	switch data.GetType().Oid {
   153  	case types.T_bool:
   154  		vs := vector.MustFixedCol[bool](vec)
   155  		return GetOffsetWithFunc(vs, v.(bool), CompareBool, skipmask)
   156  	case types.T_bit:
   157  		vs := vector.MustFixedCol[uint64](vec)
   158  		return GetOffsetOfOrdered(vs, v.(uint64), skipmask)
   159  	case types.T_int8:
   160  		vs := vector.MustFixedCol[int8](vec)
   161  		return GetOffsetOfOrdered(vs, v.(int8), skipmask)
   162  	case types.T_int16:
   163  		vs := vector.MustFixedCol[int16](vec)
   164  		return GetOffsetOfOrdered(vs, v.(int16), skipmask)
   165  	case types.T_int32:
   166  		vs := vector.MustFixedCol[int32](vec)
   167  		return GetOffsetOfOrdered(vs, v.(int32), skipmask)
   168  	case types.T_int64:
   169  		vs := vector.MustFixedCol[int64](vec)
   170  		return GetOffsetOfOrdered(vs, v.(int64), skipmask)
   171  	case types.T_uint8:
   172  		vs := vector.MustFixedCol[uint8](vec)
   173  		return GetOffsetOfOrdered(vs, v.(uint8), skipmask)
   174  	case types.T_uint16:
   175  		vs := vector.MustFixedCol[uint16](vec)
   176  		return GetOffsetOfOrdered(vs, v.(uint16), skipmask)
   177  	case types.T_uint32:
   178  		vs := vector.MustFixedCol[uint32](vec)
   179  		return GetOffsetOfOrdered(vs, v.(uint32), skipmask)
   180  	case types.T_uint64:
   181  		vs := vector.MustFixedCol[uint64](vec)
   182  		return GetOffsetOfOrdered(vs, v.(uint64), skipmask)
   183  	case types.T_float32:
   184  		vs := vector.MustFixedCol[float32](vec)
   185  		return GetOffsetOfOrdered(vs, v.(float32), skipmask)
   186  	case types.T_float64:
   187  		vs := vector.MustFixedCol[float64](vec)
   188  		return GetOffsetOfOrdered(vs, v.(float64), skipmask)
   189  	case types.T_date:
   190  		vs := vector.MustFixedCol[types.Date](vec)
   191  		return GetOffsetOfOrdered(vs, v.(types.Date), skipmask)
   192  	case types.T_time:
   193  		vs := vector.MustFixedCol[types.Time](vec)
   194  		return GetOffsetOfOrdered(vs, v.(types.Time), skipmask)
   195  	case types.T_datetime:
   196  		vs := vector.MustFixedCol[types.Datetime](vec)
   197  		return GetOffsetOfOrdered(vs, v.(types.Datetime), skipmask)
   198  	case types.T_timestamp:
   199  		vs := vector.MustFixedCol[types.Timestamp](vec)
   200  		return GetOffsetOfOrdered(vs, v.(types.Timestamp), skipmask)
   201  	case types.T_enum:
   202  		vs := vector.MustFixedCol[types.Enum](vec)
   203  		return GetOffsetOfOrdered(vs, v.(types.Enum), skipmask)
   204  	case types.T_decimal64:
   205  		vs := vector.MustFixedCol[types.Decimal64](vec)
   206  		return GetOffsetWithFunc(
   207  			vs,
   208  			v.(types.Decimal64),
   209  			types.CompareDecimal64,
   210  			skipmask)
   211  	case types.T_decimal128:
   212  		vs := vector.MustFixedCol[types.Decimal128](vec)
   213  		return GetOffsetWithFunc(
   214  			vs,
   215  			v.(types.Decimal128),
   216  			types.CompareDecimal128,
   217  			skipmask)
   218  	case types.T_TS:
   219  		return GetOffsetWithFunc(
   220  			vector.MustFixedCol[types.TS](vec),
   221  			v.(types.TS),
   222  			types.CompareTSTSAligned,
   223  			skipmask)
   224  	case types.T_Rowid:
   225  		return GetOffsetWithFunc(
   226  			vector.MustFixedCol[types.Rowid](vec),
   227  			v.(types.Rowid),
   228  			types.CompareRowidRowidAligned,
   229  			skipmask)
   230  	case types.T_Blockid:
   231  		return GetOffsetWithFunc(
   232  			vector.MustFixedCol[types.Blockid](vec),
   233  			v.(types.Blockid),
   234  			types.CompareBlockidBlockidAligned,
   235  			skipmask)
   236  	case types.T_uuid:
   237  		return GetOffsetWithFunc(
   238  			vector.MustFixedCol[types.Uuid](vec),
   239  			v.(types.Uuid),
   240  			types.CompareUuid,
   241  			skipmask)
   242  	case types.T_char, types.T_varchar, types.T_blob,
   243  		types.T_binary, types.T_varbinary, types.T_json, types.T_text,
   244  		types.T_array_float32, types.T_array_float64:
   245  		// data is retrieved from DN vector, hence T_array can be handled here.
   246  		val := v.([]byte)
   247  		start, end := 0, data.Length()-1
   248  		var mid int
   249  		for start <= end {
   250  			mid = (start + end) / 2
   251  			res := bytes.Compare(data.ShallowGet(mid).([]byte), val)
   252  			if res > 0 {
   253  				end = mid - 1
   254  			} else if res < 0 {
   255  				start = mid + 1
   256  			} else {
   257  				if skipmask != nil && skipmask.Contains(uint64(mid)) {
   258  					return
   259  				}
   260  				offset = mid
   261  				exist = true
   262  				return
   263  			}
   264  		}
   265  		return
   266  	default:
   267  		panic("unsupported type")
   268  	}
   269  }
   270  
   271  func GetOrderedMinAndMax[T types.OrderedT](vs ...T) (minv, maxv T) {
   272  	minv = vs[0]
   273  	maxv = vs[0]
   274  	for _, v := range vs[1:] {
   275  		if v < minv {
   276  			minv = v
   277  		}
   278  		if v > maxv {
   279  			maxv = v
   280  		}
   281  	}
   282  	return
   283  }
   284  
   285  func GetDecimal64MinAndMax(vs []types.Decimal64) (minv, maxv types.Decimal64) {
   286  	minv = vs[0]
   287  	maxv = vs[0]
   288  	for _, v := range vs[1:] {
   289  		if types.CompareDecimal64(v, minv) < 0 {
   290  			minv = v
   291  		}
   292  		if types.CompareDecimal64(v, maxv) > 0 {
   293  			maxv = v
   294  		}
   295  	}
   296  	return
   297  }
   298  
   299  func GetDecimal128MinAndMax(vs []types.Decimal128) (minv, maxv types.Decimal128) {
   300  	minv = vs[0]
   301  	maxv = vs[0]
   302  	for _, v := range vs[1:] {
   303  		if types.CompareDecimal128(v, minv) < 0 {
   304  			minv = v
   305  		}
   306  		if types.CompareDecimal128(v, maxv) > 0 {
   307  			maxv = v
   308  		}
   309  	}
   310  	return
   311  }