Skip to content

[client-v2] Float NaN and Infinity are silently written into integer columns (NaN -> 0, Inf -> MAX_VALUE), also into Nullable(Int*) #3319

Open

Description

@claude

Describe the bug

SerializerUtils.serializePrimitiveData converts a user-supplied Number to an integer column value with plain Java narrowing — convertToInteger / convertToLong call ((Number) value).intValue() / .longValue() (client-v2/src/main/java/com/clickhouse/client/api/data_formats/internal/SerializerUtils.java:1056-1082, used at lines 656-681 and 746).

Java narrowing of a non-finite double produces a legal-looking number instead of failing:

Written value Column Stored value
Double.NaN Nullable(Int32) 0 (not NULL, no error)
Double.POSITIVE_INFINITY Int32 2147483647
Double.POSITIVE_INFINITY Int64 9223372036854775807

No exception is raised and nothing is logged, so the row is silently corrupted. The server itself rejects both conversions (SELECT toInt32(nan) and SELECT toInt32(inf) fail with code 70, CANNOT_CONVERT_TYPE), so the client is more permissive than the server it writes to.

This is related to #3176 (silent wrapping of out-of-range integers) but is a distinct failure mode: the intValue() vs longValue() comparison suggested there (NumberConverter.toInt) does not catch NaN — both accessors return 0 for it — and does not catch +Inf for Int64 either. A non-finite check is needed on top of the range check.

ClickHouse server version

26.10.3.7 (local server at http://localhost:8123). The test below was run against it.

Reproduction

TestNG test in client-v2:

package com.clickhouse.client.insert;

import com.clickhouse.client.api.Client;
import com.clickhouse.client.api.data_formats.RowBinaryFormatWriter;
import com.clickhouse.client.api.insert.InsertResponse;
import com.clickhouse.client.api.insert.InsertSettings;
import com.clickhouse.client.api.metadata.TableSchema;
import com.clickhouse.client.api.query.GenericRecord;
import com.clickhouse.data.ClickHouseFormat;
import org.testng.annotations.Test;

import java.util.List;

public class NanIntScratchTest {

    @Test
    public void nanAndInfIntoIntegerColumns() throws Exception {
        try (Client client = new Client.Builder()
                .addEndpoint("http://localhost:8123")
                .setUsername("default").setPassword("")
                .compressServerResponse(false).compressClientRequest(false)
                .build()) {

            client.execute("DROP TABLE IF EXISTS nan_int_test").get();
            client.execute("CREATE TABLE nan_int_test (v Nullable(Int32), w Int32, i64 Int64, f Float64) ENGINE Memory").get();

            TableSchema schema = client.getTableSchema("nan_int_test");
            Object[][] rows = new Object[][]{
                    {13.0d, 13.0d, 13.0d, 13.0d},
                    {Double.NaN, Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, 1.0d},
            };
            ClickHouseFormat format = ClickHouseFormat.RowBinary;
            try (InsertResponse r = client.insert("nan_int_test", out -> {
                RowBinaryFormatWriter w = new RowBinaryFormatWriter(out, schema, format);
                for (Object[] row : rows) {
                    for (int i = 0; i < row.length; i++) {
                        w.setValue(i + 1, row[i]);
                    }
                    w.commitRow();
                }
            }, format, new InsertSettings()).get()) {
                System.out.println("written rows = " + r.getWrittenRows());
            }

            for (GenericRecord rec : client.queryAll("SELECT v, w, i64, f FROM nan_int_test ORDER BY f")) {
                System.out.println("v=" + rec.getObject(1) + " w=" + rec.getInteger(2)
                        + " i64=" + rec.getLong(3) + " f=" + rec.getDouble(4));
            }
        }
    }
}

Actual output:

written rows = 2
v=0 w=2147483647 i64=9223372036854775807 f=1.0
v=13 w=13 i64=13 f=13.0

Expected: the insert fails with a clear client-side error for the non-finite values (as the server does), instead of writing 0 / MAX_VALUE.

Suggested fix

In client-v2/src/main/java/com/clickhouse/client/api/data_formats/internal/SerializerUtils.java, reject non-finite values before narrowing — in convertToInteger (line 1056) and convertToLong (line 1070), and in the NumberConverter.toBigInteger path used for UInt64/Int128/Int256, throw when a Float/Double argument is NaN or infinite. The same check is needed for Decimal targets. Nested containers (Array(Nullable(Int*)), Tuple, Map, LowCardinality(Nullable(Int*))) go through the same converters, so one check covers them.

Link

Relayed from the equivalent bug in clickhouse-connect: ClickHouse/clickhouse-connect#1172

Activity

  1. polyglotAI-bot commented on Nov 27, 2026

    @polyglotAI-bot
    Collaborator

    Recommendation: Keep open — fix needed
    Review PR #3411: with its diff, NaN and Infinity fail with IllegalArgumentException for all integer cases below. The PR closes #2619 only, so this issue stays open when it merges unless the PR links it.

    • Category: Bug
    • Classification: impact: Data integrity · area: Data types & formats (Int32, Int64, UInt64)
    • Status on main: Reproduced on main — the issue's steps give the output that it shows; UInt64 is affected too.
    • Breaking change: No — only values that are stored wrong today start to fail.
    • Duplicates / related: No others

    Analysis: The issue names the root cause: convertToInteger and convertToLong (SerializerUtils.java:1056, :1070) and NumberConverter.toBigInteger (NumberConverter.java:102) narrow with intValue() / longValue(), which give 0 for NaN and the type maximum for Infinity. Decimal columns are not affected: NumberConverter.toBigDecimal (NumberConverter.java:148) throws NumberFormatException. POJO inserts (POJOSerDe.java:66) use the same code as RowBinaryFormatWriter.

    Reproduction: complete program and output

    Changed from the issue's steps: one table and one row for each case, UInt64 and Decimal cases added, default compression settings.

    import com.clickhouse.client.api.Client;
    import com.clickhouse.client.api.data_formats.RowBinaryFormatWriter;
    import com.clickhouse.client.api.insert.InsertResponse;
    import com.clickhouse.client.api.insert.InsertSettings;
    import com.clickhouse.client.api.metadata.TableSchema;
    import com.clickhouse.data.ClickHouseFormat;
    
    public class Repro {
        static final String URL = System.getenv().getOrDefault("CLICKHOUSE_URL", "http://localhost:8123");
        static final String PASSWORD = System.getenv().getOrDefault("CLICKHOUSE_PASSWORD", "");
    
        public static void main(String[] args) throws Exception {
            try (Client client = new Client.Builder().addEndpoint(URL)
                    .setUsername("default").setPassword(PASSWORD).build()) {
                Object[][] cases = {
                        {"Int32", 13.0d},                      // contrast: a finite double
                        {"Nullable(Int32)", Double.NaN},
                        {"Int32", Double.POSITIVE_INFINITY},
                        {"Int64", Double.POSITIVE_INFINITY},
                        {"UInt64", Double.NaN},
                        {"UInt64", Double.POSITIVE_INFINITY},
                        {"Decimal(10, 2)", Double.NaN},
                };
                for (Object[] c : cases) {
                    String label = c[0] + " <- " + c[1];
                    try {
                        client.execute("DROP TABLE IF EXISTS nan_test").get();
                        client.execute("CREATE TABLE nan_test (v " + c[0] + ") ENGINE Memory").get();
                        TableSchema schema = client.getTableSchema("nan_test");
                        try (InsertResponse r = client.insert("nan_test", out -> {
                            RowBinaryFormatWriter w = new RowBinaryFormatWriter(out, schema, ClickHouseFormat.RowBinary);
                            w.setValue(1, c[1]);
                            w.commitRow();
                        }, ClickHouseFormat.RowBinary, new InsertSettings()).get()) {
                            String stored = client.queryAll("SELECT toString(v) AS s FROM nan_test").get(0).getString("s");
                            System.out.println(label + ": stored " + stored);
                        }
                    } catch (Exception e) {
                        Throwable t = e;
                        while (t.getCause() != null) t = t.getCause();
                        System.out.println(label + ": " + t.getClass().getName() + ": " + t.getMessage());
                    }
                }
                client.execute("DROP TABLE IF EXISTS nan_test").get();
            }
        }
    }

    Output on main (8480ca001), ClickHouse 26.11.2.4:

    Int32 <- 13.0: stored 13
    Nullable(Int32) <- NaN: stored 0
    Int32 <- Infinity: stored 2147483647
    Int64 <- Infinity: stored 9223372036854775807
    UInt64 <- NaN: stored 0
    UInt64 <- Infinity: stored 9223372036854775807
    Decimal(10, 2) <- NaN: java.lang.NumberFormatException: Character N is neither a decimal digit number, decimal point, nor "e" notation exponential mark.
    

    Output with the diff of PR #3411 (33696af01):

    Int32 <- 13.0: stored 13
    Nullable(Int32) <- NaN: java.lang.IllegalArgumentException: Cannot convert NaN to BigInteger
    Int32 <- Infinity: java.lang.IllegalArgumentException: Cannot convert Infinity to BigInteger
    Int64 <- Infinity: java.lang.IllegalArgumentException: Cannot convert Infinity to BigInteger
    UInt64 <- NaN: java.lang.IllegalArgumentException: Cannot convert NaN to BigInteger
    UInt64 <- Infinity: java.lang.IllegalArgumentException: Cannot convert Infinity to BigInteger
    Decimal(10, 2) <- NaN: java.lang.NumberFormatException: Character N is neither a decimal digit number, decimal point, nor "e" notation exponential mark.
    

    Automated triage by Polyglot AI. A maintainer makes the final decision.

  2. added
    client
    data-types
    on Nov 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugclientdata-types

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions