How can I have a PPL Query transforming/reducing nested fields

Hello,

I am facing an issue when attempting to work with nested fields in a PPL query, as such fields seem to be detected as “undefined” type.

Is there anything to be changed on index mapping and/or ppl logic to interact with such nested fields ?

Versions : OpenSearch 3.7

Describe the issue: Nested fields appear as “undefined” type in the below query, and they cannot be involved in any kind of transformation/reduction

Configuration:

Index mapping is defined as below

{
“my_index”: {
“mappings”: {
“properties”: {
“description”: {
“type”: “text”
},
“id”: {
“type”: “keyword”
},
“name”: {
“type”: “text”
},
“report_columns”: {
“type”: “nested”,
“properties”: {
“label”: {
“type”: “keyword”
},
“name”: {
“type”: “keyword”
},
“position”: {
“type”: “integer”
}
}
}
}
},
“settings”: { <…> }
}
}

Sample document is

{
“_index”: “my_index”,
“_id”: “sample identifier”,
“_source”: {
“id”: “sample identifier”,
“report_columns”: [
{
“name”: “doctype”,
“label”: “Document Type”,
“position”: 1
},
{
“name”: “amount”,
“label”: “Amount”,
“position”: 2
}
],
“name”: “sample name”,
“description”: “sample description”
}
}

PPL Query that reports nested entries as “undefined”

Query

source = my_index
| eval result = transform(report_columns, x → typeof(x))
| fields id, name, result

Result

id name result
sample identifier sample name [“UNDEFINED”, “UNDEFINED”]

PPL Query that fails interacting with nested entries

Query

source = my_index
| eval result = transform(report_columns, x → concat(x.name, ’ as ', x.label, ''))
| fields id, name, result

Error message

ExecutePPLQuery received unexpected status code 500: Cannot create lambda function, Cannot create lambda function

Expected Result

id name result
sample identifier sample name [“docType as Document Type”, “amount as Amount”]

Relevant Logs or Screenshots: N/A

Kind regards
Denis

Hi Denis,

the UNDEFINED type suggests the lambda in transform doesn’t get a typed struct for the elements of a nested field, which is why x.name can’t be resolved and the lambda fails. The array functions seem to be written for arrays of primitives (or arrays built with array()), not for nested documents. A workaround that should work on 3.7 is to unpack the nested field into rows with expand, build the string per row and collect it again:

```

source = my_index

| expand report_columns

| sort id, report_columns.position

| eval col = concat(report_columns.name, ’ as ', report_columns.label)

| stats list(col) as result by id, name

```

I haven’t run this exact query on 3.7, so treat it as a starting point. If transform should support nested fields directly, it’s worth opening an issue in the opensearch-project/sql repo with your example, since it’s a clean repro.

Ahmed

Hello Ahmed,

I already experimented a strategy based on expand/stats (switching from expand to mvexpand)

But unfortunately this led me to a drawback / edge case in case report_columns is empty/null for a given document in my_index : such documents no longer appear in the results

Example : i have the below documents

Document 1

{
“_index”: “my_index”,
“_id”: “identifier1”,
“_source”: {
“id”: “identifier1”,
“report_columns”: [
{
“name”: “doctype”,
“label”: “Document Type”,
“position”: 1
},
{
“name”: “amount”,
“label”: “Amount”,
“position”: 2
}
],
“name”: “name1”,
“description”: “description1”
}
}

Document 2

{
“_index”: “my_index”,
“_id”: “identifier2”,
“_source”: {
“id”: “identifier2”,
“report_columns”: [],
“name”: “name2”,
“description”: “description2”
}
}

Document 3

{
“_index”: “my_index”,
“_id”: “identifier3”,
“_source”: {
“id”: “identifier3”,
“name”: “name3”,
“description”: “description3”
}
}

Result
(documents standing for ids identifier2 and identifier3 missing due to the empty/null value they have for report_columns.

id name col
identifier1 name1 [ “doctype as Document Type”, “amount as Amount”]

Would there be a hint to ensure that such rows/documents are not silently ignored when leverating mvexpand/stats ?

Kind regards
Denis

Hi Denis,

that’s the difference between the two commands. mvexpand drops rows where the array is empty, but expand keeps them: the 3.7 docs say that for an empty array “the row is retained with the expanded field set to null”. So with expand instead of mvexpand, identifier2 should come through with an empty or null result. The docs only mention empty arrays, though, not documents where the field is missing completely like identifier3.

If identifier3 still disappears, you can add those documents back with append:

```

source = my_index

| expand report_columns

| sort id, report_columns.position

| eval col = concat(report_columns.name, ’ as ', report_columns.label)

| stats list(col) as result by id, name

| append [ source = my_index | where isnull(report_columns) | fields id, name ]

```

Check that the append doesn’t produce duplicates for identifier2, since an empty array may also count as null there. If the transform on nested fields turns out to be a gap rather than a usage issue, it’s worth raising in the opensearch-project/sql repo, since your three documents are a clean repro.

Ahmed

Hi Ahmed,

I was missing the append trick, which helps pertaining rows with a null array, meaning I can work with this workaround for the time being, thanks !

As you suggested, I logged an issue against opensearch-project/sql repository

Kind regards,
Denis