LindaSummer commented on code in PR #3163:
URL: https://github.com/apache/kvrocks/pull/3163#discussion_r2407046539
##########
tests/gocase/unit/type/tdigest/tdigest_test.go:
##########
@@ -518,4 +519,55 @@ func tdigestTests(t *testing.T, configs
util.KvrocksServerConfigs) {
validation(newDestKey1)
validation(newDestKey2)
})
+ t.Run("tdigest.cdf with different arguments", func(t *testing.T) {
+ keyPrefix := "tdigest_cdf_"
+
+ require.ErrorContains(t, rdb.Do(ctx, "TDIGEST.CDF").Err(),
errMsgWrongNumberArg)
+ require.ErrorContains(t, rdb.Do(ctx, "TDIGEST.CDF",
keyPrefix+"key1").Err(), errMsgWrongNumberArg)
+
+ // non-existent key
+ require.ErrorContains(t, rdb.Do(ctx, "TDIGEST.CDF",
keyPrefix+"nonexistent", "1.0").Err(), errMsgKeyNotExist)
+
+ // invalid float value
+ require.ErrorContains(t, rdb.Do(ctx, "TDIGEST.CDF",
keyPrefix+"key2", "invalid").Err(), errValueIsNotFloat)
+
+ // create a tdigest and add some data
+ tdigestKey := keyPrefix + "source"
+ require.NoError(t, rdb.Do(ctx, "TDIGEST.CREATE",
tdigestKey).Err())
+ require.NoError(t, rdb.Do(ctx, "TDIGEST.ADD", tdigestKey,
"1.0", "2.0", "3.0", "4.0", "5.0").Err())
Review Comment:
Hi @SharonIV0x86 ,
We'd better add some tests with duplicated values to create different
weights for some centroids.
##########
src/commands/cmd_tdigest.cc:
##########
@@ -358,7 +358,56 @@ class CommandTDigestMerge : public Commander {
std::vector<std::string> source_keys_;
TDigestMergeOptions options_;
};
+class CommandTDigestCDF : public Commander {
+ Status Parse(const std::vector<std::string> &args) override {
+ if (args.size() == 2) return {Status::RedisParseErr,
errWrongNumOfArguments};
+ key_name_ = args[1];
+ values_.reserve(args.size() - 2);
+ for (size_t i = 2; i < args.size(); i++) {
+ auto value = ParseFloat(args[i]);
+ if (!value) {
+ return {Status::RedisParseErr, errValueIsNotFloat};
+ }
+ values_.push_back(*value);
+ }
+ return Status::OK();
+ }
+ Status Execute(engine::Context &ctx, Server *srv, Connection *conn,
std::string *output) override {
+ TDigest tdigest(srv->storage, conn->GetNamespace());
+ std::vector<std::string> cdf_result;
+ TDigestCDFResult result;
+ TDigestMetadata metadata;
+ auto meta_status = tdigest.GetMetaData(ctx, key_name_, &metadata);
+ std::vector<std::string> nan_results(values_.size(), "nan");
Review Comment:
Hi @SharonIV0x86 ,
We could move this `nan_results` construction into the empty element branch.
##########
src/commands/cmd_tdigest.cc:
##########
@@ -358,7 +358,56 @@ class CommandTDigestMerge : public Commander {
std::vector<std::string> source_keys_;
TDigestMergeOptions options_;
};
+class CommandTDigestCDF : public Commander {
+ Status Parse(const std::vector<std::string> &args) override {
+ if (args.size() == 2) return {Status::RedisParseErr,
errWrongNumOfArguments};
+ key_name_ = args[1];
+ values_.reserve(args.size() - 2);
+ for (size_t i = 2; i < args.size(); i++) {
+ auto value = ParseFloat(args[i]);
+ if (!value) {
+ return {Status::RedisParseErr, errValueIsNotFloat};
+ }
+ values_.push_back(*value);
+ }
+ return Status::OK();
+ }
+ Status Execute(engine::Context &ctx, Server *srv, Connection *conn,
std::string *output) override {
+ TDigest tdigest(srv->storage, conn->GetNamespace());
+ std::vector<std::string> cdf_result;
+ TDigestCDFResult result;
+ TDigestMetadata metadata;
+ auto meta_status = tdigest.GetMetaData(ctx, key_name_, &metadata);
+ std::vector<std::string> nan_results(values_.size(), "nan");
Review Comment:
The `nan_results` could be constructed in the empty element branch.
##########
src/types/redis_tdigest.cc:
##########
@@ -414,6 +414,66 @@ rocksdb::Status TDigest::Merge(engine::Context& ctx, const
Slice& dest_digest,
return storage_->Write(ctx, storage_->DefaultWriteOptions(),
batch->GetWriteBatch());
}
+rocksdb::Status TDigest::CDF(engine::Context& ctx, const Slice& digest_name,
const std::vector<double>& inputs,
+ TDigestCDFResult* result) {
+ auto ns_key = AppendNamespacePrefix(digest_name);
+ TDigestMetadata metadata;
+ {
+ LockGuard guard(storage_->GetLockManager(), ns_key);
+
+ if (auto status = getMetaDataByNsKey(ctx, ns_key, &metadata);
!status.ok()) {
+ return status;
+ }
+
+ if (metadata.unmerged_nodes > 0) {
+ auto batch = storage_->GetWriteBatchBase();
+ WriteBatchLogData log_data(kRedisTDigest);
+ if (auto status = batch->PutLogData(log_data.Encode()); !status.ok()) {
+ return status;
+ }
+
+ if (auto status = mergeCurrentBuffer(ctx, ns_key, batch, &metadata);
!status.ok()) {
+ return status;
+ }
+
+ std::string metadata_bytes;
+ metadata.Encode(&metadata_bytes);
+ if (auto status = batch->Put(metadata_cf_handle_, ns_key,
metadata_bytes); !status.ok()) {
+ return status;
+ }
+
+ if (auto status = storage_->Write(ctx, storage_->DefaultWriteOptions(),
batch->GetWriteBatch()); !status.ok()) {
+ return status;
+ }
+ ctx.RefreshLatestSnapshot();
+ }
+ }
+ std::vector<Centroid> centroids;
+ if (auto status = dumpCentroids(ctx, ns_key, metadata, ¢roids);
!status.ok()) {
+ return status;
+ }
+ auto dump_centroids = DummyCentroids(metadata, centroids);
+ double total_weight = dump_centroids.TotalWeight();
+ std::vector<double> results;
+ for (double val : inputs) {
+ auto iter_begin = dump_centroids.Begin();
+ auto iter_end = dump_centroids.End();
+ double eq_count = 0;
+ double smaller_count = 0;
+ for (; iter_begin->Valid(); iter_begin->Next()) {
+ auto current_centroid = iter_begin->GetCentroid();
+ if (val > current_centroid->mean) {
+ smaller_count++;
+ } else if (val == current_centroid->mean) {
+ eq_count++;
+ }
+ }
+ double cdf_val = (smaller_count / total_weight) + ((eq_count / 2) /
total_weight);
Review Comment:
Hi @SharonIV0x86 ,
It seems that we mistake the count with weight here.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]