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

Panic in simplify_expressions optimizer rules when running an aggregate query (SQLancer) #12814

Closed
2010YOUY01 opened this issue Oct 8, 2024 · 1 comment · Fixed by #12860
Closed
Assignees
Labels
bug Something isn't working

Comments

@2010YOUY01
Copy link
Contributor

Describe the bug

See the reproducer in datafusion-cli (compiled from the latest main, commit 84c9409)

> CREATE TABLE t1(v1 int);
> SELECT v1
FROM t1
WHERE ((count(v1) % 1) << 1) > 0;
thread 'main' panicked at /Users/yongting/Desktop/code/my_datafusion/arrow-datafusion/datafusion/physical-expr/src/expressions/binary/kernels.rs:87:1:
called `Option::unwrap()` on a `None` value
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

-- Backtrace
......
  22: datafusion_expr::logical_plan::tree_node::<impl datafusion_expr::logical_plan::plan::LogicalPlan>::map_expressions
             at /Users/yongting/Desktop/code/my_datafusion/arrow-datafusion/datafusion/expr/src/logical_plan/tree_node.rs:570:19
  23: datafusion_optimizer::simplify_expressions::simplify_exprs::SimplifyExpressions::optimize_internal
             at /Users/yongting/Desktop/code/my_datafusion/arrow-datafusion/datafusion/optimizer/src/simplify_expressions/simplify_exprs.rs:125:9
  24: <datafusion_optimizer::simplify_expressions::simplify_exprs::SimplifyExpressions as datafusion_optimizer::optimizer::OptimizerRule>::rewrite
             at /Users/yongting/Desktop/code/my_datafusion/arrow-datafusion/datafusion/optimizer/src/simplify_expressions/simplify_exprs.rs:73:9
.......

To Reproduce

No response

Expected behavior

The query is invalid, given aggregate function is not allowed in where clause, so it should return a planning error, instead of panics

Additional context

Found by SQLancer #11030

@2010YOUY01 2010YOUY01 added the bug Something isn't working label Oct 8, 2024
@jonathanc-n
Copy link
Contributor

take

eejbyfeldt added a commit to eejbyfeldt/datafusion that referenced this issue Oct 13, 2024
This address a bug that previously always replace % 1 expression with a
0 of type i32. This lead to panics/crashes in a lot of places since we
expect the type to not change as part of this simplification rule. This
patch fixes it by replacing it with a 0 of correct type.

This was discovered in apache#12814
alamb pushed a commit that referenced this issue Oct 16, 2024
This address a bug that previously always replace % 1 expression with a
0 of type i32. This lead to panics/crashes in a lot of places since we
expect the type to not change as part of this simplification rule. This
patch fixes it by replacing it with a 0 of correct type.

This was discovered in #12814
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants