datafusion icon indicating copy to clipboard operation
datafusion copied to clipboard

Create `Struct` table with explicit type and name

Open jayzhan211 opened this issue 1 year ago • 2 comments

Is your feature request related to a problem or challenge?

Currently we can build struct table like this

statement ok
create table t as values (struct(1, 'a')), (struct(2, 'b'));

query ?
select * from t;
----
{c0: 1, c1: a}
{c0: 2, c1: b}

If we want to name them, we need to build with named_struct

statement ok
create table t as values (named_struct('number', 1, 'varchar', 'a')), (named_struct('number', 2, 'varchar', 'b'));

query ?
select * from t;
----
{number: 1, varchar: a}
{number: 2, varchar: b}

I hope we can build the struct table with pre-defined type and name

statement error DataFusion error: This feature is not implemented: Unsupported SQL type Custom\(ObjectName\(\[Ident \{ value: "struct", quote_style: None \}\]\), \["n", "int", "s", "varchar"\]\)
create table t (s struct(n int, s varchar)) as values (struct(1, 'a')), (struct(2, 'b'));

query ?
select * from t;
----
{n: 1, s: a}
{n: 2, s: b}

Describe the solution you'd like

No response

Describe alternatives you've considered

No response

Additional context

Similar to DuckDB https://duckdb.org/docs/sql/data_types/struct#creating-structs-with-the-row-function

jayzhan211 avatar Apr 24 '24 06:04 jayzhan211

i would like to work on this

duongcongtoai avatar Apr 27 '24 05:04 duongcongtoai

1st item:

create table t (s struct(n int, s varchar))

I think upstream sqlparser lib already support similar syntax

create table t (s struct<n int, s varchar>);

But only bigquery dialect support this syntax. However in this PR where struct syntax is is added they also decided to support this syntax for both Bigquery and GenericDialect, and i think they forgot to add | GenericDialect for the column definition parsing logic here

Ticket to follow: https://github.com/sqlparser-rs/sqlparser-rs/issues/1240

duongcongtoai avatar Apr 28 '24 09:04 duongcongtoai