Repository: matrixorigin/matrixone
Pull request #8909 (2023-04-07 11:06:57)

remove dn vecwindow
## What type of PR is this?

- [ ] API-change
- [ ] BUG
- [ ] Improvement
- [ ] Documentation
- [ ] Feature
- [ ] Test and CI
- [x] Code Refactoring

## Which issue(s) this PR fixes:

issue #8875 

## What this PR does / why we need it:


Patch:
diff --git a/pkg/vm/engine/tae/containers/vector.go b/pkg/vm/engine/tae/containers/vector.go
index 31ce18a0b..edad408da 100644
--- a/pkg/vm/engine/tae/containers/vector.go
+++ b/pkg/vm/engine/tae/containers/vector.go
@@ -36,9 +36,6 @@ type vector[T any] struct {
 
 	// Used in Append()
 	mpool *mpool.MPool
-
-	// isOwner is used to implement the SharedMemory Logic from the previous DN vector implementation.
-	isOwner bool
 }
 
 func NewVector[T any](typ types.Type, opts ...Options) *vector[T] {
@@ -56,9 +53,6 @@ func NewVector[T any](typ types.Type, opts ...Options) *vector[T] {
 	}
 	vec.mpool = alloc
 
-	// So far no mpool allocation. So isOwner defaults to false.
-	vec.isOwner = false
-
 	return vec
 }
 
@@ -200,14 +194,13 @@ func (vec *vector[T]) Close() {
 }
 
 func (vec *vector[T]) releaseDownstream() {
-	if vec.isOwner {
+	if !vec.downstreamVector.NeedDup() {
 		vec.downstreamVector.Free(vec.mpool)
-		vec.isOwner = false
 	}
 }
 
 func (vec *vector[T]) Allocated() int {
-	if !vec.isOwner {
+	if vec.downstreamVector.NeedDup() {
 		return 0
 	}
 	return vec.downstreamVector.Size()
@@ -215,29 +208,27 @@ func (vec *vector[T]) Allocated() int {
 
 // When a new Append() is happening on a SharedMemory vector, we allocate the data[] from the mpool.
 func (vec *vector[T]) tryCoW() {
+	if !vec.downstreamVector.NeedDup() {
+		return
+	}
 
-	if !vec.isOwner {
-		newCnVector, err := vec.downstreamVector.Dup(vec.mpool)
-		if err != nil {
-			panic(err)
-		}
-		vec.downstreamVector = newCnVector
-		vec.isOwner = true
+	newCnVector, err := vec.downstreamVector.Dup(vec.mpool)
+	if err != nil {
+		panic(err)
 	}
+	vec.downstreamVector = newCnVector
 }
 
 func (vec *vector[T]) Window(offset, length int) Vector {
-
-	// In DN Vector, we are using SharedReference for Window.
-	// In CN Vector, we are creating a new Clone for Window.
-	// So inorder to retain the nature of DN vector, we had use vectorWindow Adapter.
-	return &vectorWindow[T]{
-		ref: vec,
-		windowBase: &windowBase{
-			offset: offset,
-			length: length,
-		},
+	var err error
+	win := new(vector[T])
+	win.mpool = vec.mpool
+	win.downstreamVector, err = vec.downstreamVector.Window(offset, offset+length)
+	if err != nil {
+		panic(err)
 	}
+
+	return win
 }
 
 func (vec *vector[T]) CloneWindow(offset, length int, allocator ...*mpool.MPool) Vector {
@@ -254,7 +245,6 @@ func (vec *vector[T]) CloneWindow(offset, length int, allocator ...*mpool.MPool)
 	if err != nil {
 		panic(err)
 	}
-	cloned.isOwner = true
 
 	return cloned
 }
@@ -378,7 +368,6 @@ func (vec *vector[T]) GetDownstreamVector() *cnVector.Vector {
 }
 
 func (vec *vector[T]) setDownstreamVector(dsVec *cnVector.Vector) {
-	vec.isOwner = false
 	vec.downstreamVector = dsVec
 }
 
diff --git a/pkg/vm/engine/tae/containers/vecwindow.go b/pkg/vm/engine/tae/containers/vecwindow.go
deleted file mode 100644
index 25176e608..000000000
--- a/pkg/vm/engine/tae/containers/vecwindow.go
+++ /dev/null
@@ -1,189 +0,0 @@
-// Copyright 2022 Matrix Origin
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-package containers
-
-import (
-	"bytes"
-	"fmt"
-	"io"
-
-	"github.com/RoaringBitmap/roaring"
-	"github.com/matrixorigin/matrixone/pkg/common/mpool"
-	cnNulls "github.com/matrixorigin/matrixone/pkg/container/nulls"
-	"github.com/matrixorigin/matrixone/pkg/container/types"
-	cnVector "github.com/matrixorigin/matrixone/pkg/container/vector"
-)
-
-type windowBase struct {
-	offset, length int
-}
-
-func (win *windowBase) Update(i int, v any)                 { panic("cannot modify window") }
-func (win *windowBase) Delete(i int)                        { panic("cannot modify window") }
-func (win *windowBase) Compact(deletes *roaring.Bitmap)     { panic("cannot modify window") }
-func (win *windowBase) Append(v any)                        { panic("cannot modify window") }
-func (win *windowBase) AppendMany(vs ...any)                { panic("cannot modify window") }
-func (win *windowBase) Extend(o Vector)                     { panic("cannot modify window") }
-func (win *windowBase) ExtendWithOffset(_ Vector, _, _ int) { panic("cannot modify window") }
-func (win *windowBase) Length() int                         { return win.length }
-func (win *windowBase) Allocated() int                      { return 0 }
-func (win *windowBase) Close()                              {}
-func (win *windowBase) ReadFrom(io.Reader) (int64, error)   { panic("cannot modify window") }
-
-type vectorWindow[T any] struct {
-	*windowBase
-	ref *vector[T]
-}
-
-func (win *vectorWindow[T]) Equals(o Vector) bool {
-	if win.Length() != o.Length() {
-		return false
-	}
-	if win.GetType() != o.GetType() {
-		return false
-	}
-
-	if win.HasNull() != o.HasNull() {
-		return false
-	}
-	if win.HasNull() {
-		if !win.NullMask().IsSame(o.NullMask()) {
-			return false
-		}
-	}
-	mask := win.NullMask()
-	for i := 0; i < win.Length(); i++ {
-		if mask != nil && mask.Contains(uint64(i)) {
-			continue
-		}
-		var v T
-		if _, ok := any(v).([]byte); ok {
-			if !bytes.Equal(win.ShallowGet(i).([]byte), o.ShallowGet(i).([]byte)) {
-				return false
-			}
-		} else if _, ok := any(v).(types.Decimal64); ok {
-			d := win.Get(i).(types.Decimal64)
-			od := win.Get(i).(types.Decimal64)
-			if d != od {
-				return false
-			}
-		} else if _, ok := any(v).(types.Decimal128); ok {
-			d := win.Get(i).(types.Decimal128)
-			od := win.Get(i).(types.Decimal128)
-			if d != od {
-				return false
-			}
-		} else if _, ok := any(v).(types.TS); ok {
-			d := win.Get(i).(types.TS)
-			od := win.Get(i).(types.TS)
-			if types.CompareTSTSAligned(d, od) != 0 {
-				return false
-			}
-		} else if _, ok := any(v).(types.Rowid); ok {
-			d := win.Get(i).(types.Rowid)
-			od := win.Get(i).(types.Rowid)
-			if types.CompareRowidRowidAligned(d, od) != 0 {
-				return false
-			}
-		} else {
-			if win.Get(i) != o.Get(i) {
-				return false
-			}
-		}
-	}
-	return true
-
-}
-
-func (win *vectorWindow[T]) CloneWindow(offset, length int, allocator ...*mpool.MPool) Vector {
-	return win.ref.CloneWindow(offset+win.offset, length, allocator...)
-}
-func (win *vectorWindow[T]) Get(i int) (v any) {
-	return win.ref.Get(i + win.offset)
-}
-func (win *vectorWindow[T]) ShallowGet(i int) (v any) {
-	return win.ref.ShallowGet(i + win.offset)
-}
-
-func (win *vectorWindow[T]) HasNull() bool { return win.ref.HasNull() }
-func (win *vectorWindow[T]) NullMask() *cnNulls.Nulls {
-	mask := win.ref.NullMask()
-	if win.offset == 0 || mask == nil {
-		return mask
-	}
-	return cnNulls.Range(mask, uint64(win.offset), uint64(win.offset+win.length), 0, cnNulls.NewWithSize(0))
-}
-
-func (win *vectorWindow[T]) IsNull(i int) bool {
-	return win.ref.IsNull(i + win.offset)
-}
-func (win *vectorWindow[T]) GetAllocator() *mpool.MPool { return win.ref.GetAllocator() }
-func (win *vectorWindow[T]) GetType() types.Type        { return win.ref.GetType() }
-func (win *vectorWindow[T]) String() string {
-	s := fmt.Sprintf("[Window[%d,%d)];%s", win.offset, win.offset+win.length, win.ref.String())
-	return s
-}
-func (win *vectorWindow[T]) PPString(num int) string {
-	s := fmt.Sprintf("[Window[%d,%d)];%s", win.offset, win.offset+win.length, win.ref.PPString(num))
-	return s
-}
-func (win *vectorWindow[T]) Slice() any {
-	return win.ref.Slice().([]T)[win.offset : win.offset+win.length]
-}
-func (win *vectorWindow[T]) Foreach(op ItOp, sels *roaring.Bitmap) (err error) {
-	return win.ref.forEachWindowWithBias(0, win.length, op, sels, win.offset, false)
-}
-
-func (win *vectorWindow[T]) ForeachWindow(offset, length int, op ItOp, sels *roaring.Bitmap) (err error) {
-	if offset+length > win.length {
-		panic("bad param")
-	}
-	return win.ref.forEachWindowWithBias(offset, length, op, sels, win.offset, false)
-}
-
-func (win *vectorWindow[T]) ForeachShallow(op ItOp, sels *roaring.Bitmap) (err error) {
-	return win.ref.forEachWindowWithBias(0, win.length, op, sels, win.offset, true)
-}
-
-func (win *vectorWindow[T]) ForeachWindowShallow(offset, length int, op ItOp, sels *roaring.Bitmap) (err error) {
-	if offset+length > win.length {
-		panic("bad param")
-	}
-	return win.ref.forEachWindowWithBias(offset, length, op, sels, win.offset, true)
-}
-
-func (win *vectorWindow[T]) WriteTo(w io.Writer) (n int64, err error) { panic("implement me") }
-func (win *vectorWindow[T]) Window(offset, length int) Vector {
-	if offset+length > win.length {
-		panic("bad window param")
-	}
-	return &vectorWindow[T]{
-		ref: win.ref,
-		windowBase: &windowBase{
-			offset: offset + win.offset,
-			length: length,
-		},
-	}
-}
-
-func (win *vectorWindow[T]) GetDownstreamVector() *cnVector.Vector {
-	res, _ := win.ref.downstreamVector.CloneWindow(win.offset, win.offset+win.length, nil)
-	return res
-}
-
-func (win *vectorWindow[T]) setDownstreamVector(vec *cnVector.Vector) {
-	// Windows are read only
-	panic("cannot modify window")
-}


Test patch:
diff --git a/pkg/vm/engine/tae/db/db_test.go b/pkg/vm/engine/tae/db/db_test.go
index 6efade367..cef9d9096 100644
--- a/pkg/vm/engine/tae/db/db_test.go
+++ b/pkg/vm/engine/tae/db/db_test.go
@@ -2470,7 +2470,7 @@ func TestNull1(t *testing.T) {
 	bat := catalog.MockBatch(schema, int(schema.BlockMaxRows*3+1))
 	defer bat.Close()
 	bats := bat.Split(4)
-	bat.Vecs[3].Update(2, types.Null{})
+	bats[0].Vecs[3].Update(2, types.Null{})
 	tae.createRelAndAppend(bats[0], true)
 
 	txn, rel := tae.getRelation()