comphead commented on code in PR #7751:
URL: https://github.com/apache/datafusion-comet/pull/7751#discussion_r4788737910
##########
native/spark-expr/src/comet_scalar_funcs.rs:
##########
@@ -123,7 +125,41 @@ pub fn create_comet_physical_fun_with_eval_mode(
fail_on_error: Option<bool>,
eval_mode: EvalMode,
) -> Result<Arc<ScalarUDF>, DataFusionError> {
- let fail_on_error = fail_on_error.unwrap_or(false);
+ resolve_comet_physical_fun(
+ fun_name,
+ data_type,
+ registry,
+ fail_on_error.unwrap_or(false),
+ eval_mode,
+ false,
+ )
+}
+
+/// Resolve a scalar function that will use the supplied execution config.
+pub fn create_comet_physical_fun_with_config(
+ fun_name: &str,
+ data_type: DataType,
+ registry: &dyn FunctionRegistry,
+ config: &ConfigOptions,
+) -> Result<Arc<ScalarUDF>, DataFusionError> {
+ resolve_comet_physical_fun(
+ fun_name,
+ data_type,
+ registry,
+ config.execution.enable_ansi_mode,
+ EvalMode::Legacy,
+ true,
+ )
+}
+
+fn resolve_comet_physical_fun(
+ fun_name: &str,
Review Comment:
This adds a public resolver, a `has_execution_config` flag and an
`inner().is::<T>()` type check. It also relies on an unchecked contract: the
caller must pass the same `config` to `ScalarFunctionExpr` (planner.rs:2220).
The resolver only reads `enable_ansi_mode` from it. The planner's `ScalarFunc`
path is the only caller that passes a non-default config, so a dedicated arm
like the others would be simpler. For example `"elt" =>
Ok(Arc::new(ScalarUDF::new_from_impl(SparkElt::new())))`, plus the planner
change that passes `ANSI_CONFIG` or `LEGACY_CONFIG`. That keeps
`create_comet_physical_fun` and `create_comet_physical_fun_with_eval_mode` as
they were. It needs no new export in `lib.rs` and no session registration, and
the fail-closed catch-all stays unchanged.
##########
native/spark-expr/src/comet_scalar_funcs.rs:
##########
@@ -330,18 +366,25 @@ pub fn create_comet_physical_fun_with_eval_mode(
"greatest" | "least" if SparkGreatestLeast::handles(&data_type) =>
Ok(Arc::new(
ScalarUDF::new_from_impl(SparkGreatestLeast::new(fun_name ==
"greatest")),
)),
- // Registry UDFs (including datafusion-spark) cannot receive
fail_on_error.
- _ if fail_on_error => Err(DataFusionError::Execution(format!(
- "Function '{fun_name}' is resolved from the UDF registry and
cannot \
- honor fail_on_error=true. Use a name-based ANSI/try variant \
- (e.g. parse_url / try_parse_url) or a dedicated match arm that \
- consumes the flag."
- ))),
- _ => registry.udf(fun_name).map_err(|e| {
- DataFusionError::Execution(format!(
- "Function {fun_name} not found in the registry: {e}",
- ))
- }),
+ _ => {
+ let udf = registry.udf(fun_name);
+ if fail_on_error
+ && !(has_execution_config
+ && udf.as_ref().is_ok_and(|udf|
udf.inner().is::<SparkElt>()))
Review Comment:
I don't think a Comet session can reach this branch. `SparkElt` is not in
`register_datafusion_spark_function`
(`native/core/src/execution/jni_api.rs:1177`), so `registry.udf("elt")` fails
there. `CometElt` is also a `CometCodegenDispatch`
(`spark/src/main/scala/org/apache/comet/serde/strings.scala:852`), so no
`ScalarFunc` named `elt` is serialized. In datafusion-spark 55.2.0 the
functions that read `enable_ansi_mode` are `abs`, `round`, `negative`,
`modulus`, `cast` and `elt`, and Comet registers none of them. So the PR does
not change any Spark query.
`SparkElt` is also not a Spark-compatible ANSI implementation yet. Its
out-of-range error is a plain `exec_err!("The index {index} is out of
bounds...")` (datafusion-spark 55.2.0 `src/function/string/elt.rs:122-125`). It
has no `[INVALID_ARRAY_INDEX]` class, so routing `elt` to it natively would
fail the `expect_error(INVALID_ARRAY_INDEX)` queries in
`spark/src/test/resources/sql-tests/expressions/string/elt_ansi.sql`. Could
this PR either wire `elt` end to end (serde with `failOnError`, registration,
the error mapped to `SparkError::InvalidArrayIndex`), or leave the allowlist
out until a function that meets Spark's contract needs it?
##########
native/core/src/execution/planner.rs:
##########
@@ -5598,6 +5613,49 @@ mod tests {
}))
}
+ #[test]
+ fn registered_spark_elt_respects_fail_on_error() -> Result<(),
ExecutionError> {
Review Comment:
This test registers `SparkElt` on a bare `SessionContext`, which a Comet
session never does. So it covers the resolver branch, not behavior a Spark
query can see. The index column is also `Int64` while Spark's `Elt` takes an
`IntegerType` index. Once `elt` runs natively, `elt.sql` and `elt_ansi.sql`
already cover ANSI off and on, including the error class, through
`CometSqlFileTestSuite`. I would rely on them and drop this Rust test.
##########
docs/source/contributor-guide/adding_a_new_expression.md:
##########
@@ -113,10 +112,9 @@ Follow these rules when wiring scalar expressions:
- If a function must receive `fail_on_error` through the proto, use
`scalarFunctionExprToProtoWithReturnType(..., failOnError, ...)`. The
function
- must also have a dedicated match arm in
- `create_comet_physical_fun_with_eval_mode` that actually consumes the flag;
- it must not fall through to registry lookup. Existing native consumers
- include `make_decimal`, `make_date`, `make_time`, and `next_day`.
+ must honor the flag in its Rust implementation. For a registry function,
verify
+ that it reads ANSI mode from `ConfigOptions` and add its implementation type
+ to the resolver’s check.
Review Comment:
Reading ANSI mode from `ConfigOptions` is not enough for a registry
function. It also has to raise Spark's error class, which `SparkElt` does not,
and it has to be registered in `register_datafusion_spark_function`. The
removed sentence named the functions that actually consume the flag
(`make_decimal`, `make_date`, `make_time`, `next_day`). They are still the only
ones that do, so I would keep that list. Lines 95-98 likewise describe a
registry path that no Comet session reaches today. `resolver’s` also uses a
curly apostrophe.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]