github.com/m3db/m3@v1.5.0/src/query/functions/binary/and.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 binary
    22  
    23  import (
    24  	"math"
    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  )
    30  
    31  // AndType uses values from left hand side for which there is a value in right
    32  // hand side with exactly matching label sets. Other elements are replaced by
    33  // NaNs. The metric name and values are carried over from the left-hand side.
    34  const AndType = "and"
    35  
    36  func makeAndBlock(
    37  	queryCtx *models.QueryContext,
    38  	lMeta, rMeta block.Metadata,
    39  	lIter, rIter block.StepIter,
    40  	controller *transform.Controller,
    41  	matching VectorMatching,
    42  ) (block.Block, error) {
    43  	if !matching.Set {
    44  		return nil, errNoMatching
    45  	}
    46  
    47  	lSeriesMetas := lIter.SeriesMeta()
    48  	lMeta, lSeriesMetas = removeNameTags(lMeta, lSeriesMetas)
    49  
    50  	rSeriesMetas := rIter.SeriesMeta()
    51  	rMeta, rSeriesMetas = removeNameTags(rMeta, rSeriesMetas)
    52  
    53  	lMeta.ResultMetadata = lMeta.ResultMetadata.
    54  		CombineMetadata(rMeta.ResultMetadata)
    55  	intersection, metas := andIntersect(matching, lSeriesMetas, rSeriesMetas)
    56  	builder, err := controller.BlockBuilder(queryCtx, lMeta, metas)
    57  	if err != nil {
    58  		return nil, err
    59  	}
    60  
    61  	if err = builder.AddCols(lIter.StepCount()); err != nil {
    62  		return nil, err
    63  	}
    64  
    65  	// NB: shortcircuit and return an empty block if no matching series.
    66  	if len(metas) == 0 {
    67  		return builder.Build(), nil
    68  	}
    69  
    70  	data := make([]float64, len(metas))
    71  	for index := 0; lIter.Next(); index++ {
    72  		if !rIter.Next() {
    73  			return nil, errRExhausted
    74  		}
    75  
    76  		lStep := lIter.Current()
    77  		lValues := lStep.Values()
    78  		rStep := rIter.Current()
    79  		rValues := rStep.Values()
    80  		for i, match := range intersection {
    81  			if math.IsNaN(rValues[match.rhsIndex]) {
    82  				data[i] = math.NaN()
    83  			} else {
    84  				data[i] = lValues[match.lhsIndex]
    85  			}
    86  		}
    87  
    88  		if err := builder.AppendValues(index, data); err != nil {
    89  			return nil, err
    90  		}
    91  	}
    92  
    93  	if err = lIter.Err(); err != nil {
    94  		return nil, err
    95  	}
    96  
    97  	if rIter.Next() {
    98  		return nil, errLExhausted
    99  	}
   100  
   101  	if err = rIter.Err(); err != nil {
   102  		return nil, err
   103  	}
   104  
   105  	return builder.Build(), nil
   106  }
   107  
   108  // intersect returns the slice of lhs indices matching rhs indices.
   109  func andIntersect(
   110  	matching VectorMatching,
   111  	lhs, rhs []block.SeriesMeta,
   112  ) ([]indexMatcher, []block.SeriesMeta) {
   113  	idFunction := hashFunc(matching.On, matching.MatchingLabels...)
   114  	// The set of signatures for the right-hand side.
   115  	rightSigs := make(map[uint64]int, len(rhs))
   116  	for idx, meta := range rhs {
   117  		rightSigs[idFunction(meta.Tags)] = idx
   118  	}
   119  
   120  	matchers := make([]indexMatcher, 0, len(lhs))
   121  	metas := make([]block.SeriesMeta, 0, len(lhs))
   122  	for lhsIndex, ls := range lhs {
   123  		if rhsIndex, ok := rightSigs[idFunction(ls.Tags)]; ok {
   124  			matcher := indexMatcher{
   125  				lhsIndex: lhsIndex,
   126  				rhsIndex: rhsIndex,
   127  			}
   128  
   129  			matchers = append(matchers, matcher)
   130  			metas = append(metas, lhs[lhsIndex])
   131  		}
   132  	}
   133  
   134  	return matchers[:len(matchers)], metas[:len(metas)]
   135  }