Contributions are welcome. See also the DataFusion contributor guide.
When implementing these functions, you can check if there are existing implementations in the Sail or Comet projects first. If you do port functionality from these sources, make sure to port over the corresponding tests too, to ensure correctness and compatibility.
simplify()DataFusion functions allow you to implement simplify() which can let you rewrite the function call during logical optimization, theoretically allowing you to avoid implementing physical execution via invoke_with_args() if the rewrite is unconditional (e.g. rewrite to an arithmetic operation).
However, invoke_with_args() must always be implemented for functions in this crate. This is because downstream users such as Comet rely on DataFusion for physical execution, and not logical planning/optimization. That means if a function doesn't have a physical implementation (invoke_with_args()) it is not usable by Comet.
A function that is null-intolerant in Spark (nullIntolerant is true, NullIntolerant before Spark 4.0), which includes most UnaryExpression, BinaryExpression and TernaryExpression functions, should declare a SparkNullability and use spark_nullability! (see function/nullability.rs) instead of hand-rolling is_nullable() in return_field_from_args() and simplify():
const NULLABILITY: SparkNullability = SparkNullability::null_intolerant(); impl ScalarUDFImpl for SparkAscii { fn return_field_from_args(&self, args: ReturnFieldArgs) -> Result<FieldRef> { Ok(NULLABILITY.return_field(self.name(), DataType::Int32, args.arg_fields)) } spark_nullability!(NULLABILITY); }
SparkNullability::null_intolerant() when Spark takes nullable from its children (children.exists(_.nullable)), and null_intolerant_always_nullable() when Spark overrides nullable to true, such as every UnaryMathExpression and a function that returns NULL for an invalid value.spark_nullability!(NULLABILITY) implements simplify(), which replaces a call that has a NULL literal argument with a typed NULL literal, as Spark's NullPropagation does. invoke_with_args() must still return NULL for NULL arguments, and it must do so before it validates the other arguments, as Spark does.spark_nullability!(NULLABILITY, return_type) also implements return_field_from_args() by calling return_type(). Use it only when return_type() computes the type from the argument types. If a function's return_type() is internal_err! only, because its type needs the argument fields, scalar arguments or the config, implement return_field_from_args() yourself with NULLABILITY.return_field(...) and use the one-argument form of the macro.NULLABILITY.nullable(args.arg_fields) with that rule.The functions in this crate need only support input types available to Spark; that is, they do not need to handle unsigned types or types such as Float16 or Decimal64.
Spark casts a function's arguments before it runs the function, and an invalid value behaves differently with ANSI mode on, off, and in the try_* functions. Prefer the shared helpers below to per-function rules. They keep results and errors consistent, and their module docs have the details.
ImplicitCastInputTypes, declare its inputTypes with SparkInputTypes (function/implicit_cast.rs), which supplies signature(), coerce_types() and with_updated_config() with Spark‘s errors and ANSI rules. See sha2. It only chooses the target types, so the cast that then runs is not yet Spark’s.ExpectsInputTypes without implicit casts, use SparkInputTypes::check_only. It shares the declared-type validation and Spark errors, accepts untyped NULLs, and unwraps dictionaries. Widen accepted inputs separately when the execution kernel needs a common representation. See timestamp_micros.array_position does, call the rules in function/type_coercion.rs (for example tightest_common_type()) from coerce_types().CAST does, call spark_cast() (function/conversion/cast/) and not Arrow’s cast. The casts are being ported from Comet (#88). One that is not ported yet returns a “not implemented” error, because Arrow‘s result would differ from Spark’s. Port the missing cast with its tests, or note in the PR that the function depends on it.spark.sql.ansi.enabled should take an EvalMode (Legacy, Ansi or Try) rather than a bool, read with EvalMode::from_config(&args.config_options). A try_* function then shares the implementation with EvalMode::Try, as parse_url and try_parse_url do.Every function should have documentation that appears in generated function references (ScalarUDFImpl::documentation(), or the aggregate/window equivalent). Write it once, using the #[user_doc] attribute macro from datafusion-macros on the struct, which generates documentation() and keeps the docs next to the type. The struct keeps only a short rustdoc comment (what it is, plus a link to the Spark docs), so that facts are not written twice.
The most useful thing to document is how the function differs from the DataFusion built-in of the same name, and its Spark edge-case behavior. Only document behavior that is covered by a test.
use datafusion_macros::user_doc; /// Spark-compatible `my_func` expression. /// /// <https://spark.apache.org/docs/latest/api/sql/index.html#my_func> #[user_doc( doc_section(label = "String Functions"), description = "What it returns, and how it differs from the DataFusion built-in: types, \ NULL handling, edge cases (out-of-range values, overflow, empty input), ANSI mode.", syntax_example = "my_func(expr[, optional])", alternative_syntax = "alias(expr)", // only if there are aliases sql_example = r#"```sql > SELECT my_func('a'); result ```"#, argument(name = "expr", description = "What it is, accepted types, NULL behavior.") )] #[derive(Debug, PartialEq, Eq, Hash)] pub struct SparkMyFunc { /* ... */ } impl ScalarUDFImpl for SparkMyFunc { fn documentation(&self) -> Option<&Documentation> { self.doc() } // ... }
See sha2 (crates/spark/src/function/hash/sha2.rs) and substring (crates/spark/src/function/string/substring.rs) for complete examples.
The function reference in docs/functions is generated from these attributes, with one page per function category. After adding or changing a function's documentation, regenerate it and commit the result (CI checks that it is up to date):
dev/update_function_docs.sh
The script also lists, on stderr, the functions that don't implement documentation() yet.
Prefer adding tests via SQLLogicTests where possible, see the Spark SQLLogicTest README. Resort to adding tests as Rust unit tests where it is impossible or difficult to test via SLT. This is because direct invocation via Rust skips steps such as input coercion, and is usually more verbose in the setup needed to pass data in (and assert output data).
datafusion-spark makes minor and patch releases between DataFusion major versions, so main must not break its public API until it moves to the next DataFusion major version. For example, do not remove or rename a public item, or change the signature of a public function. New functions and new public items are fine, and so are changes to a function's results that make it match Spark. See Versioning for the full policy.
The release process is documented in dev/release/README.md.