Skip to content

Commit becc71b

Browse files
authored
feat: Add ansi enable parameter for execution config (#18635)
## 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. --> - Closes #18634 . ## Rationale for this change Adding a ansi flag to expand coverage for Spark built in functions, Spark 4.0 sets ansi mode as true by default. Currently the flag is planned to be used to `datafusion-spark` crate via ScalarConfigArgs, however it can also be used for DF if ansi mode is in the roadmap <!-- 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. --> ## What changes are included in this PR? <!-- 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)? --> ## Are there any user-facing changes? <!-- If there are user-facing changes then we may require documentation to be updated before approving the PR. --> <!-- If there are any breaking changes to public APIs, please add the `api change` label. -->
1 parent 0d52a1e commit becc71b

File tree

3 files changed

+153
-127
lines changed

3 files changed

+153
-127
lines changed

datafusion/common/src/config.rs

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,29 @@ config_namespace! {
606606
/// written, it may be necessary to increase this size to avoid errors from
607607
/// the remote end point.
608608
pub objectstore_writer_buffer_size: usize, default = 10 * 1024 * 1024
609+
610+
/// Whether to enable ANSI SQL mode.
611+
///
612+
/// The flag is experimental and relevant only for DataFusion Spark built-in functions
613+
///
614+
/// When `enable_ansi_mode` is set to `true`, the query engine follows ANSI SQL
615+
/// semantics for expressions, casting, and error handling. This means:
616+
/// - **Strict type coercion rules:** implicit casts between incompatible types are disallowed.
617+
/// - **Standard SQL arithmetic behavior:** operations such as division by zero,
618+
/// numeric overflow, or invalid casts raise runtime errors rather than returning
619+
/// `NULL` or adjusted values.
620+
/// - **Consistent ANSI behavior** for string concatenation, comparisons, and `NULL` handling.
621+
///
622+
/// When `enable_ansi_mode` is `false` (the default), the engine uses a more permissive,
623+
/// non-ANSI mode designed for user convenience and backward compatibility. In this mode:
624+
/// - Implicit casts between types are allowed (e.g., string to integer when possible).
625+
/// - Arithmetic operations are more lenient — for example, `abs()` on the minimum
626+
/// representable integer value returns the input value instead of raising overflow.
627+
/// - Division by zero or invalid casts may return `NULL` instead of failing.
628+
///
629+
/// # Default
630+
/// `false` — ANSI SQL mode is disabled by default.
631+
pub enable_ansi_mode: bool, default = false
609632
}
610633
}
611634

@@ -1124,6 +1147,15 @@ pub struct ConfigOptions {
11241147
}
11251148

11261149
impl ConfigField for ConfigOptions {
1150+
fn visit<V: Visit>(&self, v: &mut V, _key_prefix: &str, _description: &'static str) {
1151+
self.catalog.visit(v, "datafusion.catalog", "");
1152+
self.execution.visit(v, "datafusion.execution", "");
1153+
self.optimizer.visit(v, "datafusion.optimizer", "");
1154+
self.explain.visit(v, "datafusion.explain", "");
1155+
self.sql_parser.visit(v, "datafusion.sql_parser", "");
1156+
self.format.visit(v, "datafusion.format", "");
1157+
}
1158+
11271159
fn set(&mut self, key: &str, value: &str) -> Result<()> {
11281160
// Extensions are handled in the public `ConfigOptions::set`
11291161
let (key, rem) = key.split_once('.').unwrap_or((key, ""));
@@ -1137,15 +1169,6 @@ impl ConfigField for ConfigOptions {
11371169
_ => _config_err!("Config value \"{key}\" not found on ConfigOptions"),
11381170
}
11391171
}
1140-
1141-
fn visit<V: Visit>(&self, v: &mut V, _key_prefix: &str, _description: &'static str) {
1142-
self.catalog.visit(v, "datafusion.catalog", "");
1143-
self.execution.visit(v, "datafusion.execution", "");
1144-
self.optimizer.visit(v, "datafusion.optimizer", "");
1145-
self.explain.visit(v, "datafusion.explain", "");
1146-
self.sql_parser.visit(v, "datafusion.sql_parser", "");
1147-
self.format.visit(v, "datafusion.format", "");
1148-
}
11491172
}
11501173

11511174
impl ConfigOptions {

datafusion/sqllogictest/test_files/information_schema.slt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ datafusion.catalog.newlines_in_values false
217217
datafusion.execution.batch_size 8192
218218
datafusion.execution.coalesce_batches true
219219
datafusion.execution.collect_statistics true
220+
datafusion.execution.enable_ansi_mode false
220221
datafusion.execution.enable_recursive_ctes true
221222
datafusion.execution.enforce_batch_size_in_joins false
222223
datafusion.execution.keep_partition_by_columns false
@@ -338,6 +339,7 @@ datafusion.catalog.newlines_in_values false Specifies whether newlines in (quote
338339
datafusion.execution.batch_size 8192 Default batch size while creating new batches, it's especially useful for buffer-in-memory batches since creating tiny batches would result in too much metadata memory consumption
339340
datafusion.execution.coalesce_batches true When set to true, record batches will be examined between each operator and small batches will be coalesced into larger batches. This is helpful when there are highly selective filters or joins that could produce tiny output batches. The target batch size is determined by the configuration setting
340341
datafusion.execution.collect_statistics true Should DataFusion collect statistics when first creating a table. Has no effect after the table is created. Applies to the default `ListingTableProvider` in DataFusion. Defaults to true.
342+
datafusion.execution.enable_ansi_mode false Whether to enable ANSI SQL mode. The flag is experimental and relevant only for DataFusion Spark built-in functions When `enable_ansi_mode` is set to `true`, the query engine follows ANSI SQL semantics for expressions, casting, and error handling. This means: - **Strict type coercion rules:** implicit casts between incompatible types are disallowed. - **Standard SQL arithmetic behavior:** operations such as division by zero, numeric overflow, or invalid casts raise runtime errors rather than returning `NULL` or adjusted values. - **Consistent ANSI behavior** for string concatenation, comparisons, and `NULL` handling. When `enable_ansi_mode` is `false` (the default), the engine uses a more permissive, non-ANSI mode designed for user convenience and backward compatibility. In this mode: - Implicit casts between types are allowed (e.g., string to integer when possible). - Arithmetic operations are more lenient — for example, `abs()` on the minimum representable integer value returns the input value instead of raising overflow. - Division by zero or invalid casts may return `NULL` instead of failing. # Default `false` — ANSI SQL mode is disabled by default.
341343
datafusion.execution.enable_recursive_ctes true Should DataFusion support recursive CTEs
342344
datafusion.execution.enforce_batch_size_in_joins false Should DataFusion enforce batch size in joins or not. By default, DataFusion will not enforce batch size in joins. Enforcing batch size in joins can reduce memory usage when joining large tables with a highly-selective join filter, but is also slightly slower.
343345
datafusion.execution.keep_partition_by_columns false Should DataFusion keep the columns used for partition_by in the output RecordBatches

0 commit comments

Comments
 (0)