Skip to content

Commit 5876a1a

Browse files
authored
minor: add higher-order function methods to SessionContext (#21950)
## Which issue does this PR close? Follow-up of #21679 that forgot to add these methods to `SessionContext` ## Rationale for this change <!-- 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 abfa5f2 commit 5876a1a

2 files changed

Lines changed: 55 additions & 0 deletions

File tree

  • datafusion/core/src/execution/context
  • docs/source/library-user-guide/upgrading

datafusion/core/src/execution/context/mod.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1496,6 +1496,9 @@ impl SessionContext {
14961496
RegisterFunction::Window(f) => {
14971497
self.state.write().register_udwf(f)?;
14981498
}
1499+
RegisterFunction::HigherOrder(f) => {
1500+
self.state.write().register_higher_order_function(f)?;
1501+
}
14991502
RegisterFunction::Table(name, f) => self.register_udtf(&name, f),
15001503
};
15011504

@@ -1510,6 +1513,11 @@ impl SessionContext {
15101513
dropped |= self.state.write().deregister_udaf(&stmt.name)?.is_some();
15111514
dropped |= self.state.write().deregister_udwf(&stmt.name)?.is_some();
15121515
dropped |= self.state.write().deregister_udtf(&stmt.name)?.is_some();
1516+
dropped |= self
1517+
.state
1518+
.write()
1519+
.deregister_higher_order_function(&stmt.name)?
1520+
.is_some();
15131521

15141522
// DROP FUNCTION IF EXISTS drops the specified function only if that
15151523
// function exists and in this way, it avoids error. While the DROP FUNCTION
@@ -1609,6 +1617,20 @@ impl SessionContext {
16091617
state.register_udf(Arc::new(f)).ok();
16101618
}
16111619

1620+
/// Registers a higher-order function within this context.
1621+
///
1622+
/// Note in SQL queries, function names are looked up using
1623+
/// lowercase unless the query uses quotes. For example,
1624+
///
1625+
/// - `SELECT MY_HIGHER_ORDER_FUNC(x)...` will look for a function named `"my_higher_order_func"`
1626+
/// - `SELECT "my_HIGHER_ORDER_FUNC"(x)` will look for a function named `"my_HIGHER_ORDER_FUNC"`
1627+
///
1628+
/// Any functions registered with the function name or its aliases will be overwritten with this new function
1629+
pub fn register_higher_order_function(&self, f: Arc<dyn HigherOrderUDF>) {
1630+
let mut state = self.state.write();
1631+
state.register_higher_order_function(f).ok();
1632+
}
1633+
16121634
/// Registers an aggregate UDF within this context.
16131635
///
16141636
/// Note in SQL queries, aggregate names are looked up using
@@ -1648,6 +1670,14 @@ impl SessionContext {
16481670
self.state.write().deregister_udf(name).ok();
16491671
}
16501672

1673+
/// Deregisters a higher-order function within this context.
1674+
pub fn deregister_higher_order_function(&self, name: &str) {
1675+
self.state
1676+
.write()
1677+
.deregister_higher_order_function(name)
1678+
.ok();
1679+
}
1680+
16511681
/// Deregisters a UDAF within this context.
16521682
pub fn deregister_udaf(&self, name: &str) {
16531683
self.state.write().deregister_udaf(name).ok();
@@ -2190,6 +2220,8 @@ pub enum RegisterFunction {
21902220
Aggregate(Arc<AggregateUDF>),
21912221
/// Window user defined function
21922222
Window(Arc<WindowUDF>),
2223+
/// Higher-order user defined function
2224+
HigherOrder(Arc<dyn HigherOrderUDF>),
21932225
/// Table user defined function
21942226
Table(String, Arc<dyn TableFunctionImpl>),
21952227
}

docs/source/library-user-guide/upgrading/54.0.0.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,3 +787,26 @@ a combined `OR` (e.g., index-backed sources).
787787
```sql
788788
SET datafusion.optimizer.enable_unions_to_filter = true;
789789
```
790+
791+
### The `RegisterFunction` enum has a new `HigherOrder` variant
792+
793+
`RegisterFunction` now has a `HigherOrder(Arc<dyn HigherOrderUDF>)` variant
794+
so user-defined higher-order functions can be registered
795+
796+
**Who is affected:**
797+
798+
- Users who match on a `RegisterFunction` without a default branch `_ => {}`
799+
800+
**Migration guide:**
801+
802+
Add the new branch to the match with the logic applicable to the context.
803+
804+
```diff
805+
match register_function {
806+
RegisterFunction::Scalar(scalar) => {},
807+
RegisterFunction::Aggregate(aggregate) => {},
808+
RegisterFunction::Window(window) => {},
809+
+ RegisterFunction::HigherOrder(higher_order) => {},
810+
RegisterFunction::Table(name, table) => {},
811+
}
812+
```

0 commit comments

Comments
 (0)