From a0b12531bbdc2f52546165034aa687d0989ab5ff Mon Sep 17 00:00:00 2001 From: sugghosh Date: Sun, 22 Dec 2024 13:50:47 +0530 Subject: [PATCH] Avoid writing NaN and Infinity with json format table --- .../hive/formats/line/json/JsonSerializer.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/trino-hive-formats/src/main/java/io/trino/hive/formats/line/json/JsonSerializer.java b/lib/trino-hive-formats/src/main/java/io/trino/hive/formats/line/json/JsonSerializer.java index 40debc88f4d0..c26a0bb27ab4 100644 --- a/lib/trino-hive-formats/src/main/java/io/trino/hive/formats/line/json/JsonSerializer.java +++ b/lib/trino-hive-formats/src/main/java/io/trino/hive/formats/line/json/JsonSerializer.java @@ -21,6 +21,8 @@ import io.trino.hive.formats.line.Column; import io.trino.hive.formats.line.LineSerializer; import io.trino.spi.Page; +import io.trino.spi.StandardErrorCode; +import io.trino.spi.TrinoException; import io.trino.spi.block.Block; import io.trino.spi.block.SqlMap; import io.trino.spi.block.SqlRow; @@ -123,7 +125,16 @@ else if (REAL.equals(type)) { return (generator, block, position) -> generator.writeNumber(REAL.getFloat(block, position)); } else if (DOUBLE.equals(type)) { - return (generator, block, position) -> generator.writeNumber(DOUBLE.getDouble(block, position)); + return (generator, block, position) -> { + Double value = DOUBLE.getDouble(block, position); + System.out.println(value); + if (!Double.isNaN(value) && !Double.isInfinite(value)) { + generator.writeNumber(value); + } + else { + throw new TrinoException(StandardErrorCode.INVALID_JSON_LITERAL, "Invalid value to Insert " + value); + } + }; } else if (DATE.equals(type)) { return (generator, block, position) -> generator.writeString(HiveFormatUtils.formatHiveDate(block, position));