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

DoublesSketchAggregator   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 18
c 1
b 0
f 0
dl 0
loc 82
ccs 14
cts 14
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A toArray() 0 17 3
1
<?php
2
declare(strict_types=1);
3
4
namespace Level23\Druid\Aggregations;
5
6
class DoublesSketchAggregator implements AggregatorInterface
7
{
8
    /**
9
     * A String for the name of the input field (can contain sketches or raw numeric values).
10
     *
11
     * @var string
12
     */
13
    protected $metricName;
14
15
    /**
16
     * A String for the output (result) name of the calculation.
17
     *
18
     * @var string
19
     */
20
    protected $outputName;
21
22
    /**
23
     * Parameter that determines the accuracy and size of the sketch. Higher k means higher accuracy but more space to
24
     * store sketches. Must be a power of 2 from 2 to 32768. See accuracy information in the DataSketches documentation
25
     * for details.
26
     *
27
     * @var int|null
28
     */
29
    protected $sizeAndAccuracy;
30
31
    /**
32
     * This parameter is a temporary solution to avoid a known issue. It may be removed in a future release after the
33
     * bug is fixed. This parameter defines the maximum number of items to store in each sketch. If a sketch reaches
34
     * the limit, the query can throw IllegalStateException. To workaround this issue, increase the maximum stream
35
     * length. See accuracy information in the DataSketches documentation for how many bytes are required per stream
36
     * length.
37
     *
38
     * @var int|null
39
     */
40
    protected $maxStreamLength;
41
42
    /**
43
     * @param string   $metricName      A String for the name of the input field (can contain sketches or raw numeric
44
     *                                  values).
45
     * @param string   $outputName      A String for the output (result) name of the calculation.
46
     * @param int|null $sizeAndAccuracy Parameter that determines the accuracy and size of the sketch. Higher k means
47
     *                                  higher accuracy but more space to store sketches. Must be a power of 2 from 2
48
     *                                  to 32768. See accuracy information in the DataSketches documentation for
49
     *                                  details.
50
     * @param int|null $maxStreamLength This parameter is a temporary solution to avoid a known issue. It may be
51
     *                                  removed in a future release after the bug is fixed. This parameter defines the
52
     *                                  maximum number of items to store in each sketch. If a sketch reaches the limit,
53
     *                                  the query can throw IllegalStateException. To workaround this issue, increase
54
     *                                  the maximum stream length. See accuracy information in the DataSketches
55
     *                                  documentation for how many bytes are required per stream length.
56
     *
57
     * @see https://druid.apache.org/docs/latest/development/extensions-core/datasketches-quantiles.html
58
     */
59 4
    public function __construct(
60
        string $metricName,
61
        string $outputName = '',
62
        ?int $sizeAndAccuracy = null,
63
        ?int $maxStreamLength = null
64
    ) {
65 4
        $this->metricName      = $metricName;
66 4
        $this->outputName      = $outputName ?: $metricName;
67 4
        $this->sizeAndAccuracy = $sizeAndAccuracy;
68 4
        $this->maxStreamLength = $maxStreamLength;
69
    }
70
71 4
    public function toArray(): array
72
    {
73 4
        $result = [
74
            'type'      => 'quantilesDoublesSketch',
75 4
            'name'      => $this->outputName,
76 4
            'fieldName' => $this->metricName,
77
        ];
78
79 4
        if ($this->sizeAndAccuracy) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->sizeAndAccuracy of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
80 2
            $result['k'] = $this->sizeAndAccuracy;
81
        }
82
83 4
        if ($this->maxStreamLength) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->maxStreamLength of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
84 2
            $result['maxStreamLength'] = $this->maxStreamLength;
85
        }
86
87 4
        return $result;
88
    }
89
}