apache / datafusion

Apache DataFusion SQL Query Engine
https://datafusion.apache.org/
Apache License 2.0
6.33k stars 1.2k forks source link

Unparse Struct plan to SQL string #13477

Closed goldmedal closed 16 hours ago

goldmedal commented 3 days ago

Is your feature request related to a problem or challenge?

https://github.com/apache/datafusion/pull/13418 supports unparsing the Array plan. We should do the same thing for the struct type. DataFusion will plan the struct SQL to the corresponding scalar function:

Named Struct

Try to select a struct value:

select {a:1, b:2}

The plan:

Plan: Projection: named_struct(Utf8("a"), Int64(1), Utf8("b"), Int64(2))
  EmptyRelation

Unparse the plan:

SELECT named_struct('a', 1, 'b', 2)

Access Struct field

Given a table, strcut_table with a struct column:

select struct_col.time_field from struct_table

The plan:

Projection: get_field(struct_table.struct_col, Utf8("time_field"))
  TableScan: struct_table

Unparse the plan:

SELECT get_field(struct_table.struct_col, 'time_field') FROM struct_table

Describe the solution you'd like

The SQL should be able to roundtrip for SQL-Plan-SQL.

Describe alternatives you've considered

No response

Additional context

We support two ways to access a struct:

struct_col.time_field

and

struct_col['time_field']

They will be planned to get_fields(struct_col, 'time_field'). I prefer to unparse get_fields to CompoundIdentifier because it's more common in SQL syntax.

delamarch3 commented 2 days ago

take