Skip to content

Fix DBeaver IndexOutOfBoundsException when a file starts with SET - #3182

Merged
nedtwigg merged 2 commits into
diffplug:mainfrom
toyeshhm:fix-1552-dbeaver-set-index
Nov 10, 2026
Merged

nedtwigg merged 2 commits into
diffplug:mainfrom
toyeshhm:fix-1552-dbeaver-set-index

Conversation

@toyeshhm

@toyeshhm toyeshhm commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

Fixes #1552.

Problem

Formatting a SQL file that starts with SET (for example H2's SET MODE PostgreSQL;) prints a stack trace to stderr:

java.lang.IndexOutOfBoundsException: Index -1 out of bounds for length 6
	at com.diffplug.spotless.sql.dbeaver.SQLTokenizedFormatter.insertReturnAndIndent(SQLTokenizedFormatter.java:434)
	at com.diffplug.spotless.sql.dbeaver.SQLTokenizedFormatter.format(SQLTokenizedFormatter.java:200)

The file still gets formatted, because the exception is caught, but every such file dumps a trace into the build output.

Root cause

SET shares a case with FROM, WHERE, ORDER BY and friends, which all call insertReturnAndIndent(argList, index, indent - 1) to put a line break before the keyword. When the keyword is the first token, index is 0. The token at 0 is neither whitespace nor a statement delimiter, so insertReturnAndIndent looks at the previous token with argList.get(argIndex - 1), which is get(-1). The catch (IndexOutOfBoundsException e) at the bottom of the method then calls e.printStackTrace() and returns 0.

Fix

In insertReturnAndIndent, return 0 before looking at the previous token when argIndex == 0. There is nothing in front of the first token to turn into a line break, so this is the same result the catch block gave, without the exception. The formatted output doesn't change.

Tests

  • Added DBeaverSQLFormatterStepTest.keywordAtStartOfFileDoesNotThrow, which formats sql/dbeaver/set-mode.dirty (SET MODE PostgreSQL;), checks the output against set-mode.clean, and asserts nothing was written to System.err.
    • On main it fails: stderr contains the IndexOutOfBoundsException trace above.
    • With the fix it passes.
  • ./gradlew :testlib:test --tests 'com.diffplug.spotless.sql.*' (all 5 pass), ./gradlew spotlessCheck :lib:spotbugsMain.

Copilot AI balanced review requested due to automatic review settings November 9, 2026 21:21

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

@nedtwigg
nedtwigg enabled auto-merge November 10, 2026 06:14
nedtwigg added a commit to glaforge/spotless that referenced this pull request Nov 10, 2026
@nedtwigg
nedtwigg merged commit 36b54cf into diffplug:main Nov 10, 2026
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

dbeaver throws java.lang.IndexOutOfBoundsException on set mode

3 participants