Skip to content

[BUG] regex command fails to match patterns in array fields beyond first element #4461

Closed as duplicate

Description

@alexey-temnikov

Query Information

PPL Command/Query:

source=opensearch_dashboards_sample_data_logs
| eval response_num=cast(response as int)
| where response_num >= 200 and response_num < 300
| regex tags='success'
| fields tags, response, request

Expected Result:
The query should return all documents where the tags array field contains the string "success" and the response code is between 200-299. Based on the sample data, there are thousands of documents matching this criteria.

Actual Result:
The query returns 0 rows (empty result set), even though documents with tags containing "success" exist in the index.

{
  "datarows": [],
  "total": 0,
  "size": 0
}

The same issue occurs with regex tags='warning', while regex tags='error' returns 761 rows as expected.

Dataset Information

Dataset/Schema Type

  • OpenTelemetry (OTEL)
  • Simple Schema for Observability (SS4O)
  • Open Cybersecurity Schema Framework (OCSF)
  • Custom (OpenSearch Dashboards sample data)

Index Mapping

{
  "mappings": {
    "properties": {
      "tags": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "response": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword"
          }
        }
      },
      "request": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword"
          }
        }
      }
    }
  }
}

Sample Data

{
  "tags": ["success", "info"],
  "response": 200,
  "request": "/opensearch/opensearch-1.0.0.deb"
}

Bug Description

Issue Summary:
The regex command fails to match patterns in array fields when the pattern does not match the first element of the array, even though the pattern exists in subsequent elements.

Steps to Reproduce:

  1. Load the OpenSearch Dashboards sample data (opensearch_dashboards_sample_data_logs)
  2. Execute the following query that works (matches the first array element):
source=opensearch_dashboards_sample_data_logs
| eval response_num=cast(response as int)
| where response_num >= 200 and response_num < 300
| regex tags='error'
| fields tags, response, request

Result: Returns 761 rows where tags = ["error", "info"]

  1. Execute the following query that fails (pattern exists but not as the first element):
source=opensearch_dashboards_sample_data_logs
| eval response_num=cast(response as int)
| where response_num >= 200 and response_num < 300
| regex tags='success'
| fields tags, response, request

Result: Returns 0 rows, even though thousands of documents have tags = ["success", "info"]

  1. Verify data exists:
source=opensearch_dashboards_sample_data_logs
| eval response_num=cast(response as int)
| where response_num >= 200 and response_num < 300
| fields tags, response, request
| head 10

Result: Shows documents with tags = ["success", "info"], ["warning", "info"], etc.

Impact:
This bug makes the regex command unreliable for filtering on array fields, as it only considers the first element of the array. This severely limits the usefulness of the regex command for log analysis and observability use cases where array fields are common (tags, labels, categories, etc.).

Environment Information

OpenSearch Version:
OpenSearch 3.3.0-SNAPSHOT

Additional Details:

  • SQL Plugin version: 3.3.0-SNAPSHOT (built from main branch)
  • The issue appears to be in the Calcite script execution layer

Preliminary Root Cause Analysis

Based on code analysis, the issue is in the CalciteScriptEngine.ScriptDataContext.get() method at:
./opensearch/src/main/java/org/opensearch/sql/opensearch/storage/script/CalciteScriptEngine.java:265

public Object get(String name) {
  // ...
  ScriptDocValues<?> docValue = this.docProvider.get(name);
  if (docValue == null || docValue.isEmpty()) {
    return null;
  }
  
  Object value = docValue.get(0);  // <-- BUG: Only returns first element!
  // ...
  return value;
}

When the regex command is executed on an array field like tags, the script accesses the field via doc values (keyword subfield). The get() method only returns docValue.get(0), which is the first element of the array. The REGEXP_CONTAINS function then only matches against this single value, ignoring all other elements in the array.

Why regex tags='error' works:
Documents with tags = ["error", "info"] have "error" as the first element, so the regex matches.

Why regex tags='success' fails:
Documents with tags = ["success", "info"] have "success" as the first element, but when accessed through doc values in the script, only the first value is returned and compared. However, the actual issue is more subtle - the field might be accessed differently depending on the field type and mapping.

Preliminary Proposed Fix

This is a preliminary analysis and requires further investigation:

The get() method in ScriptDataContext should handle multi-valued fields properly. When a field contains multiple values, the regex matching should either:

  1. Option A (Recommended): Check if the pattern matches ANY element in the array:
public Object get(String name) {
  // ...
  ScriptDocValues<?> docValue = this.docProvider.get(name);
  if (docValue == null || docValue.isEmpty()) {
    return null;
  }
  
  // For multi-valued fields, return all values or a representation
  // that allows regex to match against any element
  if (docValue.size() > 1) {
    // Return a list or iterate through all values
    return docValue; // Let the regex function handle the array
  }
  
  Object value = docValue.get(0);
  // ...
  return value;
}
  1. Option B: Modify the REGEXP_CONTAINS implementation to handle array inputs and match against any element.

  2. Option C: Use getFromSource() instead of doc values for array fields to get the complete array, then match against all elements.

Activity

  1. changed the title [-]regex command fails to match patterns in array fields beyond first element[/-] [+][BUG] `regex` command fails to match patterns in array fields beyond first element[/+] on Oct 8, 2025
  2. added
    bug
    pushdown
    and removed on Oct 9, 2025
  3. moved this from Not Started to Pending in PPL Trackeron Oct 20, 2025
  4. added
    PPL
    on Apr 10, 2026
  5. songkant-aws commented on Apr 10, 2026

    @songkant-aws
    Collaborator

    In the long-term, it should be resolved by this issue: #4984. Current OpenSearch DSL adopts schema-on-read but Calcite SQL is strong schema engine. Even we can fix this specific case by workaround, it won't fix other dynamic field issues.

    I'll move bug label to enhancement.

  6. added
    enhancement
    and removed
    bug
    on Apr 10, 2026
  7. qianheng-aws commented on Apr 10, 2026

    @qianheng-aws
    Collaborator

    Closing as duplicate of #5333 — consolidated umbrella issue for array values not treated as atomic values.

  8. moved this from Pending to Done in PPL Trackeron Apr 10, 2026
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

    PPLenhancementpushdown

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions