Skip to content

Support flatten command with Calcite - #3747

Merged
penghuo merged 15 commits into
opensearch-project:mainfrom
yuancu:cmd-flatten
Jun 11, 2025
Merged

penghuo merged 15 commits into
opensearch-project:mainfrom
yuancu:cmd-flatten

Conversation

@yuancu

@yuancu yuancu commented Jun 8, 2025 •

Copy link
Copy Markdown
Collaborator

Description

Implement flatten command that flattens a nested struct / object field into separate fields in a document.

Flatten extracts the fields in a struct into separate other fields in the document:

E.g. For

{
  "name": "Jack",
  "address": {
    "state": "Oregon",
    "city": "Portland"
  }
}

Flattening address will result in

{
  "name": "Jack",
  "address": {
    "state": "Oregon",
    "city": "Portland"
  },
  "state": "Oregon",
  "city": "Portland"
}

Related Issues

Resolves #3712

Check List

  • New functionality includes testing.
  • New functionality has been documented.
  • New functionality has javadoc added.
  • New functionality has a user manual doc added.
  • API changes companion pull request created.
  • Commits are signed per the DCO using --signoff.
  • Public documentation issue/PR created.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

yuancu added 4 commits June 6, 2025 17:01
Signed-off-by: Yuanchun Shen <[email protected]>
Signed-off-by: Yuanchun Shen <[email protected]>
Signed-off-by: Yuanchun Shen <[email protected]>
Signed-off-by: Yuanchun Shen <[email protected]>
Comment thread docs/user/ppl/cmd/flatten.rst Resolved
Comment thread docs/user/ppl/cmd/flatten.rst Resolved
Comment thread docs/user/ppl/cmd/appendcol.rst Resolved
LantaoJin
LantaoJin previously approved these changes Jun 9, 2025

@LantaoJin LantaoJin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, waiting CI pass

@LantaoJin LantaoJin added calcite calcite migration releated PPL Piped processing language labels Jun 9, 2025
{
"mappings": {
"properties": {
"message": {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

message is Json Object, why index as nested field. Usually, Json Array is indexed as nested field.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, using object here is more clear. Fixed.

However, the boundary between array and object is blurry in OpenSearch. Both object and array fields can store a struct or an array of structs. Both nested and object works in this case.

Comment thread docs/user/ppl/cmd/flatten.rst Outdated

Note that ``flatten`` does not work on arrays. Please use ``expand`` command
to expand an array field into multiple rows instead. If the field is an nested
array of structs, only the first element of the array will be flattened.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the field is an nested array of structs,

Why work on Array of structs if flatten does not work on Array?

@yuancu yuancu Jun 10, 2025 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's because although not expected, an object/nested field can store arrays. I have updated the doc to make it easier to understand.

* <p>The flatten command is used to flatten a struct field into multiple fields. This
* implementation simply projects the flattened fields and renames them according to the provided
* aliases or the field names in the struct. This is possible because the struct / object field
* are always read in a flattened manner in OpenSearch.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could u explain flattened manner in OpenSearch? what is expected result from OpenSearch

@yuancu yuancu Jun 10, 2025 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flatten extracts the fields in a struct into separate other fields in the document:

E.g. For

{"name": "Jack", "address": {"state": "Oregon", "city": "Portland"}}

Flattening address will result in

{"name": "Jack", "address": {"state": "Oregon", "city": "Portland"}, "state": "Oregon", "city": "Portland"}

(Also copied to PR description)

Signed-off-by: Yuanchun Shen <[email protected]>
penghuo
penghuo previously approved these changes Jun 10, 2025
Signed-off-by: Yuanchun Shen <[email protected]>
* @return RelNode representing the visited logical plan
*/
@Override
public RelNode visitFlatten(Flatten node, CalcitePlanContext context) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems the implementation relies on the flattened nested fields in the RelRowType, but it may not work for PPL:

source = index | fields message| flatten message

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It fails this test case indeed. Trying to fix it.

@qianheng-aws
qianheng-aws self-requested a review June 10, 2025 07:31

@qianheng-aws qianheng-aws left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need test for this case:

source = index | fields message| flatten message

@yuancu

yuancu commented Jun 10, 2025

Copy link
Copy Markdown
Collaborator Author

Need test for this case:

source = index | fields message| flatten message

We need complete support for typed arrays and structs in order to make it work in such scenarios.

I added an ignored test case and left a TODO item, saying that we'll correct it after fixing #3459 and #3751

Comment thread docs/user/ppl/cmd/flatten.rst Outdated

Limitations
===========
* ``flatten`` after ``fields`` is not supported under the current

@LantaoJin LantaoJin Jun 10, 2025 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change to:

flatten command may not work as expected when its flattened fields are invisible.
For example in query source=my-index | fields message | flatten message,
the flatten message command doesn't work since some flattened fields such as message.info and message.author after command fields message are invisible.
As an alternative, you can change to source=my-index | flatten message

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Signed-off-by: Yuanchun Shen <[email protected]>
@penghuo
penghuo enabled auto-merge (squash) June 10, 2025 14:15
@yuancu
yuancu requested a review from qianheng-aws June 10, 2025 14:27
@LantaoJin
LantaoJin dismissed qianheng-aws’s stale review June 11, 2025 00:37

Already addressed.

@penghuo
penghuo merged commit a0ab339 into opensearch-project:main Jun 11, 2025
penghuo pushed a commit that referenced this pull request Jun 16, 2025
* WIP: Implementing flatten

Signed-off-by: Yuanchun Shen <[email protected]>

* Update flatten IT

Signed-off-by: Yuanchun Shen <[email protected]>

* Support flatten with aliases

Signed-off-by: Yuanchun Shen <[email protected]>

* Document flatten command

Signed-off-by: Yuanchun Shen <[email protected]>

* Improve docs for flatten

Signed-off-by: Yuanchun Shen <[email protected]>

* Fix unix test for flatten command

Signed-off-by: Yuanchun Shen <[email protected]>

* Remove redundant *from 3.1.0* tag in appendcol's doc

Signed-off-by: Yuanchun Shen <[email protected]>

* Unify command doc's language

Signed-off-by: Yuanchun Shen <[email protected]>

* Revert "Unify command doc's language"

This reverts commit 8750e2f.

Signed-off-by: Yuanchun Shen <[email protected]>

* Remove since from flatten's doc

Signed-off-by: Yuanchun Shen <[email protected]>

* Update doc of flatten command

Signed-off-by: Yuanchun Shen <[email protected]>

* Test flatten null fields

Signed-off-by: Yuanchun Shen <[email protected]>

* Add an ignored test case and limitation explainations for flatten after fields

Signed-off-by: Yuanchun Shen <[email protected]>

* Rephrase flatten limitations

Signed-off-by: Yuanchun Shen <[email protected]>

---------

Signed-off-by: Yuanchun Shen <[email protected]>
Signed-off-by: xinyual <[email protected]>
@yuancu
yuancu deleted the cmd-flatten branch August 25, 2025 03:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

calcite calcite migration releated PPL Piped processing language

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE] Support flatten command with Calcite

4 participants