Expose structured ORDER BY fragment parsing - #2620
Conversation
|
Mhhh, I don't like this. I believe I do understand the requirement: simpler access to certain fragments, the walking the AST is cumbersome. But instead of specific solutions, I wonder if a kind of "querying" might be the better and more comprehensive solution here. Like XSL for an XML, something like Suggestion: lets put this on small flame for a while and focus on getting a 5.4 release out. And then craft a proper AST querying mechanism for this together. |
|
@manticore-projects |
Consumers that assemble queries from independent sort specifications currently need to wrap fragments in a SELECT or retain them as opaque expressions. For example, DBeaver's ORDER BY handling uses
CustomExpressionfor a sort fragment (pinned consumer code). A fragment API exposes the expression and sort flags directly, so callers can inspect and rewrite them through the ordinary AST.Add
CCJSqlParserUtil.parseOrderByElements(String)and its parser-configuration overload. Input such asCOALESCE(score, 0) DESC NULLS LAST, id ASCproduces aList<OrderByElement>; the input excludes the ORDER BY keywords. Null or empty input returns an empty list, and trailing tokens are rejected.The existing ORDER BY clause now delegates to a shared element-list production. Column-type parsing and the new API also share fragment parser setup, fast/complex retry, full-input checking, and exception handling. Existing column-type behavior is preserved, and each invocation starts with fresh parser state.
Validation: full Java 17 Gradle
checkpasses, including the zero-conflict JavaCC grammar gate, tests, formatting, Checkstyle, PMD, and SpotBugs. Tests compare fragments with complete ORDER BY clauses, exercise configured quoting and parameter indices, verify custom expression deparsing and round trips, and reject incomplete or trailing SQL.