apache / apache/datafusion

Adding postgres json binary operators for nested data strcutures

Open
#18,210 1 comment 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?

Hello community,

I have been thinking about adding [Postgres style JSON operators](https://www.postgresql.org/docs/18/functions-json.html#FUNCTIONS-JSON) for nested data structures, mostly for `Struct` and `List`. These operators include:

- `->`, `->>`, `#>>`: Access data field by index/key
- `@>`, `<@`, `?`, `?|`, `?&`: Containment testing
- `||`, `-`, `#-`: Data structure manipulation
- `@?`, `@@`: Predicate testing

### Describe the solution you'd like

Just want to make sure I'm in the right direction.

1. I assume we won't have built-in `json` type in datafusion, so these operators will be implemented directly on `Struct`, `List` and other `json`-like primitives directly, following postgres' semantics of them. I noticed we have VARIANT coming to arrow/datafusion, will we have a new `DataType` for `VARIANT`? If so, it will be good option for input and return type of these operators.
2. At the moment, we don't have support for operators on nested data structure and primitives. If the left input is nested, we will assume the right array is nested too, and perform compare operators recursively: https://github.com/apache/datafusion/blob/531af8e43ae3563da2a3c5ef35b2241d3ce2d621/datafusion/physical-expr/src/expressions/binary.rs#L254-L259 I will need to change this behavior.
3. Some of the operators may create dynamic results if the right input is Array. For example, if the right array of `->` is `["a", "b", "c"]`, it is expected to return 3 different data types in result set which breaks our type system. So for these `>` operators, I'm going to support scalar version only.
4. Also I expected less strict check in https://github.com/apache/datafusion/blob/531af8e43ae3563da2a3c5ef35b2241d3ce2d621/datafusion/physical-expr/src/expressions/binary.rs#L285 because these `>` will create dynamic return types.

Some of the kernels are going to be implemented in `arrow-rs` first, and integrate into datafusion.

Let me know if these changes will make sense, and align with our previous plan if any. And I will start to send pull requests on both repos.

### Describe alternatives you've considered

_No response_

### Additional context

_No response_

Contributor guide

Open the contributing guide

Research direction

Start with datafusion/physical-expr/src/expressions/binary.rs at the referenced comparison and type-checking logic, then review the PostgreSQL JSON operator semantics and the planned arrow-rs kernels. Done requires an agreed operator scope and design for nested values, return types, and integration across Arrow and DataFusion.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
databases
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.