Make PFMERGE source key optional in docs, add tests with one input key, add tests on missing source keys (#11205)

The following two cases will create an empty destkey HLL:
1. called with no source keys, like `pfmerge destkey`
2. called with non-existing source keys, like `pfmerge destkey non-existing-source-key`

In the first case, in `PFMERGE`, the dest key is actually one of the source keys too.
So `PFMERGE k1 k2` is equivalent to `SUNIONSTORE k1 k1 k2`,
and `PFMERGE k1` is equivalent to `SUNIONSTORE k1 k1`.
So the first case is reasonable, the source key is actually optional.

And the second case, `PFMERGE` on missing keys should succeed and create an empty dest.
This is consistent with `PFCOUNT`, and also with `SUNIONSTORE`, no need to change.
This commit is contained in:
Binbin 2022-10-23 01:41:17 +08:00 committed by GitHub
parent 6dd213558b
commit 9e1b879f5b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 30 additions and 2 deletions

View File

@ -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}
};

View File

@ -62,6 +62,7 @@
"name": "sourcekey",
"type": "key",
"key_spec_index": 1,
"optional": true,
"multiple": true
}
]

View File

@ -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" },

View File

@ -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} {