Skip to content

ArrayIndexOutOfBoundsException, no query parameters found, when using keyword as column alias #2478

Closed

Description

@jploski
 - jdbc-v2
  - sql-parser

Description

Reporting against 0.8.6.

ParsedPreparedStatement has argCount == 0 and ArrayIndexOutOfBoundsException occurs while trying to assign values of named/positional parameters - even though the SQL query template contains the parameters. The trigger for the issue appears to be using a keyword as a column alias, not prefixed with "AS".

Steps to reproduce

  1. create table test(val integer, dt date) ENGINE=MergeTree() ORDER BY val;
  2. select toYear(dt) year from test WHERE val=1; /* works without problems in CLI */
  3. Try parsing "select toYear(dt) year from test WHERE val=?" - no parameters detected!
  4. Try parsing "select toYear(dt) AS year from test WHERE val=?" - the parameter is detected correctly.
  5. Try parsing "select toYear(dt) yearx from test WHERE val=?" - the parameter is also detected correctly.

Error Log or Exception StackTrace

java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
	at com.clickhouse.jdbc.PreparedStatementImpl.setLong(PreparedStatementImpl.java:173)
	at org.apache.commons.dbcp.DelegatingPreparedStatement.setLong(DelegatingPreparedStatement.java:123)
	at org.apache.commons.dbcp.DelegatingPreparedStatement.setLong(DelegatingPreparedStatement.java:123)
	at org.hibernate.type.descriptor.sql.BigIntTypeDescriptor$1.doBind(BigIntTypeDescriptor.java:46)
	at org.hibernate.type.descriptor.sql.BasicBinder.bind(BasicBinder.java:73)
	at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:276)
	at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:271)
	at org.hibernate.loader.custom.sql.NamedParamBinder.bind(NamedParamBinder.java:34)
	at org.hibernate.loader.custom.CustomLoader.bindParameterValues(CustomLoader.java:475)
	at org.hibernate.loader.Loader.bindPreparedStatement(Loader.java:2150)
	at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:2127)
	at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:2059)
	at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:2037)
	at org.hibernate.loader.Loader.doQuery(Loader.java:956)
	at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:357)
	at org.hibernate.loader.Loader.doList(Loader.java:2868)
	at org.hibernate.loader.Loader.doList(Loader.java:2850)
	at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2682)
	at org.hibernate.loader.Loader.list(Loader.java:2677)
	at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:338)
	at org.hibernate.internal.StatelessSessionImpl.listCustomQuery(StatelessSessionImpl.java:680)
	at org.hibernate.internal.AbstractSharedSessionContract.list(AbstractSharedSessionContract.java:1204)
	at org.hibernate.query.internal.NativeQueryImpl.doList(NativeQueryImpl.java:177)
	at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1617)
	... 21 more

Expected Behaviour

Parsing both versions of the query (with or without AS) should result in one assignable positional parameter.

Code Example

Not included as my test case comes from Hibernate context. But I'm pretty sure Hibernate is not required to reproduce this because I see the named parameter of the query correctly translated to a positional parameter (?) in ConnectionImpl.prepareStatement.

Configuration

Not relevant

Client Configuration

Not relevant

Environment

  • Cloud
  • Client version: 0.8.6
  • Language version: Java
  • OS: not relevant

ClickHouse Server

  • ClickHouse Server version: not relevant (24.12.1.1614)
  • ClickHouse Server non-default settings, if any:
  • CREATE TABLE statements for tables involved: see above in steps to reproduce
  • Sample data for all these tables: not required

Activity

  1. jploski commented on Jun 30, 2025

    @jploski
    Author

    Note: this issue did not exist in version 0.2.6 of the JDBC driver, we spotted it as regression while trying to upgrade.

  2. jploski commented on Jul 19, 2025

    @jploski
    Author

    Adding patch for PreparedStatementTest.java to demonstrate/reproduce this bug:

    test_jdbc_select_as_2478.patch.txt

  3. added this to the 0.9.1 milestone on Jul 23, 2025
  4. chernser commented on Jul 24, 2025

    @chernser
    Contributor

    @jploski
    Thank you for reporting the problem!
    Is it because alias uses interval name year. We will look into it.

  5. self-assigned this
    on Jul 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions