Skip to content

Cannot compare Decimal and 'exact numeric literal'  #5690

Closed

Description

@fasoal

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

Activity

  1. added
    bug
    on Jun 20, 2019
  2. filimonov commented on Jun 20, 2019

    @filimonov
    Contributor

    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)

    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.

  3. added
    question
    and removed
    bug
    on Jun 20, 2019
  4. barrkel commented on Jan 10, 2020

    @barrkel

    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.

  5. barrkel commented on Jan 10, 2020

    @barrkel

    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.

  6. 4ertus2 commented on Jan 10, 2020

    @4ertus2
    Contributor

    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.

  7. 4ertus2 commented on Jan 10, 2020

    @4ertus2
    Contributor

    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?

  8. barrkel commented on Jan 10, 2020

    @barrkel

    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

    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:

    Section 4.6 in http://www.contrib.andrew.cmu.edu/~shadow/sql/sql1992.txt

    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.

  9. changed the title [-]cannot compare Decimal(18, 4) and (String or Float64)[/-] [+]Compare Decimal and 'exact numeric literal' [/+] on Jan 10, 2020
  10. changed the title [-]Compare Decimal and 'exact numeric literal' [/-] [+]Cannot compare Decimal and 'exact numeric literal' [/+] on Jan 10, 2020
  11. laowangriji commented on Nov 26, 2021

    @laowangriji

    it could be sql a > toDecimal64(1000.0000,2)

  12. qoega commented on Dec 22, 2022

    @qoega
    Member

    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:
    

    I understand that we use floating point as a default type of number with . https://clickhouse.com/docs/en/sql-reference/syntax/#numeric
    It 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.

  13. Algunenano commented on Apr 19, 2024

    @Algunenano
    Member

    It works now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions