druid icon indicating copy to clipboard operation
druid copied to clipboard

feat: json_merge expression and sql function

Open lkm opened this issue 1 year ago • 5 comments

Description

This add a new json_merge expression which takes two or more arguments to merge into a single json structure. It also added the corresponding SQL function. This is useful for transforming data in druid, for instance I use it for flattening arrays of objects using fold, e.g. fold((a, acc) -> json_merge(acc, json_object(json_value(a, '$.key'), json_value(a, '$.value'))), arrayofkv, json_object())

Fixed the bug ...

Renamed the class ...

Added a forbidden-apis entry ...

Release note


Key changed/added classes in this PR
  • json_merge expression function
  • json_merge sql function

This PR has:

  • [x] been self-reviewed.
    • [ ] using the concurrency checklist (Remove this item if the PR doesn't have any relation to concurrency.)
  • [x] added documentation for new or modified features or behaviors.
  • [ ] a release note entry in the PR description.
  • [ ] added Javadocs for most classes and all non-trivial methods. Linked related entities via Javadoc links.
  • [ ] added or updated version, license, or notice information in licenses.yaml
  • [ ] added comments explaining the "why" and the intent of the code wherever would not be obvious for an unfamiliar reader.
  • [ ] added unit tests or modified existing tests to cover new code paths, ensuring the threshold for code coverage is met.
  • [ ] added integration tests.
  • [x] been tested in a test Druid cluster.

lkm avatar Feb 19 '24 09:02 lkm

btw have you looked at https://github.com/apache/druid/pull/15521 (flatting arrays of objects using UNNEST and JSON_QUERY_ARRAY) ?

abhishekagarwal87 avatar Feb 20 '24 04:02 abhishekagarwal87

btw have you looked at #15521 (flatting arrays of objects using UNNEST and JSON_QUERY_ARRAY) ?

That looks great, but it's the other way I'm going. Using that example I would end up with {"Honda":"civic","Toyota":"prius"....} and all type and weight being overwritten by the right-most value. It's a specific use-case alright, but json_merge has many other applications of course.

lkm avatar Feb 20 '24 07:02 lkm

You are right. This function has broader usage. I think when you say "arrays of objects", you don't mean arrays in a single record but rather multiple records. Since fold is an aggregator.

abhishekagarwal87 avatar Feb 20 '24 11:02 abhishekagarwal87

This pull request has been marked as stale due to 60 days of inactivity. It will be closed in 4 weeks if no further activity occurs. If you think that's incorrect or this pull request should instead be reviewed, please simply write any comment. Even if closed, you can still revive the PR at any time or discuss it on the [email protected] list. Thank you for your contributions.

github-actions[bot] avatar Apr 21 '24 00:04 github-actions[bot]

This pull request has been marked as stale due to 60 days of inactivity. It will be closed in 4 weeks if no further activity occurs. If you think that's incorrect or this pull request should instead be reviewed, please simply write any comment. Even if closed, you can still revive the PR at any time or discuss it on the [email protected] list. Thank you for your contributions.

github-actions[bot] avatar Jul 02 '24 00:07 github-actions[bot]

This pull request/issue has been closed due to lack of activity. If you think that is incorrect, or the pull request requires review, you can revive the PR at any time.

github-actions[bot] avatar Jul 30 '24 00:07 github-actions[bot]

I've rebased this so we can revive now

lkm avatar Aug 15 '24 15:08 lkm

@lkm - are you going to revive this PR?

abhishekagarwal87 avatar Sep 16 '24 10:09 abhishekagarwal87

@abhishekagarwal87 I don't think I have the permissions to do that. I been running this in prod for a while and happy with the functionality

lkm avatar Sep 16 '24 11:09 lkm

You might have to open a separate PR since I can't figure out a way to re-open it myself.

abhishekagarwal87 avatar Sep 16 '24 12:09 abhishekagarwal87

FWIW the PR looks good to me in the current shape so we should be able to merge your PR pretty quick.

abhishekagarwal87 avatar Sep 16 '24 12:09 abhishekagarwal87

Thanks. I've created a new one here: https://github.com/apache/druid/pull/17081

lkm avatar Sep 16 '24 12:09 lkm