1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Level23\Druid\Queries; |
5
|
|
|
|
6
|
|
|
use Level23\Druid\Context\QueryContext; |
7
|
|
|
use Level23\Druid\Types\OrderByDirection; |
8
|
|
|
use Level23\Druid\Filters\FilterInterface; |
9
|
|
|
use Level23\Druid\Types\ScanQueryResultFormat; |
10
|
|
|
use Level23\Druid\Responses\ScanQueryResponse; |
11
|
|
|
use Level23\Druid\Collections\IntervalCollection; |
12
|
|
|
use Level23\Druid\Collections\VirtualColumnCollection; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class ScanQuery |
16
|
|
|
* |
17
|
|
|
* @see https://druid.apache.org/docs/latest/querying/scan-query.html |
18
|
|
|
* @package Level23\Druid\Queries |
19
|
|
|
*/ |
20
|
|
|
class ScanQuery implements QueryInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $dataSource; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var \Level23\Druid\Collections\IntervalCollection |
29
|
|
|
*/ |
30
|
|
|
protected $intervals; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $resultFormat = ScanQueryResultFormat::NORMAL_LIST; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* How many rows buffered before return to client. Default is 20480 |
39
|
|
|
* |
40
|
|
|
* @var int |
41
|
|
|
*/ |
42
|
|
|
protected $batchSize; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Return results consistent with the legacy "scan-query" contrib extension. Defaults to the value set by |
46
|
|
|
* druid.query.scan.legacy, which in turn defaults to false. See Legacy mode for details. |
47
|
|
|
* |
48
|
|
|
* @var bool |
49
|
|
|
*/ |
50
|
|
|
protected $legacy; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* The ordering of returned rows based on timestamp. "ascending", "descending" are supported. When not supplied, |
54
|
|
|
* "none" is used. Currently, "ascending" and "descending" are only supported for queries where the __time column |
55
|
|
|
* is included in the columns field and the requirements outlined in the time ordering section are met. |
56
|
|
|
* |
57
|
|
|
* @var string |
58
|
|
|
*/ |
59
|
|
|
protected $order; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* How many rows to return. If not specified, all rows will be returned. |
63
|
|
|
* |
64
|
|
|
* @var int |
65
|
|
|
*/ |
66
|
|
|
protected $limit; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Skip this many rows when returning results. |
70
|
|
|
* |
71
|
|
|
* @var int |
72
|
|
|
*/ |
73
|
|
|
protected $offset; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @var \Level23\Druid\Context\QueryContext|null |
77
|
|
|
*/ |
78
|
|
|
protected $context; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @var \Level23\Druid\Filters\FilterInterface|null |
82
|
|
|
*/ |
83
|
|
|
protected $filter; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* A String array of dimensions and metrics to scan. If left empty, all dimensions and metrics are returned. |
87
|
|
|
* |
88
|
|
|
* @var array|string[] |
89
|
|
|
*/ |
90
|
|
|
protected $columns = []; |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @var \Level23\Druid\Collections\VirtualColumnCollection|null |
94
|
|
|
*/ |
95
|
|
|
protected $virtualColumns; |
96
|
|
|
|
97
|
7 |
|
public function __construct(string $dataSource, IntervalCollection $intervals) |
98
|
|
|
{ |
99
|
7 |
|
$this->dataSource = $dataSource; |
100
|
7 |
|
$this->intervals = $intervals; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Return the query in array format so we can fire it to druid. |
105
|
|
|
* |
106
|
|
|
* @return array |
107
|
|
|
*/ |
108
|
3 |
|
public function toArray(): array |
109
|
|
|
{ |
110
|
3 |
|
$result = [ |
111
|
|
|
'queryType' => 'scan', |
112
|
3 |
|
'dataSource' => $this->dataSource, |
113
|
3 |
|
'intervals' => $this->intervals->toArray(), |
114
|
3 |
|
'resultFormat' => $this->resultFormat, |
115
|
3 |
|
'columns' => $this->columns, |
116
|
|
|
]; |
117
|
|
|
|
118
|
3 |
|
if ($this->virtualColumns) { |
119
|
2 |
|
$result['virtualColumns'] = $this->virtualColumns->toArray(); |
120
|
|
|
} |
121
|
|
|
|
122
|
3 |
|
if ($this->filter) { |
123
|
3 |
|
$result['filter'] = $this->filter->toArray(); |
124
|
|
|
} |
125
|
|
|
|
126
|
3 |
|
if ($this->batchSize !== null) { |
127
|
2 |
|
$result['batchSize'] = $this->batchSize; |
128
|
|
|
} |
129
|
|
|
|
130
|
3 |
|
if ($this->limit !== null) { |
131
|
2 |
|
$result['limit'] = $this->limit; |
132
|
|
|
} |
133
|
|
|
|
134
|
3 |
|
if ($this->offset !== null) { |
135
|
2 |
|
$result['offset'] = $this->offset; |
136
|
|
|
} |
137
|
|
|
|
138
|
3 |
|
if ($this->legacy !== null) { |
139
|
3 |
|
$result['legacy'] = $this->legacy; |
140
|
|
|
} |
141
|
|
|
|
142
|
3 |
|
if ($this->context) { |
143
|
2 |
|
$result['context'] = $this->context->toArray(); |
144
|
|
|
} |
145
|
|
|
|
146
|
3 |
|
if ($this->order) { |
147
|
2 |
|
$result['order'] = $this->order; |
148
|
|
|
} |
149
|
|
|
|
150
|
3 |
|
return $result; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Parse the response into something we can return to the user. |
155
|
|
|
* |
156
|
|
|
* @param array $response |
157
|
|
|
* |
158
|
|
|
* @return ScanQueryResponse |
159
|
|
|
*/ |
160
|
2 |
|
public function parseResponse(array $response): ScanQueryResponse |
161
|
|
|
{ |
162
|
2 |
|
return new ScanQueryResponse($response); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* How the results are represented. Use one of the ScanQueryResultFormat constants |
167
|
|
|
* |
168
|
|
|
* @param string $resultFormat |
169
|
|
|
*/ |
170
|
4 |
|
public function setResultFormat(string $resultFormat): void |
171
|
|
|
{ |
172
|
4 |
|
$this->resultFormat = ScanQueryResultFormat::validate($resultFormat); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* How many rows buffered before return to client. Default is 20480 |
177
|
|
|
* |
178
|
|
|
* @param int $batchSize |
179
|
|
|
*/ |
180
|
2 |
|
public function setBatchSize(int $batchSize): void |
181
|
|
|
{ |
182
|
2 |
|
$this->batchSize = $batchSize; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* How many rows to return. If not specified, all rows will be returned. |
187
|
|
|
* |
188
|
|
|
* @param int $limit |
189
|
|
|
*/ |
190
|
2 |
|
public function setLimit(int $limit): void |
191
|
|
|
{ |
192
|
2 |
|
$this->limit = $limit; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Skip this many rows when returning results. Skipped rows will still need to be generated internally and then |
197
|
|
|
* discarded, meaning that raising offsets to high values can cause queries to use additional resources. |
198
|
|
|
* |
199
|
|
|
* Together, "limit" and "offset" can be used to implement pagination. However, note that if the underlying |
200
|
|
|
* datasource is modified in between page fetches in ways that affect overall query results, then the |
201
|
|
|
* different pages will not necessarily align with each other. |
202
|
|
|
* |
203
|
|
|
* @param int $offset |
204
|
|
|
*/ |
205
|
2 |
|
public function setOffset(int $offset): void |
206
|
|
|
{ |
207
|
2 |
|
$this->offset = $offset; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @param bool $legacy |
212
|
|
|
*/ |
213
|
3 |
|
public function setLegacy(bool $legacy): void |
214
|
|
|
{ |
215
|
3 |
|
$this->legacy = $legacy; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @param \Level23\Druid\Context\QueryContext $context |
220
|
|
|
*/ |
221
|
2 |
|
public function setContext(QueryContext $context): void |
222
|
|
|
{ |
223
|
2 |
|
$this->context = $context; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @param \Level23\Druid\Collections\VirtualColumnCollection $virtualColumns |
228
|
|
|
*/ |
229
|
2 |
|
public function setVirtualColumns(VirtualColumnCollection $virtualColumns): void |
230
|
|
|
{ |
231
|
2 |
|
$this->virtualColumns = $virtualColumns; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* @param \Level23\Druid\Filters\FilterInterface $filter |
236
|
|
|
*/ |
237
|
3 |
|
public function setFilter(FilterInterface $filter): void |
238
|
|
|
{ |
239
|
3 |
|
$this->filter = $filter; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* The ordering of returned rows based on timestamp. "ascending", "descending", and "none" (default) are supported. |
244
|
|
|
* Currently, "ascending" and "descending" are only supported for queries where the __time column is included in |
245
|
|
|
* the columns field and the requirements outlined in the time ordering section are met. |
246
|
|
|
* |
247
|
|
|
* @param string $order |
248
|
|
|
*/ |
249
|
2 |
|
public function setOrder(string $order): void |
250
|
|
|
{ |
251
|
2 |
|
$this->order = OrderByDirection::validate($order); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* A String array of dimensions and metrics to scan. If left empty, all dimensions and metrics are returned. |
256
|
|
|
* |
257
|
|
|
* @param array|string[] $columns |
258
|
|
|
*/ |
259
|
2 |
|
public function setColumns(array $columns): void |
260
|
|
|
{ |
261
|
2 |
|
$this->columns = $columns; |
262
|
|
|
} |
263
|
|
|
} |
264
|
|
|
|