diff --git a/src/commands.c b/src/commands.c index 5ee012c79..b9b4b915d 100644 --- a/src/commands.c +++ b/src/commands.c @@ -2608,7 +2608,7 @@ struct redisCommandArg PFDEBUG_Args[] = { /* PFMERGE argument table */ struct redisCommandArg PFMERGE_Args[] = { {"destkey",ARG_TYPE_KEY,0,NULL,NULL,NULL,CMD_ARG_NONE}, -{"sourcekey",ARG_TYPE_KEY,1,NULL,NULL,NULL,CMD_ARG_MULTIPLE}, +{"sourcekey",ARG_TYPE_KEY,1,NULL,NULL,NULL,CMD_ARG_OPTIONAL|CMD_ARG_MULTIPLE}, {0} }; diff --git a/src/commands/pfmerge.json b/src/commands/pfmerge.json index ae18dad1e..648a8b43a 100644 --- a/src/commands/pfmerge.json +++ b/src/commands/pfmerge.json @@ -62,6 +62,7 @@ "name": "sourcekey", "type": "key", "key_spec_index": 1, + "optional": true, "multiple": true } ] diff --git a/src/help.h b/src/help.h index a95da3695..e8a94210e 100644 --- a/src/help.h +++ b/src/help.h @@ -1125,7 +1125,7 @@ struct commandHelp { 11, "2.8.9" }, { "PFMERGE", - "destkey sourcekey [sourcekey ...]", + "destkey [sourcekey [sourcekey ...]]", "Merge N different HyperLogLogs into a single one.", 11, "2.8.9" }, diff --git a/tests/unit/hyperloglog.tcl b/tests/unit/hyperloglog.tcl index ed09dacce..0103b2ab9 100644 --- a/tests/unit/hyperloglog.tcl +++ b/tests/unit/hyperloglog.tcl @@ -165,6 +165,33 @@ start_server {tags {"hll"}} { r pfcount hll{t} } {5} + test {PFMERGE on missing source keys will create an empty destkey} { + r del sourcekey{t} sourcekey2{t} destkey{t} destkey2{t} + + assert_equal {OK} [r pfmerge destkey{t} sourcekey{t}] + assert_equal 1 [r exists destkey{t}] + assert_equal 0 [r pfcount destkey{t}] + + assert_equal {OK} [r pfmerge destkey2{t} sourcekey{t} sourcekey2{t}] + assert_equal 1 [r exists destkey2{t}] + assert_equal 0 [r pfcount destkey{t}] + } + + test {PFMERGE with one empty input key, create an empty destkey} { + r del destkey + assert_equal {OK} [r pfmerge destkey] + assert_equal 1 [r exists destkey] + assert_equal 0 [r pfcount destkey] + } + + test {PFMERGE with one non-empty input key, dest key is actually one of the source keys} { + r del destkey + assert_equal 1 [r pfadd destkey a b c] + assert_equal {OK} [r pfmerge destkey] + assert_equal 1 [r exists destkey] + assert_equal 3 [r pfcount destkey] + } + test {PFCOUNT multiple-keys merge returns cardinality of union #1} { r del hll1{t} hll2{t} hll3{t} for {set x 1} {$x < 10000} {incr x} {