github.com/whtcorpsinc/MilevaDB-Prod@v0.0.0-20211104133533-f57f4be3b597/interlock/aggfuncs/func_lead_lag.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 aggfuncs
    15  
    16  import (
    17  	"unsafe"
    18  
    19  	"github.com/whtcorpsinc/milevadb/memex"
    20  	"github.com/whtcorpsinc/milevadb/stochastikctx"
    21  	"github.com/whtcorpsinc/milevadb/soliton/chunk"
    22  )
    23  
    24  const (
    25  	// DefPartialResult4LeadLagSize is the size of partialResult4LeadLag
    26  	DefPartialResult4LeadLagSize = int64(unsafe.Sizeof(partialResult4LeadLag{}))
    27  )
    28  
    29  type baseLeadLag struct {
    30  	baseAggFunc
    31  	valueEvaluator // TODO: move it to partial result when parallel execution is supported.
    32  
    33  	defaultExpr memex.Expression
    34  	offset      uint64
    35  }
    36  
    37  type partialResult4LeadLag struct {
    38  	rows   []chunk.Event
    39  	curIdx uint64
    40  }
    41  
    42  func (v *baseLeadLag) AllocPartialResult() (pr PartialResult, memDelta int64) {
    43  	return PartialResult(&partialResult4LeadLag{}), DefPartialResult4LeadLagSize
    44  }
    45  
    46  func (v *baseLeadLag) ResetPartialResult(pr PartialResult) {
    47  	p := (*partialResult4LeadLag)(pr)
    48  	p.rows = p.rows[:0]
    49  	p.curIdx = 0
    50  }
    51  
    52  func (v *baseLeadLag) UFIDelatePartialResult(sctx stochastikctx.Context, rowsInGroup []chunk.Event, pr PartialResult) (memDelta int64, err error) {
    53  	p := (*partialResult4LeadLag)(pr)
    54  	p.rows = append(p.rows, rowsInGroup...)
    55  	memDelta += int64(len(rowsInGroup)) * DefEventSize
    56  	return memDelta, nil
    57  }
    58  
    59  type lead struct {
    60  	baseLeadLag
    61  }
    62  
    63  func (v *lead) AppendFinalResult2Chunk(sctx stochastikctx.Context, pr PartialResult, chk *chunk.Chunk) error {
    64  	p := (*partialResult4LeadLag)(pr)
    65  	var err error
    66  	if p.curIdx+v.offset < uint64(len(p.rows)) {
    67  		_, err = v.evaluateEvent(sctx, v.args[0], p.rows[p.curIdx+v.offset])
    68  	} else {
    69  		_, err = v.evaluateEvent(sctx, v.defaultExpr, p.rows[p.curIdx])
    70  	}
    71  	if err != nil {
    72  		return err
    73  	}
    74  	v.appendResult(chk, v.ordinal)
    75  	p.curIdx++
    76  	return nil
    77  }
    78  
    79  type lag struct {
    80  	baseLeadLag
    81  }
    82  
    83  func (v *lag) AppendFinalResult2Chunk(sctx stochastikctx.Context, pr PartialResult, chk *chunk.Chunk) error {
    84  	p := (*partialResult4LeadLag)(pr)
    85  	var err error
    86  	if p.curIdx >= v.offset {
    87  		_, err = v.evaluateEvent(sctx, v.args[0], p.rows[p.curIdx-v.offset])
    88  	} else {
    89  		_, err = v.evaluateEvent(sctx, v.defaultExpr, p.rows[p.curIdx])
    90  	}
    91  	if err != nil {
    92  		return err
    93  	}
    94  	v.appendResult(chk, v.ordinal)
    95  	p.curIdx++
    96  	return nil
    97  }