github.com/matrixorigin/matrixone@v0.7.0/pkg/vm/engine/tae/containers/vecwindow.go (about)

     1  // Copyright 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 containers
    16  
    17  import (
    18  	"bytes"
    19  	"fmt"
    20  	"io"
    21  	"unsafe"
    22  
    23  	"github.com/RoaringBitmap/roaring"
    24  	"github.com/RoaringBitmap/roaring/roaring64"
    25  	"github.com/matrixorigin/matrixone/pkg/common/mpool"
    26  	"github.com/matrixorigin/matrixone/pkg/container/types"
    27  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/common"
    28  )
    29  
    30  type windowBase struct {
    31  	offset, length int
    32  }
    33  
    34  func (win *windowBase) IsView() bool                         { return true }
    35  func (win *windowBase) Update(i int, v any)                  { panic("cannot modify window") }
    36  func (win *windowBase) Delete(i int)                         { panic("cannot modify window") }
    37  func (win *windowBase) Compact(deletes *roaring.Bitmap)      { panic("cannot modify window") }
    38  func (win *windowBase) Append(v any)                         { panic("cannot modify window") }
    39  func (win *windowBase) AppendMany(vs ...any)                 { panic("cannot modify window") }
    40  func (win *windowBase) AppendNoNulls(s any)                  { panic("cannot modify window") }
    41  func (win *windowBase) Extend(o Vector)                      { panic("cannot modify window") }
    42  func (win *windowBase) ExtendWithOffset(_ Vector, _, _ int)  { panic("cannot modify window") }
    43  func (win *windowBase) Length() int                          { return win.length }
    44  func (win *windowBase) Capacity() int                        { return win.length }
    45  func (win *windowBase) Allocated() int                       { return 0 }
    46  func (win *windowBase) DataWindow(offset, length int) []byte { panic("cannot window a window") }
    47  func (win *windowBase) Close()                               {}
    48  func (win *windowBase) ReadFrom(io.Reader) (int64, error)    { panic("cannot modify window") }
    49  
    50  func (win *windowBase) ReadFromFile(common.IVFile, *bytes.Buffer) error {
    51  	panic("cannot modify window")
    52  }
    53  func (win *windowBase) Reset()                                  { panic("cannot modify window") }
    54  func (win *windowBase) ResetWithData(*Bytes, *roaring64.Bitmap) { panic("cannot modify window") }
    55  
    56  type vectorWindow[T any] struct {
    57  	*windowBase
    58  	ref *vector[T]
    59  }
    60  
    61  func (win *vectorWindow[T]) Equals(o Vector) bool {
    62  	if win.Length() != o.Length() {
    63  		return false
    64  	}
    65  	if win.GetType() != o.GetType() {
    66  		return false
    67  	}
    68  	if win.Nullable() != o.Nullable() {
    69  		return false
    70  	}
    71  	if win.HasNull() != o.HasNull() {
    72  		return false
    73  	}
    74  	if win.HasNull() {
    75  		if !win.NullMask().Equals(o.NullMask()) {
    76  			return false
    77  		}
    78  	}
    79  	mask := win.NullMask()
    80  	for i := 0; i < win.Length(); i++ {
    81  		if mask != nil && mask.ContainsInt(i) {
    82  			continue
    83  		}
    84  		var v T
    85  		if _, ok := any(v).([]byte); ok {
    86  			if !bytes.Equal(win.Get(i).([]byte), o.Get(i).([]byte)) {
    87  				return false
    88  			}
    89  		} else if _, ok := any(v).(types.Decimal64); ok {
    90  			d := win.Get(i).(types.Decimal64)
    91  			od := win.Get(i).(types.Decimal64)
    92  			if d.Ne(od) {
    93  				return false
    94  			}
    95  		} else if _, ok := any(v).(types.Decimal128); ok {
    96  			d := win.Get(i).(types.Decimal128)
    97  			od := win.Get(i).(types.Decimal128)
    98  			if d.Ne(od) {
    99  				return false
   100  			}
   101  		} else if _, ok := any(v).(types.TS); ok {
   102  			d := win.Get(i).(types.TS)
   103  			od := win.Get(i).(types.TS)
   104  			if types.CompareTSTSAligned(d, od) != 0 {
   105  				return false
   106  			}
   107  		} else if _, ok := any(v).(types.Rowid); ok {
   108  			d := win.Get(i).(types.Rowid)
   109  			od := win.Get(i).(types.Rowid)
   110  			if types.CompareRowidRowidAligned(d, od) != 0 {
   111  				return false
   112  			}
   113  		} else {
   114  			if win.Get(i) != o.Get(i) {
   115  				return false
   116  			}
   117  		}
   118  	}
   119  	return true
   120  
   121  }
   122  
   123  func (win *vectorWindow[T]) GetView() VectorView {
   124  	return &vectorWindow[T]{
   125  		ref: win.ref,
   126  		windowBase: &windowBase{
   127  			offset: win.offset,
   128  			length: win.length,
   129  		},
   130  	}
   131  }
   132  
   133  func (win *vectorWindow[T]) CloneWindow(offset, length int, allocator ...*mpool.MPool) Vector {
   134  	return win.ref.CloneWindow(offset+win.offset, length, allocator...)
   135  }
   136  
   137  func (win *vectorWindow[T]) Data() []byte {
   138  	return win.ref.DataWindow(win.offset, win.length)
   139  }
   140  func (win *vectorWindow[T]) Get(i int) (v any) {
   141  	return win.ref.Get(i + win.offset)
   142  }
   143  
   144  func (win *vectorWindow[T]) Nullable() bool { return win.ref.Nullable() }
   145  func (win *vectorWindow[T]) HasNull() bool  { return win.ref.HasNull() }
   146  func (win *vectorWindow[T]) NullMask() *roaring64.Bitmap {
   147  	mask := win.ref.NullMask()
   148  	if win.offset == 0 || mask == nil {
   149  		return mask
   150  	}
   151  	return common.BM64Window(mask, win.offset, win.offset+win.length)
   152  }
   153  func (win *vectorWindow[T]) IsNull(i int) bool {
   154  	return win.ref.IsNull(i + win.offset)
   155  }
   156  func (win *vectorWindow[T]) GetAllocator() *mpool.MPool { return win.ref.GetAllocator() }
   157  func (win *vectorWindow[T]) GetType() types.Type        { return win.ref.GetType() }
   158  func (win *vectorWindow[T]) String() string {
   159  	s := fmt.Sprintf("[Window[%d,%d)];%s", win.offset, win.offset+win.length, win.ref.String())
   160  	return s
   161  }
   162  func (win *vectorWindow[T]) PPString(num int) string {
   163  	s := fmt.Sprintf("[Window[%d,%d)];%s", win.offset, win.offset+win.length, win.ref.PPString(num))
   164  	return s
   165  }
   166  func (win *vectorWindow[T]) Slice() any {
   167  	return win.ref.fastSlice()[win.offset : win.offset+win.length]
   168  }
   169  func (win *vectorWindow[T]) SlicePtr() unsafe.Pointer {
   170  	slice := win.ref.fastSlice()[win.offset : win.offset+win.length]
   171  	return unsafe.Pointer(&slice[0])
   172  }
   173  func (win *vectorWindow[T]) Bytes() *Bytes {
   174  	bs := win.ref.Bytes()
   175  	bs = bs.Window(win.offset, win.length)
   176  	return bs
   177  }
   178  func (win *vectorWindow[T]) Foreach(op ItOp, sels *roaring.Bitmap) (err error) {
   179  	return win.ref.impl.forEachWindowWithBias(0, win.length, op, sels, win.offset)
   180  }
   181  func (win *vectorWindow[T]) ForeachWindow(offset, length int, op ItOp, sels *roaring.Bitmap) (err error) {
   182  	if offset+length > win.length {
   183  		panic("bad param")
   184  	}
   185  	return win.ref.impl.forEachWindowWithBias(offset, length, op, sels, win.offset)
   186  }
   187  func (win *vectorWindow[T]) WriteTo(w io.Writer) (n int64, err error) { panic("implement me") }
   188  func (win *vectorWindow[T]) Window(offset, length int) Vector {
   189  	if offset+length > win.length {
   190  		panic("bad window param")
   191  	}
   192  	return &vectorWindow[T]{
   193  		ref: win.ref,
   194  		windowBase: &windowBase{
   195  			offset: offset + win.offset,
   196  			length: length,
   197  		},
   198  	}
   199  }