{"record":{"id":"4e22d6bb5e0f18e3","repo":"apache/shardingsphere","slug":"column-index-out-of-range-s","errorCode":null,"errorMessage":"Column index out of range: %s","messagePattern":"Column index out of range: (.+?)","errorType":"exception","errorClass":"SQLException","httpStatus":null,"severity":"error","filePath":"infra/executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/execute/result/query/impl/raw/metadata/RawResultSetMetaData.java","lineNumber":198,"sourceCode":"            case Types.BINARY:\n            case Types.VARBINARY:\n            case Types.LONGVARBINARY:\n                return byte[].class.getName();\n            case Types.CHAR:\n            case Types.VARCHAR:\n            case Types.LONGVARCHAR:\n            case Types.NCHAR:\n            case Types.NVARCHAR:\n            case Types.LONGNVARCHAR:\n                return String.class.getName();\n            default:\n                return Object.class.getName();\n        }\n    }\n    \n    private RawQueryResultColumnMetaData getColumnMetaData(final int column) throws SQLException {\n        if (column < 1 || column > columns.size()) {\n            throw new SQLException(String.format(\"Column index out of range: %s\", column));\n        }\n        return columns.get(column - 1);\n    }\n    \n    @Override\n    public <T> T unwrap(final Class<T> iface) throws SQLException {\n        if (isWrapperFor(iface)) {\n            return iface.cast(this);\n        }\n        throw new SQLFeatureNotSupportedException(String.format(\"`%s` cannot be unwrapped as `%s`\", getClass().getName(), iface.getName()));\n    }\n    \n    @Override\n    public boolean isWrapperFor(final Class<?> iface) {\n        return iface.isInstance(this);\n    }\n}\n","sourceCodeStart":180,"sourceCodeEnd":216,"githubUrl":"https://github.com/apache/shardingsphere/blob/e952770a215630a3659c75d64369168cd3e26b82/infra/executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/execute/result/query/impl/raw/metadata/RawResultSetMetaData.java#L180-L216","documentation":"RawResultSetMetaData.getColumnMetaData validates the 1-based column index before delegating metadata lookups (getColumnName, getColumnType, getColumnTypeName, getColumnClassName, etc.) on results backed by raw (non-JDBC-driver) column lists, e.g. federation or locally built metadata. Any index < 1 or > columns.size() throws a plain SQLException 'Column index out of range: %s', mirroring the contract of standard JDBC drivers.","triggerScenarios":"Iterating metadata with a 0-based loop (for i = 0; i < md.getColumnCount(); i++ then md.getColumnName(i)); calling md.getColumnName(n) where n exceeds the projected column count after ShardingSphere rewrites or merges the query; stale cached column count after a re-executed statement.","commonSituations":"Off-by-one bugs ported from 0-based APIs; result-set merging (DISTINCT, aggregation, ORDER BY) producing fewer columns than the original SQL; code that assumes the driver tolerates out-of-range probes instead of throwing.","solutions":["Use 1-based iteration: for (int i = 1; i <= md.getColumnCount(); i++).","Guard every lookup: if (column >= 1 && column <= md.getColumnCount()) before calling any getXxx(column).","Re-read metadata after re-execution instead of caching column counts across statements."],"exampleFix":"// before\nfor (int i = 0; i < metaData.getColumnCount(); i++) {\n    names.add(metaData.getColumnName(i));\n}\n\n// after\nfor (int i = 1; i <= metaData.getColumnCount(); i++) {\n    names.add(metaData.getColumnName(i));\n}","handlingStrategy":"validation","validationCode":"int n = metaData.getColumnCount();\nif (column < 1 || column > n) { throw new IllegalArgumentException(\"column \" + column + \" not in 1..\" + n); }","typeGuard":null,"tryCatchPattern":"try { name = metaData.getColumnName(column); } catch (SQLException e) { /* log and skip column */ }","preventionTips":["Always treat JDBC metadata indexes as 1-based in loops and lookups.","Never cache column counts across re-executions of a statement."],"tags":["jdbc","metadata","off-by-one","result-set"],"backgroundTag":null,"analyzedSha":"e952770a215630a3659c75d64369168cd3e26b82","analyzedAt":"2026-08-14T13:54:53.392Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}