github.com/matrixorigin/matrixone@v0.7.0/pkg/vm/engine/tae/mergesort/varchar/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 varchar
    16  
    17  import (
    18  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/containers"
    19  )
    20  
    21  func Sort(col containers.Vector, idx []uint32) (ret containers.Vector) {
    22  	n := len(idx)
    23  	dataWithIdx := make(sortSlice, n)
    24  
    25  	for i := 0; i < n; i++ {
    26  		dataWithIdx[i] = sortElem{data: col.Get(i).([]byte), idx: uint32(i)}
    27  	}
    28  
    29  	sortUnstable(dataWithIdx)
    30  
    31  	opt := containers.Options{Allocator: col.GetAllocator()}
    32  	sorted := containers.MakeVector(col.GetType(), col.Nullable(), opt)
    33  	defer sorted.Close()
    34  	for i, v := range dataWithIdx {
    35  		idx[i] = v.idx
    36  		sorted.Append(v.data)
    37  	}
    38  	bs := col.Bytes()
    39  	dbs := sorted.Bytes()
    40  	copy(bs.HeaderBuf(), dbs.HeaderBuf())
    41  	copy(bs.Storage, dbs.Storage)
    42  	return
    43  }
    44  
    45  func Merge(col []containers.Vector, src *[]uint32, fromLayout, toLayout []uint32) (ret []containers.Vector, mapping []uint32) {
    46  	ret = make([]containers.Vector, len(toLayout))
    47  	mapping = make([]uint32, len(*src))
    48  
    49  	offset := make([]uint32, len(fromLayout))
    50  	offset[0] = 0
    51  	for i := 1; i < len(fromLayout); i++ {
    52  		offset[i] = offset[i-1] + fromLayout[i-1]
    53  	}
    54  
    55  	for i := range toLayout {
    56  		ret[i] = containers.MakeVector(col[0].GetType(), col[0].Nullable())
    57  	}
    58  
    59  	nBlk := len(col)
    60  	heap := make(heapSlice, nBlk)
    61  
    62  	for i := 0; i < nBlk; i++ {
    63  		heap[i] = heapElem{data: col[i].Get(0).([]byte), src: uint32(i), next: 1}
    64  	}
    65  	heapInit(heap)
    66  
    67  	k := 0
    68  	for i := 0; i < len(toLayout); i++ {
    69  		for j := 0; j < int(toLayout[i]); j++ {
    70  			top := heapPop(&heap)
    71  			(*src)[k] = top.src
    72  			ret[i].Append(top.data)
    73  			mapping[offset[top.src]+top.next-1] = uint32(k)
    74  			k++
    75  			if int(top.next) < int(fromLayout[top.src]) {
    76  				heapPush(&heap, heapElem{data: col[top.src].Get(int(top.next)).([]byte), src: top.src, next: top.next + 1})
    77  			}
    78  		}
    79  	}
    80  	return
    81  }