Skip to content

Apache DataFusion SQL Query Engine

minor: add higher-order function methods to SessionContext (#21950)

gstvg5876a1af38cf
diff --git a/datafusion/core/src/execution/context/mod.rs b/datafusion/core/src/execution/context/mod.rs
index 5770c20262..67dbe6b740 100644
--- a/datafusion/core/src/execution/context/mod.rs
+++ b/datafusion/core/src/execution/context/mod.rs
@@ -1496,6 +1496,9 @@ impl SessionContext {
RegisterFunction::Window(f) => {
self.state.write().register_udwf(f)?;
}
+ RegisterFunction::HigherOrder(f) => {
+ self.state.write().register_higher_order_function(f)?;
+ }
RegisterFunction::Table(name, f) => self.register_udtf(&name, f),
};
@@ -1510,6 +1513,11 @@ impl SessionContext {
dropped |= self.state.write().deregister_udaf(&stmt.name)?.is_some();
dropped |= self.state.write().deregister_udwf(&stmt.name)?.is_some();
dropped |= self.state.write().deregister_udtf(&stmt.name)?.is_some();
+ dropped |= self
+ .state
+ .write()
+ .deregister_higher_order_function(&stmt.name)?
+ .is_some();
// DROP FUNCTION IF EXISTS drops the specified function only if that
// function exists and in this way, it avoids error. While the DROP FUNCTION
@@ -1609,6 +1617,20 @@ impl SessionContext {
state.register_udf(Arc::new(f)).ok();
}
+ /// Registers a higher-order function within this context.
+ ///
+ /// Note in SQL queries, function names are looked up using
+ /// lowercase unless the query uses quotes. For example,
+ ///
+ /// - `SELECT MY_HIGHER_ORDER_FUNC(x)...` will look for a function named `"my_higher_order_func"`
+ /// - `SELECT "my_HIGHER_ORDER_FUNC"(x)` will look for a function named `"my_HIGHER_ORDER_FUNC"`
+ ///
+ /// Any functions registered with the function name or its aliases will be overwritten with this new function
+ pub fn register_higher_order_function(&self, f: Arc<dyn HigherOrderUDF>) {
+ let mut state = self.state.write();
+ state.register_higher_order_function(f).ok();
+ }
+
/// Registers an aggregate UDF within this context.
///
/// Note in SQL queries, aggregate names are looked up using
@@ -1648,6 +1670,14 @@ impl SessionContext {
self.state.write().deregister_udf(name).ok();
}
+ /// Deregisters a higher-order function within this context.
+ pub fn deregister_higher_order_function(&self, name: &str) {
+ self.state
+ .write()
+ .deregister_higher_order_function(name)
+ .ok();
+ }
+
/// Deregisters a UDAF within this context.
pub fn deregister_udaf(&self, name: &str) {
self.state.write().deregister_udaf(name).ok();
@@ -2190,6 +2220,8 @@ pub enum RegisterFunction {
Aggregate(Arc<AggregateUDF>),
/// Window user defined function
Window(Arc<WindowUDF>),
+ /// Higher-order user defined function
+ HigherOrder(Arc<dyn HigherOrderUDF>),
/// Table user defined function
Table(String, Arc<dyn TableFunctionImpl>),
}
diff --git a/docs/source/library-user-guide/upgrading/54.0.0.md b/docs/source/library-user-guide/upgrading/54.0.0.md
index 833164f58b..9643d1e936 100644
--- a/docs/source/library-user-guide/upgrading/54.0.0.md
+++ b/docs/source/library-user-guide/upgrading/54.0.0.md
@@ -787,3 +787,26 @@ a combined `OR` (e.g., index-backed sources).
```sql
SET datafusion.optimizer.enable_unions_to_filter = true;
```
+
+### The `RegisterFunction` enum has a new `HigherOrder` variant
+
+`RegisterFunction` now has a `HigherOrder(Arc<dyn HigherOrderUDF>)` variant
+so user-defined higher-order functions can be registered
+
+**Who is affected:**
+
+- Users who match on a `RegisterFunction` without a default branch `_ => {}`
+
+**Migration guide:**
+
+Add the new branch to the match with the logic applicable to the context.
+
+```diff
+match register_function {
+ RegisterFunction::Scalar(scalar) => {},
+ RegisterFunction::Aggregate(aggregate) => {},
+ RegisterFunction::Window(window) => {},
++ RegisterFunction::HigherOrder(higher_order) => {},
+ RegisterFunction::Table(name, table) => {},
+}
+```