fluent / fluent/fluent-bit

stream_processor: precedence of AND, OR, and NOT not defined in grammar

Open
#3,763 4 comments 0 reactions 1 assignee Claimed by @koleini View on GitHub
bug enhancement
Dominant language
C
Stars
8.1k
Forks
2k
Avg merge
4d 16h
Merged PRs (30d)
58

Description

## Bug Report

**Describe the bug**
The sql.y bison file currently does not define any precedence between `AND` and `OR` in a condition.
https://github.com/fluent/fluent-bit/blob/afd25aa02b88b8c19af051557785f7c7626672d8/src/stream_processor/parser/sql.y#L301-L309

(Note: There are several issues with the ambiguously defined grammar in that file causing various shift/reduce and reduce/reduce conflicts.)

From what I can tell, this means that bison will effectively consider them both to have equal precedence and simply group them from right to left. However, in standard SQL (and most programming languages), `AND` is supposed to have higher precedence. For example, `A AND B OR C` should be interpreted as `(A AND B) OR C`, but the current grammar would interpret it as `A AND (B OR C)`. Note that even if bison is grouping from left to right, it would incorrectly interpret `A OR B AND C` as `(A OR B) AND C` instead of `A OR (B AND C)`.

None of the existing unit tests seem to use both `AND` and `OR` in a single expression, and I don't understand the tests well enough to add a new one, so I cannot say with 100% certainty that the existing behavior is as described. I'm just going by what I can piece together from bison. Unfortunately, the Fluent Bit documentation is pretty vague on the matter and doesn't even mention `AND` and `OR` specifically. https://docs.fluentbit.io/manual/stream-processing/getting-started/fluent-bit-sql

There is potentially a similar issue with the way that `NOT` was defined.
https://github.com/fluent/fluent-bit/blob/afd25aa02b88b8c19af051557785f7c7626672d8/src/stream_processor/parser/sql.y#L296-L299

From testing with bison, it appears that this would, for example, incorrectly parse `NOT A OR B` as `NOT (A OR B)` instead of `(NOT A) OR B`. Again, I have not been able to test what the stream processor does specifically.

@koleini @edsiper Can you confirm whether the stream processor has the aforementioned issues with `AND`, `OR`, and `NOT`? If so, what should be done about it, given that changing the grammar to assign their standard precedence could break existing queries?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.