Skip to content

ClickHouseBinaryFormatReader::getBigDecimal silently truncates large integer values #2748

@AndreasP-EP

Description

@AndreasP-EP

High Level Area Labels:
- client-api-v2

Specific Area Labels:
- data-type
- client-read

Description

It appears that ClickHouseBinaryFormatReader::getBigDecimal silently truncates large integers, at least in cases when the column is returned as an Int64. The same happens for UInt64.

Steps to reproduce

  1. Update the username, password and hostname in the example to point to your ClickHouse instance
  2. Run the main method in the example

Error Log or Exception StackTrace

Got 9223372036854776000 instead of expected 9223372036854775807 is printed, as the returned value has been silently truncated.

Expected Behaviour

getBigDecimal shouldn't truncate the values, ideally it would behave like

new BigDecimal(reader.getString))

In the example, the returned value should be 9223372036854775807.

Code Example

package org.example;

import java.math.BigDecimal;
import java.util.concurrent.CompletableFuture;

import com.clickhouse.client.api.Client;
import com.clickhouse.client.api.Client.Builder;
import com.clickhouse.client.api.data_formats.ClickHouseBinaryFormatReader;
import com.clickhouse.client.api.enums.Protocol;
import com.clickhouse.client.api.query.QueryResponse;

public final class WrongValue {

    public static void main(String[] args) throws Exception {
        Client client = new Builder()
            .addEndpoint(Protocol.HTTP, "127.0.0.1", 8123, false)
            .setUsername("user")
            .setPassword("123")
            .build();
        try (client) {
            String sql = "SELECT toInt64(%d)".formatted(Long.MAX_VALUE);
            CompletableFuture<QueryResponse> future = client.query(sql);
            QueryResponse response = future.get();
            try (ClickHouseBinaryFormatReader reader = client.newBinaryFormatReader(response)) {
                reader.next();
                BigDecimal actual = reader.getBigDecimal(1);
                BigDecimal expected = BigDecimal.valueOf(Long.MAX_VALUE);

                if (actual.compareTo(expected) != 0) {
                    System.err.printf("Got %s instead of expected %s%n", actual.toPlainString(), expected.toPlainString());
                }
            }
        }
    }
}

Configuration

Environment

  • Client version: 0.9.6
  • Language version: Java 17
  • OS: Windows 11 (the ClickHouse server is running on AlmaLinux release 9.1)

ClickHouse Server

  • ClickHouse Server version: 25.3.6.10034.altinitystable
  • ClickHouse Server non-default settings, if any:

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions