diff --git a/pinot-common/src/test/java/org/apache/pinot/common/utils/PinotDataTypeTest.java b/pinot-common/src/test/java/org/apache/pinot/common/utils/PinotDataTypeTest.java index 245ea7235dc5..47807d674b6f 100644 --- a/pinot-common/src/test/java/org/apache/pinot/common/utils/PinotDataTypeTest.java +++ b/pinot-common/src/test/java/org/apache/pinot/common/utils/PinotDataTypeTest.java @@ -18,11 +18,13 @@ */ package org.apache.pinot.common.utils; +import com.fasterxml.jackson.core.JsonProcessingException; import java.math.BigDecimal; import java.sql.Timestamp; import java.util.Arrays; import java.util.HashMap; import java.util.Map; +import org.apache.pinot.spi.utils.JsonUtils; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; @@ -220,6 +222,22 @@ public void testJSON() { assertEquals(JSON.convert(new Timestamp(1620324238610L), TIMESTAMP), "1620324238610"); } + @Test + public void testJSONArray() + throws JsonProcessingException { + assertEquals(JSON.convert(new Object[]{false}, BOOLEAN), "[false]"); + assertEquals(JSON.convert(new Object[]{true}, BOOLEAN), "[true]"); // Base64 encoding. + assertEquals(JSON.convert(new Object[]{ + JsonUtils.stringToObject("{\"bytes\":\"AAE=\"}", Map.class), + JsonUtils.stringToObject("{\"map\":{\"key1\":\"value\",\"key2\":null,\"array\":[-5.4,4,\"2\"]}}", + Map.class), + JsonUtils.stringToObject("{\"timestamp\":1620324238610}", Map.class)}, JSON), + "[{\"bytes\":\"AAE=\"},{\"map\":{\"key1\":\"value\",\"key2\":null,\"array\":[-5.4,4,\"2\"]}}," + + "{\"timestamp\":1620324238610}]"); + assertEquals(JSON.convert(new Object[]{}, JSON), "[]"); + assertEquals(JSON.convert(new Object[]{new Timestamp(1620324238610L)}, TIMESTAMP), "[1620324238610]"); + } + @Test public void testObject() { assertEquals(OBJECT.toInt(new NumberObject("123")), 123); diff --git a/pinot-segment-local/src/main/java/org/apache/pinot/segment/local/recordtransformer/DataTypeTransformer.java b/pinot-segment-local/src/main/java/org/apache/pinot/segment/local/recordtransformer/DataTypeTransformer.java index 65019549ece2..df1722b78f1d 100644 --- a/pinot-segment-local/src/main/java/org/apache/pinot/segment/local/recordtransformer/DataTypeTransformer.java +++ b/pinot-segment-local/src/main/java/org/apache/pinot/segment/local/recordtransformer/DataTypeTransformer.java @@ -94,7 +94,12 @@ public GenericRow transform(GenericRow record) { if (value instanceof Object[]) { // Multi-value column Object[] values = (Object[]) value; - source = PinotDataType.getMultiValueType(values[0].getClass()); + // JSON is not standardised for empty json array + if (dest == PinotDataType.JSON && values.length == 0) { + source = PinotDataType.JSON; + } else { + source = PinotDataType.getMultiValueType(values[0].getClass()); + } } else { // Single-value column source = PinotDataType.getSingleValueType(value.getClass());