github.com/m3db/m3@v1.5.0/src/query/functions/binary/unless.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/functions/utils" 29 "github.com/m3db/m3/src/query/models" 30 ) 31 32 // UnlessType uses all values from lhs which do not exist in rhs 33 const UnlessType = "unless" 34 35 func makeUnlessBlock( 36 queryCtx *models.QueryContext, 37 lMeta, rMeta block.Metadata, 38 lIter, rIter block.StepIter, 39 controller *transform.Controller, 40 matching VectorMatching, 41 ) (block.Block, error) { 42 if !matching.Set { 43 return nil, errNoMatching 44 } 45 46 lSeriesMetas := lIter.SeriesMeta() 47 lMeta, lSeriesMetas = removeNameTags(lMeta, lSeriesMetas) 48 49 rSeriesMetas := rIter.SeriesMeta() 50 rMeta, rSeriesMetas = removeNameTags(rMeta, rSeriesMetas) 51 52 // NB: need to flatten metadata for cases where 53 // e.g. lhs: common tags {a:b}, series tags: {c:d}, {e:f} 54 // e.g. rhs: common tags {c:d}, series tags: {a:b}, {e:f} 55 // If not flattened before calculating distinct values, 56 // both series on lhs would be added 57 lSeriesMetas = utils.FlattenMetadata(lMeta, lSeriesMetas) 58 rSeriesMetas = utils.FlattenMetadata(rMeta, rSeriesMetas) 59 indices := matchingIndices(matching, lSeriesMetas, rSeriesMetas) 60 61 lMeta.ResultMetadata = lMeta.ResultMetadata. 62 CombineMetadata(rMeta.ResultMetadata) 63 64 builder, err := controller.BlockBuilder(queryCtx, lMeta, lSeriesMetas) 65 if err != nil { 66 return nil, err 67 } 68 69 if err = builder.AddCols(lIter.StepCount()); err != nil { 70 return nil, err 71 } 72 73 for index := 0; lIter.Next(); index++ { 74 if !rIter.Next() { 75 return nil, errRExhausted 76 } 77 78 lStep := lIter.Current() 79 lValues := lStep.Values() 80 rStep := rIter.Current() 81 rValues := rStep.Values() 82 for _, indexMatcher := range indices { 83 if !math.IsNaN(rValues[indexMatcher.rhsIndex]) { 84 lValues[indexMatcher.lhsIndex] = math.NaN() 85 } 86 } 87 88 if err := builder.AppendValues(index, lValues); 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 // matchingIndices returns a slice representing which index in the lhs the rhs 109 // series maps to. If it does not map to an existing index, this is set to -1. 110 func matchingIndices( 111 matching VectorMatching, 112 lhs, rhs []block.SeriesMeta, 113 ) []indexMatcher { 114 idFunction := hashFunc(matching.On, matching.MatchingLabels...) 115 // The set of signatures for the left-hand side. 116 leftSigs := make(map[uint64]int, len(lhs)) 117 for idx, meta := range lhs { 118 leftSigs[idFunction(meta.Tags)] = idx 119 } 120 121 rhsIndices := make([]indexMatcher, 0, len(rhs)) 122 for i, rs := range rhs { 123 // If this series matches a series on the lhs, add its index. 124 id := idFunction(rs.Tags) 125 if lhsIndex, ok := leftSigs[id]; ok { 126 matcher := indexMatcher{ 127 lhsIndex: lhsIndex, 128 rhsIndex: i, 129 } 130 131 rhsIndices = append(rhsIndices, matcher) 132 } 133 } 134 135 return rhsIndices[:len(rhsIndices)] 136 }