This is an automated email from the ASF dual-hosted git repository.
JingsongLi pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/paimon.git
The following commit(s) were added to refs/heads/master by this push:
new 09e7a7a56b [format] Report unresolvable JSON string casts instead of a
bare NPE (#9588)
09e7a7a56b is described below
commit 09e7a7a56b0361d9454ef994542043a3deaab0a8
Author: YangJie <[email protected]>
AuthorDate: Fri Sep 4 03:22:37 2026 -0400
[format] Report unresolvable JSON string casts instead of a bare NPE (#9588)
---
.../apache/paimon/format/json/JsonFileReader.java | 8 +++--
.../paimon/format/json/JsonFileFormatTest.java | 35 ++++++++++++++++++++++
2 files changed, 41 insertions(+), 2 deletions(-)
diff --git
a/paimon-format/src/main/java/org/apache/paimon/format/json/JsonFileReader.java
b/paimon-format/src/main/java/org/apache/paimon/format/json/JsonFileReader.java
index d5ecf3dc8f..5004396871 100644
---
a/paimon-format/src/main/java/org/apache/paimon/format/json/JsonFileReader.java
+++
b/paimon-format/src/main/java/org/apache/paimon/format/json/JsonFileReader.java
@@ -234,9 +234,13 @@ public class JsonFileReader extends AbstractTextFileReader
{
case VARCHAR:
return BinaryString.fromString(str);
default:
- BinaryString binaryString = BinaryString.fromString(str);
CastExecutor cast =
CastExecutors.resolve(DataTypes.STRING(), dataType);
- return cast.cast(binaryString);
+ if (cast == null) {
+ // resolve returns null when no rule matches the
target type.
+ throw new UnsupportedOperationException(
+ "Unsupported data type for JSON format: " +
dataType);
+ }
+ return cast.cast(BinaryString.fromString(str));
}
} catch (Exception e) {
return handleParseError(e);
diff --git
a/paimon-format/src/test/java/org/apache/paimon/format/json/JsonFileFormatTest.java
b/paimon-format/src/test/java/org/apache/paimon/format/json/JsonFileFormatTest.java
index 56485da501..5b79fab95b 100644
---
a/paimon-format/src/test/java/org/apache/paimon/format/json/JsonFileFormatTest.java
+++
b/paimon-format/src/test/java/org/apache/paimon/format/json/JsonFileFormatTest.java
@@ -36,18 +36,21 @@ import org.apache.paimon.fs.Path;
import org.apache.paimon.fs.PositionOutputStream;
import org.apache.paimon.options.Options;
import org.apache.paimon.reader.RecordReader;
+import org.apache.paimon.types.DataType;
import org.apache.paimon.types.DataTypes;
import org.apache.paimon.types.RowType;
import org.junit.jupiter.api.Test;
import java.io.IOException;
+import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertThrows;
/** Test for {@link JsonFileFormat}. */
@@ -69,6 +72,38 @@ public class JsonFileFormatTest extends FormatReadWriteTest {
return HadoopCompressionType.NONE.value();
}
+ @Test
+ public void testUnresolvableCastFailsWithClearMessage() throws Exception {
+ JsonFileFormat format =
+ new JsonFileFormat(new FileFormatFactory.FormatContext(new
Options(), 1024, 1024));
+
+ Path testFile = new Path(parent, "unresolvable_cast_" +
UUID.randomUUID() + ".json");
+ try (PositionOutputStream out = fileIO.newOutputStream(testFile,
true)) {
+ out.write("{\"f0\":{\"a\":1}}".getBytes(StandardCharsets.UTF_8));
+ }
+
+ // MULTISET has no cast rule from STRING. A format table is created
without going
+ // through SchemaValidation, so such a column reaches the reader.
+ RowType rowType =
+ RowType.of(
+ new DataType[]
{DataTypes.MULTISET(DataTypes.STRING())},
+ new String[] {"f0"});
+
+ try (RecordReader<InternalRow> reader =
+ format.createReaderFactory(rowType, rowType, new ArrayList<>())
+ .createReader(
+ new FormatReaderContext(
+ fileIO,
+ testFile,
+ fileIO.getFileSize(testFile),
+ null,
+ null))) {
+ assertThatThrownBy(() -> reader.forEachRemaining(row -> {}))
+
.hasRootCauseInstanceOf(UnsupportedOperationException.class)
+ .hasRootCauseMessage("Unsupported data type for JSON
format: MULTISET<STRING>");
+ }
+ }
+
@Test
public void testIgnoreParseErrorsEnabled() throws IOException {
RowType rowType = DataTypes.ROW(DataTypes.INT().notNull(),
DataTypes.STRING());