Skip to content

Commit 58c32cb

Browse files
authored
Add DuckDB struct test and row as alias (#12841)
* add duckdb struct n row Signed-off-by: jayzhan211 <[email protected]> * fmt Signed-off-by: jayzhan211 <[email protected]> --------- Signed-off-by: jayzhan211 <[email protected]>
1 parent d5d9d30 commit 58c32cb

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

datafusion/functions/src/core/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,5 +107,6 @@ pub fn functions() -> Vec<Arc<ScalarUDF>> {
107107
get_field(),
108108
coalesce(),
109109
version(),
110+
r#struct(),
110111
]
111112
}

datafusion/functions/src/core/struct.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ fn struct_expr(args: &[ColumnarValue]) -> Result<ColumnarValue> {
5757
#[derive(Debug)]
5858
pub struct StructFunc {
5959
signature: Signature,
60+
aliases: Vec<String>,
6061
}
6162

6263
impl Default for StructFunc {
@@ -69,6 +70,7 @@ impl StructFunc {
6970
pub fn new() -> Self {
7071
Self {
7172
signature: Signature::variadic_any(Volatility::Immutable),
73+
aliases: vec![String::from("row")],
7274
}
7375
}
7476
}
@@ -81,6 +83,10 @@ impl ScalarUDFImpl for StructFunc {
8183
"struct"
8284
}
8385

86+
fn aliases(&self) -> &[String] {
87+
&self.aliases
88+
}
89+
8490
fn signature(&self) -> &Signature {
8591
&self.signature
8692
}

datafusion/sqllogictest/test_files/struct.slt

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,3 +373,52 @@ You reached the bottom!
373373

374374
statement ok
375375
drop view complex_view;
376+
377+
# Test row alias
378+
379+
query ?
380+
select row('a', 'b');
381+
----
382+
{c0: a, c1: b}
383+
384+
##################################
385+
# Switch Dialect to DuckDB
386+
##################################
387+
388+
statement ok
389+
set datafusion.sql_parser.dialect = 'DuckDB';
390+
391+
statement ok
392+
CREATE TABLE struct_values (
393+
s1 struct(a int, b varchar),
394+
s2 struct(a int, b varchar)
395+
) AS VALUES
396+
(row(1, 'red'), row(1, 'string1')),
397+
(row(2, 'blue'), row(2, 'string2')),
398+
(row(3, 'green'), row(3, 'string3'))
399+
;
400+
401+
statement ok
402+
drop table struct_values;
403+
404+
statement ok
405+
create table t (c1 struct(r varchar, b int), c2 struct(r varchar, b float)) as values (
406+
row('red', 2),
407+
row('blue', 2.3)
408+
);
409+
410+
query ??
411+
select * from t;
412+
----
413+
{r: red, b: 2} {r: blue, b: 2.3}
414+
415+
# TODO: Should be coerced to float
416+
query T
417+
select arrow_typeof(c1) from t;
418+
----
419+
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: {} }])
420+
421+
query T
422+
select arrow_typeof(c2) from t;
423+
----
424+
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 commit comments

Comments
 (0)