Skip to content

Commit

Permalink
stream_processor: fix operator precedence for logical operations
Browse files Browse the repository at this point in the history
Signed-off-by: Aleksandr Cupacenko <[email protected]>

revert definition removal

Signed-off-by: Aleksandr Cupacenko <[email protected]>

add operator precedence unit tests

correct precedencer order
  • Loading branch information
unitmatrix committed Sep 22, 2024
1 parent db27a84 commit 1acc74d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/stream_processor/parser/sql.y
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
%name-prefix="flb_sp_" // replace with %define api.prefix {flb_sp_}
%name-prefix "flb_sp_" // replace with %define api.prefix {flb_sp_}
%define api.pure full
%define parse.error verbose
%parse-param { struct flb_sp_cmd *cmd };
Expand Down Expand Up @@ -98,6 +98,10 @@ void yyerror(struct flb_sp_cmd *cmd, const char *query, void *scanner, const cha
%type <integer> aggregate_func
%type <integer> COUNT AVG SUM MAX MIN TIMESERIES_FORECAST

/* Define operator precedence and associativity for logical operations in conditions */
%left OR // Lowest precedence for OR
%left AND // Middle precedence for AND
%right NOT // Highest precedence for NOT

%destructor { flb_free ($$); } IDENTIFIER

Expand Down
20 changes: 20 additions & 0 deletions tests/internal/include/sp_cb_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,26 @@ static void cb_record_not_contains(int id, struct task_check *check,
TEST_CHECK(ret == 0);
}

static void cb_select_and_or_precedence(int id, struct task_check *check,
char *buf, size_t size)
{
int ret;

/* Expect all 11 rows */
ret = mp_count_rows(buf, size);
TEST_CHECK_(ret == 11, "expected 11 rows but got %d", ret);
}

static void cb_select_not_precedence(int id, struct task_check *check,
char *buf, size_t size)
{
int ret;

/* Expect all 11 rows */
ret = mp_count_rows(buf, size);
TEST_CHECK_(ret == 11, "expected 11 rows but got %d", ret);
}

/* Callback functions to perform checks over results */
static void cb_select_sub_blue(int id, struct task_check *check,
char *buf, size_t size)
Expand Down
14 changes: 14 additions & 0 deletions tests/internal/include/sp_select_keys.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,20 @@ struct task_check select_keys_checks[] = {
"SELECT id FROM TAG:'samples' WHERE @record.contains(x);",
cb_record_not_contains,
},

/* Operator precedence */
{
18, 0, 0, 0,
"and_or_precedence",
"SELECT id FROM STREAM:FLB WHERE false AND true OR true;",
cb_select_and_or_precedence,
},
{
19, 0, 0, 0,
"not_precedence",
"SELECT id FROM STREAM:FLB WHERE NOT true OR true;",
cb_select_not_precedence,
},
};

#endif

0 comments on commit 1acc74d

Please sign in to comment.