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

Conversation

sug-ghosh
Copy link

fixes #20395 #20395

This pr will fix inserting NaN and Infinity into table as Json Format.

Screenshot 2024-12-22 at 7 22 08 PM Screenshot 2024-12-22 at 7 22 24 PM

In hive, if we insert NaN or Infinity in json format table, it would store that as NULL, which readable from Trino. We can go that path also if we want to create table to be successful.

@ebyhr @hashhar

Description

Additional context and related issues

Release notes

( ) This is not user-visible or is docs only, and no release notes are required.
( ) Release notes are required. Please propose a release note for me.
( ) Release notes are required, with the following suggested text:

## Section
* Fix some things. ({issue}`issuenumber`)

Copy link
Member

@ebyhr ebyhr left a comment

Choose a reason for hiding this comment

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

Could you add a test?

@@ -123,7 +123,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

@pettyjamesm pettyjamesm requested a review from zhaner08 January 6, 2025 15:38
Comment on lines +127 to +129
if (!Float.isNaN(value) && !Float.isInfinite(value)) {
generator.writeNumber(value);
}
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

@@ -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 ?

generator.writeNumber(value);
}
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

@hashhar hashhar removed their request for review January 7, 2025 10:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

Hive connector can write numeric NaN and ±infinity with JSON format, but can't read the value
4 participants