github.com/m3db/m3@v1.5.0/src/query/functions/binary/logical.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  	"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  )
    28  
    29  type makeBlockFn func(
    30  	queryCtx *models.QueryContext,
    31  	lMeta, rMeta block.Metadata,
    32  	lIter, rIter block.StepIter,
    33  	controller *transform.Controller,
    34  	matching VectorMatching,
    35  ) (block.Block, error)
    36  
    37  // Builds a logical processing function if able. If wrong opType supplied,
    38  // returns no function and false.
    39  func buildLogicalFunction(
    40  	opType string,
    41  	params NodeParams,
    42  ) (processFunc, bool) {
    43  	var makeBlock makeBlockFn
    44  	switch opType {
    45  	case AndType:
    46  		makeBlock = makeAndBlock
    47  	case OrType:
    48  		makeBlock = makeOrBlock
    49  	case UnlessType:
    50  		makeBlock = makeUnlessBlock
    51  	default:
    52  		return nil, false
    53  	}
    54  
    55  	return createLogicalProcessingStep(params, makeBlock), true
    56  }
    57  
    58  func createLogicalProcessingStep(
    59  	params NodeParams,
    60  	fn makeBlockFn,
    61  ) processFunc {
    62  	return func(queryCtx *models.QueryContext, lhs, rhs block.Block,
    63  		controller *transform.Controller) (block.Block, error) {
    64  		return processLogical(queryCtx, lhs, rhs, controller,
    65  			params.VectorMatcherBuilder, fn)
    66  	}
    67  }
    68  
    69  func processLogical(
    70  	queryCtx *models.QueryContext,
    71  	lhs, rhs block.Block,
    72  	controller *transform.Controller,
    73  	matcherBuilder VectorMatcherBuilder,
    74  	makeBlock makeBlockFn,
    75  ) (block.Block, error) {
    76  	lIter, err := lhs.StepIter()
    77  	if err != nil {
    78  		return nil, err
    79  	}
    80  
    81  	rIter, err := rhs.StepIter()
    82  	if err != nil {
    83  		return nil, err
    84  	}
    85  
    86  	if lIter.StepCount() != rIter.StepCount() {
    87  		return nil, errMismatchedStepCounts
    88  	}
    89  
    90  	matching := matcherBuilder(lhs, rhs)
    91  	return makeBlock(queryCtx, lhs.Meta(), rhs.Meta(),
    92  		lIter, rIter, controller, matching)
    93  }