Skip to content

JDBC driver passes decimal value without quotes, then where clause works wrong #2762

Todo

Description

@anesterenok
  • jdbc-v2
  • jdbc-read

Description

I have a MergeTree() test table with Decimal(38,10) values like 12345678901234567001.1234567890.

When I execute a PreparedStatement like:

        try (PreparedStatement pstmt = conn.prepareStatement("select * from test_table where col_decimal < ?")) {
            pstmt.setBigDecimal(1, new BigDecimal("12345678901234567008.1234567890"));
            ResultSet rs = pstmt.executeQuery();
...

no rows are returned (though you may see that 12345678901234567001.1234567890 < 12345678901234567008.1234567890)

I believe this is because the JDBC driver sends the following statement to the database:

http-outgoing-0 >> "User-Agent: ClickHouse JDBC Driver V2/0.8.6 clickhouse-java-v2/0.8.6 (Linux; jvm:17.0.7) Apache-HttpClient/5.3.1[\r][\n]"
...
http-outgoing-0 >> "SELECT * FROM test_table WHERE col_decimal < 12345678901234567008.1234567890[\r][\n]"
http-outgoing-0 >> "0[\r][\n]"
http-outgoing-0 >> "[\r][\n]"
http-outgoing-0 << "HTTP/1.1 200 OK[\r][\n]"
http-outgoing-0 << "X-ClickHouse-Summary: {"read_rows":"11","read_bytes":"187","written_rows":"0","written_bytes":"0","total_rows_to_read":"11","result_rows":"0","result_bytes":"0","elapsed_ns":"3362900"}[\r][\n]"

If I execute the same statement in SQL client, I also do not receive the rows. However, if I quote the literal and execute:

SELECT * FROM test_table WHERE col_decimal < '12345678901234567008.1234567890'

then rows are filtered correctly.

So it looks like a bug on JDBC driver side, it should had quoted the decimal literal.

Workaround

If I set PreparedStatement param as String, not BigDecimal, like this:

        try (PreparedStatement pstmt = conn.prepareStatement("select * from test_table where col_decimal < ?")) {
            pstmt.setString(1, "12345678901234567008.1234567890");
            ResultSet rs = pstmt.executeQuery();
...

then the driver does send a quoted literal and filter works fine. But it's certainly not how things are supposed to be, setBigDecimal() should have worked as well.

Expected Behaviour

Row with value 12345678901234567001.1234567890 is returned by filter.

Code Example

see above

Configuration

Client Configuration

Environment

  • Cloud
  • Client version: JDBC driver clickhouse-jdbc-0.8.6-all.jar
  • Language version: JDK 17
  • OS: Linux

ClickHouse Server

  • ClickHouse Server version: 25.8.16.34
  • ClickHouse Server non-default settings, if any: none
  • CREATE TABLE statements for tables involved:
  • Sample data for all these tables, use clickhouse-obfuscator if necessary

Activity

  1. added
    jdbc-v2
    area:data-type
    on Apr 15, 2026
  2. added this to the 0.12.0 milestone on Jul 29, 2026
  3. chernser commented on Jul 29, 2026

    @chernser
    Contributor

    @anesterenok

    Thank you for reporting!
    What is the table definition?
    JDBC should not change representation as server should handle numeric correctly. If we wrap value as literal then we break queries not expecting it.

  4. added
    investigating
    on Jul 29, 2026
  5. anesterenok commented on Jul 30, 2026

    @anesterenok
    Author

    My table is simple, just

    create table test_table (nrow Int32, col_decimal Nullable(Decimal(38,10))) engine = MergeTree() primary key (nrow)

    I believe the analysis in ClickHouse/clickhouse-connect#808 is correct.

    But I strongly disagree with its notion of "narrow high-precision edge case".
    Yes, 38 digits is pretty rare, but Float64 has only ~15 significant digits, and so is not suited for e.g. most financial applications. Some legacy systems in that field still use int64 types for amounts, because they don't have decimal type but need 18 significant digits.
    And the issue here would affect decimal(18, S) just as well.

  6. chernser commented on Jul 30, 2026

    @chernser
    Contributor

    @anesterenok

    Thank you for explanation! I will look into it.
    We have related feature to map big integers to string for systems that do not handle them well. May be it will be an answer to this.

  7. added
    planned_work
    on Nov 18, 2026
  8. chernser commented on Nov 24, 2026

    @chernser
    Contributor

    @anesterenok
    I agree that this case is still significant. However currently we may not reliably parse SQL to understand where to use quotes. Such things are easier and more reliable to handle on application side because it knows schema better.

  9. chernser commented on Nov 24, 2026

    @chernser
    Contributor

    @polyglotAI-bot

    Please update documentation following this instructions:

    What to document
    Why precision is lost:
    
    jdbc-v2 PreparedStatement renders ? placeholders into SQL text on the client (PreparedStatementImpl.encodeObject), and BigDecimal is emitted unquoted via BigDecimal.toString().
    ClickHouse parses an unquoted literal with a decimal point as Float64 (~15–17 significant digits), so comparisons like WHERE col_decimal < ? widen to Float64 and lose precision for Decimal(18, S), Decimal(38, S), Decimal(76, S), and similar high-precision columns.
    WriterStatementImpl (when beta.row_binary_for_simple_insert=true) writes BigDecimal in binary format for INSERT ... VALUES, so inserts on that path are not affected.
    Recommended ways to pass high-precision BigDecimal values in queries:
    
    Option A — Explicit Decimal cast with setString(..., value.toPlainString()) (safest across WHERE, SELECT, and arithmetic): ClickHouse parses the string directly into Decimal(P, S) without going through Float64, and the expression keeps its Decimal type.
    
    
    public List<BigDecimal> findBelowThreshold(Connection conn, BigDecimal threshold) throws SQLException {
        String sql = "SELECT col_decimal FROM test_table WHERE col_decimal < CAST(? AS Decimal(38, 10))";
        List<BigDecimal> results = new ArrayList<>();
        try (PreparedStatement ps = conn.prepareStatement(sql)) {
            // toPlainString() avoids scientific notation (e.g. 1E+20)
            ps.setString(1, threshold.toPlainString());
            try (ResultSet rs = ps.executeQuery()) {
                while (rs.next()) {
                    results.add(rs.getBigDecimal(1));
                }
            }
        }
        return results;
    }
    (Equivalently, toDecimal128(?, 10) / toDecimal256(?, 10) works when matching a specific Decimal width and scale.)
    
    Option B — setString(..., value.toPlainString()) directly in WHERE col_decimal < ? (simplest workaround, comparison-only): When comparing directly against a Decimal column, ClickHouse coerces the constant string literal to the column's Decimal type. Document the caveat that setString sends a String literal, so it should not be used in arithmetic (? + 1), numeric functions (abs(?)), or untyped SELECT ? projections.
    
    Option C — client-v2 typed named parameters ({name:Decimal(P, S)}): If using client-v2 (Client.query) instead of JDBC ? placeholders, server-side typed parameters parse the unquoted BigDecimal directly as Decimal(P, S) with full precision.
    
    Behavior of Float64 as String (worth noting alongside Decimal):
    
    Reading a Float64 column via ResultSet.getString() (or mapping jdbc_type_mappings=Float64=String) formats the IEEE 754 double with Double.toString(double) (~15–17 significant digits, scientific notation outside 10^-3..10^7).
    Passing a number via setString makes ClickHouse treat it as String, not Float64; ResultSet.getDouble() / getBigDecimal() parses strings back via Double.parseDouble / new BigDecimal(String).
    Where this fits in the repository docs
    docs/clickhouse-docs/jdbc.mdx — in the Decimal notes under Data Types (and/or the PreparedStatement section), where Decimal formatting notes already live.
    docs/integration-jdbc.md — under Common Pitfalls / Querying (PreparedStatement), following the write-doc-examples style (method-wrapped snippets).
    docs/features.md — under jdbc-v2 compatibility-sensitive traits if you want to pin the current setBigDecimal SQL literal behavior in the compatibility contract.
    
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

area:data-typebuginvestigatingjdbc-v2planned_work

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions