github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/query/functions/binary/common.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 "bytes" 25 "errors" 26 27 "github.com/m3db/m3/src/query/block" 28 "github.com/m3db/m3/src/query/models" 29 ) 30 31 var ( 32 errRExhausted = errors.New("right iter exhausted while left iter has values") 33 errLExhausted = errors.New("left iter exhausted while right iter has values") 34 ) 35 36 type indexMatcher struct { 37 lhsIndex int 38 rhsIndex int 39 } 40 41 // hashFunc returns a function that calculates the signature for a metric 42 // ignoring the provided labels. If on, then only the given labels are used. 43 func hashFunc(on bool, names ...[]byte) func(models.Tags) uint64 { 44 if on { 45 return func(tags models.Tags) uint64 { 46 return tags.TagsWithKeys(names).HashedID() 47 } 48 } 49 50 return func(tags models.Tags) uint64 { 51 return tags.TagsWithoutKeys(names).HashedID() 52 } 53 } 54 55 const initIndexSliceLength = 10 56 57 var ( 58 errMismatchedBounds = errors.New("block bounds are mismatched") 59 errMismatchedStepCounts = errors.New("block step counts are mismatched") 60 errLeftScalar = errors.New("expected left scalar but node type" + 61 " incorrect") 62 errRightScalar = errors.New("expected right scalar but node" + 63 " type incorrect") 64 errNoModifierForComparison = errors.New("comparisons between scalars must" + 65 " use BOOL modifier") 66 errNoMatching = errors.New("vector matching parameters must" + 67 " be provided for binary operations between series") 68 ) 69 70 func tagMap(t models.Tags) map[string]models.Tag { 71 m := make(map[string]models.Tag, t.Len()) 72 for _, tag := range t.Tags { 73 m[string(tag.Name)] = tag 74 } 75 76 return m 77 } 78 79 // Iff one of left or right is a time block, match match one to many 80 // against it, and match everything. 81 func defaultVectorMatcherBuilder(lhs, rhs block.Block) VectorMatching { 82 left := lhs.Info().BaseType() == block.BlockTime 83 right := rhs.Info().BaseType() == block.BlockTime 84 85 if left { 86 if right { 87 return VectorMatching{ 88 Set: true, 89 Card: CardOneToOne, 90 } 91 } 92 93 return VectorMatching{ 94 Set: true, 95 Card: CardOneToMany, 96 On: true, 97 } 98 } 99 100 if right { 101 return VectorMatching{ 102 Set: true, 103 Card: CardManyToOne, 104 On: true, 105 } 106 } 107 108 return VectorMatching{Set: false} 109 } 110 111 func combineMetaAndSeriesMeta( 112 meta, otherMeta block.Metadata, 113 seriesMeta, otherSeriesMeta []block.SeriesMeta, 114 ) (block.Metadata, []block.SeriesMeta, []block.SeriesMeta, error) { 115 if !meta.Bounds.Equals(otherMeta.Bounds) { 116 return block.Metadata{}, 117 []block.SeriesMeta{}, 118 []block.SeriesMeta{}, 119 errMismatchedBounds 120 } 121 122 // NB (arnikola): mutating tags in `meta` to avoid allocations 123 leftTags := meta.Tags 124 otherTags := tagMap(otherMeta.Tags) 125 126 metaTagsToAdd := models.NewTags(leftTags.Len(), leftTags.Opts) 127 otherMetaTagsToAdd := models.NewTags(len(otherTags), leftTags.Opts) 128 tags := models.NewTags(leftTags.Len(), leftTags.Opts) 129 130 for _, t := range leftTags.Tags { 131 name := string(t.Name) 132 if otherTag, ok := otherTags[name]; ok { 133 if bytes.Equal(t.Value, otherTag.Value) { 134 tags = tags.AddTag(t) 135 } else { 136 // If both metas have the same common tag with different 137 // values explicitly add it to each seriesMeta. 138 metaTagsToAdd = metaTagsToAdd.AddTag(t) 139 otherMetaTagsToAdd = otherMetaTagsToAdd.AddTag(otherTag) 140 } 141 142 // NB(arnikola): delete common tag from otherTags as it 143 // has already been handled 144 delete(otherTags, name) 145 } else { 146 // Tag does not exist on otherMeta explicitly add it to each seriesMeta 147 metaTagsToAdd = metaTagsToAdd.AddTag(t) 148 } 149 } 150 151 // Iterate over otherMeta common tags and explicitly add 152 // remaining tags to otherSeriesMeta 153 for _, otherTag := range otherTags { 154 otherMetaTagsToAdd = otherMetaTagsToAdd.AddTag(otherTag) 155 } 156 157 // Set common tags 158 meta.Tags = tags 159 for i, m := range seriesMeta { 160 seriesMeta[i].Tags = m.Tags.Add(metaTagsToAdd) 161 } 162 163 for i, m := range otherSeriesMeta { 164 otherSeriesMeta[i].Tags = m.Tags.Add(otherMetaTagsToAdd) 165 } 166 167 meta.ResultMetadata = meta.ResultMetadata. 168 CombineMetadata(otherMeta.ResultMetadata) 169 170 return meta, 171 seriesMeta, 172 otherSeriesMeta, 173 nil 174 } 175 176 // NB: binary functions should remove the name tag from relevant series. 177 func removeNameTags( 178 meta block.Metadata, 179 metas []block.SeriesMeta, 180 ) (block.Metadata, []block.SeriesMeta) { 181 meta.Tags = meta.Tags.WithoutName() 182 for i, sm := range metas { 183 metas[i].Tags = sm.Tags.WithoutName() 184 } 185 186 return meta, metas 187 }