You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "sgrebnov (via GitHub)" <gi...@apache.org> on 2024/12/27 05:23:02 UTC
[PR] Custom scalar to sql overrides support for DuckDB Unparser dialect(#68) [datafusion]
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Custom scalar to sql overrides support for DuckDB Unparser dialect [datafusion]
Posted by "sgrebnov (via GitHub)" <gi...@apache.org>.
sgrebnov commented on PR #13915:
URL: https://github.com/apache/datafusion/pull/13915#issuecomment-2577037902
@goldmedal - thank you, updated implementation to define `with_custom_scalar_overrides` on `Dialect` trait level. Moving forward we will be able to add support/implementation for each pre-defined dialects similar to DuckDB.
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Custom scalar to sql overrides support for DuckDB Unparser dialect [datafusion]
Posted by "goldmedal (via GitHub)" <gi...@apache.org>.
goldmedal commented on PR #13915:
URL: https://github.com/apache/datafusion/pull/13915#issuecomment-2578995368
Thanks @sgrebnov. Look great 👍
> Note: please let me know if you prefer me to add support for all dialects as part of this PR - I'll be able to do this: add impl + tests.
It's fine to me for now. We can add them in the follow-up PR.
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Custom scalar to sql overrides support for DuckDB Unparser dialect [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #13915:
URL: https://github.com/apache/datafusion/pull/13915#issuecomment-2581302761
Thanks for the contribution @sgrebnov and for the review @goldmedal
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Custom scalar to sql overrides support for DuckDB Unparser dialect [datafusion]
Posted by "sgrebnov (via GitHub)" <gi...@apache.org>.
sgrebnov commented on PR #13915:
URL: https://github.com/apache/datafusion/pull/13915#issuecomment-2568661287
@goldmedal - thank you for the feedback. Yeah, other dialects will benefit from similar logic as well. I was exploring how to make this work for all dialects, but it is not possible to fully implement it at the `Dialect` trait level because traits cannot have fields (to store extra overrides).
Alternatively:
1. I can add a `with_custom_scalar_overrides` function definition with a blank "not supported yet" implementation at the `Dialect` trait level. This would allow each dialect to implement support as needed (similar to DuckDB) and maintain a unified signature while ensuring that implementation is optional for dialects where it is not currently required.
2. I can implement logic similar to DuckDB for other official dialects so that all dialects support this feature.
Regarding adding overrides at the `Unparser` level: I like that this approach would work for all dialects. However, my concern is that there are cases where configuration is done solely at the `Dialect` level without access to the `Unparser`. For example, when using [datafusion-federation](https://github.com/datafusion-contrib/datafusion-federation/blob/d3626ce72f723d4c715d2012e72cbac6b4fca67d/datafusion-federation/src/sql/mod.rs#L673). Additionally, splitting configuration between two entities—`Unparser` and `Dialect`—where both control SQL overrides during unparsing, could introduce complexity.
@goldmedal - WDYT if we proceed with approach 1 above, where we add the `with_custom_scalar_overrides` function definition at the `Dialect` trait level and add implementation based on needs?
```rust
pub trait Dialect: Send + Sync {
...
fn with_custom_scalar_overrides(
self,
_handlers: Vec<(&str, ScalarFnToSqlHandler)>,
) -> Self
where
Self: Sized,
{
unimplemented!("Dialect does not support defining custom scalar overrides");
}
...
```
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Custom scalar to sql overrides support for DuckDB Unparser dialect [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb merged PR #13915:
URL: https://github.com/apache/datafusion/pull/13915
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Custom scalar to sql overrides support for DuckDB Unparser dialect [datafusion]
Posted by "goldmedal (via GitHub)" <gi...@apache.org>.
goldmedal commented on PR #13915:
URL: https://github.com/apache/datafusion/pull/13915#issuecomment-2572217309
> Regarding adding overrides at the `Unparser` level: I like that this approach would work for all dialects. However, my concern is that there are cases where configuration is done solely at the `Dialect` level without access to the `Unparser`. For example, when using [datafusion-federation](https://github.com/datafusion-contrib/datafusion-federation/blob/d3626ce72f723d4c715d2012e72cbac6b4fca67d/datafusion-federation/src/sql/mod.rs#L673). Additionally, splitting configuration between two entities—`Unparser` and `Dialect`—where both control SQL overrides during unparsing, could introduce extra complexity/conflicts.
>
Thanks for the information. Indeed, the custom behavior should depend on the dialect instead of the unparser.
> @goldmedal - WDYT if we proceed with approach 1 above, where we add the `with_custom_scalar_overrides` function definition at the `Dialect` trait level and add implementation based on needs?
>
> ```rust
> pub trait Dialect: Send + Sync {
> ...
> fn with_custom_scalar_overrides(
> self,
> _handlers: Vec<(&str, ScalarFnToSqlHandler)>,
> ) -> Self
> where
> Self: Sized,
> {
> unimplemented!("Dialect does not support defining custom scalar overrides");
> }
> ...
> ```
It makes sense to me 👍. The user can use this method in `scalar_function_to_sql_overrides` if required.
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org