|
8 | 8 | import static org.junit.jupiter.api.Assertions.assertAll; |
9 | 9 | import static org.junit.jupiter.api.Assertions.assertEquals; |
10 | 10 | import static org.junit.jupiter.api.Assertions.assertFalse; |
| 11 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
11 | 12 | import static org.junit.jupiter.api.Assertions.assertThrows; |
12 | 13 | import static org.junit.jupiter.api.Assertions.assertTrue; |
13 | 14 | import static org.opensearch.sql.data.model.ExprValueUtils.booleanValue; |
@@ -1101,6 +1102,77 @@ public void constructWithMiddleDotsFieldNameReturnsValue() { |
1101 | 1102 | assertEquals(stringValue("value"), structValue.tupleValue().get("a..b")); |
1102 | 1103 | } |
1103 | 1104 |
|
| 1105 | + @Test |
| 1106 | + public void constructWithTopLevelTrailingDotFieldName() { |
| 1107 | + // Top-level field name "field." should be preserved |
| 1108 | + Map<String, ExprValue> result = tupleValue("{\"field.\":\"value\"}"); |
| 1109 | + assertEquals(stringValue("value"), result.get("field.")); |
| 1110 | + } |
| 1111 | + |
| 1112 | + @Test |
| 1113 | + public void constructWithTopLevelLeadingDotFieldName() { |
| 1114 | + // Top-level field name ".field" should be preserved |
| 1115 | + Map<String, ExprValue> result = tupleValue("{\".field\":\"value\"}"); |
| 1116 | + assertEquals(stringValue("value"), result.get(".field")); |
| 1117 | + } |
| 1118 | + |
| 1119 | + @Test |
| 1120 | + public void constructWithMultipleMalformedFieldsAtSameLevel() { |
| 1121 | + // Multiple malformed fields at same level should all be preserved |
| 1122 | + Map<String, ExprValue> result = |
| 1123 | + tupleValue("{\"structV\":{\".\":\"v1\",\"..\":\"v2\",\"...\":\"v3\"}}"); |
| 1124 | + ExprValue structValue = result.get("structV"); |
| 1125 | + assertEquals(stringValue("v1"), structValue.tupleValue().get(".")); |
| 1126 | + assertEquals(stringValue("v2"), structValue.tupleValue().get("..")); |
| 1127 | + assertEquals(stringValue("v3"), structValue.tupleValue().get("...")); |
| 1128 | + } |
| 1129 | + |
| 1130 | + @Test |
| 1131 | + public void constructWithMalformedFieldContainingValue() { |
| 1132 | + // Malformed field name "a." containing a value should be preserved |
| 1133 | + Map<String, ExprValue> result = tupleValue("{\"structV\":{\"a.\":\"value\"}}"); |
| 1134 | + ExprValue structValue = result.get("structV"); |
| 1135 | + ExprValue aValue = structValue.tupleValue().get("a."); |
| 1136 | + assertNotNull(aValue); |
| 1137 | + assertEquals(stringValue("value"), aValue); |
| 1138 | + } |
| 1139 | + |
| 1140 | + @Test |
| 1141 | + public void constructWithVariousMalformedFieldPatterns() { |
| 1142 | + // Test various malformed field name patterns |
| 1143 | + Map<String, ExprValue> result = |
| 1144 | + tupleValue("{\"structV\":{\"a.b.\":\"v1\",\".a.b\":\"v2\",\"a..b\":\"v3\"}}"); |
| 1145 | + ExprValue structValue = result.get("structV"); |
| 1146 | + assertEquals(stringValue("v1"), structValue.tupleValue().get("a.b.")); |
| 1147 | + assertEquals(stringValue("v2"), structValue.tupleValue().get(".a.b")); |
| 1148 | + assertEquals(stringValue("v3"), structValue.tupleValue().get("a..b")); |
| 1149 | + } |
| 1150 | + |
| 1151 | + @Test |
| 1152 | + public void jsonPathLiteralPreservesFieldName() { |
| 1153 | + // JsonPath.literal() should preserve the exact field name |
| 1154 | + JsonPath path = JsonPath.literal("a..b"); |
| 1155 | + assertEquals(1, path.getPaths().size()); |
| 1156 | + assertEquals("a..b", path.getRootPath()); |
| 1157 | + } |
| 1158 | + |
| 1159 | + @Test |
| 1160 | + public void jsonPathLiteralWithDotOnlyFieldName() { |
| 1161 | + // JsonPath.literal() with "." should create single-element path |
| 1162 | + JsonPath path = JsonPath.literal("."); |
| 1163 | + assertEquals(1, path.getPaths().size()); |
| 1164 | + assertEquals(".", path.getRootPath()); |
| 1165 | + } |
| 1166 | + |
| 1167 | + @Test |
| 1168 | + public void jsonPathFromPathSplitsByDots() { |
| 1169 | + // JsonPath.fromPath() should split by dots for nested paths |
| 1170 | + JsonPath path = JsonPath.fromPath("a.b.c"); |
| 1171 | + assertEquals(3, path.getPaths().size()); |
| 1172 | + assertEquals("a", path.getRootPath()); |
| 1173 | + assertEquals(List.of("b", "c"), path.getChildPath().getPaths()); |
| 1174 | + } |
| 1175 | + |
1104 | 1176 | public Map<String, ExprValue> tupleValue(String jsonString) { |
1105 | 1177 | final ExprValue construct = exprValueFactory.construct(jsonString, false); |
1106 | 1178 | return construct.tupleValue(); |
|
0 commit comments