Skip to content

Commit 9d6b2a1

Browse files
fix(search_family): Fix FT.AGGREGATE output (#4311)
Signed-off-by: Stepan Bagritsevich <stefan@dragonflydb.io>
1 parent 027d76c commit 9d6b2a1

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

src/server/search/search_family.cc

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -998,16 +998,20 @@ void SearchFamily::FtAggregate(CmdArgList args, const CommandContext& cmd_cntx)
998998
rb->StartArray(result_size + 1);
999999
rb->SendLong(result_size);
10001000

1001-
const size_t field_count = agg_results.fields_to_print.size();
10021001
for (const auto& value : agg_results.values) {
1003-
rb->StartArray(field_count * 2);
1002+
size_t fields_count = 0;
10041003
for (const auto& field : agg_results.fields_to_print) {
1005-
rb->SendBulkString(field);
1004+
if (value.find(field) != value.end()) {
1005+
fields_count++;
1006+
}
1007+
}
10061008

1007-
if (auto it = value.find(field); it != value.end()) {
1009+
rb->StartArray(fields_count * 2);
1010+
for (const auto& field : agg_results.fields_to_print) {
1011+
auto it = value.find(field);
1012+
if (it != value.end()) {
1013+
rb->SendBulkString(field);
10081014
std::visit(sortable_value_sender, it->second);
1009-
} else {
1010-
rb->SendNull();
10111015
}
10121016
}
10131017
}

src/server/search/search_family_test.cc

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1635,26 +1635,37 @@ TEST_F(SearchFamilyTest, AggregateResultFields) {
16351635
Run({"JSON.SET", "j2", ".", R"({"a":"4","b":"5","c":"6"})"});
16361636
Run({"JSON.SET", "j3", ".", R"({"a":"7","b":"8","c":"9"})"});
16371637

1638-
auto resp = Run({"FT.CREATE", "index", "ON", "JSON", "SCHEMA", "$.a", "AS", "a", "TEXT",
1639-
"SORTABLE", "$.b", "AS", "b", "TEXT", "$.c", "AS", "c", "TEXT"});
1638+
auto resp = Run({"FT.CREATE", "i1", "ON", "JSON", "SCHEMA", "$.a", "AS", "a", "TEXT", "SORTABLE",
1639+
"$.b", "AS", "b", "TEXT", "$.c", "AS", "c", "TEXT"});
16401640
EXPECT_EQ(resp, "OK");
16411641

1642-
resp = Run({"FT.AGGREGATE", "index", "*"});
1642+
resp = Run({"FT.AGGREGATE", "i1", "*"});
16431643
EXPECT_THAT(resp, IsUnordArrayWithSize(IsMap(), IsMap(), IsMap()));
16441644

1645-
resp = Run({"FT.AGGREGATE", "index", "*", "SORTBY", "1", "a"});
1645+
resp = Run({"FT.AGGREGATE", "i1", "*", "SORTBY", "1", "a"});
16461646
EXPECT_THAT(resp, IsUnordArrayWithSize(IsMap("a", "1"), IsMap("a", "4"), IsMap("a", "7")));
16471647

1648-
resp = Run({"FT.AGGREGATE", "index", "*", "LOAD", "1", "@b", "SORTBY", "1", "a"});
1648+
resp = Run({"FT.AGGREGATE", "i1", "*", "LOAD", "1", "@b", "SORTBY", "1", "a"});
16491649
EXPECT_THAT(resp,
16501650
IsUnordArrayWithSize(IsMap("b", "\"2\"", "a", "1"), IsMap("b", "\"5\"", "a", "4"),
16511651
IsMap("b", "\"8\"", "a", "7")));
16521652

1653-
resp = Run({"FT.AGGREGATE", "index", "*", "SORTBY", "1", "a", "GROUPBY", "2", "@b", "@a",
1654-
"REDUCE", "COUNT", "0", "AS", "count"});
1653+
resp = Run({"FT.AGGREGATE", "i1", "*", "SORTBY", "1", "a", "GROUPBY", "2", "@b", "@a", "REDUCE",
1654+
"COUNT", "0", "AS", "count"});
16551655
EXPECT_THAT(resp, IsUnordArrayWithSize(IsMap("b", "\"8\"", "a", "7", "count", "1"),
16561656
IsMap("b", "\"2\"", "a", "1", "count", "1"),
16571657
IsMap("b", "\"5\"", "a", "4", "count", "1")));
1658+
1659+
Run({"JSON.SET", "j4", ".", R"({"id":1, "number":4})"});
1660+
Run({"JSON.SET", "j5", ".", R"({"id":2})"});
1661+
1662+
resp = Run({"FT.CREATE", "i2", "ON", "JSON", "SCHEMA", "$.id", "AS", "id", "NUMERIC", "$.number",
1663+
"AS", "number", "NUMERIC"});
1664+
EXPECT_EQ(resp, "OK");
1665+
1666+
resp = Run({"FT.AGGREGATE", "i2", "*", "LOAD", "2", "@id", "@number"});
1667+
EXPECT_THAT(resp, IsUnordArrayWithSize(IsMap("id", "1", "number", "4"), IsMap("id", "2"), IsMap(),
1668+
IsMap(), IsMap()));
16581669
}
16591670

16601671
} // namespace dfly

0 commit comments

Comments
 (0)