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

nail down semantics of arithmetic expressions and numeric literals #423

Closed
gavinking opened this issue Jul 1, 2023 · 1 comment
Closed
Projects

Comments

@gavinking
Copy link
Contributor

gavinking commented Jul 1, 2023

The specification leaves the implementor guessing as to the semantics of numeric expression, and the type assigned to numeric literals.

§4.6.17.1 lists the arithmetic operators and says:

Arithmetic operations use numeric promotion.

§4.6.1 says:

Exact numeric literals support the use of Java integer literal syntax as well as SQL exact numeric literal syntax.
Approximate literals support the use Java floating point literal syntax as well as SQL approximate numeric literal syntax.

But this leaves a lot unspecified. For example, what type does the following query return:

select 1.23 from Entity

Double? Float? BigDecimal?

Worse, what value does this query return:

select 1/2 from Entity

That is, does "use numeric promotion" mean that this is an integer division or is it promoted to float? Or to double? (Note that different SQL databases disagree on this question.)

I propose that we nail this stuff down by simply saying that:

  • the semantics of numeric literals and arithmetic expressions are the same as in Java (so that 1/2 is integer division),
  • with the additional rule that (a) integral types may be widened to BigInteger or BigDecimal, (b) BigInteger may be widened to BigDecimal, and (c) floating point types may be widened to BigDecimal.

Actually, I'm not completely clear on whether FP types should be widened to BigDecimal, but I'm thinking they should be, simply because that's something every SQL database allows.

@gavinking
Copy link
Contributor Author

Please see proposal #427, which clarifies section 4.6.1, and introduces proper support for BigInteger and BigDecimal literals.

does "use numeric promotion" mean that this is an integer division or is it promoted to float? Or to double?

Actually I guess this is sort of covered by section 4.8.6, though that section should be generalized to apply not only to the select clause.

@lukasj lukasj added this to To do in 3.2.0 via automation Aug 21, 2023
@lukasj lukasj moved this from To do to Done in 3.2.0 Aug 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
3.2.0
Done
Development

No branches or pull requests

1 participant