Skip to content

Map(K, V) type support issue #3047

Open
Open

Description

@E1izabeth

Description

In Clickhouse Map(K, V) is not a collection of unique-by-key pairs, see https://clickhouse.com/docs/sql-reference/data-types/map :

i.e. a map can contain two elements with the same key

But there is no way to obtain any representation other than a Map<,> of unique entries.

All the key-value pairs are being delivered by the underlying protocol, but they are effectively merged while reading an instance of a map in the corresponding readMap(..) method.

The issue affects all scenarios of map usage: top-level value, nested map-in-map, map-in-array, etc.

Steps to reproduce

  1. Execute minimal reproduction program:
public class Program {
	public static void main(String[] args) throws SQLException {
		String queryText = "select map('key', 'X', 'key', 'Y')";
		try (
			Connection cnn = DriverManager.getConnection("jdbc:clickhouse://localhost:11049/default", "default", "");			
			PreparedStatement stmt = cnn.prepareStatement(queryText);
			ResultSet rs = stmt.executeQuery()
		) {
			if (rs.next()) {
				System.out.println(rs.getObject(1)); // gives {key=Y}
			} else {
				throw new IllegalStateException();
			}
		}
	}
}
  1. Observe only one value per key returned.
  2. Any attempt to get other value representations than what getObject() does gives the same or results in an exception being thrown.

Expected Behavior

Supporting existing behavior by default, it is expected to be able to use type hints to affect the actual return value of the getObject(..) method of the java.sql.ResultSet implementation (such overloads as <T> T getObject(int columnIndex, Class<T> type) and Object getObject(int columnIndex, java.util.Map<String,Class<?>> map) specifically, including their counterparts having columnLabel instead of columnIndex):

public class Program {
	public static void main(String[] args) throws SQLException {
		String queryText = "select map('key', 'X', 'key', 'Y')";
		try (
			Connection cnn = DriverManager.getConnection("jdbc:clickhouse://localhost:11049/default", "default", "");			
			PreparedStatement stmt = cnn.prepareStatement(queryText);
			ResultSet rs = stmt.executeQuery()
		) {
			if (rs.next()) {
				System.out.println(rs.getObject(1)); // gives {key=Y} by default

				System.out.println(rs.getObject(1, List.class)); // to give [key=X, key=Y] only for top level
				System.out.println(rs.getObject(1, Map.of("Map", List.class))); // to give [key=X, key=Y] same as above including nested values 
			} else {
				throw new IllegalStateException();
			}
		}
	}
}

Consider changing the default behavior by the time of a certain upcoming major release.

Code Example

select map('key', 'X', 'key', 'Y');

Environment

  • Cloud
  • Client version: 0.10.0-rc2
  • Language version: 21
  • OS: Windows 11

ClickHouse Server

  • ClickHouse Server version: 24.5.3.5

Activity

  1. chernser commented on Aug 31, 2026

    @chernser
    Contributor

    @E1izabeth

    Thank you for reporting! We will look into the issue shortly.

  2. added this to the 0.12.0 milestone on Aug 31, 2026
  3. removed this from the 0.12.0 milestone on Nov 11, 2026
  4. polyglotAI-bot commented on Nov 27, 2026

    @polyglotAI-bot
    Collaborator

    Recommendation: Keep open — maintainer decision
    jdbc-v2 and client-v2 keep only the last value of a duplicate Map key, with no error. A maintainer decides how callers read all pairs, because a java.util.Map cannot hold them. Then the reader needs a fix.

    Analysis: BinaryStreamReader.readMap (client-v2/src/main/java/com/clickhouse/client/api/data_formats/internal/BinaryStreamReader.java:1041-1045) reads every pair, but puts it into a LinkedHashMap, so a later pair with the same key replaces the earlier one, and the stream stays in sync. jdbc-v2 reads through the same reader, and readNestedValue (BinaryStreamReader.java:848) uses readMap for a Map inside Array, Map, or Tuple.

    Reproduction: complete program and output

    Changed from the issue's steps: added client-v2 cases for a top-level Map and for Array(Map).

    import com.clickhouse.client.api.Client;
    import com.clickhouse.client.api.query.GenericRecord;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.util.List;
    import java.util.Properties;
    
    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 {
            // The issue's steps (jdbc-v2): a top-level Map with two pairs for one key.
            String jdbcUrl = "jdbc:clickhouse:" + URL.replaceFirst("^https?:", "") + "/default";
            try (Connection cnn = DriverManager.getConnection(jdbcUrl, "default", PASSWORD);
                 PreparedStatement stmt = cnn.prepareStatement("select map('key', 'X', 'key', 'Y')");
                 ResultSet rs = stmt.executeQuery()) {
                rs.next();
                System.out.println("jdbc-v2 getObject(1): " + rs.getObject(1));
            } catch (Exception e) {
                System.out.println("jdbc-v2: " + e);
            }
            // client-v2, top-level and nested in an Array.
            try (Client client = new Client.Builder().addEndpoint(URL)
                    .setUsername("default").setPassword(PASSWORD).build()) {
                List<GenericRecord> rows = client.queryAll(
                        "select map('key', 'X', 'key', 'Y') AS m, [map('key', 'X', 'key', 'Y')] AS am");
                System.out.println("client-v2 Map: " + rows.get(0).getObject("m"));
                System.out.println("client-v2 Array(Map): " + rows.get(0).getList("am"));
            } catch (Exception e) {
                System.out.println("client-v2: " + e);
            }
        }
    }

    Output on main (8480ca001), ClickHouse 26.11.2.4:

    jdbc-v2 getObject(1): {key=Y}
    client-v2 Map: {key=Y}
    client-v2 Array(Map): [{key=Y}]
    

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

  5. added
    client
    data-types
    jdbc
    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-typesjdbc

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions