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