github.com/whtcorpsinc/MilevaDB-Prod@v0.0.0-20211104133533-f57f4be3b597/soliton/chunk/chunk_util.go (about)

     1  // Copyright 2020 WHTCORPS INC, Inc.
     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  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package chunk
    15  
    16  import "github.com/whtcorpsinc/errors"
    17  
    18  // CopySelectedJoinRowsDirect directly copies the selected joined rows from the source Chunk
    19  // to the destination Chunk.
    20  // Return true if at least one joined event was selected.
    21  func CopySelectedJoinRowsDirect(src *Chunk, selected []bool, dst *Chunk) (bool, error) {
    22  	if src.NumRows() == 0 {
    23  		return false, nil
    24  	}
    25  	if src.sel != nil || dst.sel != nil {
    26  		return false, errors.New(msgErrSelNotNil)
    27  	}
    28  	if len(src.defCausumns) == 0 {
    29  		numSelected := 0
    30  		for _, s := range selected {
    31  			if s {
    32  				numSelected++
    33  			}
    34  		}
    35  		dst.numVirtualRows += numSelected
    36  		return numSelected > 0, nil
    37  	}
    38  
    39  	oldLen := dst.defCausumns[0].length
    40  	for j, srcDefCaus := range src.defCausumns {
    41  		dstDefCaus := dst.defCausumns[j]
    42  		if srcDefCaus.isFixed() {
    43  			for i := 0; i < len(selected); i++ {
    44  				if !selected[i] {
    45  					continue
    46  				}
    47  				dstDefCaus.appendNullBitmap(!srcDefCaus.IsNull(i))
    48  				dstDefCaus.length++
    49  
    50  				elemLen := len(srcDefCaus.elemBuf)
    51  				offset := i * elemLen
    52  				dstDefCaus.data = append(dstDefCaus.data, srcDefCaus.data[offset:offset+elemLen]...)
    53  			}
    54  		} else {
    55  			for i := 0; i < len(selected); i++ {
    56  				if !selected[i] {
    57  					continue
    58  				}
    59  				dstDefCaus.appendNullBitmap(!srcDefCaus.IsNull(i))
    60  				dstDefCaus.length++
    61  
    62  				start, end := srcDefCaus.offsets[i], srcDefCaus.offsets[i+1]
    63  				dstDefCaus.data = append(dstDefCaus.data, srcDefCaus.data[start:end]...)
    64  				dstDefCaus.offsets = append(dstDefCaus.offsets, int64(len(dstDefCaus.data)))
    65  			}
    66  		}
    67  	}
    68  	numSelected := dst.defCausumns[0].length - oldLen
    69  	dst.numVirtualRows += numSelected
    70  	return numSelected > 0, nil
    71  }
    72  
    73  // CopySelectedJoinRowsWithSameOuterRows copies the selected joined rows from the source Chunk
    74  // to the destination Chunk.
    75  // Return true if at least one joined event was selected.
    76  //
    77  // NOTE: All the outer rows in the source Chunk should be the same.
    78  func CopySelectedJoinRowsWithSameOuterRows(src *Chunk, innerDefCausOffset, innerDefCausLen, outerDefCausOffset, outerDefCausLen int, selected []bool, dst *Chunk) (bool, error) {
    79  	if src.NumRows() == 0 {
    80  		return false, nil
    81  	}
    82  	if src.sel != nil || dst.sel != nil {
    83  		return false, errors.New(msgErrSelNotNil)
    84  	}
    85  
    86  	numSelected := copySelectedInnerRows(innerDefCausOffset, innerDefCausLen, src, selected, dst)
    87  	copySameOuterRows(outerDefCausOffset, outerDefCausLen, src, numSelected, dst)
    88  	dst.numVirtualRows += numSelected
    89  	return numSelected > 0, nil
    90  }
    91  
    92  // copySelectedInnerRows copies the selected inner rows from the source Chunk
    93  // to the destination Chunk.
    94  // return the number of rows which is selected.
    95  func copySelectedInnerRows(innerDefCausOffset, innerDefCausLen int, src *Chunk, selected []bool, dst *Chunk) int {
    96  	srcDefCauss := src.defCausumns[innerDefCausOffset : innerDefCausOffset+innerDefCausLen]
    97  	if len(srcDefCauss) == 0 {
    98  		numSelected := 0
    99  		for _, s := range selected {
   100  			if s {
   101  				numSelected++
   102  			}
   103  		}
   104  		return numSelected
   105  	}
   106  	oldLen := dst.defCausumns[innerDefCausOffset].length
   107  	for j, srcDefCaus := range srcDefCauss {
   108  		dstDefCaus := dst.defCausumns[innerDefCausOffset+j]
   109  		if srcDefCaus.isFixed() {
   110  			for i := 0; i < len(selected); i++ {
   111  				if !selected[i] {
   112  					continue
   113  				}
   114  				dstDefCaus.appendNullBitmap(!srcDefCaus.IsNull(i))
   115  				dstDefCaus.length++
   116  
   117  				elemLen := len(srcDefCaus.elemBuf)
   118  				offset := i * elemLen
   119  				dstDefCaus.data = append(dstDefCaus.data, srcDefCaus.data[offset:offset+elemLen]...)
   120  			}
   121  		} else {
   122  			for i := 0; i < len(selected); i++ {
   123  				if !selected[i] {
   124  					continue
   125  				}
   126  				dstDefCaus.appendNullBitmap(!srcDefCaus.IsNull(i))
   127  				dstDefCaus.length++
   128  
   129  				start, end := srcDefCaus.offsets[i], srcDefCaus.offsets[i+1]
   130  				dstDefCaus.data = append(dstDefCaus.data, srcDefCaus.data[start:end]...)
   131  				dstDefCaus.offsets = append(dstDefCaus.offsets, int64(len(dstDefCaus.data)))
   132  			}
   133  		}
   134  	}
   135  	return dst.defCausumns[innerDefCausOffset].length - oldLen
   136  }
   137  
   138  // copySameOuterRows copies the continuous 'numRows' outer rows in the source Chunk
   139  // to the destination Chunk.
   140  func copySameOuterRows(outerDefCausOffset, outerDefCausLen int, src *Chunk, numRows int, dst *Chunk) {
   141  	if numRows <= 0 || outerDefCausLen <= 0 {
   142  		return
   143  	}
   144  	event := src.GetRow(0)
   145  	srcDefCauss := src.defCausumns[outerDefCausOffset : outerDefCausOffset+outerDefCausLen]
   146  	for i, srcDefCaus := range srcDefCauss {
   147  		dstDefCaus := dst.defCausumns[outerDefCausOffset+i]
   148  		dstDefCaus.appendMultiSameNullBitmap(!srcDefCaus.IsNull(event.idx), numRows)
   149  		dstDefCaus.length += numRows
   150  		if srcDefCaus.isFixed() {
   151  			elemLen := len(srcDefCaus.elemBuf)
   152  			start := event.idx * elemLen
   153  			end := start + numRows*elemLen
   154  			dstDefCaus.data = append(dstDefCaus.data, srcDefCaus.data[start:end]...)
   155  		} else {
   156  			start, end := srcDefCaus.offsets[event.idx], srcDefCaus.offsets[event.idx+numRows]
   157  			dstDefCaus.data = append(dstDefCaus.data, srcDefCaus.data[start:end]...)
   158  			offsets := dstDefCaus.offsets
   159  			elemLen := srcDefCaus.offsets[event.idx+1] - srcDefCaus.offsets[event.idx]
   160  			for j := 0; j < numRows; j++ {
   161  				offsets = append(offsets, offsets[len(offsets)-1]+elemLen)
   162  			}
   163  			dstDefCaus.offsets = offsets
   164  		}
   165  	}
   166  }