Apache DataFusion SQL Query Engine
Add DuckDB struct test and row as alias (#12841)
Jay Zhan58c32cbbb546
diff --git a/datafusion/functions/src/core/mod.rs b/datafusion/functions/src/core/mod.rsindex 1c69f9c9b2..cf64c03766 100644--- a/datafusion/functions/src/core/mod.rs+++ b/datafusion/functions/src/core/mod.rs@@ -107,5 +107,6 @@ pub fn functions() -> Vec<Arc<ScalarUDF>> {get_field(),coalesce(),version(),+ r#struct(),]}diff --git a/datafusion/functions/src/core/struct.rs b/datafusion/functions/src/core/struct.rsindex bdddbb81be..5eea7dd48d 100644--- a/datafusion/functions/src/core/struct.rs+++ b/datafusion/functions/src/core/struct.rs@@ -57,6 +57,7 @@ fn struct_expr(args: &[ColumnarValue]) -> Result<ColumnarValue> {#[derive(Debug)]pub struct StructFunc {signature: Signature,+ aliases: Vec<String>,}impl Default for StructFunc {@@ -69,6 +70,7 @@ impl StructFunc {pub fn new() -> Self {Self {signature: Signature::variadic_any(Volatility::Immutable),+ aliases: vec![String::from("row")],}}}@@ -81,6 +83,10 @@ impl ScalarUDFImpl for StructFunc {"struct"}+ fn aliases(&self) -> &[String] {+ &self.aliases+ }+fn signature(&self) -> &Signature {&self.signature}diff --git a/datafusion/sqllogictest/test_files/struct.slt b/datafusion/sqllogictest/test_files/struct.sltindex d2e7160d00..67cd7d71fc 100644--- a/datafusion/sqllogictest/test_files/struct.slt+++ b/datafusion/sqllogictest/test_files/struct.slt@@ -373,3 +373,52 @@ You reached the bottom!statement okdrop view complex_view;++# Test row alias++query ?+select row('a', 'b');+----+{c0: a, c1: b}++##################################+# Switch Dialect to DuckDB+##################################++statement ok+set datafusion.sql_parser.dialect = 'DuckDB';++statement ok+CREATE TABLE struct_values (+ s1 struct(a int, b varchar),+ s2 struct(a int, b varchar)+) AS VALUES+ (row(1, 'red'), row(1, 'string1')),+ (row(2, 'blue'), row(2, 'string2')),+ (row(3, 'green'), row(3, 'string3'))+;++statement ok+drop table struct_values;++statement ok+create table t (c1 struct(r varchar, b int), c2 struct(r varchar, b float)) as values (+ row('red', 2),+ row('blue', 2.3)+);++query ??+select * from t;+----+{r: red, b: 2} {r: blue, b: 2.3}++# TODO: Should be coerced to float+query T+select arrow_typeof(c1) from t;+----+Struct([Field { name: "r", data_type: Utf8, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }, Field { name: "b", data_type: Int32, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }])++query T+select arrow_typeof(c2) from t;+----+Struct([Field { name: "r", data_type: Utf8, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }, Field { name: "b", data_type: Float32, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }])