-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Implement arithmetic overflow error handling #17554
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
base: main
Are you sure you want to change the base?
Conversation
Add SQL-compliant overflow checking for arithmetic operations by default. Previously, DataFusion allowed numeric overflow to wrap silently, which differs from SQL standard behavior and other databases. Changes: - Add `fail_on_overflow` configuration option (defaults to true) - Update BinaryExpr to use checked arithmetic when fail_on_overflow is enabled - Modify binary() function signature to accept ExecutionProps for config access - Fix all call sites to pass ExecutionProps parameter - Add helper functions for test code to provide default ExecutionProps The behavior now matches PostgreSQL, Trino, and Snowflake: - SELECT 10000000000 * 10000000000 returns overflow error by default - Can be disabled with SET datafusion.execution.fail_on_overflow = false Fixes apache#17539
PR body claims this has been tested but it doesn't look like it builds successfully in the first place? |
.as_ref() | ||
.map(|cfg| cfg.execution.fail_on_overflow) | ||
.unwrap_or(true); | ||
Ok(Arc::new(BinaryExpr::new(lhs, op, rhs).with_fail_on_overflow(fail_on_overflow))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's deprecate BinaryExpr::new
and make sure all usages within DataFusion codebase are updated correctly.
} | ||
|
||
/// Helper function for tests that creates a binary expression with default ExecutionProps | ||
fn binary_test( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
binary_test is suitable for a boolean-returning binary operators, e.g. <
or =
it's not suitable for others, e.g. +
, /
fn binary_test( | |
fn binary_expr( |
use datafusion_expr::Operator; | ||
use datafusion_physical_expr_common::physical_expr::fmt_sql; | ||
|
||
/// Helper function for tests that creates a binary expression with default ExecutionProps | ||
fn binary_test( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fn binary_test( | |
fn binary_expr( |
use datafusion_expr::Operator; | ||
|
||
/// Helper function for tests that provides default ExecutionProps for binary function calls | ||
fn binary_test( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fn binary_test( | |
fn binary_expr( |
rhs: Arc<dyn PhysicalExpr>, | ||
schema: &Schema, | ||
) -> Result<Arc<dyn PhysicalExpr>> { | ||
let execution_props = ExecutionProps::new(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
make sure that default behavior configured by ExecutionProps::new()
is to fail on overflow
@EeshanBembi, I am curious about the performance impact of enabling overflow checking by default. Could you add criterion benchmarks? |
Summary
Add SQL-compliant overflow checking for arithmetic operations by default. Previously,
DataFusion allowed numeric overflow to wrap silently, which differs from SQL standard
behavior and other databases like PostgreSQL, Trino, and Snowflake.
Changes
fail_on_overflow
configuration option that defaults totrue
for SQL-standardbehavior
BinaryExpr
to use checked arithmetic operations when overflow checking is enabledbinary()
function signature to acceptExecutionProps
for configuration accessExecutionProps
parameterExecutionProps
Behavior
Before:
Testing
Fixes #17539