Skip to content

Commit

Permalink
Enforce 0 value for count reducer (#1745)
Browse files Browse the repository at this point in the history
* Enforce 0 values for count

* fix rstest

* fix for centos
  • Loading branch information
ashtul committed Dec 23, 2020
1 parent b77e714 commit 0ab57d6
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
6 changes: 5 additions & 1 deletion src/aggregate/reducers/count.c
Expand Up @@ -23,7 +23,11 @@ static RSValue *counterFinalize(Reducer *r, void *instance) {
return RS_NumVal(dd->count);
}

Reducer *RDCRCount_New(const ReducerOptions *unused) {
Reducer *RDCRCount_New(const ReducerOptions *options) {
if (options->args->argc != 0) {
QueryError_SetError(options->status, QUERY_EBADATTR, "Count accepts 0 values only");
return NULL;
}
Reducer *r = rm_calloc(1, sizeof(*r));
r->Add = counterAdd;
r->Finalize = counterFinalize;
Expand Down
10 changes: 8 additions & 2 deletions tests/cpptests/test_cpp_agg.cpp
Expand Up @@ -157,7 +157,10 @@ TEST_F(AggTest, testGroupBy) {
Grouper *gr = Grouper_New((const RLookupKey **)&ctx.rkvalue, (const RLookupKey **)&v_out, 1);
ASSERT_TRUE(gr != NULL);

Grouper_AddReducer(gr, RDCRCount_New(NULL), count_out);
ArgsCursor args = {0};
ReducerOptions opt = {0};
opt.args = &args;
Grouper_AddReducer(gr, RDCRCount_New(&opt), count_out);
ReducerOptionsCXX sumOptions("SUM", &rk_in, "score");
auto sumReducer = RDCRSum_New(&sumOptions);
ASSERT_TRUE(sumReducer != NULL) << QueryError_GetError(sumOptions.status);
Expand Down Expand Up @@ -197,7 +200,10 @@ TEST_F(AggTest, testGroupSplit) {
RLookupKey *val_out = RLookup_GetKey(&lk_out, "value", RLOOKUP_F_OCREAT);
RLookupKey *count_out = RLookup_GetKey(&lk_out, "COUNT", RLOOKUP_F_OCREAT);
Grouper *gr = Grouper_New((const RLookupKey **)&gen.kvalue, (const RLookupKey **)&val_out, 1);
Grouper_AddReducer(gr, RDCRCount_New(NULL), count_out);
ArgsCursor args = {0};
ReducerOptions opt = {0};
opt.args = &args;
Grouper_AddReducer(gr, RDCRCount_New(&opt), count_out);
SearchResult res = {0};
size_t ii = 0;

Expand Down
21 changes: 20 additions & 1 deletion tests/pytests/test_aggregate.py
Expand Up @@ -448,7 +448,26 @@ def testMultiSortBy(self):
'SORTBY', 2, '@brand', 'DESC',
'SORTBY', 2, '@price', 'DESC').error()\
.contains('Multiple SORTBY steps are not allowed. Sort multiple fields in a single step')


def testCountError(self):
# With 0 values
res = self.env.cmd('ft.aggregate', 'games', '*',
'GROUPBY', '2', '@brand', '@price',
'REDUCE', 'COUNT', 0)
self.env.assertEqual(len(res), 1245)

# With count 1 and 1 value
res = self.env.expect('ft.aggregate', 'games', '*',
'GROUPBY', '2', '@brand', '@price',
'REDUCE', 'COUNT', 1, '@brand').error() \
.contains('Count accepts 0 values only')

# With count 1 and 0 values
res = self.env.expect('ft.aggregate', 'games', '*',
'GROUPBY', '2', '@brand', '@price',
'REDUCE', 'COUNT', 1).error() \
.contains('Bad arguments for COUNT: Expected an argument, but none provided')

# def testLoadAfterSortBy(self):
# with self.env.assertResponseError():
# self.env.cmd('ft.aggregate', 'games', '*',
Expand Down

0 comments on commit 0ab57d6

Please sign in to comment.