github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/query/functions/binary/comparison.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  const (
    32  	// EqType checks that lhs is equal to rhs.
    33  	EqType = "=="
    34  
    35  	// NotEqType checks that lhs is equal to rhs.
    36  	NotEqType = "!="
    37  
    38  	// GreaterType checks that lhs is equal to rhs.
    39  	GreaterType = ">"
    40  
    41  	// LesserType checks that lhs is equal to rhs.
    42  	LesserType = "<"
    43  
    44  	// GreaterEqType checks that lhs is equal to rhs.
    45  	GreaterEqType = ">="
    46  
    47  	// LesserEqType checks that lhs is equal to rhs.
    48  	LesserEqType = "<="
    49  
    50  	// suffix to return bool values instead of lhs values.
    51  	returnBoolSuffix = "BOOL"
    52  )
    53  
    54  // convert true to 1, false to 0.
    55  func toFloat(b bool) float64 {
    56  	if b {
    57  		return 1
    58  	}
    59  	return 0
    60  }
    61  
    62  // convert true to x, false to NaN.
    63  func toComparisonValue(b bool, x float64) float64 {
    64  	if b {
    65  		return x
    66  	}
    67  	return math.NaN()
    68  }
    69  
    70  var (
    71  	comparisonFuncs = map[string]binaryFunction{
    72  		EqType:        func(x, y float64) float64 { return toComparisonValue(x == y, x) },
    73  		NotEqType:     func(x, y float64) float64 { return toComparisonValue(x != y, x) },
    74  		GreaterType:   func(x, y float64) float64 { return toComparisonValue(x > y, x) },
    75  		LesserType:    func(x, y float64) float64 { return toComparisonValue(x < y, x) },
    76  		GreaterEqType: func(x, y float64) float64 { return toComparisonValue(x >= y, x) },
    77  		LesserEqType:  func(x, y float64) float64 { return toComparisonValue(x <= y, x) },
    78  
    79  		EqType + returnBoolSuffix:        func(x, y float64) float64 { return toFloat(x == y) },
    80  		NotEqType + returnBoolSuffix:     func(x, y float64) float64 { return toFloat(x != y) },
    81  		GreaterType + returnBoolSuffix:   func(x, y float64) float64 { return toFloat(x > y) },
    82  		LesserType + returnBoolSuffix:    func(x, y float64) float64 { return toFloat(x < y) },
    83  		GreaterEqType + returnBoolSuffix: func(x, y float64) float64 { return toFloat(x >= y) },
    84  		LesserEqType + returnBoolSuffix:  func(x, y float64) float64 { return toFloat(x <= y) },
    85  	}
    86  )
    87  
    88  // Builds a comparison processing function if able. If wrong opType supplied,
    89  // returns no function and false.
    90  func buildComparisonFunction(
    91  	opType string,
    92  	params NodeParams,
    93  ) (processFunc, bool) {
    94  	if params.ReturnBool {
    95  		opType += returnBoolSuffix
    96  	}
    97  	fn, ok := comparisonFuncs[opType]
    98  	if !ok {
    99  		return nil, false
   100  	}
   101  
   102  	return func(
   103  		queryCtx *models.QueryContext,
   104  		lhs, rhs block.Block,
   105  		controller *transform.Controller) (block.Block, error) {
   106  		return processBinary(
   107  			queryCtx,
   108  			lhs, rhs,
   109  			params,
   110  			controller,
   111  			true,
   112  			fn)
   113  	}, true
   114  }