Add a tuple read format for Map columns - #1005
Open
Kayvan-Zahiri wants to merge 1 commit into
Open
Conversation
ClickHouse stores a Map as Array(Tuple(key, value)) and permits repeated
keys, so materializing every Map as a dict silently drops pairs. Reading
with the tuple format returns a list of (key, value) tuples instead,
which is the server's own representation.
The default read format stays native and still returns a dict, so
existing behavior is unchanged. Enable it per query with
query_formats={'Map': 'tuple'} or globally with set_read_format.
Confirmed against the server source at v26.3.21.7-lts: ColumnMap holds a
nested Array(Tuple(key, value)) column, DataTypeMap builds that nested
type from the key and value types, and neither deduplicates keys.
Closes ClickHouse#949
Kayvan-Zahiri
requested review from
joe-clickhouse and
peter-leonov-ch
as code owners
August 24, 2026 23:28
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #949.
A ClickHouse
Mapis not a unique-key collection, so materializing every one asa Python
dictdrops pairs. This adds an opt-intupleread format that returnsthe pairs as a list of tuples.
I checked this against the server source rather than inferring it from a JSON
round trip. At
v26.3.21.7-lts:src/Columns/ColumnMap.hdescribesColumnMapas a column that stores anested
Array(Tuple(key, value))columnsrc/DataTypes/DataTypeMap.cppbuilds that nested type asDataTypeArray(DataTypeTuple({key_type, value_type}, {"keys", "values"}))So a list of
(key, value)tuples is the server's own representation, not aconvention I picked.
Behavior:
map('k','1','k','2')native(default, unchanged){'k': '2'}tuple[('k', '1'), ('k', '2')]Enable per query with
query_formats={'Map': 'tuple'}or globally withset_read_format('Map', 'tuple').Tests are parametrized over the shapes the issue names: top level
Map,Array(Map(...)), and aMapnested as another map's value. Each shape isasserted in both formats, so the
nativecases pin that the default did notmove. Four fail without the change.
pytest tests/unit_testsgoes from 1453 to1460 passing with the 37 pre-existing failures unchanged,
ruffandmypyareclean on the changed files.
Not included: insert side handling of duplicate keys, and sync/async integration
coverage against a live server, which I could not run here. Happy to add the
integration tests if you want them in this PR.