1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Level23\Druid\Queries; |
5
|
|
|
|
6
|
|
|
use Level23\Druid\Limits\Limit; |
7
|
|
|
use Level23\Druid\Types\Granularity; |
8
|
|
|
use Level23\Druid\Limits\LimitInterface; |
9
|
|
|
use Level23\Druid\Filters\FilterInterface; |
10
|
|
|
use Level23\Druid\Context\ContextInterface; |
11
|
|
|
use Level23\Druid\Collections\IntervalCollection; |
12
|
|
|
use Level23\Druid\Responses\GroupByQueryResponse; |
13
|
|
|
use Level23\Druid\Collections\DimensionCollection; |
14
|
|
|
use Level23\Druid\Collections\AggregationCollection; |
15
|
|
|
use Level23\Druid\Collections\VirtualColumnCollection; |
16
|
|
|
use Level23\Druid\HavingFilters\HavingFilterInterface; |
17
|
|
|
use Level23\Druid\Collections\PostAggregationCollection; |
18
|
|
|
|
19
|
|
|
class GroupByQuery implements QueryInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $dataSource; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var \Level23\Druid\Collections\DimensionCollection |
28
|
|
|
*/ |
29
|
|
|
protected $dimensions; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $granularity; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var \Level23\Druid\Filters\FilterInterface|null |
38
|
|
|
*/ |
39
|
|
|
protected $filter; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var \Level23\Druid\Collections\AggregationCollection|null |
43
|
|
|
*/ |
44
|
|
|
protected $aggregations; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var \Level23\Druid\Collections\PostAggregationCollection|null |
48
|
|
|
*/ |
49
|
|
|
protected $postAggregations; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var \Level23\Druid\Collections\VirtualColumnCollection|null |
53
|
|
|
*/ |
54
|
|
|
protected $virtualColumns; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var \Level23\Druid\HavingFilters\HavingFilterInterface|null |
58
|
|
|
*/ |
59
|
|
|
protected $having; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var \Level23\Druid\Context\ContextInterface|null |
63
|
|
|
*/ |
64
|
|
|
protected $context; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var \Level23\Druid\Limits\LimitInterface|null |
68
|
|
|
*/ |
69
|
|
|
protected $limit; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @var \Level23\Druid\Collections\IntervalCollection |
73
|
|
|
*/ |
74
|
|
|
protected $intervals; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @var array |
78
|
|
|
*/ |
79
|
|
|
protected $subtotals = []; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* GroupByQuery constructor. |
83
|
|
|
* |
84
|
|
|
* @param string $dataSource |
85
|
|
|
* @param \Level23\Druid\Collections\DimensionCollection $dimensions |
86
|
|
|
* @param \Level23\Druid\Collections\IntervalCollection $intervals |
87
|
|
|
* @param null|array|AggregationCollection $aggregations |
88
|
|
|
* @param string $granularity |
89
|
|
|
*/ |
90
|
5 |
|
public function __construct( |
91
|
|
|
string $dataSource, |
92
|
|
|
DimensionCollection $dimensions, |
93
|
|
|
IntervalCollection $intervals, |
94
|
|
|
$aggregations = null, |
95
|
|
|
$granularity = 'all' |
96
|
|
|
) { |
97
|
5 |
|
$this->dataSource = $dataSource; |
98
|
5 |
|
$this->dimensions = $dimensions; |
99
|
5 |
|
$this->granularity = Granularity::validate($granularity); |
100
|
4 |
|
$this->intervals = $intervals; |
101
|
|
|
|
102
|
4 |
|
if ($aggregations) { |
103
|
1 |
|
$this->setAggregations($aggregations); |
104
|
|
|
} |
105
|
4 |
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Return the query in array format so we can fire it to druid. |
109
|
|
|
* |
110
|
|
|
* @return array |
111
|
|
|
*/ |
112
|
1 |
|
public function toArray(): array |
113
|
|
|
{ |
114
|
|
|
$query = [ |
115
|
1 |
|
'queryType' => 'groupBy', |
116
|
1 |
|
'dataSource' => $this->dataSource, |
117
|
1 |
|
'intervals' => $this->intervals->toArray(), |
118
|
1 |
|
'dimensions' => $this->dimensions->toArray(), |
119
|
1 |
|
'granularity' => $this->granularity, |
120
|
|
|
]; |
121
|
|
|
|
122
|
1 |
|
if ($this->filter) { |
123
|
1 |
|
$query['filter'] = $this->filter->toArray(); |
124
|
|
|
} |
125
|
|
|
|
126
|
1 |
|
if ($this->aggregations) { |
127
|
1 |
|
$query['aggregations'] = $this->aggregations->toArray(); |
128
|
|
|
} |
129
|
|
|
|
130
|
1 |
|
if ($this->postAggregations) { |
131
|
1 |
|
$query['postAggregations'] = $this->postAggregations->toArray(); |
132
|
|
|
} |
133
|
|
|
|
134
|
1 |
|
if ($this->virtualColumns) { |
135
|
1 |
|
$query['virtualColumns'] = $this->virtualColumns->toArray(); |
136
|
|
|
} |
137
|
|
|
|
138
|
1 |
|
if ($this->having) { |
139
|
1 |
|
$query['having'] = $this->having->toArray(); |
140
|
|
|
} |
141
|
|
|
|
142
|
1 |
|
if ($this->context) { |
143
|
1 |
|
$query['context'] = $this->context->toArray(); |
144
|
|
|
} |
145
|
|
|
|
146
|
1 |
|
if ($this->limit) { |
147
|
1 |
|
$query['limitSpec'] = $this->limit->toArray(); |
148
|
|
|
} |
149
|
|
|
|
150
|
1 |
|
if (count($this->subtotals) > 0) { |
151
|
1 |
|
$query['subtotalsSpec'] = $this->subtotals; |
152
|
|
|
} |
153
|
|
|
|
154
|
1 |
|
return $query; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param \Level23\Druid\Filters\FilterInterface $filter |
159
|
|
|
*/ |
160
|
1 |
|
public function setFilter(FilterInterface $filter) |
161
|
|
|
{ |
162
|
1 |
|
$this->filter = $filter; |
163
|
1 |
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param \Level23\Druid\Collections\AggregationCollection|array $aggregations |
167
|
|
|
*/ |
168
|
1 |
|
public function setAggregations($aggregations) |
169
|
|
|
{ |
170
|
1 |
|
if (is_array($aggregations)) { |
171
|
1 |
|
$aggregations = new AggregationCollection(...$aggregations); |
172
|
|
|
} |
173
|
|
|
|
174
|
1 |
|
$this->aggregations = $aggregations; |
175
|
1 |
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @param \Level23\Druid\Collections\PostAggregationCollection|array $postAggregations |
179
|
|
|
*/ |
180
|
1 |
|
public function setPostAggregations($postAggregations) |
181
|
|
|
{ |
182
|
1 |
|
if (is_array($postAggregations)) { |
183
|
1 |
|
$postAggregations = new PostAggregationCollection(...$postAggregations); |
184
|
|
|
} |
185
|
|
|
|
186
|
1 |
|
$this->postAggregations = $postAggregations; |
187
|
1 |
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @param \Level23\Druid\HavingFilters\HavingFilterInterface $having |
191
|
|
|
*/ |
192
|
1 |
|
public function setHaving(HavingFilterInterface $having) |
193
|
|
|
{ |
194
|
1 |
|
$this->having = $having; |
195
|
1 |
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @param \Level23\Druid\Context\ContextInterface $context |
199
|
|
|
*/ |
200
|
1 |
|
public function setContext(ContextInterface $context) |
201
|
|
|
{ |
202
|
1 |
|
$this->context = $context; |
203
|
1 |
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* @param \Level23\Druid\Limits\LimitInterface|int $limit |
207
|
|
|
*/ |
208
|
1 |
|
public function setLimit($limit) |
209
|
|
|
{ |
210
|
1 |
|
if ($limit instanceof LimitInterface) { |
211
|
1 |
|
$this->limit = $limit; |
212
|
|
|
} else { |
213
|
|
|
|
214
|
1 |
|
if (!$this->limit) { |
215
|
|
|
$this->limit = new Limit(); |
216
|
|
|
} |
217
|
|
|
|
218
|
1 |
|
$this->limit->setLimit($limit); |
219
|
|
|
} |
220
|
1 |
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* The "offset" parameter tells Druid to skip this many rows when returning results. |
224
|
|
|
* |
225
|
|
|
* @param int $offset |
226
|
|
|
*/ |
227
|
1 |
|
public function setOffset(int $offset) |
228
|
|
|
{ |
229
|
1 |
|
if (!$this->limit) { |
230
|
|
|
$this->limit = new Limit(); |
231
|
|
|
} |
232
|
1 |
|
$this->limit->setOffset($offset); |
233
|
1 |
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Parse the response into something we can return to the user. |
237
|
|
|
* |
238
|
|
|
* @param array $response |
239
|
|
|
* |
240
|
|
|
* @return GroupByQueryResponse |
241
|
|
|
*/ |
242
|
1 |
|
public function parseResponse(array $response): GroupByQueryResponse |
243
|
|
|
{ |
244
|
1 |
|
return new GroupByQueryResponse($response); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* @param \Level23\Druid\Collections\VirtualColumnCollection $virtualColumns |
249
|
|
|
*/ |
250
|
1 |
|
public function setVirtualColumns(VirtualColumnCollection $virtualColumns): void |
251
|
|
|
{ |
252
|
1 |
|
$this->virtualColumns = $virtualColumns; |
253
|
1 |
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* @param array $subtotals |
257
|
|
|
*/ |
258
|
1 |
|
public function setSubtotals(array $subtotals): void |
259
|
|
|
{ |
260
|
1 |
|
$this->subtotals = $subtotals; |
261
|
|
|
} |
262
|
|
|
} |