Skip to content

[FEATURE]Add flatten Command to PPL #3030

Closed

Description

@YANG-DB

Is your feature request related to a problem?
OpenSearch Piped Processing Language (PPL) currently lacks a native command to flatten nested objects or arrays in documents. Many datasets, especially those containing JSON objects, have deeply nested fields that are difficult to work with in their raw form. The flatten command will simplify these structures and make it easier to analyze and extract data.

What solution would you like?
Introduce a flatten command in PPL that can handle arrays or nested fields, producing a flattened result that contains all the nested elements at the top level.

Syntax:

source=<data_source> | flatten <nested_field>  | fields <fields_to_select>
  • The flatten command takes a nested array or object field and returns each element as part of a flat structure.

Example Use Cases

  1. Flattening an Array Field
source=my-index  | flatten bridges | fields _time, bridges, city, country

This query flattens the bridges array field.

Example Input:

{
  "_time": "2024-09-13T12:00:00",
  "bridges": [
    {"name": "Tower Bridge", "length": 801},
    {"name": "London Bridge", "length": 928}
  ],
  "city": "London",
  "country": "England"
}

Expected Output:

[
  {
    "_time": "2024-09-13T12:00:00",
    "name": "Tower Bridge",
    "length": 801,
    "city": "London",
    "country": "England"
  },
  {
    "_time": "2024-09-13T12:00:00",
    "name": "London Bridge",
    "length": 928,
    "city": "London",
    "country": "England"
  }
]
  1. Flattening a Nested Object
source=my-index | flatten details | fields _time, details 

This query flattens the details object field.

Example Input:

{
  "_time": "2024-09-13T12:00:00",
  "details": {
    "name": "Alice",
    "age": 30,
    "address": {
      "street": "Main St",
      "city": "New York"
    }
  }
}

Expected Output:

{
  "_time": "2024-09-13T12:00:00",
  "name": "Alice",
  "age": 30,
  "street": "Main St",
  "city": "New York"
}

Additional Considerations

  • The flatten command should work efficiently with large arrays or deeply nested structures.
  • It must handle complex JSON objects where multiple levels of nesting exist.
  • Consider supporting multi-level flattening for more deeply nested fields (e.g., flatten details.address).

Activity

  1. added
    enhancement
    PPL
    and removed on Sep 16, 2024
  2. acarbonetto commented on Jan 30, 2025

    @acarbonetto
    Collaborator

    Note: recommend that we update the proposed syntax as per opensearch-project/opensearch-spark#1024 to only accept STRUCT data types (not ARRAY) and instead recommend that users pipe expand into flatten to explain columns and rows.

  3. YANG-DB commented on Feb 3, 2025

    @YANG-DB
    MemberAuthor

    This command has a slightly updated syntax - see spark ppl flatten for additional details

    ### Description
    Using `flatten` command to flatten a field of type:
    - `struct<?,?>`
    - `array<struct<?,?>>`
    
    
    ### Syntax
    `flatten <field> [As aliasSequence]`
    
    * field: to be flattened. The field must be of supported type.
    * aliasSequence: to be used as aliasSequence for the flattened-output fields. Better to put the aliasSequence in brace if there is more than one field.
  4. LantaoJin commented on Jun 16, 2025

    @LantaoJin
    Member

    resolved by #3747

  5. moved this from Not Started to Done in PPL Trackeron Jun 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    PPLenhancement

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions