Skip to content

Commit 41bfb79

Browse files
authored
feat: support Spark-compatible abs math function part 2 - ANSI mode (#18828)
## Which issue does this PR close? <!-- We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123. --> - Part of #15914 ## Rationale for this change <!-- Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed. Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes. --> | Non-ANSI mode | ANSI mode | ANSI Interval Types | | - | - | - | | #18205 | This PR | TODO | - Support ANSI mode Spark-compatible `abs` math function ## What changes are included in this PR? - When [enable_ansi_mode](#18635) is true, `abs` throws exception on arithmetic overflow. <!-- There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR. --> ## Are these changes tested? <!-- We typically require tests for all PRs in order to: 1. Prevent the code from being accidentally broken by subsequent changes 2. Serve as another way to document the expected behavior of the code If tests are not included in your PR, please explain why (for example, are they covered by existing tests)? --> - unit tests - SQL logic tests w/ `datafusion.execution.enable_ansi_mode = true` ## Are there any user-facing changes? <!-- If there are user-facing changes then we may require documentation to be updated before approving the PR. --> Yes, arithmetic overflow will be thrown when ANSI mode is on, i.e. `datafusion.execution.enable_ansi_mode=true` <!-- If there are any breaking changes to public APIs, please add the `api change` label. -->
1 parent 8152b44 commit 41bfb79

File tree

5 files changed

+421
-159
lines changed

5 files changed

+421
-159
lines changed

datafusion/execution/src/config.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,12 @@ impl SessionConfig {
480480
self.options.execution.enforce_batch_size_in_joins
481481
}
482482

483+
/// Toggle SQL ANSI mode for expressions, casting, and error handling
484+
pub fn with_enable_ansi_mode(mut self, enable_ansi_mode: bool) -> Self {
485+
self.options_mut().execution.enable_ansi_mode = enable_ansi_mode;
486+
self
487+
}
488+
483489
/// Convert configuration options to name-value pairs with values
484490
/// converted to strings.
485491
///

datafusion/functions/src/math/abs.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ macro_rules! make_abs_function {
5050
}};
5151
}
5252

53+
#[macro_export]
5354
macro_rules! make_try_abs_function {
5455
($ARRAY_TYPE:ident) => {{
5556
|input: &ArrayRef| {
@@ -62,7 +63,8 @@ macro_rules! make_try_abs_function {
6263
x
6364
))
6465
})
65-
})?;
66+
})
67+
.and_then(|v| Ok(v.with_data_type(input.data_type().clone())))?; // maintain decimal's precision and scale
6668
Ok(Arc::new(res) as ArrayRef)
6769
}
6870
}};

0 commit comments

Comments
 (0)