Skip to content

Commit

Permalink
Add DuckDB struct test and row as alias (#12841)
Browse files Browse the repository at this point in the history
* add duckdb struct n row

Signed-off-by: jayzhan211 <jayzhan211@gmail.com>

* fmt

Signed-off-by: jayzhan211 <jayzhan211@gmail.com>

---------

Signed-off-by: jayzhan211 <jayzhan211@gmail.com>
  • Loading branch information
jayzhan211 authored Oct 10, 2024
1 parent d5d9d30 commit 58c32cb
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions datafusion/functions/src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,5 +107,6 @@ pub fn functions() -> Vec<Arc<ScalarUDF>> {
get_field(),
coalesce(),
version(),
r#struct(),
]
}
6 changes: 6 additions & 0 deletions datafusion/functions/src/core/struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -69,6 +70,7 @@ impl StructFunc {
pub fn new() -> Self {
Self {
signature: Signature::variadic_any(Volatility::Immutable),
aliases: vec![String::from("row")],
}
}
}
Expand All @@ -81,6 +83,10 @@ impl ScalarUDFImpl for StructFunc {
"struct"
}

fn aliases(&self) -> &[String] {
&self.aliases
}

fn signature(&self) -> &Signature {
&self.signature
}
Expand Down
49 changes: 49 additions & 0 deletions datafusion/sqllogictest/test_files/struct.slt
Original file line number Diff line number Diff line change
Expand Up @@ -373,3 +373,52 @@ You reached the bottom!

statement ok
drop 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: {} }])

0 comments on commit 58c32cb

Please sign in to comment.