Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom scalar to sql overrides support for DuckDB Unparser dialect #13915

Merged
merged 3 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions datafusion/sql/src/unparser/dialect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// specific language governing permissions and limitations
// under the License.

use std::sync::Arc;
use std::{collections::HashMap, sync::Arc};

use arrow_schema::TimeUnit;
use datafusion_common::Result;
Expand All @@ -29,6 +29,9 @@ use sqlparser::{

use super::{utils::character_length_to_sql, utils::date_part_to_sql, Unparser};

pub type ScalarFnToSqlHandler =
Box<dyn Fn(&Unparser, &[Expr]) -> Result<Option<ast::Expr>> + Send + Sync>;

/// `Dialect` to use for Unparsing
///
/// The default dialect tries to avoid quoting identifiers unless necessary (e.g. `a` instead of `"a"`)
Expand Down Expand Up @@ -305,7 +308,30 @@ impl PostgreSqlDialect {
}
}

pub struct DuckDBDialect {}
#[derive(Default)]
pub struct DuckDBDialect {
custom_scalar_fn_overrides: HashMap<String, ScalarFnToSqlHandler>,
}

impl DuckDBDialect {
#[must_use]
pub fn new() -> Self {
Self {
custom_scalar_fn_overrides: HashMap::new(),
}
}

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
}
}

impl Dialect for DuckDBDialect {
fn identifier_quote_style(&self, _: &str) -> Option<char> {
Expand All @@ -326,6 +352,10 @@ impl Dialect for DuckDBDialect {
func_name: &str,
args: &[Expr],
) -> Result<Option<ast::Expr>> {
if let Some(handler) = self.custom_scalar_fn_overrides.get(func_name) {
return handler(unparser, args);
}

if func_name == "character_length" {
return character_length_to_sql(
unparser,
Expand Down
26 changes: 25 additions & 1 deletion datafusion/sql/src/unparser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1636,7 +1636,7 @@ mod tests {

use crate::unparser::dialect::{
CharacterLengthStyle, CustomDialect, CustomDialectBuilder, DateFieldExtractStyle,
Dialect, PostgreSqlDialect,
Dialect, DuckDBDialect, PostgreSqlDialect, ScalarFnToSqlHandler,
};

use super::*;
Expand Down Expand Up @@ -2722,4 +2722,28 @@ mod tests {

Ok(())
}

#[test]
fn test_custom_scalar_overrides_duckdb() -> Result<()> {
let duckdb_default = DuckDBDialect::new();
let duckdb_extended = DuckDBDialect::new().with_custom_scalar_overrides(vec![(
"dummy_udf",
Box::new(|unparser: &Unparser, args: &[Expr]| {
unparser.scalar_function_to_sql("smart_udf", args).map(Some)
}) as ScalarFnToSqlHandler,
)]);

for (dialect, expected) in [
(duckdb_default, r#"dummy_udf("a", "b")"#),
(duckdb_extended, r#"smart_udf("a", "b")"#),
] {
let unparser = Unparser::new(&dialect);
let expr =
ScalarUDF::new_from_impl(DummyUDF::new()).call(vec![col("a"), col("b")]);
let actual = format!("{}", unparser.expr_to_sql(&expr)?);
assert_eq!(actual, expected);
}

Ok(())
}
}
Loading