Skip to content

Commit

Permalink
Support Utf8View in Unparser expr_to_sql (#13462)
Browse files Browse the repository at this point in the history
* Support Utf8View in Unparser expr_to_sql

* Add another test

* Update expr.rs

Co-authored-by: Sherin Jacob <[email protected]>

* Fix import

* feedback

* Add null/is_not_null test

---------

Co-authored-by: Sherin Jacob <[email protected]>
  • Loading branch information
phillipleblanc and jcsherin authored Nov 18, 2024
1 parent 900552c commit adcf90f
Showing 1 changed file with 48 additions and 4 deletions.
52 changes: 48 additions & 4 deletions datafusion/sql/src/unparser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:?}")
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(())
}
}

0 comments on commit adcf90f

Please sign in to comment.