Skip to content

(Calcite PPL) spath command dynamic columns support #4307

Description

@ykmr1224

Support dynamic columns use case with spath PPL command.

Example: following spath command will parse doc attribute and extract all the attributes as columns.

source=idx
| spath input=doc

Each item could be any type since JSON attribute could be string/number/array/null

Activity

  1. added theissue type on Sep 16, 2025
  2. added
    PPL
    calcite
    on Sep 16, 2025
  3. changed the title [-](Calcite PPL) Spath dynamic columns support[/-] [+](Calcite PPL) `spath` command dynamic columns support[/+] on Sep 23, 2025
  4. penghuo commented on Dec 12, 2025

    @penghuo
    Collaborator

    source=idx
    | spath input=doc

    @ykmr1224 if we add change requirement of spath as

    • input: mandatory. The field to scan for JSON data.
    • output: The destination field that the data will be loaded to. Default: value of path.
      • optional, if path exist.
      • mandatory, if path does not exist.
    • path: The path of the data to load for the object.
      • optional if output exist.
      • mandatory if output does not exist.

    then PPL does not need to support dynamic columns. does it sounds as workaround?

  5. penghuo commented on Feb 3, 2026

    @penghuo
    Collaborator

    Proposal - Remove dynamic-column expansion

    • Add extract-all-to-map mode for spath that flattens JSON into a single map field. Remove dynamic-column expansion entirely.

    Behavior

    • Path-based (unchanged): spath input=f [output=o] path=p → o = json_extract(f, p); default output = path.
    • Extract-all: spath input=f output=o or spath input=f → o = json_extract_all(f); default o = f.
    • No dynamic columns: input-only no longer expands columns; it produces a map instead.

    Flattening & Merging

    • Dotted keys for objects (a.b = 1).
    • Arrays use {} suffix (list{} = [1,2]; list{}.x = [1,2]).
    • Duplicate logical keys merge into arrays (e.g., a.b: [1,2]).
    • Nulls preserved; key order not guaranteed.

    Types

    • Return map<string,string>: stringify all values for consistency now.
    • Consider map<string,any> as a future enhancement.

    Compatibility

    • Syntax remains compatible.
    • Semantic change: spath input=f now yields a map in f (not dynamic columns). Clearly document removal of dynamic-field expansion and the new default.

    Corner Cases

    • Invalid or empty JSON → null map.
    • Large payloads: still runs on coordinator; filter before spath.

    Example

    • Input: {"body": "{\"a\":1, \"b\":2}"}
      • | spath input=body output=body → body = {"a":"1","b":"2"}
  6. anasalkouz commented on Feb 6, 2026

    @anasalkouz
    Member

    I agree with @penghuo proposal. Let's remove the Dynamic field expansion and use output param. The only concern I have in this proposal is

    default o = f.

    Because this means it will override input f, which might not be the intent of the customer. I would say let's avoid this default behavior

  7. dai-chen commented on Feb 10, 2026

    @dai-chen
    Collaborator

    Proof of Concept

    Summary

    Design Decisions

    Design decisions made based on the proposal above:

    • Path-based mode (spath input=f path=p) is unchanged and continue to rewrites to json_extract(f, p).
    • Extract-all mode rewrites to the existing internal function json_extract_all(...), which returns map<string, string>.
      • Flattening rules:
        • nested objects → dotted keys (user.age, user.name)
        • arrays → {} suffix (tags{}, users{}.name)
        • duplicate logical keys → merge into arrays (c{}.b = [2, 3])
      • Accessing extracted values: use element_at(map, key) (Calcite ITEM) for now.

    Implementation Plan

    High level implementation plan:

    1. Revert the merged dynamic-column spath PRs after OS 3.4
    2. Add auto extract mode to spath (when path is omitted) by rewriting to json_extract_all function, returning a single map column (with the flattening rules above).
    3. Improve map access ergonomics [TBD] (keep element_at(map, key) as baseline; consider bracket access and/or PartiQL-like navigation as follow-ups).

    Open Questions

    1. Default output in extract-all mode: when path is omitted and output is not specified, the result currently overwrites input field. @anasalkouz suggested avoiding slient overwrite.
    2. Null handling: json_extract_all currently drops null-valued keys. Needs fix if "nulls preserved" is a requirement.
    3. Ergonomic map access syntax: element_at(map, key) is correct but verbose. Potential UX improvements:
      • (A) SQL style bracket access: source=test_flatten | spath input=doc | eval age = doc['user.age'] + 1;
      • (B) PartiQL-like path navigation: source=test_flatten | spath input=doc | eval age = doc.user.age + 1;

    Proof of Concept Details

    0. Test Data

    opensearchsql> source=test_spath;
    fetched rows / total rows = 5/5
    +--------------------------------------------------------------------------+------------------+
    | doc                                                                      | case             |
    |--------------------------------------------------------------------------+------------------|
    | {"user":{"name":"John","age":30},"active":true}                          | dotted_keys      |
    | {"tags":["java","sql","opensearch"]}                                     | array_suffix     |
    | {"users":[{"name":"John","role":"admin"},{"name":"Jane","role":"user"}]} | array_of_objects |
    | {"a":{"b":1},"c":[{"b":2},{"b":3}]}                                      | duplicate_merge  |
    | {"x":1,"y":null,"z":"hello"}                                             | nulls_preserved  |
    +--------------------------------------------------------------------------+------------------+
    

    1. Path-based extraction (unchanged)

    opensearchsql> source=test_spath | spath input=doc path=user.age;
    fetched rows / total rows = 5/5
    +--------------------------------------------------------------------------+------------------+------------+
    | doc                                                                      | case             | user.age   |
    |--------------------------------------------------------------------------+------------------+------------|
    | {"user":{"name":"John","age":30},"active":true}                          | dotted_keys      | 30         |
    | {"tags":["java","sql","opensearch"]}                                     | array_suffix     | null       |
    | {"users":[{"name":"John","role":"admin"},{"name":"Jane","role":"user"}]} | array_of_objects | null       |
    | {"a":{"b":1},"c":[{"b":2},{"b":3}]}                                      | duplicate_merge  | null       |
    | {"x":1,"y":null,"z":"hello"}                                             | nulls_preserved  | null       |
    +--------------------------------------------------------------------------+------------------+------------+
    

    2. Extract-all to map (new)

    When path is omitted, spath flattens the entire JSON into a map<string, any> column instead of expanding dynamic columns.

    As proposed above, we follow flattening rules as below:

    • Dotted keys for nested objects: user.name, user.age
    • {} suffix for arrays: tags{}, users{}.name
    • Duplicate logical keys merge into arrays: c{}.b = [2, 3]

    Note: Null values (y: null) are currently dropped by json_extract_all function. This needs to be fixed to match the "nulls preserved" requirement.

    opensearchsql> source=test_spath | spath input=doc;
    fetched rows / total rows = 5/5
    +------------------+-----------------------------------------------------------------------+
    | case             | doc                                                                   |
    |------------------+-----------------------------------------------------------------------|
    | dotted_keys      | {'user.age': 30, 'user.name': 'John', 'active': True}                 |
    | array_suffix     | {'tags{}': ['java', 'sql', 'opensearch']}                             |
    | array_of_objects | {'users{}.name': ['John', 'Jane'], 'users{}.role': ['admin', 'user']} |
    | duplicate_merge  | {'a.b': 1, 'c{}.b': [2, 3]}                                           |
    | nulls_preserved  | {'x': 1, 'z': 'hello'}                                                |
    +------------------+-----------------------------------------------------------------------+
    

    3. Accessing map values after extract-all

    Use SQL standard element_at(map, key) (wraps Calcite's SqlStdOperatorTable.ITEM) to access individual keys from the map:

    opensearchsql> source=test_spath
    .............. | spath input=doc output=result
    .............. | fields result
    .............. | eval age = element_at(result, 'user.age') + 1;
    fetched rows / total rows = 5/5
    +-----------------------------------------------------------------------+-------+
    | result                                                                | age   |
    |-----------------------------------------------------------------------+-------|
    | {'user.age': 30, 'user.name': 'John', 'active': True}                 | 31.0  |
    | {'tags{}': ['java', 'sql', 'opensearch']}                             | null  |
    | {'users{}.name': ['John', 'Jane'], 'users{}.role': ['admin', 'user']} | null  |
    | {'a.b': 1, 'c{}.b': [2, 3]}                                           | null  |
    | {'x': 1, 'z': 'hello'}                                                | null  |
    +-----------------------------------------------------------------------+-------+
    

    For path-based extraction, the existing approach still works and is simpler when you know the target field:

    opensearchsql> source=test_spath
    .............. | spath input=doc output=result <b>path=user.age</b>
    .............. | fields result
    .............. | eval age = result + 1;
    fetched rows / total rows = 5/5
    +----------+-------+
    | result   | age   |
    |----------+-------|
    | 30       | 31.0  |
    | null     | null  |
    | null     | null  |
    | null     | null  |
    | null     | null  |
    +----------+-------+
    

    Appendix

    Curl commands:

    curl -XDELETE 'localhost:9200/test_spath'
    
    curl -XPUT 'localhost:9200/test_spath' -H 'Content-Type: application/json' -d '{
      "mappings": {"properties": {"doc": {"type": "keyword"}}}
    }'
    
    curl -XPOST 'localhost:9200/_bulk?refresh=true' -H 'Content-Type: application/json' -d '
    {"index": {"_index": "test_spath", "_id": "1"}}
    {"case": "dotted_keys", "doc": "{\"user\":{\"name\":\"John\",\"age\":30},\"active\":true}"}
    {"index": {"_index": "test_spath", "_id": "2"}}
    {"case": "array_suffix", "doc": "{\"tags\":[\"java\",\"sql\",\"opensearch\"]}"}
    {"index": {"_index": "test_spath", "_id": "3"}}
    {"case": "array_of_objects", "doc": "{\"users\":[{\"name\":\"John\",\"role\":\"admin\"},{\"name\":\"Jane\",\"role\":\"user\"}]}"}
    {"index": {"_index": "test_spath", "_id": "4"}}
    {"case": "duplicate_merge", "doc": "{\"a\":{\"b\":1},\"c\":[{\"b\":2},{\"b\":3}]}"}
    {"index": {"_index": "test_spath", "_id": "5"}}
    {"case": "nulls_preserved", "doc": "{\"x\":1,\"y\":null,\"z\":\"hello\"}"}
    '
    
  8. penghuo commented on Feb 11, 2026

    @penghuo
    Collaborator

    Ergonomic map access syntax

    +1 on eval age = doc.user.age

    Question, Does map<string, any> works fine with our type coercion implemntation. I suggest rewrite our existing type coercion IT to use spath to validate it.

  9. moved this from Not Started to Done in PPL Trackeron Feb 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

PPLcalcite

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions