Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid writing NaN and Infinity with json format table #24558

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) -> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about REAL type?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'NaN' and 'Infinity' getting casted as Double it seems. It goes through double datatype.

Copy link
Member

@ebyhr ebyhr Dec 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so. Please try CREATE TABLE test_nan WITH (format = 'json') AS SELECT real 'NaN' a;.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay

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));
Expand Down
Loading