sgrebnov opened a new pull request, #13915:
URL: https://github.com/apache/datafusion/pull/13915
## Which issue does this PR close?
Unparser does not currently support extending dialect with custom handlers
for internal UDFs or new engine extensions. Possible workarounds here are
creating a custom dialect wrapper around original dialect with overrided
`scalar_function_to_sql_overrides` or using `CustomDialect`. This is not very
convenient and robust as requires tracking changes to the main dialect and
reflecting the same improvements in the custom dialect.
Unparser does not currently support extending the dialect with custom
handlers for internal UDFs or new engine extensions. Possible workarounds
include:
- Creating a custom dialect wrapper around the original dialect with
overridden `scalar_function_to_sql_overrides`.
- Using `CustomDialect`.
Both approaches are not very convenient to use and robust robust, as they
require tracking changes to the main dialect and reflecting those improvements
in the custom dialect.
## Rationale for this change
Propose introducing a pattern and approach for extending
`scalar_function_to_sql_overrides` for dialects with additional handlers,
example usage.
```rust
/// Creates a new instance of the `DuckDB` dialect with support for custom
internal UDFs
pub fn new_duckdb_dialect() -> Arc<dyn Dialect> {
let dialect = DuckDBDialect::new().with_custom_scalar_overrides(vec![(
"cosine_distance",
Box::new(duckdb::cosine_distance_to_sql) as ScalarFnToSqlHandler,
)]);
Arc::new(dialect) as Arc<dyn Dialect>
}
```
## What changes are included in this PR?
PR introduces new pattern for extending dialects with custom
`scalar_function_to_sql_overrides` handlers for DuckDB that could be extended
to other dialects based on needs,
```rust
pub type ScalarFnToSqlHandler = Box<dyn Fn(&Unparser, &[Expr]) ->
Result<Option<ast::Expr>> + Send + Sync>;
pub fn with_custom_scalar_overrides(
mut self,
handlers: Vec<(&str, ScalarFnToSqlHandler)>,
) -> Self {
for (func_name, handler) in handlers {
self.custom_scalar_fn_overrides
.insert(func_name.to_string(), handler);
}
self
}
```
## Are these changes tested?
Unit test is in progress
## Are there any user-facing changes?
Breaking change: as we are adding additional field to DuckDB dialect,
existing approach to construct `DuckDBDialect {}` can't be used and needs to be
updated to `DuckDBDialect::new()`
--
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]