Skip to content

Commit

Permalink
Clarify SQL type assignment for numeric literals [HZ-1748] (#800)
Browse files Browse the repository at this point in the history
* Clarify SQL type assignment for numeric literals [HZ-1748]

* Minor edits.

---------

Co-authored-by: Serdar Ozmen <[email protected]>
  • Loading branch information
burakgok and Serdaro authored Aug 21, 2023
1 parent 85ccde0 commit 6bf4627
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion docs/modules/sql/pages/data-types.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,16 @@

== Casting and Data Type Conversion

To convert one data type to another (also known as casting), you can use the xref:functions-and-operators.adoc#cast[`CAST(expression AS type_name)`] function.
To convert one data type to another (also known as casting), you can use the xref:functions-and-operators.adoc#cast[`CAST(expression AS type_name)`] function.

== Numeric Literals

When numeric literals are assigned SQL types:

. Integers, e.g., 1 and 42, receive the narrowest possible among `TINYINT`, `SMALLINT`, `INTEGER`, `BIGINT` and `DECIMAL`.
. Floating-point numbers, e.g., 1.2 and 4.2, receive `DECIMAL`.
. Scientific notation (exponential form), e.g., 1e1 or 4.2e2, receives `DOUBLE`.

As you realized, integers are optimized for storage, whereas floating-point numbers are not subject to such optimization. This is because `REAL` and `DOUBLE` cannot exactly represent all decimal numbers (see link:https://en.wikipedia.org/wiki/IEEE_754[IEEE 754]), and exactness cannot be compromised for financial applications. Finally, because of its rare usage, scientific notation is not optimized for storage, i.e., not tried to narrow to `REAL`.

Numbers that are represented in scientific notation and out of range for `DOUBLE` will cause overflow and be interpreted as ∞ or -∞. For example, 1.8e+308 is greater than the largest possible finite value of `DOUBLE`, which is 1.7976931348623157e+308, and so it is only valid in the form 18000...0.

0 comments on commit 6bf4627

Please sign in to comment.