From 5c913a16fa955fbf5d94da8a82006bd2c1f89fd1 Mon Sep 17 00:00:00 2001 From: goutamadwant Date: Fri, 7 Aug 2026 23:08:53 -0700 Subject: [PATCH] feat(cli): add opt-in Spark function registration --- Cargo.lock | 1 + datafusion-cli/Cargo.toml | 1 + datafusion-cli/src/main.rs | 11 ++++++++- datafusion-cli/tests/cli_integration.rs | 30 +++++++++++++++++++++++++ docs/source/user-guide/cli/usage.md | 14 ++++++++++++ 5 files changed, 56 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 3ddb32f60ffd5..4aeccccae8ef6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1866,6 +1866,7 @@ dependencies = [ "ctor", "datafusion", "datafusion-common", + "datafusion-spark", "dirs", "env_logger", "futures", diff --git a/datafusion-cli/Cargo.toml b/datafusion-cli/Cargo.toml index 62eedafe798d4..50b4002541f89 100644 --- a/datafusion-cli/Cargo.toml +++ b/datafusion-cli/Cargo.toml @@ -56,6 +56,7 @@ datafusion = { workspace = true, features = [ "unicode_expressions", ] } datafusion-common = { workspace = true } +datafusion-spark = { workspace = true, features = ["core"] } dirs = "6.0.0" env_logger = { workspace = true } futures = { workspace = true } diff --git a/datafusion-cli/src/main.rs b/datafusion-cli/src/main.rs index 20a2537d7c10c..dd1a3940302d8 100644 --- a/datafusion-cli/src/main.rs +++ b/datafusion-cli/src/main.rs @@ -143,6 +143,12 @@ struct Args { #[clap(long, help = "Enables console syntax highlighting")] color: bool, + #[clap( + long, + help = "Enable Apache Spark-compatible functions, overriding DataFusion functions with the same name" + )] + spark: bool, + #[clap( short = 'd', long, @@ -244,8 +250,11 @@ async fn main_inner() -> Result<()> { let runtime_env = rt_builder.build_arc()?; // enable dynamic file query - let ctx = SessionContext::new_with_config_rt(session_config, runtime_env) + let mut ctx = SessionContext::new_with_config_rt(session_config, runtime_env) .enable_url_table(); + if args.spark { + datafusion_spark::register_all(&mut ctx)?; + } ctx.refresh_catalogs().await?; // install dynamic catalog provider that can register required object stores ctx.register_catalog_list(Arc::new(DynamicObjectStoreCatalog::new( diff --git a/datafusion-cli/tests/cli_integration.rs b/datafusion-cli/tests/cli_integration.rs index 4dc244445a2eb..ec92fac190a3a 100644 --- a/datafusion-cli/tests/cli_integration.rs +++ b/datafusion-cli/tests/cli_integration.rs @@ -173,6 +173,36 @@ fn cli_quick_test<'a>( assert_cmd_snapshot!(cmd); } +#[test] +fn spark_functions_require_opt_in() { + let query = "SELECT next_day('2015-07-27'::DATE, 'Sun'::STRING);"; + + let output = cli() + .args(["-q", "--command", query]) + .output() + .expect("failed to run datafusion-cli without --spark"); + let stdout = String::from_utf8_lossy(&output.stdout); + assert!(!output.status.success(), "query unexpectedly succeeded"); + assert!( + stdout.contains("Invalid function 'next_day'"), + "expected next_day to be unavailable without --spark, got:\n{stdout}" + ); + + let output = cli() + .args(["-q", "--spark", "--command", query]) + .output() + .expect("failed to run datafusion-cli with --spark"); + let stdout = String::from_utf8_lossy(&output.stdout); + assert!( + output.status.success(), + "query failed with --spark, got:\n{stdout}" + ); + assert!( + stdout.contains("2015-08-02"), + "expected next_day result, got:\n{stdout}" + ); +} + /// Read data piped into the CLI via the `/dev/stdin` pseudo-path. /// /// Unix-only: `/dev/stdin` does not exist on Windows. This drives the real diff --git a/docs/source/user-guide/cli/usage.md b/docs/source/user-guide/cli/usage.md index 75c6698f007a5..5cb5c55a69e80 100644 --- a/docs/source/user-guide/cli/usage.md +++ b/docs/source/user-guide/cli/usage.md @@ -52,6 +52,8 @@ Options: [possible values: numbers(0/10/...), inf(no limit)] [default: 40] --color Enables console syntax highlighting + --spark + Enable Apache Spark-compatible functions, overriding DataFusion functions with the same name -d, --disk-limit Available disk space for spilling queries (e.g. '10g'), default to None (uses DataFusion's default value of '100g') --object-store-profiling @@ -63,6 +65,18 @@ Options: Print version ``` +## Apache Spark-compatible functions + +Use `--spark` to register the Apache Spark-compatible function library for the +CLI session: + +```bash +datafusion-cli --spark -c "SELECT next_day('2015-07-27'::DATE, 'Sun'::STRING);" +``` + +Spark-compatible functions are disabled by default. Enabling them overrides +DataFusion functions with the same name for the duration of the session. + ## Commands Available commands inside DataFusion CLI are: