#42222·dbeaver

Query is never executed when an aggregate has both an inner `ORDER BY` and a `FILTER` clause (NPE in JSqlParser)

Author: boxferrari-2312Created Sep 25, 2026Updated Sep 28, 2026
Labelsxf:sql executionregression

Description

Summary

A query combining an inner ORDER BY with a FILTER (WHERE ...) clause in the same aggregate is never sent to the server. There is no error dialog: the results tab opens showing "No data", or the Ctrl+Enter shortcut behaves like a plain Enter and inserts a line break.

The cause is a NullPointerException thrown by the bundled JSqlParser while DBeaver analyses the statement to decide whether it modifies data. The exception aborts execution before the query reaches the database.

java.lang.NullPointerException: Cannot invoke "java.util.List.iterator()"
because the return value of "net.sf.jsqlparser.expression.AnalyticExpression.getOrderByElements()" is null

Symptoms

Pressing Ctrl+Enter produces one of three behaviours, none of which reports an error:

  1. the results tab opens showing "No data — Execute query with Ctrl+Enter…" and the query never runs
  2. nothing happens at all
  3. the shortcut behaves like a plain Enter, inserting a line break at the cursor

Alt+X (Execute script) does surface a message — "An internal error occurred" — with the same NPE text.

Selecting several statements at once and executing them works, which is consistent with the analysis below: that path does not call isMutatingStatement.

Stack trace

SQLEditorHandlerExecute.execute                    <- Ctrl+Enter
  SQLEditor.processSQL
    QueryProcessor.processQueries
      ResultSetViewer.resetDataFilter              <- opens the results tab ("No data")
        ResultSetViewer.setDataFilter
          ResultSetViewer.updateFiltersText
            ResultSetViewer.supportsDataFilter
              DBSDataContainer.isFeatureSupported
                QueryResultsContainer.getSupportedFeatures
                  SQLQuery.isModifying
                    SQLQuery.isMutatingStatement   <- "does this query modify data?"
                      TablesNamesFinder.getTables  <- JSqlParser walks the AST
                        visit(SelectItem)             TablesNamesFinder.java:358
                          visit(CastExpression)       TablesNamesFinder.java:972
                            visit(AnalyticExpression) TablesNamesFinder.java:802
                              NullPointerException

DBeaver opens the results tab before running the query, to decide whether the grid can show its filter bar. Answering that requires knowing whether the statement modifies data, which sends JSqlParser walking the AST. The visitor throws, the exception propagates, and execution is aborted — so the tab exists but the query never ran.

Analysis

TablesNamesFinder.visit(AnalyticExpression) calls getOrderByElements().iterator() with no null check. AnalyticExpression.getOrderByElements() returns null when the expression carries no ORDER BY of its own — documented behaviour, and other visitors in JSqlParser guard against it.

JSqlParser models an aggregate carrying a FILTER (WHERE ...) clause as an AnalyticExpression. When the aggregate also has an inner ORDER BY, the resulting tree apparently leaves orderByElements unset on the node the visitor reaches, and the unguarded iteration throws.

This looks closely related to JSQLParser/JSqlParser#2655 — fix: support FILTER after ordered-set aggregates, which addresses the same shape (percentile_cont(0.5) WITHIN GROUP (ORDER BY score) FILTER (WHERE active)).

Both FILTER and inner ORDER BY are standard SQL and common in PostgreSQL: string_agg(x, ',' order by y) filter (where z) is an everyday pattern for building an ordered list from a subset of rows.

Impact

The failure is silent, which makes it hard to attribute. In our case the statement was a reporting query; the first assumption was a syntax problem, and it took a while to establish that the SQL was valid and never reached the server. Users hitting this will most likely blame their own SQL.

It also fails intermittently across similar queries: a statement JSqlParser cannot fully parse for other reasons appears to bail out earlier and therefore executes, so the same construct works in one query and not in another.

dbeaver-diagnostic-info-redacted.zip

The attached diagnostic bundle contains 178 occurrences of this exception, all with the same stack trace.

DBeaver Version

26.2.1, build 202609210342 (Community, Windows, .exe distribution)

Operating System

Windows 11

Database and driver

|---|---| | JSqlParser | 5.3.0 (bundled) | | Java | 25.0.3 (Microsoft build) | | Database | PostgreSQL 18.6 | | Driver | PostgreSQL JDBC |

Steps to reproduce

Open a SQL editor on any PostgreSQL connection, paste the statement below, put the cursor inside it and press Ctrl+Enter. No table is required.

sql
select string_agg(x::text, ',' order by x) filter (where true) from (values (1),(2)) t(x);

Expected: returns 1,2. Actual: the results tab opens showing "No data"; the query is never executed. The NPE is logged.

The same statement returns 1,2 in psql.

Control cases — both of these work

Removing either half of the combination makes the problem disappear, which isolates the trigger:

sql
-- no inner ORDER BY -> works
select string_agg(x::text, ',') filter (where true) from (values (1),(2)) t(x);

-- no FILTER -> works
select string_agg(x::text, ',' order by x) from (values (1),(2)) t(x);

It is the combination that fails. An aggregate with FILTER but no inner ORDER BY executes normally — for example sum(x) filter (where true)::numeric(14,2) is fine, with or without a cast, a GROUP BY or a join.

Additional context

Suggested fix

Two independent improvements, either of which resolves the symptom:

  1. Upgrade the bundled JSqlParser. Current master has the null guard:
java
if (aexpr.getOrderByElements() != null) {
    for (OrderByElement ob : aexpr.getOrderByElements()) {
        ob.accept(this);
    }
}
  1. Make SQLQuery.isMutatingStatement fail safe. A parser error there should degrade gracefully rather than abort execution — the result only decides whether the grid shows its filter bar, so falling back to a conservative default would be far better than not running the user's query.