Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# Issue: https://github.com/opensearch-project/sql/issues/4896
# ArrayIndexOutOfBoundsException when querying index with dot-only field name
#
# Root cause: When a document has a field name that is just "." (a single dot),
# the JsonPath constructor's split("\\.") returns an empty array, causing
# paths.get(0) to crash with ArrayIndexOutOfBoundsException.
#
# This can happen when an index has a disabled object field (enabled: false),
# which allows storing documents without validating inner field names.
#
# Fix: When split returns empty array, treat the original string as a literal
# field name, allowing the data to be returned properly.

setup:
- do:
query.settings:
body:
transient:
plugins.calcite.enabled: true
# Create index with disabled object field
- do:
indices.create:
index: test_disabled_object_4896
body:
mappings:
properties:
log:
type: object
enabled: false
"@timestamp":
type: date
message:
type: text

# Index document with "." field name inside disabled object
# OpenSearch allows this because the object is disabled (no validation of inner fields)
- do:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Change L35-L112 single doc index to bulk index

  - do:
      bulk:
        index: test
        refresh: true
        body:
          - '{"index": {}}'
          - '{"id": 1}'
          - '{"index": {}}'
          - '{"id": 2}'
          - '{"index": {}}'
          - '{"id": 3}'

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated

index:
index: test_disabled_object_4896
id: 1
refresh: true
body:
"@timestamp": "2025-11-26T17:10:00.000Z"
message: "test message"
log:
".": "dot field value"

---
teardown:
- do:
query.settings:
body:
transient:
plugins.calcite.enabled: false
- do:
indices.delete:
index: test_disabled_object_4896

---
"Query index with dot-only field name should return actual value":
- skip:
features:
- headers

# Test 1: Query valid fields - should succeed
# Before fix: This would crash because the entire document is parsed
# After fix: Query succeeds, all fields returned normally
- do:
headers:
Content-Type: 'application/json'
ppl:
body:
query: source=test_disabled_object_4896 | fields @timestamp, message
- match: { "total": 1 }
- match: { "datarows.0.0": "2025-11-26 17:10:00" }
- match: { "datarows.0.1": "test message" }

# Test 2: Query the log field with "." subfield
# Before fix: ArrayIndexOutOfBoundsException crash
# After fix: Returns the log object with the "." field containing actual value
- do:
headers:
Content-Type: 'application/json'
ppl:
body:
query: source=test_disabled_object_4896 | fields log
- match: { "total": 1 }
# The log field should contain the "." subfield with its value
- match: { "datarows.0.0": { ".": "dot field value" } }
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,14 @@ static class JsonPath {
private final List<String> paths;

public JsonPath(String rawPath) {
this.paths = List.of(rawPath.split("\\."));
String[] parts = rawPath.split("\\.");
// If split returns empty array (e.g., "." or ".."), treat the original string as a literal
// field name instead of a path separator
if (parts.length == 0) {
this.paths = List.of(rawPath);
} else {
this.paths = List.of(parts);
}
}

public JsonPath(List<String> paths) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1041,6 +1041,41 @@ public void testPopulateValueRecursive() {
assertEquals(expectedValue, tupleValue);
}

@Test
public void constructWithDotOnlyFieldNameReturnsValue() {
// Field name "." (single dot) should return the actual value, not crash or return null
// This can happen with disabled object fields in OpenSearch
Map<String, ExprValue> result = tupleValue("{\"structV\":{\".\":\"value\"}}");
ExprValue structValue = result.get("structV");
// The "." field should contain the actual value
assertEquals(stringValue("value"), structValue.tupleValue().get("."));
}

@Test
public void constructWithMultipleDotsFieldNameReturnsValue() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

np: Could you verify what if the malformed field is not on top level? E.g., a...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated to have more comprehensive test cases for both UT and yaml test

// Field name ".." (multiple dots) should also return the actual value
Map<String, ExprValue> result = tupleValue("{\"structV\":{\"..\":\"value\"}}");
ExprValue structValue = result.get("structV");
assertEquals(stringValue("value"), structValue.tupleValue().get(".."));
}

@Test
public void constructWithDotFieldAlongsideValidFieldsReturnsAll() {
// Both dot field and valid fields should be returned
Map<String, ExprValue> result =
tupleValue(
"{\"structV\":{\".\":\"dotValue\",\"id\":1,\"state\":\"WA\"},\"stringV\":\"test\"}");

// stringV field should be returned normally
assertEquals(stringValue("test"), result.get("stringV"));

// All fields inside structV should be returned
ExprValue structValue = result.get("structV");
assertEquals(stringValue("dotValue"), structValue.tupleValue().get("."));
assertEquals(integerValue(1), structValue.tupleValue().get("id"));
assertEquals(stringValue("WA"), structValue.tupleValue().get("state"));
}

public Map<String, ExprValue> tupleValue(String jsonString) {
final ExprValue construct = exprValueFactory.construct(jsonString, false);
return construct.tupleValue();
Expand Down
Loading