Skip to content

[jdbc-v2] A java.util.Map parameter is written as {'k': v}, so it fails with SYNTAX_ERROR outside INSERT ... VALUES #3558

Backlog

Description

@polyglotAI-bot

Description

PreparedStatement.setObject(i, map) with a java.util.Map works only in INSERT ... VALUES (?). In all other parts of a statement, the statement fails with Code: 62 ... Expected substitution name (identifier). (SYNTAX_ERROR): a SELECT expression (also CAST(? AS Map(...)) and function arguments such as mapKeys(?)), a WHERE condition, INSERT ... SELECT, and a List of maps. An empty map fails in the same way. A List parameter in the same places works.

Steps to reproduce

  1. Run the code example. It creates t_map (id Int32, m Map(String, Int32)).
  2. For each statement of the log below, it binds the map {k=1, j=2} (or an empty map, a List of two maps, a List<Integer>) with setObject(1, value) and calls execute().

Error Log or Exception StackTrace

OK    INSERT INTO t_map VALUES (1, ?)                  map
FAIL  INSERT INTO t_map SELECT 2, ?                    map: Syntax error: failed at position 30 ('k'): 'k': 1,'j': 2}. Expected one of: substitution name (identifier), end of query. (SYNTAX_ERROR)
FAIL  SELECT toString(CAST(? AS Map(String, Int32)))   map: Syntax error: failed at position 23 ('k'): 'k': 1,'j': 2} AS Map(String, Int32))). Expected substitution name (identifier). (SYNTAX_ERROR)
FAIL  SELECT toString(CAST(? AS Map(String, Int32)))   empty: Syntax error: failed at position 23 (}): } AS Map(String, Int32))). Expected substitution name (identifier). (SYNTAX_ERROR)
FAIL  SELECT id FROM t_map WHERE m = ?                 map: Syntax error: failed at position 33 ('k'): 'k': 1,'j': 2}. Expected substitution name (identifier). (SYNTAX_ERROR)
FAIL  SELECT toString(mapKeys(?))                      map: Syntax error: failed at position 26 ('k'): 'k': 1,'j': 2})). Expected substitution name (identifier). (SYNTAX_ERROR)
FAIL  SELECT toString(?)                               list of maps: Syntax error: failed at position 19 ('k'): 'k': 1,'j': 2},{'k': 1,'j': 2}]). Expected substitution name (identifier). (SYNTAX_ERROR)
OK    SELECT toString(?)                               list

Expected Behaviour

Each statement runs with the parameter as a Map value, as INSERT ... VALUES does. The server reads {'k': 1} only in the Values input format. In an SQL expression, { starts a query parameter {name:Type}, and a map value is map(key, value, ...). The Map(K, V) docs use {...} only in INSERT ... VALUES. On the server:

SELECT {'k': 1}                                   -> Code: 62 ... Expected substitution name (identifier). (SYNTAX_ERROR)
SELECT map('k', 1)                                -> {'k':1}
INSERT INTO t (id, m) VALUES (5, map('k', 5))     -> OK, stored {'k':5}

A workaround is mapFromArrays(?, ?) with a List of keys and a List of values.

Root cause

PreparedStatementImpl.encodeObject writes a Map as {key: value, ...} into the SQL text (jdbc-v2/src/main/java/com/clickhouse/jdbc/PreparedStatementImpl.java:836-849). INSERT ... VALUES works only because the server parses that row with the parser of the Values format.

The fix is in the Map branch of encodeObject. Polyglot AI can open a fix PR if a maintainer assigns this issue to @polyglotAI-bot.

Related: the open PR #3524 (streaming PreparedStatement) uses encodeObject for maps too, so one fix covers both paths.

Code Example

Reproduction program (Java, 41 lines)
import java.sql.*;
import java.util.*;

public class Repro {
    public static void main(String[] args) throws Exception {
        Properties p = new Properties();
        p.setProperty("user", "default");
        p.setProperty("password", System.getenv().getOrDefault("CLICKHOUSE_PASSWORD", ""));
        Map<String, Integer> m = new LinkedHashMap<>();
        m.put("k", 1);
        m.put("j", 2);
        String[][] cases = {
            {"INSERT INTO t_map VALUES (1, ?)", "map"},
            {"INSERT INTO t_map SELECT 2, ?", "map"},
            {"SELECT toString(CAST(? AS Map(String, Int32)))", "map"},
            {"SELECT toString(CAST(? AS Map(String, Int32)))", "empty"},
            {"SELECT id FROM t_map WHERE m = ?", "map"},
            {"SELECT toString(mapKeys(?))", "map"},
            {"SELECT toString(?)", "list of maps"},
            {"SELECT toString(?)", "list"},
        };
        try (Connection c = new com.clickhouse.jdbc.Driver().connect("jdbc:clickhouse://" + System.getenv().getOrDefault("CH_HOST", "localhost") + ":8123/default", p);
             Statement st = c.createStatement()) {
            st.execute("DROP TABLE IF EXISTS t_map");
            st.execute("CREATE TABLE t_map (id Int32, m Map(String, Int32)) ENGINE = MergeTree ORDER BY id");
            for (String[] tc : cases) {
                Object v = tc[1].equals("map") ? m : tc[1].equals("empty") ? Collections.emptyMap()
                        : tc[1].equals("list") ? Arrays.asList(1, 2) : Arrays.asList(m, m);
                try (PreparedStatement ps = c.prepareStatement(tc[0])) {
                    ps.setObject(1, v);
                    ps.execute();
                    System.out.printf("OK    %-48s %s%n", tc[0], tc[1]);
                } catch (SQLException e) {
                    String msg = e.getMessage();
                    System.out.printf("FAIL  %-48s %s: %s%n", tc[0], tc[1],
                            msg.substring(msg.indexOf("Syntax error")).split("\\(version")[0].trim());
                }
            }
        }
    }
}

Configuration

Client Configuration

// default properties, user default

Environment

  • Cloud
  • Client version: jdbc-v2 0.12.0-rc1-SNAPSHOT, main at f27f886
  • Language version: Java 17.0.21.1
  • OS: Linux (Docker)

ClickHouse Server

  • ClickHouse Server version: 26.11.7.10
  • ClickHouse Server non-default settings, if any: none
  • CREATE TABLE statements for tables involved: in the code example
  • Sample data for all these tables: in the code example

Classification: impact: none · area: Queries (Map parameters)


Found by automated analysis (Polyglot AI) while working on ClickHouse/clickhouse-cs#815. Verified against a live server.

Activity

  1. added
    jdbc
    queries
    on Dec 5, 2026
  2. Inf1mum commented on Dec 5, 2026

    @Inf1mum

    Reproduced on jdbc-v2 against server 26.11.11: with setObject(1, map), SELECT CAST(? AS Map(String, Int32)) fails with the same Code: 62 ... Expected substitution name (identifier). (SYNTAX_ERROR), because the map is rendered into the SQL text as {'k': 1,'j': 2}, which the server only parses in the Values input format.

    I'll take this one: the Map branch of PreparedStatementImpl#encodeObject will emit a map(key, value, ...) expression instead, which parses in any position and also works in INSERT ... VALUES.

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

    bugjdbcqueries

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions