You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Cannot compare Decimal and 'exact numeric literal' #5690
version:ClickHouse 18.16.1
column a type is Decimal(18, 4)
1 excute sql a > '1000.0000'
exception: Illegal types of arguments (Decimal(18, 4), String) of function greater
2 excute sql a > 1000.0000
exception: No operation greater between Decimal(18, 4) and Float64
If you do need strict values you use Decimal. If you do not need strict values you use Floats. If you use both Float and Decimal it's not clear what result do you want: strict or not. You should explicitly cast one of arguments to say it.
I think this is a bug, or at the very least a feature deficiency compared with standard SQL. Such comparisons work in almost every other database product, because they don't perform early conversion of strings of decimal digits to floating point.
A literal 1000.0000 can be safely interpreted as a Decimal(18,4); it doesn't need to go via Float64, and if the parser is intelligent enough, it wouldn't convert it to floating point so soon.
One approach would to introduce an AST-only type of LiteralDecimal(precision,scale) and convert on demand to Decimal or Float64 when an operator or assignment is applied, and the type of the other side known. If both sides are LiteralDecimal, they can both be converted to Float64 - relative comparison and ordering should be consistent, and it should preserve existing behaviour.
or at the very least a feature deficiency compared with standard SQL
Yes. ClickHouse has strict data typing and it's incompatible with SQL in some cases. It's not a bug. It's a choise convert data types in runtime as seldom as possible. ClickHouse use fast but not safe data types. That means if you call some internals with wrong data type, i.e. UInt32 instead of UInt64 it leads to server crash. It's not possible to make safe data types implementation without performance penalties. Strict typing is very important restriction to make code both stable and fast.
There are some cases where we cannot save strict typing for sure. The main case is nullable types: Nullable(Int32) is not Int32 but SQL expects that types are nullable. So nullable type could appear anywhere where not nullable can. And we have lots of bugs with Nullable types for this reason.
Specifically in your case (comparison with Decimals) there could be some changes in future to make better SQL compatibility. It's possible to make issues with "sql compatibility" tag and references to SQL paragraths what behaviour is expected.
One approach would to introduce an AST-only type of LiteralDecimal(precision,scale) and convert on demand to Decimal or Float64 when an operator or assignment is applied
Do you mean we should implement comparison with literals but not implement comparison with Float and String columns? It's something strange. Could you print here what does SQL standard think about it?
It shouldn't happen at runtime; it should happen at analysis time, at the same time that type checking is being performed. This shouldn't happen per row processed; it should be when the parsed SQL is analyzed.
SQL-92 would interpret the literal 1000.0000 as effectively typed decimal(8,4).
The data type of an <exact numeric literal> is exact numeric. The precision of an <exact numeric literal> is the number of <digit>s that it contains. The scale of an <exact numeric literal> is the number of <digit>s to the right of the <period>.
SQL-92 permits implicit conversions with implementation-defined rounding / truncation between them, but errors for size loss:
Values of the data types NUMERIC, DECIMAL, INTEGER, SMALLINT,
FLOAT, REAL, and DOUBLE PRECISION are numbers and are all mutually
comparable and mutually assignable. If an assignment would result
in a loss of the most significant digits, an exception condition
is raised. If least significant digits are lost, implementation-
defined rounding or truncating occurs with no exception condition
being raised. The rules for arithmetic are generally governed by
Subclause 6.12, "".
I understand and appreciate that ClickHouse gets its performance by minimizing the amount of code that needs to be executed on a per-row basis. I'd accept an error at parse / analysis time if it avoids an expensive implicit operation on a per-row basis. My point is that, as a user, it would be much easier to search for exact decimals by permitting implicit conversion at analysis time.
In other words:
select foo from bar where value = 1000.0000
should be interpreted as
select foo from bar where value = toDecimal64(1000.0000,4)
if value is of type decimal64(8,4) or larger. Implicit conversions can be performed at analysis time on constant (e.g. literal) values.
Is it possible to add literal parsing that is aware of types that we expect in expression?
Here is an example:
SELECT if(number > 0, CAST('1.5', 'Decimal(7, 2)'), 0.)
FROM numbers(2)
Query id: 86a19fd1-058e-48e1-991d-adaa8ea12b76
0 rows in set. Elapsed: 0.002 sec.
Received exception from server (version 22.13.1):
Code: 386. DB::Exception: Received from localhost:9000. DB::Exception: There is no supertype for types Decimal(7, 2), Float64 because some of them have no lossless conversion to Decimal: While processing if(number > 0, CAST('1.5', 'Decimal(7, 2)'), 0.). Stack trace:
Activity
filimonov commentedon Jun 20, 2019
It's the expected behavior.
With string - it's unclear what you want to use: string comparison or number comparison.
With floats - see: #5483 (comment)
barrkel commentedon Jan 10, 2020
I think this is a bug, or at the very least a feature deficiency compared with standard SQL. Such comparisons work in almost every other database product, because they don't perform early conversion of strings of decimal digits to floating point.
A literal
1000.0000can be safely interpreted as a Decimal(18,4); it doesn't need to go via Float64, and if the parser is intelligent enough, it wouldn't convert it to floating point so soon.barrkel commentedon Jan 10, 2020
One approach would to introduce an AST-only type of
LiteralDecimal(precision,scale)and convert on demand to Decimal or Float64 when an operator or assignment is applied, and the type of the other side known. If both sides are LiteralDecimal, they can both be converted to Float64 - relative comparison and ordering should be consistent, and it should preserve existing behaviour.4ertus2 commentedon Jan 10, 2020
Yes. ClickHouse has strict data typing and it's incompatible with SQL in some cases. It's not a bug. It's a choise convert data types in runtime as seldom as possible. ClickHouse use fast but not safe data types. That means if you call some internals with wrong data type, i.e. UInt32 instead of UInt64 it leads to server crash. It's not possible to make safe data types implementation without performance penalties. Strict typing is very important restriction to make code both stable and fast.
There are some cases where we cannot save strict typing for sure. The main case is nullable types: Nullable(Int32) is not Int32 but SQL expects that types are nullable. So nullable type could appear anywhere where not nullable can. And we have lots of bugs with Nullable types for this reason.
Specifically in your case (comparison with Decimals) there could be some changes in future to make better SQL compatibility. It's possible to make issues with "sql compatibility" tag and references to SQL paragraths what behaviour is expected.
4ertus2 commentedon Jan 10, 2020
Do you mean we should implement comparison with literals but not implement comparison with Float and String columns? It's something strange. Could you print here what does SQL standard think about it?
barrkel commentedon Jan 10, 2020
It shouldn't happen at runtime; it should happen at analysis time, at the same time that type checking is being performed. This shouldn't happen per row processed; it should be when the parsed SQL is analyzed.
SQL-92 would interpret the literal 1000.0000 as effectively typed
decimal(8,4).Section 5.3 (13) in http://www.contrib.andrew.cmu.edu/~shadow/sql/sql1992.txt
SQL-92 permits implicit conversions with implementation-defined rounding / truncation between them, but errors for size loss:
Section 4.6 in http://www.contrib.andrew.cmu.edu/~shadow/sql/sql1992.txt
I understand and appreciate that ClickHouse gets its performance by minimizing the amount of code that needs to be executed on a per-row basis. I'd accept an error at parse / analysis time if it avoids an expensive implicit operation on a per-row basis. My point is that, as a user, it would be much easier to search for exact decimals by permitting implicit conversion at analysis time.
In other words:
should be interpreted as
if value is of type decimal64(8,4) or larger. Implicit conversions can be performed at analysis time on constant (e.g. literal) values.
[-]cannot compare Decimal(18, 4) and (String or Float64)[/-][+]Compare Decimal and 'exact numeric literal' [/+][-]Compare Decimal and 'exact numeric literal' [/-][+]Cannot compare Decimal and 'exact numeric literal' [/+]laowangriji commentedon Nov 26, 2021
it could be sql a > toDecimal64(1000.0000,2)
qoega commentedon Dec 22, 2022
Is it possible to add literal parsing that is aware of types that we expect in expression?
Here is an example:
I understand that we use floating point as a default type of number with
.https://clickhouse.com/docs/en/sql-reference/syntax/#numericIt makes sense to allow queries that do not lose any precision while converting literals to expected common types. Operations with Decimal/Double columns will still be prohibited.
Algunenano commentedon Apr 19, 2024
It works now