github.com/m3db/m3@v1.5.0/src/query/test/executor/transform.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 executor
    22  
    23  import (
    24  	"github.com/m3db/m3/src/query/block"
    25  	"github.com/m3db/m3/src/query/executor/transform"
    26  	"github.com/m3db/m3/src/query/models"
    27  	"github.com/m3db/m3/src/query/parser"
    28  )
    29  
    30  // NewControllerWithSink creates a new controller which has a sink useful for comparison in tests
    31  func NewControllerWithSink(ID parser.NodeID) (*transform.Controller, *SinkNode) {
    32  	c := &transform.Controller{
    33  		ID: ID,
    34  	}
    35  
    36  	node := &SinkNode{
    37  		Values: make([][]float64, 0),
    38  		Metas:  make([]block.SeriesMeta, 0),
    39  	}
    40  	c.AddTransform(node)
    41  	return c, node
    42  }
    43  
    44  // SinkNode is a test node useful for comparisons
    45  type SinkNode struct {
    46  	Values [][]float64
    47  	Meta   block.Metadata
    48  	Metas  []block.SeriesMeta
    49  	Info   block.BlockInfo
    50  }
    51  
    52  // Process processes and stores the last block output in the sink node
    53  func (s *SinkNode) Process(_ *models.QueryContext, ID parser.NodeID, block block.Block) error {
    54  	s.Meta = block.Meta()
    55  	iter, err := block.StepIter()
    56  	if err != nil {
    57  		return err
    58  	}
    59  
    60  	seriesCount := len(iter.SeriesMeta())
    61  	steps := iter.StepCount()
    62  	s.Values = make([][]float64, seriesCount)
    63  	bulkAllocValues := make([]float64, seriesCount*steps)
    64  	for i := 0; i < seriesCount; i++ {
    65  		s.Values[i] = bulkAllocValues[:steps]
    66  		bulkAllocValues = bulkAllocValues[steps:]
    67  	}
    68  
    69  	row := 0
    70  	for iter.Next() {
    71  		val := iter.Current()
    72  		for series, v := range val.Values() {
    73  			s.Values[series][row] = v
    74  		}
    75  
    76  		row++
    77  	}
    78  
    79  	if err = iter.Err(); err != nil {
    80  		return err
    81  	}
    82  
    83  	s.Metas = iter.SeriesMeta()
    84  	s.Info = block.Info()
    85  
    86  	return nil
    87  }