LindaSummer commented on code in PR #2803:
URL: https://github.com/apache/kvrocks/pull/2803#discussion_r1970742108


##########
src/commands/cmd_tdigest.cc:
##########
@@ -131,6 +131,54 @@ class CommandTDigestInfo : public Commander {
   std::string key_name_;
 };
 
+class CommandTDigestAdd : public Commander {
+ public:
+  Status Parse(const std::vector<std::string> &args) override {
+    if (args.size() < 3) {
+      return {Status::RedisParseErr, errWrongNumOfArguments};
+    }
+    key_name_ = args[1];
+
+    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());
+
+    auto s = tdigest.Add(ctx, key_name_, values_);
+    if (!s.ok()) {
+      if (s.IsNotFound()) {
+        return {Status::RedisExecErr, errKeyNotFound};
+      }
+      return {Status::RedisExecErr, s.ToString()};
+    }
+
+    *output = redis::RESP_OK;
+    return Status::OK();
+  }
+
+ private:
+  std::string key_name_;
+  std::vector<double> values_;
+
+  static StatusOr<double> parseFloat(const std::string &str) {
+    try {
+      return std::stod(str);
+    } catch (const std::exception &) {
+      return {Status::RedisParseErr, errValueIsNotFloat};
+    }
+  }
+};
+
 REDIS_REGISTER_COMMANDS(TDigest, 
MakeCmdAttr<CommandTDigestCreate>("tdigest.create", -2, "write", 1, 1, 1),
-                        MakeCmdAttr<CommandTDigestInfo>("tdigest.info", 2, 
"read-only", 1, 1, 1));
+                        MakeCmdAttr<CommandTDigestInfo>("tdigest.info", 2, 
"read-only", 1, 1, 1),
+                        MakeCmdAttr<CommandTDigestAdd>("tdigest.add", -3, 
"write", 2, -1, 1));

Review Comment:
   It seems that the 5th argument is confused to be `-1` here.
   It should be the `last_key` of command's key.



##########
tests/gocase/unit/type/tdigest/tdigest_test.go:
##########
@@ -141,4 +141,45 @@ func tdigestTests(t *testing.T, configs 
util.KvrocksServerConfigs) {
                        require.EqualValues(t, 0, info.TotalCompressions)
                }
        })
+
+       t.Run("tdigest.add with different arguments", func(t *testing.T) {
+               keyPrefix := "tdigest_add_"
+               
+               require.ErrorContains(t, rdb.Do(ctx, "TDIGEST.ADD").Err(), 
errMsgWrongNumberArg)
+               require.ErrorContains(t, rdb.Do(ctx, "TDIGEST.ADD", 
keyPrefix+"key").Err(), errMsgWrongNumberArg)
+               require.ErrorContains(t, rdb.Do(ctx, "TDIGEST.ADD", 
keyPrefix+"key", "abc").Err(), "not a valid float")
+               require.ErrorContains(t, rdb.Do(ctx, "TDIGEST.ADD", 
keyPrefix+"nonexistent", "1.0").Err(), errMsgKeyNotExist)
+       
+               key := keyPrefix + "test1"
+               require.NoError(t, rdb.Do(ctx, "TDIGEST.CREATE", key, 
"compression", "100").Err())
+       
+               require.NoError(t, rdb.Do(ctx, "TDIGEST.ADD", key, 
"42.0").Err())
+               
+               rsp := rdb.Do(ctx, "TDIGEST.INFO", key)
+               require.NoError(t, rsp.Err())
+               info := toTdigestInfo(t, rsp.Val())
+               require.EqualValues(t, 1, info.UnmergedNodes)

Review Comment:
   If we want to test this property here, we'd better add a case that triggers 
the merge and test both the `Merged nodes`, `Unmerged nodes` and `Total 
compressions`.
   We can just add some simple test cases here and I will do a more complex one 
in `tdigest.quantile` command.



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