From adcf90f18c55e560c40215c8856999eb6cab9e1e Mon Sep 17 00:00:00 2001 From: Phillip LeBlanc Date: Tue, 19 Nov 2024 06:06:29 +0900 Subject: [PATCH] Support Utf8View in Unparser `expr_to_sql` (#13462) * Support Utf8View in Unparser expr_to_sql * Add another test * Update expr.rs Co-authored-by: Sherin Jacob * Fix import * feedback * Add null/is_not_null test --------- Co-authored-by: Sherin Jacob --- datafusion/sql/src/unparser/expr.rs | 52 ++++++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 4 deletions(-) diff --git a/datafusion/sql/src/unparser/expr.rs b/datafusion/sql/src/unparser/expr.rs index 3bf4ae304721..f1f28258f9bd 100644 --- a/datafusion/sql/src/unparser/expr.rs +++ b/datafusion/sql/src/unparser/expr.rs @@ -1458,9 +1458,7 @@ impl Unparser<'_> { } DataType::Utf8 => Ok(self.dialect.utf8_cast_dtype()), DataType::LargeUtf8 => Ok(self.dialect.large_utf8_cast_dtype()), - DataType::Utf8View => { - not_impl_err!("Unsupported DataType: conversion: {data_type:?}") - } + DataType::Utf8View => Ok(self.dialect.utf8_cast_dtype()), DataType::List(_) => { not_impl_err!("Unsupported DataType: conversion: {data_type:?}") } @@ -1520,7 +1518,7 @@ mod tests { use datafusion_common::TableReference; use datafusion_expr::expr::WildcardOptions; use datafusion_expr::{ - case, col, cube, exists, grouping_set, interval_datetime_lit, + case, cast, col, cube, exists, grouping_set, interval_datetime_lit, interval_year_month_lit, lit, not, not_exists, out_ref_col, placeholder, rollup, table_scan, try_cast, when, wildcard, ColumnarValue, ScalarUDF, ScalarUDFImpl, Signature, Volatility, WindowFrame, WindowFunctionDefinition, @@ -2540,4 +2538,50 @@ mod tests { } Ok(()) } + + #[test] + fn test_utf8_view_to_sql() -> Result<()> { + let dialect = CustomDialectBuilder::new() + .with_utf8_cast_dtype(ast::DataType::Char(None)) + .build(); + let unparser = Unparser::new(&dialect); + + let ast_dtype = unparser.arrow_dtype_to_ast_dtype(&DataType::Utf8View)?; + + assert_eq!(ast_dtype, ast::DataType::Char(None)); + + let expr = cast(col("a"), DataType::Utf8View); + let ast = unparser.expr_to_sql(&expr)?; + + let actual = format!("{}", ast); + let expected = r#"CAST(a AS CHAR)"#.to_string(); + + assert_eq!(actual, expected); + + let expr = col("a").eq(lit(ScalarValue::Utf8View(Some("hello".to_string())))); + let ast = unparser.expr_to_sql(&expr)?; + + let actual = format!("{}", ast); + let expected = r#"(a = 'hello')"#.to_string(); + + assert_eq!(actual, expected); + + let expr = col("a").is_not_null(); + + let ast = unparser.expr_to_sql(&expr)?; + let actual = format!("{}", ast); + let expected = r#"a IS NOT NULL"#.to_string(); + + assert_eq!(actual, expected); + + let expr = col("a").is_null(); + + let ast = unparser.expr_to_sql(&expr)?; + let actual = format!("{}", ast); + let expected = r#"a IS NULL"#.to_string(); + + assert_eq!(actual, expected); + + Ok(()) + } }