apache / apache/datafusion

Unparse Map plan to SQL string

Open
#13,478 2 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
Rust
Stars
9.3k
Forks
2.4k
Avg merge
3d 7h
Merged PRs (30d)
344

Description

### Is your feature request related to a problem or challenge?

It's similar to https://github.com/apache/datafusion/issues/13477. We use the scalar function to handle the Map type but we should unparse it back to the right SQL.
## Map Literal
The SQL for creating a MAP:
``` sql
select MAP {'a': 1, 'b': 2}
```
The plan:
```
Plan: Projection: map(make_array(Utf8("a"), Utf8("b")), make_array(Int64(1), Int64(2)))
EmptyRelation
```
Unparse the plan:
```sql
SELECT map(make_array('a', 'b'), make_array(1, 2))
```
## Access a MAP
The SQL for accessing a MAP:
```sql
select map_col['a'] from (select MAP {'a': 1, 'b': 2} as map_col)
```
The plan:
```
Projection: get_field(map_col, Utf8("a"))
Projection: map(make_array(Utf8("a"), Utf8("b")), make_array(Int64(1), Int64(2))) AS map_col
EmptyRelation
```
Unparse the plan
```sql
SELECT get_field(map_col, 'a') FROM (SELECT map(make_array('a', 'b'), make_array(1, 2)) AS map_col)
```

### Describe the solution you'd like

The SQL should be able to roundtrip for SQL-Plan-SQL.
- The `map` with `make_array` function should be unparsed to `ast::Expr::Map`
- The `get_fields` for MAP should be unparsed to `ast::Expr::MapAccess`

### Describe alternatives you've considered

_No response_

### Additional context

_No response_

Contributor guide

Open the contributing guide

Research direction

Start by tracing the plan unparser for scalar functions, especially map/make_array and get_field or get_fields, then inspect the SQL AST variants ast::Expr::Map and ast::Expr::MapAccess. The work is done when MAP literals and MAP access unparse to those AST forms and the resulting SQL roundtrips through SQL-Plan-SQL as shown in the examples.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust, sql
Domain
databases
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.