Skip to content

bug: Fix Nullable(JSON) with nil map#1667

Merged
kavirajk merged 4 commits intomainfrom
kavirajk/json-nullable-bug-fix
Sep 24, 2025
Merged

bug: Fix Nullable(JSON) with nil map#1667
kavirajk merged 4 commits intomainfrom
kavirajk/json-nullable-bug-fix

Conversation

@kavirajk
Copy link
Copy Markdown
Contributor

@kavirajk kavirajk commented Sep 22, 2025

Fixes: #1638

See this discussion

The actual bug is in the Nullable column here.

We check if the value is nil only if type is reflect.Pointer. In our case, it's reflect.Map (which can also be nil).

Following is verification using clickhouse-client with and without patch.

SELECT *
FROM test_nullable_json

Query id: 485b3606-fe0e-45e9-8719-f7a88e0daaf9

   ┌─id─┬─payload─┐
1. │  2 │ {}      │
   └────┴─────────┘

1 row in set. Elapsed: 0.006 sec.
SELECT *
FROM test_nullable_json

Query id: f626b8e0-e71e-4ec0-ad29-256903d4a348

   ┌─id─┬─payload─┐
1. │  2 │ ᴺᵁᴸᴸ    │
   └────┴─────────┘

1 row in set. Elapsed: 0.003 sec.

Notice how after the patch, the column value is populated correctly as NULL.

Also verified with original code in the description. We are able to get back the NULL column correctly as nil value. Locked the behavior via integration tests

Summary

Checklist

Delete items not relevant to your PR:

Fixes: #1638

The actual bug is in the `Nullable` [column here.](https://github.com/ClickHouse/clickhouse-go/blob/12088fe6b4a62f27e7cdb43d3bbebbcfccd9a5cb/lib/column/nullable.go?plain=1#L134-L136)

We check if the value is nil only if type is `reflect.Pointer`. In our case, it's `reflect.Map` (which can also be nil).

Following is verification using `clickhouse-client` with and without patch.

```
SELECT *
FROM test_nullable_json

Query id: 485b3606-fe0e-45e9-8719-f7a88e0daaf9

   ┌─id─┬─payload─┐
1. │  2 │ {}      │
   └────┴─────────┘

1 row in set. Elapsed: 0.006 sec.
```

```
SELECT *
FROM test_nullable_json

Query id: f626b8e0-e71e-4ec0-ad29-256903d4a348

   ┌─id─┬─payload─┐
1. │  2 │ ᴺᵁᴸᴸ    │
   └────┴─────────┘

1 row in set. Elapsed: 0.003 sec.
```

Notice how after the patch, the column value is populated correctly as `NULL`.

Also verified with original code in the description. We are able to get back the NULL column correctly as `nil` value. Locked the behavior via integration tests

Signed-off-by: Kaviraj <[email protected]>
@kavirajk kavirajk merged commit ff9b797 into main Sep 24, 2025
14 checks passed
@mshustov mshustov deleted the kavirajk/json-nullable-bug-fix branch September 24, 2025 07:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Inserting Null into a Nullable(JSON) Column Via A Batch Appears To Insert A Non-Null Value

3 participants