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

sql/parser: avoid float -> unsigned conversions #15847

Merged
merged 1 commit into from
May 10, 2017
Merged

sql/parser: avoid float -> unsigned conversions #15847

merged 1 commit into from
May 10, 2017

Conversation

tamird
Copy link
Contributor

@tamird tamird commented May 10, 2017

While investigating this, I discovered that our current round
implementation is quite dubious, and does not rely on any referenced
source material or any material that I could find. I also found that
Postgres does not implement 2-ary round where the first argument is
a float.

The above is addressed by:

  • replacing round(float) with a transcription of Postgres' rint.
  • replacing round(float, int) with an implementation that round-trips
    through apd. This is likely much slower, but likely correct.

FOR DOCS: in addition to the two points above, mention in docs that users that wish to have simple, straightforward results with floats are encouraged to use ceil() or floor() if applicable in their code, or possibly floor(x+0.5) if they want "round to nearest".

Updates #14405.

cc @vielmetti

@tamird tamird requested review from maddyblue and knz May 10, 2017 16:14
@cockroach-teamcity
Copy link
Member

This change is Reviewable

@tamird tamird changed the title *: do not convert float point numbers to unsigned integers sql/parser: do not convert float point numbers to unsigned integers May 10, 2017
While investigating this, I discovered that our current `round`
implementation is quite dubious, and does not rely on any referenced
source material or any material that I could find. I also found that
Postgres does not implement 2-ary `round` where the first argument is
a float.

The above is addressed by:
- replacing `round(float)` with a transcription of Postgres' `rint`.
- replacing `round(float, int)` with an implementation that round-trips
  through apd. This is likely much slower, but likely correct.

Updates #14405.
@tamird tamird changed the title sql/parser: do not convert float point numbers to unsigned integers sql/parser: avoid float -> unsigned conversions May 10, 2017
@knz
Copy link
Contributor

knz commented May 10, 2017

LGTM, but Matt should chime in as well.

What do you mean pg does not support this function? Do you mean it's not supported on floats? I'm pretty sure most SQL databases do support a round function with some semantics, and when I was looking at this last at least Sybase, Oracle and MSSQL were pretty explicit about what they are doing.

@tamird
Copy link
Contributor Author

tamird commented May 10, 2017

What do you mean pg does not support this function? Do you mean it's not supported on floats?

Exactly, SELECT ROUND(1.5::float, 1) on Postgres is not supported.

@knz
Copy link
Contributor

knz commented May 10, 2017

It would not hurt me to simply remove the round overload for floats altogether. People who really care about rounding on floats usually know what they want, and we support floor and ceil already.

@tamird
Copy link
Contributor Author

tamird commented May 10, 2017

Understood, yet that would be a breaking change.

@knz
Copy link
Contributor

knz commented May 10, 2017

Ack. Your current change moves us forward anyway.

@knz knz added the docs-todo label May 10, 2017
@maddyblue
Copy link
Contributor

:lgtm:


Review status: 0 of 4 files reviewed at latest revision, all discussions resolved, all commit checks successful.


Comments from Reviewable

@tamird tamird added the 1.0.1 label May 10, 2017
@tamird
Copy link
Contributor Author

tamird commented May 10, 2017

We should cherry pick this into 1.0.1; the previous behaviour was definitely incorrect.

@tamird tamird merged commit a8f1b09 into cockroachdb:master May 10, 2017
@tamird tamird deleted the dont-float-unsigned branch May 10, 2017 20:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants