-
Notifications
You must be signed in to change notification settings - Fork 459
/
Copy pathabsent.go
179 lines (152 loc) · 4.4 KB
/
absent.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Copyright (c) 2019 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package aggregation
import (
"math"
"github.com/m3db/m3/src/query/block"
"github.com/m3db/m3/src/query/executor/transform"
"github.com/m3db/m3/src/query/functions/utils"
"github.com/m3db/m3/src/query/models"
"github.com/m3db/m3/src/query/parser"
)
const (
// AbsentType returns 1 if there are no elements in this step, or if no series
// are present in the current block.
AbsentType = "absent"
)
// NewAbsentOp creates a new absent operation.
func NewAbsentOp() parser.Params {
return newAbsentOp()
}
// absentOp stores required properties for absent ops.
type absentOp struct{}
// OpType for the operator.
func (o absentOp) OpType() string {
return AbsentType
}
// String representation.
func (o absentOp) String() string {
return "type: absent"
}
// Node creates an execution node.
func (o absentOp) Node(
controller *transform.Controller,
_ transform.Options,
) transform.OpNode {
return &absentNode{
op: o,
controller: controller,
}
}
func newAbsentOp() absentOp {
return absentOp{}
}
// absentNode is different from base node as it uses no grouping and has
// special handling for the 0-series case.
type absentNode struct {
op parser.Params
controller *transform.Controller
}
func (n *absentNode) Params() parser.Params {
return n.op
}
func (n *absentNode) Process(queryCtx *models.QueryContext,
ID parser.NodeID, b block.Block) error {
return transform.ProcessSimpleBlock(n, n.controller, queryCtx, ID, b)
}
func (n *absentNode) ProcessBlock(
queryCtx *models.QueryContext,
ID parser.NodeID,
bl block.Block,
) (block.Block, error) {
stepIter, err := bl.StepIter()
if err != nil {
return nil, err
}
var (
meta = bl.Meta()
seriesMetas = stepIter.SeriesMeta()
tagOpts = meta.Tags.Opts
)
// If no series in the input, return a scalar block with value 1.
if len(seriesMetas) == 0 {
return block.NewScalar(1, meta), nil
}
// NB: pull any common tags out into the created series.
dupeTags, _ := utils.DedupeMetadata(seriesMetas, tagOpts)
meta.Tags = meta.Tags.Add(dupeTags).Normalize()
emptySeriesMeta := []block.SeriesMeta{
block.SeriesMeta{
Tags: models.NewTags(0, tagOpts),
Name: []byte{},
},
}
setupBuilderWithValuesToIndex := func(idx int) (block.Builder, error) {
builder, err := n.controller.BlockBuilder(queryCtx, meta, emptySeriesMeta)
if err != nil {
return nil, err
}
if err = builder.AddCols(stepIter.StepCount()); err != nil {
return nil, err
}
for i := 0; i < idx; i++ {
if err := builder.AppendValue(i, math.NaN()); err != nil {
return nil, err
}
}
if err := builder.AppendValue(idx, 1); err != nil {
return nil, err
}
return builder, err
}
var builder block.Builder
for idx := 0; stepIter.Next(); idx++ {
var (
step = stepIter.Current()
values = step.Values()
val float64 = 1
)
for _, v := range values {
if !math.IsNaN(v) {
val = math.NaN()
break
}
}
if builder == nil {
if !math.IsNaN(val) {
builder, err = setupBuilderWithValuesToIndex(idx)
if err != nil {
return nil, err
}
}
} else {
if err := builder.AppendValue(idx, val); err != nil {
return nil, err
}
}
}
if err = stepIter.Err(); err != nil {
return nil, err
}
if builder == nil {
return block.NewEmptyBlock(meta), nil
}
return builder.Build(), nil
}