Skip to content

java.lang.ArrayIndexOutOfBoundsException when trying to bind a variable to a query that uses keywords as aliases #2568

Closed

Description

@lukaseder

Description

When using keywords (I think?) as aliases without AS keyword and without quoting, then the JDBC driver fails later on when trying to bind variables. This is due to the JDBC driver's parser failing on the query, despite the query being accepted by the server.

Steps to reproduce

try (PreparedStatement s = connection.prepareStatement(
    """
    select
        1 table
    where
        1 = ?
    """)) {
    s.setInt(1, 1);

    try (ResultSet rs = s.executeQuery()) {
        while (rs.next())
            println(rs.getInt(1));
    }
}

Error Log or Exception StackTrace

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
	at com.clickhouse.jdbc.PreparedStatementImpl.setInt(PreparedStatementImpl.java:180)
	at org.jooq.testscripts.JDBC.main(JDBC.java:46)

Expected Behaviour

The query should work and print 1, since the server also accepts the query as being valid. E.g. this static statement works just fine:

try (Statement s = connection.createStatement()) {
    try (ResultSet rs = s.executeQuery(
        """
        select
            1 table
        where
            1 = 1
        """
    )) {
        while (rs.next())
            println(rs.getInt(1));
    }
}

Alternatively, if these keyword-style aliases are really not supported, then there should be a more explicit error message, but I think they should be supported, since the server supports this syntax.

Workarounds

Use the AS keyword for aliasing

try (PreparedStatement s = connection.prepareStatement(
    """
    select
        1 as table
    where
        1 = ?
    """)) {
    s.setInt(1, 1);

    try (ResultSet rs = s.executeQuery()) {
        while (rs.next())
            println(rs.getInt(1));
    }
}

Quote the identifier

try (PreparedStatement s = connection.prepareStatement(
    """
    select
        1 "table"
    where
        1 = ?
    """)) {
    s.setInt(1, 1);

    try (ResultSet rs = s.executeQuery()) {
        while (rs.next())
            println(rs.getInt(1));
    }
}

Configuration

Environment

  • Client version: com.clickhouse:clickhouse-jdbc:0.9.2
  • Language version: OpenJDK Runtime Environment Temurin-21.0.7+6 (build 21.0.7+6-LTS)
  • OS: Microsoft Windows [Version 10.0.26100.4946]

ClickHouse Server

  • ClickHouse Server version: 25.4.4.25

Activity

  1. added
    jdbc
    sql-parser
    on Sep 9, 2025
  2. barisinanc commented on Sep 10, 2025

    @barisinanc

    I have tried 0.9.1 and 0.9.2 both versions have same problems when updating and deleting records. Is there any version that does not give that error.

  3. lukaseder commented on Sep 10, 2025

    @lukaseder
    Author

    @barisinanc I upgraded from 0.7.0 to 0.9.0/0.9.2 when encountering this error, so 0.7.0 shouldn't have these issues (and tons of others that I've reported recently). I think 0.9.x is not ready for production usage, the parser causing these problems appears to be in an experimental state, see also:

  4. barisinanc commented on Sep 10, 2025

    @barisinanc

    Thank you @lukaseder, We are using spring boot and spring data, I have tried 0.8.5 but encountered a lot more errors. I will try 0.7.0 and report back.

  5. added this to the 0.9.3 milestone on Sep 10, 2025
  6. chernser commented on Oct 27, 2025

    @chernser
    Contributor

    Verified with com.clickhouse.jdbc.internal.BaseSqlParserFacadeTest#testStatementWithoutResultSetDP

  7. added
    queries
    on Nov 26, 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

    bugjdbcqueriessql-parser

    Type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions