Skip to content

[client-v2] Insert of a NULL value into a Variant column fails: the write path never emits the 0xFF NULL discriminator #3400

Description

@claude

Description

A Variant(...) column can hold NULL on the server: NULL is encoded as the discriminator byte 0xFF, instead of a type discriminator plus a value. The client-v2 read path handles this (BinaryStreamReader.readVariant, client-v2/src/main/java/com/clickhouse/client/api/data_formats/internal/BinaryStreamReader.java:1093-1096 returns null for 0xFF), but the write path has no counterpart.

When a row carries null for a Variant column, RowBinaryFormatSerializer.writeValuePreamble sees a column with isNullable() == false and a data type that is not Dynamic, so it throws:

java.lang.IllegalArgumentException: An attempt to write null into not nullable column 'var'

The result: a value read from a Variant column cannot be written back. Any batch of POJOs/rows that contains a null Variant field aborts the whole insert, so the rows before and after the null one are lost too.

No data corruption occurs — non-null Variant values are serialized correctly (discriminator + value, with no extra null marker), and the failing insert writes nothing. The failure is a hard client-side exception.

Note: PR #2817 ("Fix NULL handling for Variant/Dynamic") added the Dynamic null case to writeValuePreamble and the 0xFF read case, but Variant did not get a write-side null case.

ClickHouse server version

26.10.4.207 (local server, HTTP). Verified with a real insert, not only code analysis.
Client: main at commit 74b0a62, version 0.12.0-rc1-SNAPSHOT.

Reproduction

Integration test (TestNG, client-v2), run through failsafe:

package com.clickhouse.client.datatypes;

import com.clickhouse.client.api.Client;
import com.clickhouse.client.api.command.CommandSettings;
import com.clickhouse.client.api.enums.Protocol;
import com.clickhouse.client.api.query.GenericRecord;
import org.testng.Assert;
import org.testng.annotations.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class VariantNullInsertTest {

    public static class VarRow {
        private Object var;
        public VarRow() {}
        public VarRow(Object var) { this.var = var; }
        public Object getVar() { return var; }
        public void setVar(Object var) { this.var = var; }
    }

    @Test(groups = {"integration"})
    public void testInsertNullIntoVariant() throws Exception {
        final String table = "variant_null_insert";
        try (Client client = new Client.Builder()
                .addEndpoint(Protocol.HTTP, "localhost", 8123, false)
                .setUsername("default").setPassword("").setDefaultDatabase("default")
                .serverSetting("wait_end_of_query", "1")
                .serverSetting("wait_for_async_insert", "1")
                .build()) {

            client.execute("DROP TABLE IF EXISTS " + table).get();
            client.execute("CREATE TABLE " + table + " (var Variant(String, UInt64)) ENGINE MergeTree ORDER BY ()",
                    (CommandSettings) new CommandSettings().serverSetting("allow_experimental_variant_type", "1")).get();
            client.register(VarRow.class, client.getTableSchema(table));

            // Non-null values: this part works.
            client.insert(table, new ArrayList<>(Arrays.asList(new VarRow(5L), new VarRow(6L)))).get().close();
            Assert.assertEquals(client.queryAll("SELECT * FROM " + table).size(), 2);

            // A null in the middle of the batch.
            client.execute("TRUNCATE TABLE " + table).get();
            client.insert(table, new ArrayList<>(Arrays.asList(
                    new VarRow("hello"), new VarRow(null), new VarRow(42L)))).get().close();

            List<GenericRecord> rows = client.queryAll(
                    "SELECT toString(var) AS v, toString(variantType(var)) AS t FROM " + table);
            Assert.assertEquals(rows.size(), 3); // expects 'hello', NULL, 42
        }
    }
}

Expected: the second insert stores 3 rows — 'hello' (String), NULL, 42 (UInt64).

Actual: client.insert throws and the table stays empty. Output of the same scenario with the outcome printed instead of asserted:

### stored rows after non-null insert: 2
### row: (5, UInt64)
### row: (6, UInt64)
### null insert outcome: java.lang.IllegalArgumentException: An attempt to write null into not nullable column 'var'
### stored rows after mixed insert: 0

Suggested fix

Give Variant the same treatment as Dynamic on the write path:

  • client-v2/src/main/java/com/clickhouse/client/api/data_formats/RowBinaryFormatSerializer.java:199-235 — in writeValuePreamble, let a null value pass through for ClickHouseDataType.Variant (both the defaultsSupport branch and the plain branch) instead of throwing.
  • client-v2/src/main/java/com/clickhouse/client/api/data_formats/internal/SerializerUtils.java:891 — in serializerVariant, write 0xFF when the value is null, which mirrors BinaryStreamReader.readVariant.

A round-trip test (read a NULL from a Variant column, write it back) would cover the asymmetry.

Link

Found while checking whether ClickHouse/clickhouse-rs#539 ("Insert of Option<T> into a Variant column fails or stores wrong rows", ClickHouse/clickhouse-rs#539) applies to clickhouse-java. The Rust client writes a Nullable marker byte in front of the Variant value and silently stores wrong rows; clickhouse-java does not have that corruption, but it also cannot write a NULL into a Variant column at all.

Activity

  1. polyglotAI-bot commented on Nov 26, 2026

    @polyglotAI-bot
    Collaborator

    Recommendation: Keep open — fix needed
    Review PR #3402: with its diff, the issue's steps store the 3 rows (hello, NULL, 42). It writes the 0xFF NULL discriminator and lets a null pass for Variant, which is the root cause below.

    • Category: Bug
    • Classification: impact: none · area: Data types & formats (Variant)
    • Status on main: Reproduced on main — the issue's steps give the output that it shows (IllegalArgumentException, 0 rows stored), on 8480ca001 with ClickHouse 26.11.2.4.
    • Breaking change: No.
    • Duplicates / related: No others.

    Analysis: Confirmed: writeValuePreamble lets a null value through only for Dynamic and throws for every other column that is not Nullable (RowBinaryFormatSerializer.java:215-219, :230-233), and serializerVariant has no case for a null value (SerializerUtils.java:891). The error stops the insert, so no wrong data is stored.

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

  2. added
    client
    data-types
    on Nov 26, 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