Skip to content

Commit 8d5d3b2

Browse files
committed
Improve Aggregate implementation
1 parent 78f8960 commit 8d5d3b2

File tree

16 files changed

+257
-32
lines changed

16 files changed

+257
-32
lines changed

.php_cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ return PhpCsFixer\Config::create()
143143
// Replaces superfluous `elseif` with `if`.
144144
'no_superfluous_elseif' => true,
145145
// Removes `@param`, `@return` and `@var` tags that don't provide any useful information.
146-
'no_superfluous_phpdoc_tags' => true,
146+
'no_superfluous_phpdoc_tags' => ['allow_mixed'=>true,'remove_inheritdoc'=>true],
147147
// Remove trailing commas in list function calls.
148148
'no_trailing_comma_in_list_call' => true,
149149
// PHP single-line arrays should not have trailing comma.

CHANGELOG.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,27 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
### Added
10+
11+
- Shorthand for Reducer functions
12+
- Add **Deprecated** section in `CHANGELOG.md`
13+
14+
### Changed
15+
16+
- Allow `GROUPBY` with no properties
17+
- Allow `GROUPBY` with no reducers
18+
- (dev) Replace `php-parallel-lint` lib.
19+
20+
### Fixed
21+
22+
- Fix namespace of tests
23+
- Aggregation result can now be an array
24+
25+
### Deprecated
26+
27+
- `MacFJA\RediSearch\Aggregate\Exception\NotEnoughPropertiesException`
28+
- `MacFJA\RediSearch\Aggregate\Exception\NotEnoughReducersException`
29+
930
## [1.2.0]
1031

1132
### Fixed
@@ -45,6 +66,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
4566
- Rename namespace from `MacFJA\RedisSearch` to `MacFJA\RediSearch`(Depreciation, keeping BC)
4667
- Uniformize Builder interface (Deprecation, keeping BC)
4768

69+
### Deprecated
70+
71+
- The whole namespace `MacFJA\RedisSearch` (replaced by `MacFJA\RediSearch`)
72+
- `MacFJA\RediSearch\Aggregate::aggregate`
73+
- `MacFJA\RediSearch\Index::addFromArray`
74+
- `MacFJA\RediSearch\Index\Builder::create`
75+
- `MacFJA\RediSearch\Search::search`
76+
4877
## [1.0.0] - 2020-12-05
4978

5079
First version

Makefile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.PHONY: analyze fix-code test
1+
.PHONY: analyze fix-code test coverage
22

33
analyze: | vendor
44
$(COMPOSER) install --optimize-autoloader --no-suggest --prefer-dist
@@ -20,7 +20,10 @@ fix-code: | vendor
2020
@#$(COMPOSER) exec -v psalm -- --alter --issues=all src
2121

2222
test: | vendor
23-
$(COMPOSER) exec -v phpunit -- --whitelist=src tests
23+
$(COMPOSER) exec -v phpunit
24+
25+
coverage: | vendor
26+
$(COMPOSER) exec -v phpunit -- --coverage-text --color
2427

2528
vendor:
2629
ifneq (prod,${BUILD_MODE})

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
"ergebnis/composer-normalize": "^2.13",
2626
"friendsofphp/php-cs-fixer": "^2.16",
2727
"insolita/unused-scanner": "^2.2",
28-
"jakub-onderka/php-parallel-lint": "^1.0",
2928
"phan/phan": "^3.2",
29+
"php-parallel-lint/php-parallel-lint": "^1.2",
3030
"phpmd/phpmd": "^2.9",
3131
"phpstan/phpstan": "^0.12.57",
3232
"phpunit/phpunit": "^9.5",

src/Aggregate/Exception/NotEnoughPropertiesException.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use OutOfRangeException;
2525
use Throwable;
2626

27+
/** @deprecated This exception is never used. */
2728
class NotEnoughPropertiesException extends OutOfRangeException
2829
{
2930
public function __construct(string $message = 'You must have at least one property', int $code = 0, ?Throwable $previous = null)

src/Aggregate/Exception/NotEnoughReducersException.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use OutOfRangeException;
2525
use Throwable;
2626

27+
/** @deprecated This exception is never used. */
2728
class NotEnoughReducersException extends OutOfRangeException
2829
{
2930
public function __construct(string $message = 'You must have at least one reducer', int $code = 0, ?Throwable $previous = null)

src/Aggregate/GroupBy.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@
2121

2222
namespace MacFJA\RediSearch\Aggregate;
2323

24-
use function count;
25-
use MacFJA\RediSearch\Aggregate\Exception\NotEnoughPropertiesException;
26-
use MacFJA\RediSearch\Aggregate\Exception\NotEnoughReducersException;
24+
use function array_map;
2725
use MacFJA\RediSearch\Helper\DataHelper;
2826
use MacFJA\RediSearch\Helper\RedisHelper;
2927
use MacFJA\RediSearch\PartialQuery;
@@ -45,8 +43,6 @@ class GroupBy implements PartialQuery
4543
*/
4644
public function __construct(array $properties, array $reducers)
4745
{
48-
DataHelper::assert(count($properties) > 0, NotEnoughPropertiesException::class);
49-
DataHelper::assert(count($reducers) > 0, NotEnoughReducersException::class);
5046
DataHelper::assertArrayOf($properties, 'string');
5147
DataHelper::assertArrayOf($reducers, Reducer::class);
5248

@@ -56,7 +52,10 @@ public function __construct(array $properties, array $reducers)
5652

5753
public function getQueryParts(): array
5854
{
59-
$query = RedisHelper::buildQueryList([], ['GROUPBY' => $this->properties]);
55+
$atProperties = array_map(static function (string $property) {
56+
return '@'.$property;
57+
}, $this->properties);
58+
$query = RedisHelper::buildQueryList([], ['GROUPBY' => $atProperties], true);
6059

6160
return RedisHelper::buildQueryPartial($query, $this->reducers);
6261
}

src/Aggregate/Reducer.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,15 @@
2222
namespace MacFJA\RediSearch\Aggregate;
2323

2424
use function count;
25+
use function is_bool;
2526
use function is_string;
2627
use MacFJA\RediSearch\Helper\DataHelper;
2728
use MacFJA\RediSearch\PartialQuery;
2829
use Throwable;
2930

31+
/**
32+
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
33+
*/
3034
class Reducer implements PartialQuery
3135
{
3236
/** @var string */
@@ -64,4 +68,73 @@ public function getQueryParts(): array
6468

6569
return $query;
6670
}
71+
72+
public static function count(?string $name = null): self
73+
{
74+
return new self('COUNT', [], $name);
75+
}
76+
77+
public static function countDistinct(string $property, ?string $name = null): self
78+
{
79+
return new self('COUNT_DISTINCT', ['@'.$property], $name);
80+
}
81+
82+
public static function countDistinctish(string $property, ?string $name = null): self
83+
{
84+
return new self('COUNT_DISTINCTISH', ['@'.$property], $name);
85+
}
86+
87+
public static function sum(string $property, ?string $name = null): self
88+
{
89+
return new self('SUM', ['@'.$property], $name);
90+
}
91+
92+
public static function minimum(string $property, ?string $name = null): self
93+
{
94+
return new self('MIN', ['@'.$property], $name);
95+
}
96+
97+
public static function maximum(string $property, ?string $name = null): self
98+
{
99+
return new self('MAX', ['@'.$property], $name);
100+
}
101+
102+
public static function average(string $property, ?string $name = null): self
103+
{
104+
return new self('AVG', ['@'.$property], $name);
105+
}
106+
107+
public static function standardDeviation(string $property, ?string $name = null): self
108+
{
109+
return new self('STDDEV', ['@'.$property], $name);
110+
}
111+
112+
public static function quantile(string $property, float $quantile, ?string $name = null): self
113+
{
114+
return new self('QUANTILE', ['@'.$property, $quantile], $name);
115+
}
116+
117+
public static function median(string $property, ?string $name = null): self
118+
{
119+
return new self('QUANTILE', ['@'.$property, 0.5], $name);
120+
}
121+
122+
public static function toList(string $property, ?string $name = null): self
123+
{
124+
return new self('TOLIST', ['@'.$property], $name);
125+
}
126+
127+
public static function firstValue(string $property, ?string $sortByProperty = null, ?bool $ascending = null, ?string $name = null): self
128+
{
129+
$arguments = ['@'.$property];
130+
if (is_string($sortByProperty)) {
131+
$arguments[] = 'BY';
132+
$arguments[] = '@'.$sortByProperty;
133+
if (is_bool($ascending)) {
134+
$arguments[] = true === $ascending ? 'ASC' : 'DESC';
135+
}
136+
}
137+
138+
return new self('FIRST_VALUE', $arguments, $name);
139+
}
67140
}

src/Aggregate/Result.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,37 +21,35 @@
2121

2222
namespace MacFJA\RediSearch\Aggregate;
2323

24-
use MacFJA\RediSearch\Helper\DataHelper;
2524
use Throwable;
2625

2726
class Result
2827
{
29-
/** @var array<string,null|float|int|string> */
28+
/** @var array<string,null|array<mixed>|float|int|string> */
3029
private $fields = [];
3130

3231
/**
33-
* @param array<string,null|float|int|string> $fields
32+
* @param array<string,null|array<mixed>|float|int|string> $fields
3433
*
3534
* @throws Throwable
3635
*/
3736
public function __construct(array $fields)
3837
{
39-
DataHelper::assertArrayOf($fields, '?scalar');
4038
$this->fields = $fields;
4139
}
4240

4341
/**
44-
* @return array<string,null|float|int|string>
42+
* @return array<string,null|array<mixed>|float|int|string>
4543
*/
4644
public function getFields(): array
4745
{
4846
return $this->fields;
4947
}
5048

5149
/**
52-
* @param null|float|int|string $default
50+
* @param null|array<mixed>|float|int|string $default
5351
*
54-
* @return null|float|int|string
52+
* @return null|array<mixed>|float|int|string
5553
*/
5654
public function getValue(string $fieldName, $default = null)
5755
{

src/Aggregate/SortBy.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,6 @@ public function __construct(array $properties, ?int $max = null)
7171
$this->max = $max;
7272
}
7373

74-
/**
75-
* {@inheritdoc}
76-
*/
7774
public function getQueryParts(): array
7875
{
7976
$query = ['SORTBY', count($this->properties) * 2];

0 commit comments

Comments
 (0)