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 @@ -120,10 +122,26 @@ else if (type instanceof DecimalType decimalType) {
};
}
else if (REAL.equals(type)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Similar logic is also in createMapKeyFunction(Type type)

Copy link
Contributor

Choose a reason for hiding this comment

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

even though they will not break as these, but still worth discussing on how those values supposed to be read back especially considering cases like duplicate map keys of NaN etc., or just leave them as is and that the downstream consumer decide

Copy link
Member

Choose a reason for hiding this comment

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

In other cases, we've preferred to emulate the Hive SerDe behavior. What happens in Hive if you serialize maps with NaN / Infinite / -Infinite keys there, @sug-ghosh ?

return (generator, block, position) -> generator.writeNumber(REAL.getFloat(block, position));
return (generator, block, position) -> {
float value = REAL.getFloat(block, position);
if (!Float.isNaN(value) && !Float.isInfinite(value)) {
generator.writeNumber(value);
}
Comment on lines +127 to +129
Copy link
Contributor

Choose a reason for hiding this comment

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

Float.isFinite() will cover both infinite and NaN, as the comparison of NaN with any Float (MAX_VALUE in this case) will return false

Same apply to Double type handling below

else {
throw new TrinoException(StandardErrorCode.INVALID_JSON_LITERAL, "Invalid value to Insert Real " + value);
Copy link
Contributor

Choose a reason for hiding this comment

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

would prefer to add : to separate the value out more obviously

}
};
}
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);
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