Extended support by implementing TDIGEST.QUANTILE command (#1101)

* add support for CF.ADDNX

* fix key name

* fix wrong command

* Pulling changes

* Support TDIGEST.QUANTILE

* fix returned value

* fix test

* fix linter error

* add not exsist key test

* fixes after review

* fix linter problem
This commit is contained in:
shacharPash
2023-02-07 13:37:44 +02:00
committed by GitHub
parent 699d023280
commit 23356ceb8e
4 changed files with 123 additions and 0 deletions
+1
View File
@@ -180,6 +180,7 @@ use Predis\Command\CommandInterface;
* @method $this tdigestcreate(string $key, int $compression = 0)
* @method $this tdigestinfo(string $key)
* @method $this tdigestmax(string $key)
* @method $this tdigestquantile(string $key, float ...$quantile)
* @method $this tdigestmin(string $key)
* @method $this tdigestrank(string $key, ...$value)
* @method $this tdigestreset(string $key)
+1
View File
@@ -190,6 +190,7 @@ use Predis\Response\Status;
* @method Status tdigestcreate(string $key, int $compression = 0)
* @method array tdigestinfo(string $key)
* @method string tdigestmax(string $key)
* @method string[] tdigestquantile(string $key, float ...$quantile)
* @method string tdigestmin(string $key)
* @method array tdigestrank(string $key, float ...$value)
* @method Status tdigestreset(string $key)
@@ -0,0 +1,28 @@
<?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.quantile/
*
* Returns, for each input fraction, an estimation of the value (floating point) that is smaller than the given fraction of observations.
*/
class TDIGESTQUANTILE extends RedisCommand
{
public function getId()
{
return 'TDIGEST.QUANTILE';
}
}
@@ -0,0 +1,93 @@
<?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 TDIGESTQUANTILE_Test extends PredisCommandTestCase
{
/**
* {@inheritDoc}
*/
protected function getExpectedCommand(): string
{
return TDIGESTQUANTILE::class;
}
/**
* {@inheritDoc}
*/
protected function getExpectedId(): string
{
return 'TDIGESTQUANTILE';
}
/**
* @group disconnected
*/
public function testFilterArguments(): void
{
$actualArguments = ['key', 1, 2, 3];
$expectedArguments = ['key', 1, 2, 3];
$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
* @return void
* @requiresRedisBfVersion >= 2.4.0
*/
public function testReturnsValuesBelowGivenQuantile(): void
{
$redis = $this->getClient();
$redis->tdigestcreate('key', 1000);
$addResponse = $redis->tdigestadd('key', 1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5);
$quantileResponse = $redis->tdigestquantile('key', 0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0);
$this->assertEquals('OK', $addResponse);
$this->assertSame(['1', '2', '3', '3', '4', '4', '4', '5', '5', '5', '5'], $quantileResponse);
$redis->tdigestcreate('empty_key');
$this->assertSame(['nan', 'nan'], $redis->tdigestquantile('empty_key', 0.0, 0.1));
}
/**
* @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->tdigestquantile('key', 1, 2, 3);
}
}