github.com/matrixorigin/matrixone@v0.7.0/pkg/vm/engine/tae/mergesort/txnts/func.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 txnts
    16  
    17  import (
    18  	"github.com/matrixorigin/matrixone/pkg/container/types"
    19  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/containers"
    20  )
    21  
    22  func Sort(col containers.Vector, idx []uint32) (ret containers.Vector) {
    23  	n := len(idx)
    24  	dataWithIdx := make(sortSlice, n)
    25  
    26  	for i := 0; i < n; i++ {
    27  		dataWithIdx[i] = sortElem{data: col.Get(i).(types.TS), idx: uint32(i)}
    28  	}
    29  
    30  	sortUnstable(dataWithIdx)
    31  
    32  	for i, v := range dataWithIdx {
    33  		idx[i] = v.idx
    34  		col.Update(i, v.data)
    35  	}
    36  	ret = col
    37  	return
    38  }
    39  
    40  func Shuffle(col containers.Vector, idx []uint32) {
    41  	ret := containers.MakeVector(col.GetType(), col.Nullable())
    42  	for _, j := range idx {
    43  		ret.Append(col.Get(int(j)))
    44  	}
    45  
    46  	col.ResetWithData(ret.Bytes(), ret.NullMask())
    47  	ret.Close()
    48  }
    49  
    50  func Merge(col []containers.Vector, src *[]uint32, fromLayout, toLayout []uint32) (ret []containers.Vector, mapping []uint32) {
    51  	ret = make([]containers.Vector, len(toLayout))
    52  	mapping = make([]uint32, len(*src))
    53  
    54  	offset := make([]uint32, len(fromLayout))
    55  	offset[0] = 0
    56  	for i := 1; i < len(fromLayout); i++ {
    57  		offset[i] = offset[i-1] + fromLayout[i-1]
    58  	}
    59  
    60  	for i := range toLayout {
    61  		ret[i] = containers.MakeVector(col[0].GetType(), col[0].Nullable())
    62  	}
    63  
    64  	nBlk := len(col)
    65  	heap := make(heapSlice, nBlk)
    66  
    67  	for i := 0; i < nBlk; i++ {
    68  		heap[i] = heapElem{data: col[i].Get(0).(types.TS), src: uint32(i), next: 1}
    69  	}
    70  	heapInit(heap)
    71  
    72  	k := 0
    73  	for i := 0; i < len(toLayout); i++ {
    74  		for j := 0; j < int(toLayout[i]); j++ {
    75  			top := heapPop(&heap)
    76  			(*src)[k] = top.src
    77  			ret[i].Append(top.data)
    78  			mapping[offset[top.src]+top.next-1] = uint32(k)
    79  			k++
    80  			if int(top.next) < int(fromLayout[top.src]) {
    81  				heapPush(&heap, heapElem{data: col[top.src].Get(int(top.next)).(types.TS), src: top.src, next: top.next + 1})
    82  			}
    83  		}
    84  	}
    85  	return
    86  }
    87  
    88  func Reshape(col []containers.Vector, fromLayout, toLayout []uint32) (ret []containers.Vector) {
    89  	ret = make([]containers.Vector, len(toLayout))
    90  	fromIdx := 0
    91  	fromOffset := 0
    92  	for i := 0; i < len(toLayout); i++ {
    93  		ret[i] = containers.MakeVector(col[0].GetType(), col[0].Nullable())
    94  		toOffset := 0
    95  		for toOffset < int(toLayout[i]) {
    96  			fromLeft := fromLayout[fromIdx] - uint32(fromOffset)
    97  			if fromLeft == 0 {
    98  				fromIdx++
    99  				fromOffset = 0
   100  				fromLeft = fromLayout[fromIdx]
   101  			}
   102  			length := 0
   103  			if fromLeft < toLayout[i]-uint32(toOffset) {
   104  				length = int(fromLeft)
   105  			} else {
   106  				length = int(toLayout[i]) - toOffset
   107  			}
   108  			cloned := col[fromIdx].CloneWindow(fromOffset, length)
   109  			defer cloned.Close()
   110  			ret[i].Extend(cloned)
   111  			fromOffset += length
   112  			toOffset += length
   113  		}
   114  	}
   115  	for _, v := range col {
   116  		v.Close()
   117  	}
   118  	return
   119  }
   120  
   121  func Multiplex(col []containers.Vector, src []uint32, fromLayout, toLayout []uint32) (ret []containers.Vector) {
   122  	ret = make([]containers.Vector, len(toLayout))
   123  	to := len(toLayout)
   124  	cursors := make([]int, len(fromLayout))
   125  
   126  	for i := 0; i < to; i++ {
   127  		ret[i] = containers.MakeVector(col[0].GetType(), col[0].Nullable())
   128  	}
   129  
   130  	k := 0
   131  	for i := 0; i < to; i++ {
   132  		for j := 0; j < int(toLayout[i]); j++ {
   133  			s := src[k]
   134  			ret[i].Append(col[s].Get(cursors[s]))
   135  			cursors[s]++
   136  			k++
   137  		}
   138  	}
   139  
   140  	for _, v := range col {
   141  		v.Close()
   142  	}
   143  	return
   144  }