Skip to content

NullPointerException when creating PreparedStatement for INSERT statement when inserting into certain tables #2450

@lukaseder

Description

@lukaseder

Description

It's not possible to insert into certain table names using prepared statements, unless the table names are quoted. Static statements don't require quoting.

Steps to reproduce

try (Statement s = connection.createStatement()) {
    try {
        s.executeUpdate("create table events (s String) engine Log()");
        s.executeUpdate("insert into events (s) values ('a')");

        try (PreparedStatement p = connection.prepareStatement("insert into events (s) values ('a')")) {
            p.executeUpdate();
        }
    }
    finally {
        s.executeUpdate("drop table events");
    }
}

Error Log or Exception StackTrace

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "org.antlr.v4.runtime.tree.TerminalNode.getText()" because the return value of "com.clickhouse.jdbc.internal.ClickHouseParser$IdentifierContext.IDENTIFIER()" is null
	at com.clickhouse.jdbc.internal.ParsedPreparedStatement.enterInsertStmt(ParsedPreparedStatement.java:218)
	at com.clickhouse.jdbc.internal.ClickHouseParser$InsertStmtContext.enterRule(ClickHouseParser.java:7712)
	at org.antlr.v4.runtime.tree.ParseTreeWalker.enterRule(ParseTreeWalker.java:50)
	at org.antlr.v4.runtime.tree.ParseTreeWalker.walk(ParseTreeWalker.java:33)
	at org.antlr.v4.runtime.tree.ParseTreeWalker.walk(ParseTreeWalker.java:36)
	at com.clickhouse.jdbc.internal.SqlParser.parsePreparedStatement(SqlParser.java:40)
	at com.clickhouse.jdbc.ConnectionImpl.prepareStatement(ConnectionImpl.java:383)
	at com.clickhouse.jdbc.ConnectionImpl.prepareStatement(ConnectionImpl.java:161)
	at org.jooq.testscripts.JDBC.main(JDBC.java:43)

Expected Behaviour

Since the static statement works, the prepared statement should work as well.

A workaround is to quote the name: "events".

Environment

  • Client version: 0.9.0
  • Language version: OpenJDK Runtime Environment Temurin-21.0.2+13 (build 21.0.2+13-LTS)
  • OS: Microsoft Windows [Version 10.0.26100.4061]

ClickHouse Server

  • ClickHouse Server version: 25.5.2.47

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions