Added support for TDIGEST.TRIMMED_MEAN command (#1106)

Co-authored-by: Vladyslav Vildanov <vladyslavvildanov@Vladyslav-Vildanov-MacBook-Pro.local>
This commit is contained in:
Vladyslav Vildanov
2023-02-07 16:03:10 +02:00
committed by GitHub
parent 55e2f74447
commit 89dd528235
4 changed files with 174 additions and 0 deletions
+1
View File
@@ -185,6 +185,7 @@ use Predis\Command\CommandInterface;
* @method $this tdigestrank(string $key, ...$value)
* @method $this tdigestreset(string $key)
* @method $this tdigestrevrank(string $key, float ...$value)
* @method $this tdigesttrimmed_mean(string $key, float $lowCutQuantile, float $highCutQuantile)
* @method $this zadd($key, array $membersAndScoresDictionary)
* @method $this zcard($key)
* @method $this zcount($key, $min, $max)
+1
View File
@@ -195,6 +195,7 @@ use Predis\Response\Status;
* @method array tdigestrank(string $key, float ...$value)
* @method Status tdigestreset(string $key)
* @method array tdigestrevrank(string $key, float ...$value)
* @method string tdigesttrimmed_mean(string $key, float $lowCutQuantile, float $highCutQuantile)
* @method string xadd(string $key, array $dictionary, string $id = '*', array $options = null)
* @method int xdel(string $key, string ...$id)
* @method int xlen(string $key)
@@ -0,0 +1,29 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) 2009-2020 Daniele Alessandri
* (c) 2021-2023 Till Krüss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command\Redis\TDigest;
use Predis\Command\Command as RedisCommand;
/**
* @see https://redis.io/commands/tdigest.trimmed_mean/
*
* Returns an estimation of the mean value from the sketch,
* excluding observation values outside the low and high cutoff quantiles.
*/
class TDIGESTTRIMMED_MEAN extends RedisCommand
{
public function getId()
{
return 'TDIGEST.TRIMMED_MEAN';
}
}
@@ -0,0 +1,143 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) 2009-2020 Daniele Alessandri
* (c) 2021-2023 Till Krüss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command\Redis\TDigest;
use Predis\Command\Redis\PredisCommandTestCase;
use Predis\Response\ServerException;
class TDIGESTTRIMMED_MEAN_Test extends PredisCommandTestCase
{
/**
* {@inheritDoc}
*/
protected function getExpectedCommand(): string
{
return TDIGESTTRIMMED_MEAN::class;
}
/**
* {@inheritDoc}
*/
protected function getExpectedId(): string
{
return 'TDIGESTTRIMMED_MEAN';
}
/**
* @group disconnected
*/
public function testFilterArguments(): void
{
$actualArguments = ['key', 0.1, 0.2];
$expectedArguments = ['key', 0.1, 0.2];
$command = $this->getCommand();
$command->setArguments($actualArguments);
$this->assertSameValues($expectedArguments, $command->getArguments());
}
/**
* @group disconnected
*/
public function testParseResponse(): void
{
$this->assertSame(1, $this->getCommand()->parseResponse(1));
}
/**
* @group connected
* @dataProvider sketchesProvider
* @param array $addArguments
* @param string $key
* @param array $trimmedMeanArguments
* @param string $expectedResponse
* @return void
* @requiresRedisBfVersion >= 2.4.0
*/
public function testReturnsMeanValueWithinGivenQuantilesRange(
array $addArguments,
string $key,
array $trimmedMeanArguments,
string $expectedResponse
): void {
$redis = $this->getClient();
$redis->tdigestcreate($key);
$redis->tdigestadd(...$addArguments);
$actualResponse = $redis->tdigesttrimmed_mean(...$trimmedMeanArguments);
$this->assertSame($expectedResponse, $actualResponse);
}
/**
* @group connected
* @return void
* @requiresRedisBfVersion >= 2.4.0
*/
public function testReturnsNanOnEmptySketchKey(): void
{
$redis = $this->getClient();
$redis->tdigestcreate('key');
$actualResponse = $redis->tdigesttrimmed_mean('key', 0, 1);
$this->assertSame('nan', $actualResponse);
}
/**
* @group connected
* @return void
* @requiresRedisBfVersion >= 2.4.0
*/
public function testThrowsExceptionOnNonExistingTDigestSketch(): void
{
$redis = $this->getClient();
$this->expectException(ServerException::class);
$this->expectExceptionMessage('ERR T-Digest: key does not exist');
$redis->tdigesttrimmed_mean('key', 0, 1);
}
public function sketchesProvider(): array
{
return [
'between low and high cut' => [
['key', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
'key',
['key', 0.1, 0.6],
'4',
],
'without low cut' => [
['key', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
'key',
['key', 0, 0.6],
'3.5',
],
'without high cut' => [
['key', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
'key',
['key', 0.6, 1],
'8.5',
],
'without low and high cut' => [
['key', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
'key',
['key', 0, 1],
'5.5',
],
];
}
}