Skip to content

Error handling the CTE INSERT command: Failed to parse SQL. #3279

Open

Description

@fiend19961
High Level Area Labels: 
 - jdbc-v2

Specific Area Labels:
  - jdbc-insert
  - client-insert

Description

Steps to reproduce

  1. for examples , create table test123
CREATE TABLE IF NOT EXISTS test123
(
   `timestamp`     DateTime64(9, 'UTC'),
    `eid`       UInt64,
) ENGINE = MergeTree()
        PARTITION BY toYYYYMMDD(timestamp)
        ORDER BY (timestamp);
  1. execute this sql
with a as (
SELECT timestamp,eid
FROM test123 
),
b as ( SELECT timestamp,eid
FROM test123 )

insert into test123(`timestamp`,eid) 
SELECT a.`timestamp`,b.eid
FROM a left join b on a.eid =b.eid ;
  1. The SQL server executed successfully, but the client-side parsing failed,Maybe this is a bug in the JavaCC parser

Error Log or Exception StackTrace

WARN  c.c.j.i.p.j.ClickHouseSqlParser [76] - Failed to parse SQL. If you believe the SQL is valid, please feel free to open an issue on GitHub with this warning. Enable DEBUG logging for details.

Expected Behaviour

Code Example

Configuration

Client Configuration

Environment

  • Client version: JDBC-0.11.0
  • Language version: JDK17
  • OS: linux,windows10

ClickHouse Server

  • ClickHouse Server version: 25.8.24.21

Activity

  1. added
    jdbc
    sql-parser
    on Oct 30, 2026
  2. added this to the 0.12.0 milestone on Oct 30, 2026
  3. removed this from the 0.12.0 milestone on Nov 10, 2026
  4. fiend19961 commented on Nov 12, 2026

    @fiend19961
    Author
  5. chernser commented on Nov 17, 2026

    @chernser
    Contributor

    @fiend19961
    Thank you for reporting!

    Will be fixed as part of #3300

    This is a logging issue because client may not follow ClickHouse in syntax parsing and we just use parser to get needed information.
    Fix in parser may break someone`s case.

  6. added
    queries
    on Nov 26, 2026
  7. polyglotAI-bot commented on Nov 27, 2026

    @polyglotAI-bot
    Collaborator

    Recommendation: Keep open — fix needed
    Only the log is wrong: the statement runs and gives the correct update count. Open PR #3300 removes this WARN (its parser change alone makes the run below print none), but it has merge conflicts with main and a change request.

    • Category: Bug
    • Classification: impact: none · area: Queries (SQL parser)
    • Status on main: Reproduced on main — the issue's statement logs the WARN for each run; execute() and executeUpdate() give the same results as an INSERT without WITH.
    • Breaking change: No — the removal of the WARN changes only the log output.
    • Duplicates / related: No others

    Analysis: The default JavaCC grammar accepts WITH only in front of SELECT (selectStmt, ClickHouseSqlParser.jj:668), and insertStmt starts with INSERT INTO (:594). So the parse fails, ClickHouseSqlParser.jj:101 logs the WARN, and the statement stays UNKNOWN; the run shows that this does not change the execute() result or the update count.

    Reproduction: complete program and output

    Changed from the issue's steps: one row in the table, executeUpdate(), and an INSERT without WITH as a contrast. Run with slf4j-simple on the classpath, so that the WARN prints.

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;
    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", "");
    
        static final String CTE_INSERT = "with a as (\nSELECT timestamp,eid\nFROM test123 \n),\nb as ( SELECT timestamp,eid\nFROM test123 )\n\n"
                + "insert into test123(`timestamp`,eid) \nSELECT a.`timestamp`,b.eid\nFROM a left join b on a.eid =b.eid ;";
        static final String PLAIN_INSERT = "insert into test123(`timestamp`,eid) SELECT timestamp, eid FROM test123";
    
        public static void main(String[] args) throws Exception {
            Properties props = new Properties();
            props.setProperty("user", "default");
            props.setProperty("password", PASSWORD);
            String jdbcUrl = "jdbc:clickhouse:" + URL.replaceFirst("^https?:", "") + "/default";
            try (Connection conn = DriverManager.getConnection(jdbcUrl, props);
                 Statement stmt = conn.createStatement()) {
                stmt.execute("DROP TABLE IF EXISTS test123");
                stmt.execute("CREATE TABLE IF NOT EXISTS test123\n(\n   `timestamp`     DateTime64(9, 'UTC'),\n    `eid`       UInt64,\n)"
                        + " ENGINE = MergeTree()\n        PARTITION BY toYYYYMMDD(timestamp)\n        ORDER BY (timestamp);");
                stmt.execute("INSERT INTO test123 VALUES ('2024-01-01 00:00:00', 1)");
    
                try {
                    boolean isResultSet = stmt.execute(CTE_INSERT);
                    System.out.println("CTE INSERT execute(): " + isResultSet + ", getUpdateCount(): " + stmt.getUpdateCount()
                            + ", rows now: " + count(stmt));
                } catch (Exception e) {
                    System.out.println("CTE INSERT execute(): " + e.getClass().getName() + ": " + e.getMessage());
                }
                try {
                    System.out.println("CTE INSERT executeUpdate(): " + stmt.executeUpdate(CTE_INSERT) + ", rows now: " + count(stmt));
                } catch (Exception e) {
                    System.out.println("CTE INSERT executeUpdate(): " + e.getClass().getName() + ": " + e.getMessage());
                }
                try {
                    boolean isResultSet = stmt.execute(PLAIN_INSERT);
                    System.out.println("INSERT without WITH execute(): " + isResultSet + ", getUpdateCount(): " + stmt.getUpdateCount()
                            + ", rows now: " + count(stmt));
                } catch (Exception e) {
                    System.out.println("INSERT without WITH execute(): " + e.getClass().getName() + ": " + e.getMessage());
                }
                stmt.execute("DROP TABLE test123");
            }
        }
    
        static long count(Statement stmt) throws Exception {
            try (ResultSet rs = stmt.executeQuery("SELECT count() FROM test123")) {
                rs.next();
                return rs.getLong(1);
            }
        }
    }

    Output on main (8480ca001), ClickHouse 26.11.2.4:

    WARN ClickHouseSqlParser - Failed to parse SQL. If you believe the SQL is valid, …
    CTE INSERT execute(): false, getUpdateCount(): 1, rows now: 2
    WARN ClickHouseSqlParser - Failed to parse SQL. If you believe the SQL is valid, …
    CTE INSERT executeUpdate(): 4, rows now: 6
    INSERT without WITH execute(): false, getUpdateCount(): 6, rows now: 12
    

    Output with the ClickHouseSqlParser.jj change of PR #3300 (the full diff does not apply to main):

    build=0
    CTE INSERT execute(): false, getUpdateCount(): 1, rows now: 2
    CTE INSERT executeUpdate(): 4, rows now: 6
    INSERT without WITH execute(): false, getUpdateCount(): 6, rows now: 12
    

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

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

    bugjdbcqueriessql-parser

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions