Passed
Pull Request — master (#35)
by Teye
14:54
created

HasPostAggregations::histogram()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 15
ccs 7
cts 7
cp 1
rs 9.9666
cc 2
nc 2
nop 4
crap 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Level23\Druid\Concerns;
5
6
use Closure;
7
use InvalidArgumentException;
8
use Level23\Druid\Types\DataType;
9
use Level23\Druid\PostAggregations\CdfPostAggregator;
10
use Level23\Druid\PostAggregations\RankPostAggregator;
11
use Level23\Druid\PostAggregations\LeastPostAggregator;
12
use Level23\Druid\Collections\PostAggregationCollection;
13
use Level23\Druid\PostAggregations\ConstantPostAggregator;
14
use Level23\Druid\PostAggregations\GreatestPostAggregator;
15
use Level23\Druid\PostAggregations\QuantilePostAggregator;
16
use Level23\Druid\PostAggregations\PostAggregationsBuilder;
17
use Level23\Druid\PostAggregations\PostAggregatorInterface;
18
use Level23\Druid\PostAggregations\QuantilesPostAggregator;
19
use Level23\Druid\PostAggregations\HistogramPostAggregator;
20
use Level23\Druid\PostAggregations\ArithmeticPostAggregator;
21
use Level23\Druid\PostAggregations\JavaScriptPostAggregator;
22
use Level23\Druid\PostAggregations\FieldAccessPostAggregator;
23
use Level23\Druid\PostAggregations\SketchSummaryPostAggregator;
24
use Level23\Druid\PostAggregations\HyperUniqueCardinalityPostAggregator;
25
26
trait HasPostAggregations
27
{
28
    /**
29
     * @var array|\Level23\Druid\PostAggregations\PostAggregatorInterface[]
30
     */
31
    protected $postAggregations = [];
32
33
    /**
34
     * Build our input field for the post aggregation.
35
     * This array can contain:
36
     *  - A string, referring to a metric or dimension in the query
37
     *  - A Closure, which allows you to build another postAggretator
38
     *
39
     * @param array $fields
40
     *
41
     * @return PostAggregationCollection
42
     * @throws InvalidArgumentException
43
     */
44 26
    protected function buildFields(array $fields): PostAggregationCollection
45
    {
46 26
        $first = reset($fields);
47
48 26
        if (is_array($first)) {
49 16
            $fields = $first;
50
        }
51
52 26
        $collection = new PostAggregationCollection();
53
54 26
        foreach ($fields as $field) {
55 26
            if (is_string($field)) {
56 25
                $collection->add(new FieldAccessPostAggregator($field, $field));
57 10
            } elseif ($field instanceof PostAggregatorInterface) {
58 1
                $collection->add($field);
59 10
            } elseif ($field instanceof Closure) {
60 9
                $builder = new PostAggregationsBuilder();
61 9
                call_user_func($field, $builder);
62 9
                $postAggregations = $builder->getPostAggregations();
63
64 9
                $collection->add(...$postAggregations);
65
            } else {
66 1
                throw new InvalidArgumentException(
67
                    'Incorrect field type given in postAggregation fields. Only strings (which will become' .
68
                    'FieldAccess types), Objects of the type PostAggregatorInterface and Closure\'s are allowed!'
69
                );
70
            }
71
        }
72
73 25
        return $collection;
74
    }
75
76
    /**
77
     * Divide two or more fields with each other.
78
     *
79
     * @param string               $as                The name which will be used in the output
80
     * @param string|Closure|array ...$fieldOrClosure One or more fields which will be used. When a string is given,
81
     *                                                we assume that it refers to another field in the query. If you
82
     *                                                give a closure, it will receive an instance of the
83
     *                                                PostAggregationsBuilder. With this builder you can build other
84
     *                                                post-aggregations or use constants as input for this method.
85
     *
86
     * @return $this
87
     */
88 7
    public function divide(string $as, ...$fieldOrClosure)
89
    {
90 7
        $this->postAggregations[] = new ArithmeticPostAggregator(
91
            $as,
92
            '/',
93 7
            $this->buildFields($fieldOrClosure),
94
            true
95
        );
96
97 7
        return $this;
98
    }
99
100
    /**
101
     * This returns an approximation to the value that would be preceded by a given fraction of a hypothetical sorted
102
     * version of the input stream.
103
     *
104
     * To use this aggregator, make sure you include the extension in your druid server config file:
105
     *
106
     * druid.extensions.loadList=["druid-datasketches"]
107
     *
108
     * @param string         $as             The name which will be used in the output
109
     * @param string|Closure $fieldOrClosure Field which will be used that refers to a DoublesSketch  (fieldAccess or
110
     *                                       another post aggregator). When a string is given, we assume that it refers
111
     *                                       to another field in the query. If you give a closure, it will receive an
112
     *                                       instance of the PostAggregationsBuilder. With this builder you can build
113
     *                                       another post-aggregation or use constants as input for this method.
114
     * @param float          $fraction       Fractional position in the hypothetical sorted stream, number from 0 to 1
115
     *                                       inclusive
116
     *
117
     * @return $this
118
     */
119 1
    public function quantile(string $as, $fieldOrClosure, float $fraction): self
120
    {
121 1
        $fields = $this->buildFields([$fieldOrClosure]);
122 1
        if ($fields->count() != 1) {
123 1
            throw new InvalidArgumentException('You can only provide one post-aggregation, field access or constant as input field');
124
        }
125
126 1
        $this->postAggregations[] = new QuantilePostAggregator(
127 1
            $fields[0],
0 ignored issues
show
Bug introduced by
It seems like $fields[0] can also be of type null; however, parameter $dimension of Level23\Druid\PostAggreg...gregator::__construct() does only seem to accept Level23\Druid\PostAggreg...PostAggregatorInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

127
            /** @scrutinizer ignore-type */ $fields[0],
Loading history...
128
            $as,
129
            $fraction
130
        );
131
132 1
        return $this;
133
    }
134
135
    /**
136
     * This returns an approximation to the value that would be preceded by a given fraction of a hypothetical sorted
137
     * version of the input stream. This returns an array of quantiles corresponding to a given array of fractions.
138
     *
139
     * To use this aggregator, make sure you include the extension in your druid server config file:
140
     *
141
     * druid.extensions.loadList=["druid-datasketches"]
142
     *
143
     * @param string         $as             The name which will be used in the output
144
     * @param string|Closure $fieldOrClosure Field which will be used that refers to a DoublesSketch  (fieldAccess or
145
     *                                       another post aggregator). When a string is given, we assume that it refers
146
     *                                       to another field in the query. If you give a closure, it will receive an
147
     *                                       instance of the PostAggregationsBuilder. With this builder you can build
148
     *                                       another post-aggregation or use constants as input for this method.
149
     * @param float[]        $fractions      Array of Fractional positions in the hypothetical sorted stream, number
150
     *                                       from 0 to 1 inclusive
151
     *
152
     * @return $this
153
     */
154 1
    public function quantiles(string $as, $fieldOrClosure, array $fractions): self
155
    {
156 1
        $fields = $this->buildFields([$fieldOrClosure]);
157 1
        if ($fields->count() != 1) {
158 1
            throw new InvalidArgumentException('You can only provide one post-aggregation, field access or constant as input field');
159
        }
160
161 1
        $this->postAggregations[] = new QuantilesPostAggregator(
162 1
            $fields[0],
0 ignored issues
show
Bug introduced by
It seems like $fields[0] can also be of type null; however, parameter $dimension of Level23\Druid\PostAggreg...gregator::__construct() does only seem to accept Level23\Druid\PostAggreg...PostAggregatorInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

162
            /** @scrutinizer ignore-type */ $fields[0],
Loading history...
163
            $as,
164
            $fractions
165
        );
166
167 1
        return $this;
168
    }
169
170
    /**
171
     * This returns an approximation to the histogram given an array of split points that define the histogram bins or
172
     * a number of bins (not both). An array of m unique, monotonically increasing split points divide the real number
173
     * line into m+1 consecutive disjoint intervals. The definition of an interval is inclusive of the left split point
174
     * and exclusive of the right split point. If the number of bins is specified instead of split points, the interval
175
     * between the minimum and maximum values is divided into the given number of equally-spaced bins.
176
     *
177
     * To use this aggregator, make sure you include the extension in your druid server config file:
178
     *
179
     * druid.extensions.loadList=["druid-datasketches"]
180
     *
181
     * @param string                $as             The name which will be used in the output
182
     * @param string|Closure        $fieldOrClosure Field which will be used that refers to a DoublesSketch
183
     *                                              (fieldAccess or another post aggregator). When a string is given,
184
     *                                              we assume that it refers to another field in the query. If you give
185
     *                                              a closure, it will receive an instance of the
186
     *                                              PostAggregationsBuilder. With this builder you can build another
187
     *                                              post-aggregation or use constants as input for this method.
188
     * @param array<int|float>|null $splitPoints    Array of split points (optional)
189
     * @param int|null              $numBins        Number of bins (optional, defaults to 10)
190
     *
191
     * @return $this
192
     */
193 3
    public function histogram(string $as, $fieldOrClosure, ?array $splitPoints = null, ?int $numBins = null): self
194
    {
195 3
        $fields = $this->buildFields([$fieldOrClosure]);
196 3
        if ($fields->count() != 1) {
197 3
            throw new InvalidArgumentException('You can only provide one post-aggregation, field access or constant as input field');
198
        }
199
200 3
        $this->postAggregations[] = new HistogramPostAggregator(
201 3
            $fields[0],
0 ignored issues
show
Bug introduced by
It seems like $fields[0] can also be of type null; however, parameter $dimension of Level23\Druid\PostAggreg...gregator::__construct() does only seem to accept Level23\Druid\PostAggreg...PostAggregatorInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

201
            /** @scrutinizer ignore-type */ $fields[0],
Loading history...
202
            $as,
203
            $splitPoints,
204
            $numBins
205
        );
206
207 3
        return $this;
208
    }
209
210
    /**
211
     * This returns an approximation to the rank of a given value that is the fraction of the distribution less than
212
     * that value.
213
     *
214
     * To use this aggregator, make sure you include the extension in your druid server config file:
215
     *
216
     * druid.extensions.loadList=["druid-datasketches"]
217
     *
218
     * @param string         $as             The name which will be used in the output
219
     * @param string|Closure $fieldOrClosure Field which will be used that refers to a DoublesSketch  (fieldAccess or
220
     *                                       another post aggregator). When a string is given, we assume that it refers
221
     *                                       to another field in the query. If you give a closure, it will receive an
222
     *                                       instance of the PostAggregationsBuilder. With this builder you can build
223
     *                                       another post-aggregation or use constants as input for this method.
224
     * @param float|int      $value          This returns an approximation to the rank of a given value that is the
225
     *                                       fraction of the distribution less than that value.
226
     *
227
     * @return $this
228
     */
229 1
    public function rank(string $as, $fieldOrClosure, $value): self
230
    {
231 1
        $fields = $this->buildFields([$fieldOrClosure]);
232 1
        if ($fields->count() != 1) {
233 1
            throw new InvalidArgumentException('You can only provide one post-aggregation, field access or constant as input field');
234
        }
235
236 1
        $this->postAggregations[] = new RankPostAggregator(
237 1
            $fields[0],
0 ignored issues
show
Bug introduced by
It seems like $fields[0] can also be of type null; however, parameter $dimension of Level23\Druid\PostAggreg...gregator::__construct() does only seem to accept Level23\Druid\PostAggreg...PostAggregatorInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

237
            /** @scrutinizer ignore-type */ $fields[0],
Loading history...
238
            $as,
239
            $value
240
        );
241
242 1
        return $this;
243
    }
244
245
    /**
246
     * This returns an approximation to the Cumulative Distribution Function given an array of split points that define
247
     * the edges of the bins. An array of m unique, monotonically increasing split points divide the real number line
248
     * into m+1 consecutive disjoint intervals. The definition of an interval is inclusive of the left split point and
249
     * exclusive of the right split point. The resulting array of fractions can be viewed as ranks of each split point
250
     * with one additional rank that is always 1.
251
     *
252
     * To use this aggregator, make sure you include the extension in your druid server config file:
253
     *
254
     * druid.extensions.loadList=["druid-datasketches"]
255
     *
256
     * @param string         $as             The name which will be used in the output
257
     * @param string|Closure $fieldOrClosure Field which will be used that refers to a DoublesSketch  (fieldAccess or
258
     *                                       another post aggregator). When a string is given, we assume that it refers
259
     *                                       to another field in the query. If you give a closure, it will receive an
260
     *                                       instance of the PostAggregationsBuilder. With this builder you can build
261
     *                                       another post-aggregation or use constants as input for this method.
262
     * @param array          $splitPoints    Array of split points
263
     *
264
     * @return $this
265
     */
266 1
    public function cdf(string $as, $fieldOrClosure, array $splitPoints): self
267
    {
268 1
        $fields = $this->buildFields([$fieldOrClosure]);
269 1
        if ($fields->count() != 1) {
270 1
            throw new InvalidArgumentException('You can only provide one post-aggregation, field access or constant as input field');
271
        }
272
273 1
        $this->postAggregations[] = new CdfPostAggregator(
274 1
            $fields[0],
0 ignored issues
show
Bug introduced by
It seems like $fields[0] can also be of type null; however, parameter $dimension of Level23\Druid\PostAggreg...gregator::__construct() does only seem to accept Level23\Druid\PostAggreg...PostAggregatorInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

274
            /** @scrutinizer ignore-type */ $fields[0],
Loading history...
275
            $as,
276
            $splitPoints
277
        );
278
279 1
        return $this;
280
    }
281
282
    /**
283
     * This returns a summary of the sketch that can be used for debugging. This is the result of calling toString()
284
     * method.
285
     *
286
     * To use this aggregator, make sure you include the extension in your druid server config file:
287
     *
288
     * druid.extensions.loadList=["druid-datasketches"]
289
     *
290
     * @param string         $as             The name which will be used in the output
291
     * @param string|Closure $fieldOrClosure Field which will be used that refers to a DoublesSketch  (fieldAccess or
292
     *                                       another post aggregator). When a string is given, we assume that it refers
293
     *                                       to another field in the query. If you give a closure, it will receive an
294
     *                                       instance of the PostAggregationsBuilder. With this builder you can build
295
     *                                       another post-aggregation or use constants as input for this method.
296
     *
297
     * @return $this
298
     */
299 1
    public function sketchSummary(string $as, $fieldOrClosure): self
300
    {
301 1
        $fields = $this->buildFields([$fieldOrClosure]);
302 1
        if ($fields->count() != 1) {
303 1
            throw new InvalidArgumentException('You can only provide one post-aggregation, field access or constant as input field');
304
        }
305
306 1
        $this->postAggregations[] = new SketchSummaryPostAggregator($fields[0], $as);
0 ignored issues
show
Bug introduced by
It seems like $fields[0] can also be of type null; however, parameter $dimension of Level23\Druid\PostAggreg...gregator::__construct() does only seem to accept Level23\Druid\PostAggreg...PostAggregatorInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

306
        $this->postAggregations[] = new SketchSummaryPostAggregator(/** @scrutinizer ignore-type */ $fields[0], $as);
Loading history...
307
308 1
        return $this;
309
    }
310
311
    /**
312
     * Multiply two or more fields with each other.
313
     *
314
     * @param string               $as                The name which will be used in the output
315
     * @param string|Closure|array ...$fieldOrClosure One or more fields which will be used. When a string is given,
316
     *                                                we assume that it refers to another field in the query. If you
317
     *                                                give a closure, it will receive an instance of the
318
     *                                                PostAggregationsBuilder. With this builder you can build other
319
     *                                                post-aggregations or use constants as input for this method.
320
     *
321
     * @return $this
322
     */
323 1
    public function multiply(string $as, ...$fieldOrClosure)
324
    {
325 1
        $this->postAggregations[] = new ArithmeticPostAggregator(
326
            $as,
327
            '*',
328 1
            $this->buildFields($fieldOrClosure),
329
            true
330
        );
331
332 1
        return $this;
333
    }
334
335
    /**
336
     * Subtract two or more fields with each other.
337
     *
338
     * @param string               $as                The name which will be used in the output
339
     * @param string|Closure|array ...$fieldOrClosure One or more fields which will be used. When a string is given,
340
     *                                                we assume that it refers to another field in the query. If you
341
     *                                                give a closure, it will receive an instance of the
342
     *                                                PostAggregationsBuilder. With this builder you can build other
343
     *                                                post-aggregations or use constants as input for this method.
344
     *
345
     * @return $this
346
     */
347 1
    public function subtract(string $as, ...$fieldOrClosure)
348
    {
349
350 1
        $this->postAggregations[] = new ArithmeticPostAggregator(
351
            $as,
352
            '-',
353 1
            $this->buildFields($fieldOrClosure),
354
            true
355
        );
356
357 1
        return $this;
358
    }
359
360
    /**
361
     * Add two or more fields with each other.
362
     *
363
     * @param string               $as                The name which will be used in the output
364
     * @param string|Closure|array ...$fieldOrClosure One or more fields which will be used. When a string is given,
365
     *                                                we assume that it refers to another field in the query. If you
366
     *                                                give a closure, it will receive an instance of the
367
     *                                                PostAggregationsBuilder. With this builder you can build other
368
     *                                                post-aggregations or use constants as input for this method.
369
     *
370
     * @return $this
371
     */
372 1
    public function add(string $as, ...$fieldOrClosure)
373
    {
374 1
        $this->postAggregations[] = new ArithmeticPostAggregator(
375
            $as,
376
            '+',
377 1
            $this->buildFields($fieldOrClosure),
378
            true
379
        );
380
381 1
        return $this;
382
    }
383
384
    /**
385
     * Return the quotient of two or more fields.
386
     *
387
     * @param string               $as                The name which will be used in the output
388
     * @param string|Closure|array ...$fieldOrClosure One or more fields which will be used. When a string is given,
389
     *                                                we assume that it refers to another field in the query. If you
390
     *                                                give a closure, it will receive an instance of the
391
     *                                                PostAggregationsBuilder. With this builder you can build other
392
     *                                                post-aggregations or use constants as input for this method.
393
     *
394
     * @return $this
395
     */
396 1
    public function quotient(string $as, ...$fieldOrClosure)
397
    {
398 1
        $this->postAggregations[] = new ArithmeticPostAggregator(
399
            $as,
400
            'quotient',
401 1
            $this->buildFields($fieldOrClosure),
402
            true
403
        );
404
405 1
        return $this;
406
    }
407
408
    /**
409
     * Field accessor post-aggregators
410
     *
411
     * These post-aggregators return the value produced by the specified aggregator.
412
     *
413
     * $aggregatorOutputName refers to the output name of the aggregator given in the aggregations portion of the
414
     * query. For complex aggregators, like "cardinality" and "hyperUnique", the type of the post-aggregator determines
415
     * what the post-aggregator will return.
416
     * Set $finalizing to `false`  to return the raw aggregation object, or use `true`
417
     * to return a finalized value, such as an estimated cardinality.
418
     *
419
     * @param string $aggregatorOutputName This refers to the output name of the aggregator given in the aggregations
420
     *                                     portion of the query
421
     * @param string $as                   The output name as how we can access it
422
     * @param bool   $finalizing           Set this to true if you want to return a finalized value, such as an
423
     *                                     estimated cardinality.
424
     *
425
     * @return $this
426
     */
427 11
    public function fieldAccess(string $aggregatorOutputName, string $as = '', bool $finalizing = false)
428
    {
429 11
        $this->postAggregations[] = new FieldAccessPostAggregator(
430
            $aggregatorOutputName,
431 11
            ($as ?: $aggregatorOutputName),
432
            $finalizing
433
        );
434
435 11
        return $this;
436
    }
437
438
    /**
439
     * The constant post-aggregator always returns the specified value.
440
     *
441
     * @param int|float $numericValue This will be our static value
442
     * @param string    $as           The output name as how we can access it
443
     *
444
     * @return $this
445
     */
446 11
    public function constant($numericValue, string $as)
447
    {
448 11
        $this->postAggregations[] = new ConstantPostAggregator($as, $numericValue);
449
450 11
        return $this;
451
    }
452
453
    /**
454
     * Return the highest value of multiple columns in one row.
455
     *
456
     * @param string               $as                The name which will be used in the output
457
     * @param string|Closure|array ...$fieldOrClosure One or more fields which will be used. When a string is given,
458
     *                                                we assume that it refers to another field in the query. If you
459
     *                                                give a closure, it will receive an instance of the
460
     *                                                PostAggregationsBuilder. With this builder you can build other
461
     *                                                post-aggregations or use constants as input for this method.
462
     *
463
     * @return $this
464
     */
465 1
    public function longGreatest(string $as, ...$fieldOrClosure)
466
    {
467 1
        $this->postAggregations[] = new GreatestPostAggregator(
468
            $as,
469 1
            $this->buildFields($fieldOrClosure),
470
            DataType::LONG
471
        );
472
473 1
        return $this;
474
    }
475
476
    /**
477
     * Return the highest value of multiple columns in one row.
478
     *
479
     * @param string               $as                The name which will be used in the output
480
     * @param string|Closure|array ...$fieldOrClosure One or more fields which will be used. When a string is given,
481
     *                                                we assume that it refers to another field in the query. If you
482
     *                                                give a closure, it will receive an instance of the
483
     *                                                PostAggregationsBuilder. With this builder you can build other
484
     *                                                post-aggregations or use constants as input for this method.
485
     *
486
     * @return $this
487
     */
488 1
    public function doubleGreatest(string $as, ...$fieldOrClosure)
489
    {
490 1
        $this->postAggregations[] = new GreatestPostAggregator(
491
            $as,
492 1
            $this->buildFields($fieldOrClosure),
493
            DataType::DOUBLE
494
        );
495
496 1
        return $this;
497
    }
498
499
    /**
500
     * Return the lowest value of multiple columns in one row.
501
     *
502
     * @param string               $as                The name which will be used in the output
503
     * @param string|Closure|array ...$fieldOrClosure One or more fields which will be used. When a string is given,
504
     *                                                we assume that it refers to another field in the query. If you
505
     *                                                give a closure, it will receive an instance of the
506
     *                                                PostAggregationsBuilder. With this builder you can build other
507
     *                                                post-aggregations or use constants as input for this method.
508
     *
509
     * @return $this
510
     */
511 1
    public function longLeast(string $as, ...$fieldOrClosure)
512
    {
513 1
        $this->postAggregations[] = new LeastPostAggregator(
514
            $as,
515 1
            $this->buildFields($fieldOrClosure),
516
            DataType::LONG
517
        );
518
519 1
        return $this;
520
    }
521
522
    /**
523
     * Return the lowest value of multiple columns in one row.
524
     *
525
     * @param string               $as                The name which will be used in the output
526
     * @param string|Closure|array ...$fieldOrClosure One or more fields which will be used. When a string is given,
527
     *                                                we assume that it refers to another field in the query. If you
528
     *                                                give a closure, it will receive an instance of the
529
     *                                                PostAggregationsBuilder. With this builder you can build other
530
     *                                                post-aggregations or use constants as input for this method.
531
     *
532
     * @return $this
533
     */
534 1
    public function doubleLeast(string $as, ...$fieldOrClosure)
535
    {
536 1
        $this->postAggregations[] = new LeastPostAggregator(
537
            $as,
538 1
            $this->buildFields($fieldOrClosure),
539
            DataType::DOUBLE
540
        );
541
542 1
        return $this;
543
    }
544
545
    /**
546
     * This Post Aggregation function applies the provided JavaScript function to the given fields. Fields are passed
547
     * as arguments to the JavaScript function in the given order.
548
     *
549
     * NOTE: JavaScript-based functionality is disabled by default. Please refer to the Druid JavaScript programming
550
     * guide for guidelines about using Druid's JavaScript functionality, including instructions on how to enable it.
551
     *
552
     * @param string               $as                The output name
553
     * @param string               $function          The javascript function which should be applied.
554
     * @param string|Closure|array ...$fieldOrClosure One or more fields which will be used. When a string is given,
555
     *                                                we assume that it refers to another field in the query. If you
556
     *                                                give a closure, it will receive an instance of the
557
     *                                                PostAggregationsBuilder. With this builder you can build other
558
     *                                                post-aggregations or use constants as input for this method.
559
     *
560
     * @return $this
561
     * @see https://druid.apache.org/docs/latest/querying/post-aggregations.html#javascript-post-aggregator
562
     */
563 1
    public function postJavascript(string $as, string $function, ...$fieldOrClosure)
564
    {
565 1
        $this->postAggregations[] = new JavaScriptPostAggregator(
566
            $as,
567 1
            $this->buildFields($fieldOrClosure),
568
            $function
569
        );
570
571 1
        return $this;
572
    }
573
574
    /**
575
     * The hyperUniqueCardinality post aggregator is used to wrap a hyperUnique object such that it can be used in post
576
     * aggregations.
577
     *
578
     * This post-aggregator will inherit the rounding behavior of the aggregator it references. Note that this
579
     * inheritance is only effective if you directly reference an aggregator. Going through another post-aggregator,
580
     * for example, will cause the user-specified rounding behavior to get lost and default to "no rounding".
581
     *
582
     * @see https://druid.apache.org/docs/latest/querying/post-aggregations.html#hyperunique-cardinality-post-aggregator
583
     *
584
     * @param string      $hyperUniqueField The name field value of the hyperUnique aggregator
585
     * @param string|null $as               The output name
586
     *
587
     * @return $this
588
     */
589 1
    public function hyperUniqueCardinality(string $hyperUniqueField, string $as = null)
590
    {
591 1
        $this->postAggregations[] = new HyperUniqueCardinalityPostAggregator($hyperUniqueField, $as);
592
593 1
        return $this;
594
    }
595
596
    /**
597
     * @return array|\Level23\Druid\PostAggregations\PostAggregatorInterface[]
598
     */
599 10
    public function getPostAggregations(): array
600
    {
601 10
        return $this->postAggregations;
602
    }
603
}