github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/query/functions/lazy/base.go (about)

     1  // Copyright (c) 2018 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package lazy
    22  
    23  import (
    24  	"fmt"
    25  
    26  	"github.com/m3db/m3/src/query/block"
    27  	"github.com/m3db/m3/src/query/executor/transform"
    28  	"github.com/m3db/m3/src/query/models"
    29  	"github.com/m3db/m3/src/query/parser"
    30  )
    31  
    32  const (
    33  	// OffsetType offsets incoming data point timestamps and metadata by the given offset.
    34  	OffsetType = "offset"
    35  
    36  	// UnaryType offsets incoming data point values by the given operator.
    37  	UnaryType = "unary"
    38  )
    39  
    40  // NewLazyOp creates a new lazy operation
    41  func NewLazyOp(
    42  	opType string,
    43  	lazyOpts block.LazyOptions,
    44  ) (parser.Params, error) {
    45  	return baseOp{
    46  		opType:   opType,
    47  		lazyOpts: lazyOpts,
    48  	}, nil
    49  }
    50  
    51  // baseOp stores required properties for the baseOp
    52  type baseOp struct {
    53  	opType   string
    54  	lazyOpts block.LazyOptions
    55  }
    56  
    57  func (o baseOp) OpType() string {
    58  	return o.opType
    59  }
    60  
    61  func (o baseOp) String() string {
    62  	return fmt.Sprintf("type: %s", o.opType)
    63  }
    64  
    65  func (o baseOp) Node(
    66  	controller *transform.Controller,
    67  	_ transform.Options,
    68  ) transform.OpNode {
    69  	return &baseNode{
    70  		op:         o,
    71  		controller: controller,
    72  	}
    73  }
    74  
    75  type baseNode struct {
    76  	op         baseOp
    77  	controller *transform.Controller
    78  }
    79  
    80  func (n *baseNode) Params() parser.Params {
    81  	return n.op
    82  }
    83  
    84  func (n *baseNode) processBlock(b block.Block) block.Block {
    85  	return block.NewLazyBlock(b, n.op.lazyOpts)
    86  }
    87  
    88  func (n *baseNode) Process(
    89  	queryCtx *models.QueryContext,
    90  	_ parser.NodeID,
    91  	b block.Block,
    92  ) error {
    93  	nextBlock := n.processBlock(b)
    94  	return n.controller.Process(queryCtx, nextBlock)
    95  }