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

Fix casting from varchar to numeric with spaces #23375

Merged
merged 1 commit into from
Sep 12, 2024

Conversation

wendigo
Copy link
Contributor

@wendigo wendigo commented Sep 12, 2024

Java has inconsistent behaviour of the [Primitive].parse* methods:

Long.parseLong(" 1337 ") throws the NumberFormatException, while Float.parseFloat(" 1337 ") parses successfully.

FloatingDecimal class has the following code:

            in = in.trim(); // don't fool around with white space.

Let's not fool around and trim every varchar before parsing.

Fixes #23359

Description

Additional context and related issues

Release notes

( ) This is not user-visible or is docs only, and no release notes are required.
(X) 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`)

Java has inconsistent behaviour of the [Primitive].parse* methods:

Long.parseLong(" 1337 ") throws the NumberFormatException, while
Float.parseFloat(" 1337 ") parses successfully.

FloatingDecimal class has the following code:

```
            in = in.trim(); // don't fool around with white space.
```

Let's not fool around and trim every varchar before parsing.
@cla-bot cla-bot bot added the cla-signed label Sep 12, 2024
@wendigo wendigo requested review from findepi and losipiuk September 12, 2024 07:56
@findepi findepi requested review from martint and removed request for findepi September 12, 2024 08:00
@losipiuk
Copy link
Member

What does standard say about it?

@losipiuk
Copy link
Member

What does standard say about it?

Or postgres at least

@wendigo
Copy link
Contributor Author

wendigo commented Sep 12, 2024

After this change this is consistent postgresql:

postgres=# select CAST(VARCHAR ' 1 ' AS INTEGER)
;
 int4
------
    1
(1 row)

postgres=# select CAST(VARCHAR ' 10' AS INTEGER)
;
 int4
------
   10
(1 row)

postgres=# select CAST(' 10' AS INTEGER)
;
 int4
------
   10
(1 row)

postgres=# select CAST(' 10 ' AS BIGINT)
;
 int8
------
   10
(1 row)

@wendigo
Copy link
Contributor Author

wendigo commented Sep 12, 2024

Screenshot 2024-09-12 at 11 16 56

@wendigo wendigo merged commit 33f7817 into master Sep 12, 2024
103 of 104 checks passed
@wendigo wendigo deleted the serafin/fix-varchar-to-integer-cast branch September 12, 2024 10:55
@github-actions github-actions bot added this to the 458 milestone Sep 12, 2024
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.

Inconsistent whitespace handling in numeric cast from varchar
3 participants