Copilot commented on code in PR #3163:
URL: https://github.com/apache/kvrocks/pull/3163#discussion_r2332163165


##########
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, &centroids); 
!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:
   The CDF calculation is incorrect. It's counting the number of centroids 
rather than their weights. It should accumulate `current_centroid->weight` 
instead of incrementing by 1 for proper cumulative distribution calculation.
   ```suggestion
       double eq_weight = 0;
       double smaller_weight = 0;
       for (; iter_begin->Valid(); iter_begin->Next()) {
         auto current_centroid = iter_begin->GetCentroid();
         if (val > current_centroid->mean) {
           smaller_weight += current_centroid->weight;
         } else if (val == current_centroid->mean) {
           eq_weight += current_centroid->weight;
         }
       }
       double cdf_val = (smaller_weight / total_weight) + ((eq_weight / 2) / 
total_weight);
   ```



##########
src/commands/cmd_tdigest.cc:
##########
@@ -371,5 +415,6 @@ REDIS_REGISTER_COMMANDS(TDigest, 
MakeCmdAttr<CommandTDigestCreate>("tdigest.crea
                         MakeCmdAttr<CommandTDigestMin>("tdigest.min", 2, 
"read-only", 1, 1, 1),
                         
MakeCmdAttr<CommandTDigestQuantile>("tdigest.quantile", -3, "read-only", 1, 1, 
1),
                         MakeCmdAttr<CommandTDigestReset>("tdigest.reset", 2, 
"write", 1, 1, 1),
-                        MakeCmdAttr<CommandTDigestMerge>("tdigest.merge", -4, 
"write", GetMergeKeyRange));
+                        MakeCmdAttr<CommandTDigestMerge>("tdigest.merge", -4, 
"write", GetMergeKeyRange),
+                        MakeCmdAttr<CommandTDigestCDF>("tdigest.cdf", -4, 
"write", 1, 1, 1));

Review Comment:
   The TDIGEST.CDF command should be marked as 'read-only' instead of 'write' 
since it only reads data and doesn't modify the T-Digest structure.
   ```suggestion
                           MakeCmdAttr<CommandTDigestCDF>("tdigest.cdf", -4, 
"read-only", 1, 1, 1));
   ```



##########
src/commands/cmd_tdigest.cc:
##########
@@ -358,7 +358,51 @@ 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 {
+    key_name_ = args[1];
+    if (args.size() == 2) return {Status::RedisParseErr, 
errWrongNumOfArguments};

Review Comment:
   The command registration specifies minimum 4 arguments (-4), but this 
validation only checks for exactly 2 arguments. It should validate that there 
are at least 3 arguments (command + key + at least one value).
   ```suggestion
       if (args.size() < 3) return {Status::RedisParseErr, 
errWrongNumOfArguments};
   ```



-- 
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]

Reply via email to