Skip to content
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
8 changes: 6 additions & 2 deletions 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 Expand Up @@ -293,7 +297,7 @@ select: SELECT keys FROM source window where groupby limit ';'
$$ = flb_sp_cmd_operation(cmd, $2, NULL, FLB_EXP_PAR);
}
|
NOT condition
NOT condition %prec NOT
{
$$ = flb_sp_cmd_operation(cmd, $2, NULL, FLB_EXP_NOT);
}
Expand Down
30 changes: 30 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,36 @@ 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_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_and_precedence(int id, struct task_check *check,
char *buf, size_t size)
{
int ret;

/* Expect 0 rows */
ret = mp_count_rows(buf, size);
TEST_CHECK_(ret == 0, "expected 0 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
20 changes: 20 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,26 @@ 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_or_precedence",
"SELECT id FROM STREAM:FLB WHERE NOT true OR true;",
cb_select_not_or_precedence,
},
{
20, 0, 0, 0,
"not_and_precedence",
"SELECT id FROM STREAM:FLB WHERE NOT true AND false;",
cb_select_not_and_precedence,
},
};

#endif