{"record":{"id":"b0a0e580c25d27f8","repo":"apache/shardingsphere","slug":"column-index-d-is-out-of-range","errorCode":null,"errorMessage":"Column index '%d' is out of range.","messagePattern":"Column index '(.+?)' is out of range\\.","errorType":"exception","errorClass":"ColumnIndexOutOfRangeException","httpStatus":null,"severity":"error","filePath":"jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/resultset/ShardingSphereResultSetMetaData.java","lineNumber":110,"sourceCode":"            checkColumnIndex(column);\n            return ((SelectStatementContext) sqlStatementContext).getProjectionsContext().getExpandProjections().get(column - 1).getColumnLabel();\n        }\n        return resultSetMetaData.getColumnLabel(column);\n    }\n    \n    @Override\n    public String getColumnName(final int column) throws SQLException {\n        if (sqlStatementContext instanceof SelectStatementContext && ((SelectStatementContext) sqlStatementContext).containsDerivedProjections()) {\n            checkColumnIndex(column);\n            return ((SelectStatementContext) sqlStatementContext).getProjectionsContext().getExpandProjections().get(column - 1).getColumnName();\n        }\n        return resultSetMetaData.getColumnName(column);\n    }\n    \n    private void checkColumnIndex(final int column) throws SQLException {\n        List<Projection> actualProjections = ((SelectStatementContext) sqlStatementContext).getProjectionsContext().getExpandProjections();\n        if (column > actualProjections.size()) {\n            throw new ColumnIndexOutOfRangeException(column).toSQLException();\n        }\n    }\n    \n    @Override\n    public String getSchemaName(final int column) {\n        return DefaultDatabase.LOGIC_NAME;\n    }\n    \n    @Override\n    public int getPrecision(final int column) throws SQLException {\n        return resultSetMetaData.getPrecision(column);\n    }\n    \n    @Override\n    public int getScale(final int column) throws SQLException {\n        return resultSetMetaData.getScale(column);\n    }\n    ","sourceCodeStart":92,"sourceCodeEnd":128,"githubUrl":"https://github.com/apache/shardingsphere/blob/e952770a215630a3659c75d64369168cd3e26b82/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/resultset/ShardingSphereResultSetMetaData.java#L92-L128","documentation":"ColumnIndexOutOfRangeException from ShardingSphereResultSetMetaData.checkColumnIndex, used by getColumnName(int): for SELECT statements containing derived projections (ORDER BY items, derived columns added by ShardingSphere), the requested column index must not exceed the expanded projections list size, otherwise the JDBC-standard out-of-range error is thrown.","triggerScenarios":"Calling getColumnName/getColumnLabel on a ShardingSphere ResultSetMetaData with an index greater than the number of expand projections, on a query where ShardingSphere added derived projections (e.g. ORDER BY on a column not in SELECT).","commonSituations":"Report/ORM tools iterating up to getColumnCount() of the underlying driver metadata while the projection context has a different expanded size; index mismatches after sharding rewrites queries with derived order-by columns; off-by-one with 0-based loops.","solutions":["Always loop 1..getColumnCount() inclusive and never assume extra columns beyond it.","Prefer label-based access over positional access for rewritten sharded queries.","If a mismatch between getColumnCount() and projections persists on a derived-projection query, capture SQL + config and report as a potential bug; as a workaround select the ORDER BY column explicitly."],"exampleFix":"// before\nfor (int i = 0; i < md.getColumnCount(); i++) { md.getColumnName(i); } // 0-based -> throws\n// after\nfor (int i = 1; i <= md.getColumnCount(); i++) { md.getColumnName(i); }","handlingStrategy":"validation","validationCode":"int count = md.getColumnCount();\nif (column < 1 || column > count) throw new IndexOutOfBoundsException(\"1..\" + count);","typeGuard":null,"tryCatchPattern":"catch (SQLException ex) { if (column > md.getColumnCount()) fallbackToLabelAccess(); else throw ex; }","preventionTips":["Iterate 1..getColumnCount() inclusive","Prefer labels for rewritten sharded queries","Select ORDER BY columns explicitly to stabilize projections"],"tags":["jdbc","metadata","column-index","sharding"],"backgroundTag":null,"analyzedSha":"e952770a215630a3659c75d64369168cd3e26b82","analyzedAt":"2026-08-14T13:54:53.392Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}