Skip to content

Commit 28cbf06

Browse files
committed
Add type mapping for UNDEFINED
Signed-off-by: Tomoyuki Morita <moritato@amazon.com>
1 parent 0b7ad9f commit 28cbf06

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

integ-test/src/test/java/org/opensearch/sql/calcite/remote/CalcitePPLSpathCommandIT.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,18 @@ public void testSpathAndStats() throws IOException {
184184
verifyDataRows(result, rows(1L, null), rows(1L, "Jane"), rows(1L, "John"));
185185
}
186186

187+
@Test
188+
public void testSpathAndEventStats() throws IOException {
189+
JSONObject result =
190+
executeQuery(
191+
source(
192+
TEST_INDEX_DYNAMIC_FIELDS,
193+
"spath input=json_data | eventstats avg(age) as avg | fields id, name, avg"));
194+
195+
verifySchema(result, schema("id", "bigint"), schema("name", "string"), schema("avg", "double"));
196+
verifyDataRows(result, rows(1, "John", 27.5), rows(2, "Jane", 27.5), rows(3, null, 27.5));
197+
}
198+
187199
@Test
188200
public void testSpathDynamicFieldsExplain() throws IOException {
189201
executeQuery(

opensearch/src/main/java/org/opensearch/sql/opensearch/data/value/OpenSearchExprValueFactory.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import static org.opensearch.sql.data.type.ExprCoreType.STRUCT;
1818
import static org.opensearch.sql.data.type.ExprCoreType.TIME;
1919
import static org.opensearch.sql.data.type.ExprCoreType.TIMESTAMP;
20+
import static org.opensearch.sql.data.type.ExprCoreType.UNDEFINED;
2021
import static org.opensearch.sql.utils.DateTimeFormatters.STRICT_HOUR_MINUTE_SECOND_FORMATTER;
2122
import static org.opensearch.sql.utils.DateTimeFormatters.STRICT_YEAR_MONTH_DAY_FORMATTER;
2223

@@ -133,6 +134,7 @@ public void extendTypeMapping(Map<String, OpenSearchDataType> typeMapping) {
133134
.put(
134135
OpenSearchDataType.of(OpenSearchDataType.MappingType.Binary),
135136
(c, dt) -> new OpenSearchExprBinaryValue(c.stringValue()))
137+
.put(OpenSearchDataType.of(UNDEFINED), (c, dt) -> parseContent(c))
136138
.build();
137139

138140
/** Constructor of OpenSearchExprValueFactory. */
@@ -213,7 +215,7 @@ private ExprValue parse(
213215
}
214216
}
215217

216-
private ExprValue parseContent(Content content) {
218+
private static ExprValue parseContent(Content content) {
217219
if (content.isNumber()) {
218220
if (content.isInt()) {
219221
return new ExprIntegerValue(content.intValue());

opensearch/src/test/java/org/opensearch/sql/opensearch/data/value/OpenSearchExprValueFactoryTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
import org.opensearch.sql.data.model.ExprTupleValue;
5959
import org.opensearch.sql.data.model.ExprValue;
6060
import org.opensearch.sql.data.model.ExprValueUtils;
61+
import org.opensearch.sql.data.type.ExprCoreType;
6162
import org.opensearch.sql.opensearch.data.type.OpenSearchDataType;
6263
import org.opensearch.sql.opensearch.data.type.OpenSearchDateType;
6364
import org.opensearch.sql.opensearch.data.type.OpenSearchTextType;
@@ -122,6 +123,7 @@ class OpenSearchExprValueFactoryTest {
122123
.put(fieldIp, OpenSearchDataType.of(OpenSearchDataType.MappingType.Ip))
123124
.put("geoV", OpenSearchDataType.of(OpenSearchDataType.MappingType.GeoPoint))
124125
.put("binaryV", OpenSearchDataType.of(OpenSearchDataType.MappingType.Binary))
126+
.put("undefinedV", OpenSearchDataType.of(ExprCoreType.UNDEFINED))
125127
.build();
126128
private static final double TOLERANCE = 1E-5;
127129
private final OpenSearchExprValueFactory exprValueFactory =
@@ -948,6 +950,17 @@ public void constructFromInvalidJsonThrowException() {
948950
assertEquals("invalid json: {\"invalid_json:1}.", exception.getMessage());
949951
}
950952

953+
@Test
954+
public void constructUndefined() {
955+
assertAll(
956+
() ->
957+
assertEquals(
958+
stringValue("text"), tupleValue("{\"undefinedV\":\"text\"}").get("undefinedV")),
959+
() -> assertEquals(stringValue("text"), constructFromObject("undefinedV", "text")),
960+
() -> assertEquals(integerValue(1), tupleValue("{\"undefinedV\":1}").get("undefinedV")),
961+
() -> assertEquals(integerValue(1), constructFromObject("undefinedV", 1)));
962+
}
963+
951964
@Test
952965
public void noTypeFoundForMapping() {
953966
assertEquals(nullValue(), tupleValue("{\"not_exist\":[]}").get("not_exist"));

0 commit comments

Comments
 (0)