|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Level23\Druid\Filters; |
|
5
|
|
|
|
|
6
|
|
|
use Level23\Druid\Types\DataType; |
|
7
|
|
|
use Level23\Druid\Types\SortingOrder; |
|
8
|
|
|
use Level23\Druid\Types\BoundOperator; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class RangeFilter |
|
12
|
|
|
* |
|
13
|
|
|
* The range filter is a replacement for the bound filter. It compares against any type of column and is designed to |
|
14
|
|
|
* have has more SQL compliant behavior than the bound filter. It won't match null values, even if you |
|
15
|
|
|
* don't specify a lower bound. |
|
16
|
|
|
* |
|
17
|
|
|
* @package Level23\Druid\Filters |
|
18
|
|
|
*/ |
|
19
|
|
|
class RangeFilter implements FilterInterface |
|
20
|
|
|
{ |
|
21
|
|
|
protected string $column; |
|
22
|
|
|
|
|
23
|
|
|
protected BoundOperator $operator; |
|
24
|
|
|
|
|
25
|
|
|
protected string|int|float $value; |
|
26
|
|
|
|
|
27
|
|
|
protected SortingOrder $ordering; |
|
28
|
|
|
|
|
29
|
|
|
protected DataType $valueType; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* BoundFilter constructor. |
|
33
|
|
|
* |
|
34
|
|
|
* @param string $column Input column or virtual column name to filter. |
|
35
|
|
|
* |
|
36
|
|
|
* @param string|BoundOperator $operator The operator to use. Use ">", ">=", "<", or "<=" Or use the |
|
37
|
|
|
* BoundOperator constants. |
|
38
|
|
|
* @param string|int|float $value The value to compare with. This can either be a numeric or a |
|
39
|
|
|
* string. |
|
40
|
|
|
* @param DataType|null $valueType String specifying the type of bounds to match. The valueType |
|
41
|
|
|
* determines how Druid interprets the matchValue to assist in |
|
42
|
|
|
* converting to the type of the matched column and also defines |
|
43
|
|
|
* the type of comparison used when matching values. |
|
44
|
|
|
*/ |
|
45
|
13 |
|
public function __construct( |
|
46
|
|
|
string $column, |
|
47
|
|
|
string|BoundOperator $operator, |
|
48
|
|
|
string|int|float $value, |
|
49
|
|
|
DataType $valueType = null |
|
50
|
|
|
) { |
|
51
|
13 |
|
$this->column = $column; |
|
52
|
13 |
|
$this->operator = is_string($operator) ? BoundOperator::from($operator) : $operator; |
|
53
|
12 |
|
$this->value = $value; |
|
54
|
|
|
|
|
55
|
12 |
|
if (is_null($valueType)) { |
|
56
|
|
|
|
|
57
|
12 |
|
if (is_int($value)) { |
|
58
|
4 |
|
$valueType = DataType::LONG; |
|
59
|
8 |
|
} elseif (is_float($value)) { |
|
60
|
4 |
|
$valueType = DataType::DOUBLE; |
|
61
|
|
|
} else { |
|
62
|
4 |
|
$valueType = DataType::STRING; |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
12 |
|
$this->valueType = $valueType; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Return the filter as it can be used in the druid query. |
|
71
|
|
|
* |
|
72
|
|
|
* @return array<string,string|int|float|bool|array<string,string|int|bool|array<mixed>>> |
|
73
|
|
|
*/ |
|
74
|
12 |
|
public function toArray(): array |
|
75
|
|
|
{ |
|
76
|
12 |
|
$result = [ |
|
77
|
12 |
|
'type' => 'range', |
|
78
|
12 |
|
'column' => $this->column, |
|
79
|
12 |
|
'matchValueType' => $this->valueType->value, |
|
80
|
12 |
|
]; |
|
81
|
|
|
|
|
82
|
12 |
|
switch ($this->operator) { |
|
83
|
12 |
|
case BoundOperator::GE: |
|
84
|
3 |
|
$result['lower'] = $this->value; |
|
85
|
3 |
|
$result['lowerOpen'] = false; |
|
86
|
3 |
|
break; |
|
87
|
9 |
|
case BoundOperator::GT: |
|
88
|
3 |
|
$result['lower'] = $this->value; |
|
89
|
3 |
|
$result['lowerOpen'] = true; |
|
90
|
3 |
|
break; |
|
91
|
6 |
|
case BoundOperator::LE: |
|
92
|
3 |
|
$result['upper'] = $this->value; |
|
93
|
3 |
|
$result['upperOpen'] = false; |
|
94
|
3 |
|
break; |
|
95
|
3 |
|
case BoundOperator::LT: |
|
96
|
3 |
|
$result['upper'] = $this->value; |
|
97
|
3 |
|
$result['upperOpen'] = true; |
|
98
|
3 |
|
break; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
12 |
|
return $result; |
|
102
|
|
|
} |
|
103
|
|
|
} |