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

[BUG] Predicates expression containing multiple AND and OR operators evaluated with wrong order #770

Closed
LantaoJin opened this issue Oct 11, 2024 · 0 comments · Fixed by #771
Assignees
Labels
bug Something isn't working correctness Does not produce the expected outcome even though it doesn’t crash

Comments

@LantaoJin
Copy link
Member

What is the bug?
a predicate expression containing multiple AND and OR operators does have an order of evaluation. This order is determined by precedence rules, just like in arithmetic where multiplication has higher precedence than addition.

In SQL:
AND has higher precedence than OR.
Parentheses can be used to explicitly define the desired order of operations and to override the default precedence.

Example Without Parentheses:

SELECT *
FROM employees
WHERE department = 'HR'
   OR job_title = 'Manager'
   AND salary > 50000;

In this query, the AND between job_title = 'Manager' and salary > 50000 is evaluated first, because AND has higher precedence than OR. It equals to

WHERE department = 'HR'
   OR (job_title = 'Manager' AND salary > 50000);

But in PPL, this query will be evaluated with wrong order:

source=employees
| where department = 'HR' OR job_title = 'Manager' AND salary > 50000

was evaluated to

source=employees
| where (department = 'HR' OR job_title = 'Manager') AND salary > 50000
@LantaoJin LantaoJin added bug Something isn't working untriaged and removed untriaged labels Oct 11, 2024
@LantaoJin LantaoJin self-assigned this Oct 11, 2024
@LantaoJin LantaoJin added the correctness Does not produce the expected outcome even though it doesn’t crash label Oct 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working correctness Does not produce the expected outcome even though it doesn’t crash
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant