Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Operator Precedence for Logical Operations in Stream Processor #9388

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading