neilconway commented on code in PR #23850:
URL: https://github.com/apache/datafusion/pull/23850#discussion_r3666875064
##########
datafusion/core/src/execution/session_state.rs:
##########
@@ -1067,6 +1067,83 @@ pub struct SessionStateBuilder {
physical_optimizer_rules: Option<Vec<Arc<dyn PhysicalOptimizerRule + Send
+ Sync>>>,
}
+/// A function registrable under its primary name plus every alias.
+pub trait NamedFunction {
+ /// The function's primary name.
+ fn name(&self) -> &str;
+ /// The function's aliases, if any.
+ fn aliases(&self) -> &[String];
+}
+
+impl NamedFunction for ScalarUDF {
+ fn name(&self) -> &str {
+ ScalarUDF::name(self)
+ }
+ fn aliases(&self) -> &[String] {
+ ScalarUDF::aliases(self)
+ }
+}
+
+impl NamedFunction for HigherOrderUDF {
+ fn name(&self) -> &str {
+ HigherOrderUDF::name(self)
+ }
+ fn aliases(&self) -> &[String] {
+ HigherOrderUDF::aliases(self)
+ }
+}
+
+impl NamedFunction for AggregateUDF {
+ fn name(&self) -> &str {
+ AggregateUDF::name(self)
+ }
+ fn aliases(&self) -> &[String] {
+ AggregateUDF::aliases(self)
+ }
+}
+
+impl NamedFunction for WindowUDF {
+ fn name(&self) -> &str {
+ WindowUDF::name(self)
+ }
+ fn aliases(&self) -> &[String] {
+ WindowUDF::aliases(self)
+ }
+}
+
+/// Key each function by its primary name and every alias (last-writer-wins)
into a registry map.
+pub fn index_by_name_and_aliases<T: NamedFunction>(
+ functions: Vec<Arc<T>>,
+) -> HashMap<String, Arc<T>> {
+ let mut map = HashMap::new();
+ for function in functions {
+ for alias in function.aliases() {
+ map.insert(alias.clone(), Arc::clone(&function));
+ }
+ map.insert(function.name().to_string(), function);
+ }
+ map
+}
+
+/// Re-specialize `udf` for `config` (only datetime funcs do; others keep the
same `Arc`).
+fn refresh_udf(udf: Arc<ScalarUDF>, config: &ConfigOptions) -> Arc<ScalarUDF> {
+ match udf.inner().with_updated_config(config) {
+ Some(new_udf) => Arc::new(new_udf),
+ None => udf,
+ }
+}
+
+/// Re-specialize the config-dependent entries of a scalar registry,
preserving every name/alias key.
+fn refresh_scalar_functions(
+ functions: HashMap<String, Arc<ScalarUDF>>,
+ config: &ConfigOptions,
+) -> HashMap<String, Arc<ScalarUDF>> {
+ functions
+ .into_iter()
+ .map(|(key, udf)| (key, refresh_udf(udf, config)))
+ .collect()
+}
Review Comment:
This strikes me as pretty fragile and unsightly, but I realize the root
cause is not in this PR. I wonder if (separarely) we can change the function
APIs to avoid the need for this; e.g., maybe we could pass the `SessionConfig`
to `return_field_from_args`.
--
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]