Reading the documentation. In the comparison expressions it says:
If both arguments are integers, they are compared as integers. ...
If one of the arguments is a decimal value, comparison depends on the other argument. The arguments are compared as decimal values if the other argument is a decimal or integer value, or as floating-point values if the other argument is a floating-point value.
In all other cases, the arguments are compared as floating-point (double-precision) numbers. For example, a comparison of string and numeric operands takes place as a comparison of floating-point numbers. ...
There are two things I don't understand:
What does the documentation mean by "...they are compared as integers"? Does the documentation refer to what is compared as
INT
orBIGINT
orTINYINT
, etc?What does the documentation mean by "...or as floating-point values if the other argument is a floating-point value"? Does the documentation refer to a comparison as
FLOAT
(single precision) orDOUBLE
(double precision)? Or does it depend on the context? For example, if it is:
CREATE OR REPLACE TABLE my_table (
id INT NOT NULL PRIMARY KEY,
float_v FLOAT,
double_v DOUBLE,
decimal_v DECIMAL
);
SELECT
float_v = double_v as "The precision is DOUBLE",
float_v = float_v as "The precision is FLOAT",
float_v = decimal_v as "The precision is FLOAT",
double_v = decimal_v as "The precision is DOUBLE",
decimal_v = decimal_v as "The precision is DECIMAL",
decimal_v = 1 as "The precision is DECIMAL";
/* etc.*/
This is correct?
As for arithmetic expressions:
If any approximate values are present, the expression is approximate and is evaluated using floating-point arithmetic.
If no approximate values are present, the expression contains only exact values. If any exact value contains a fractional part (a value following the decimal point), the expression is evaluated using DECIMAL exact arithmetic and has a precision of 65 digits. The term “exact” is subject to the limits of what can be represented in binary. For example, 1.0/3.0 can be approximated in decimal notation as .333..., but not written as an exact number, so (1.0/3.0)*3.0 does not evaluate to exactly 1.0.
Otherwise, the expression contains only integer values. The expression is exact and is evaluated using integer arithmetic and has a precision the same as BIGINT (64 bits).
If a numeric expression contains any strings, they are converted to double-precision floating-point values and the expression is approximate.
Also, there are two things I don't understand:
- Approximate data types are
FLOAT
andDOUBLE
, so what do you mean by "... floating-point arithmetic"? Does it use single (FLOAT
) or double (DOUBLE
) arithmetic precision? Or just like comparison expressions, these are context dependent, for example:
SELECT
float_v + double_v as "The precision is DOUBLE",
float_v + float_v as "The precision is FLOAT",
float_v + decimal_v as "The precision is FLOAT",
double_v + decimal_v as "The precision is DOUBLE",
decimal_v + decimal_v as "The precision is DECIMAL",
decimal_v + 1 as "The precision is DECIMAL";
/* etc.*/
- Expression handling Does it apply to expression comparisons, or even to expressions in general? For example, when it says "...Otherwise, the expression contains only integer values. The expression is exact and is evaluated using integer arithmetic and has a precision the same as BIGINT (64 bits)", Does this also apply to arithmetic, relational and logical expressions?
Thanks.