diff --git a/src/yb/docdb/doc_operation.cc b/src/yb/docdb/doc_operation.cc index 5f75b16e7a40..30aca820c092 100644 --- a/src/yb/docdb/doc_operation.cc +++ b/src/yb/docdb/doc_operation.cc @@ -772,6 +772,7 @@ Status RedisWriteOperation::ApplyDel(const DocOperationApplyData& data) { RETURN_NOT_OK(PrimitiveValueFromSubKeyStrict(kv.subkey(i), *data_type, &primitive_value)); values.SetChild(primitive_value, SubDocument(ValueType::kTombstone)); } + num_keys = kv.subkey_size(); break; } case REDIS_TYPE_SORTEDSET: { @@ -839,9 +840,12 @@ Status RedisWriteOperation::ApplyDel(const DocOperationApplyData& data) { break; } } - DocPath doc_path = DocPath::DocPathFromRedisKey(kv.hash_code(), kv.key()); - RETURN_NOT_OK(data.doc_write_batch->ExtendSubDocument( - doc_path, values, redis_query_id())); + + if (num_keys != 0) { + DocPath doc_path = DocPath::DocPathFromRedisKey(kv.hash_code(), kv.key()); + RETURN_NOT_OK(data.doc_write_batch->ExtendSubDocument(doc_path, values, redis_query_id())); + } + response_.set_code(RedisResponsePB_RedisStatusCode_OK); if (EmulateRedisResponse(kv.type())) { // If the flag is true, we respond with the number of keys actually being deleted. We don't diff --git a/src/yb/yql/redis/redisserver/redisserver-test.cc b/src/yb/yql/redis/redisserver/redisserver-test.cc index 12b41be1e688..f9fb7cb08cdc 100644 --- a/src/yb/yql/redis/redisserver/redisserver-test.cc +++ b/src/yb/yql/redis/redisserver/redisserver-test.cc @@ -1339,6 +1339,17 @@ TEST_F(TestRedisService, TestSortedSets) { // Cannot have incr with multiple score value pairs. DoRedisTestExpectError(__LINE__, {"ZADD", "z_key", "INCR", "0", "v1", "1", "v2"}); + // Test ZREM on non-existent key and then add the same key. + DoRedisTestInt(__LINE__, {"ZREM", "my_z_set", "v1"}, 0); + SyncClient(); + DoRedisTestInt(__LINE__, {"ZCARD", "my_z_set"}, 0); + SyncClient(); + DoRedisTestInt(__LINE__, {"ZADD", "my_z_set", "1", "v1"}, 1); + SyncClient(); + DoRedisTestInt(__LINE__, {"ZCARD", "my_z_set"}, 1); + SyncClient(); + DoRedisTestArray(__LINE__, {"ZRANGEBYSCORE", "my_z_set", "1", "1"}, {"v1"}); + SyncClient(); VerifyCallbacks(); }