github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/query/functions/binary/arithmetic.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  	// PlusType adds datapoints in both series.
    33  	PlusType = "+"
    34  
    35  	// MinusType subtracts rhs from lhs.
    36  	MinusType = "-"
    37  
    38  	// MultiplyType multiplies datapoints by series.
    39  	MultiplyType = "*"
    40  
    41  	// DivType divides datapoints by series.
    42  	// Special cases are:
    43  	// 	 X / 0 = +Inf
    44  	// 	-X / 0 = -Inf
    45  	// 	 0 / 0 =  NaN
    46  	DivType = "/"
    47  
    48  	// ExpType raises lhs to the power of rhs.
    49  	// NB: to keep consistency with prometheus (and go)
    50  	//  0 ^ 0 = 1
    51  	//  NaN ^ 0 = 1
    52  	ExpType = "^"
    53  
    54  	// ModType takes the modulo of lhs by rhs.
    55  	// Special cases are:
    56  	// 	 X % 0 = NaN
    57  	// 	 NaN % X = NaN
    58  	// 	 X % NaN = NaN
    59  	ModType = "%"
    60  )
    61  
    62  var (
    63  	arithmeticFuncs = map[string]binaryFunction{
    64  		PlusType:     func(x, y float64) float64 { return x + y },
    65  		MinusType:    func(x, y float64) float64 { return x - y },
    66  		MultiplyType: func(x, y float64) float64 { return x * y },
    67  		DivType:      func(x, y float64) float64 { return x / y },
    68  		ModType:      math.Mod,
    69  		ExpType:      math.Pow,
    70  	}
    71  )
    72  
    73  // Builds an arithmetic processing function if able. If wrong opType supplied,
    74  // returns no function and false.
    75  func buildArithmeticFunction(
    76  	opType string,
    77  	params NodeParams,
    78  ) (processFunc, bool) {
    79  	fn, ok := arithmeticFuncs[opType]
    80  	if !ok {
    81  		return nil, false
    82  	}
    83  
    84  	// Build the binary processing step.
    85  	return func(
    86  		queryCtx *models.QueryContext,
    87  		lhs, rhs block.Block,
    88  		controller *transform.Controller) (block.Block, error) {
    89  		return processBinary(
    90  			queryCtx,
    91  			lhs, rhs,
    92  			params,
    93  			controller,
    94  			false,
    95  			fn)
    96  	}, true
    97  }