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 |
|
@manticore-projects Following up on the AST querying idea, I compared 59 DDL/expression cases on 5.4 and current master (
These seem complementary to AST querying: enriching the model would give the querying layer structured fields to traverse. I suggest handling them in focused PRs that preserve existing accessors and rendering, while keeping general selection/traversal design in this discussion. The quoted-name and negative-scale parser fixes are already in #2638. Does that separation fit your plan, or are there node/property conventions you would like those AST additions to follow for the future querying API? |
|
I spent some tokens quickly to mock something: get me the where expression of the second union query of the 3rd with clause becomes a simple path Expression w = AstPath.get(root, "/withItemsList[2]/select/selects[1]/where", Expression.class);Three variants, all verified against the mock:
Main problem is: our path expressions would be unstable. Any API change and a formerly working path possible won't work. Also there is no schema to verify a path against. Maybe we would need to create such schemas using JAXB. Also, there's no sibling axis, so "the union branch after the one matching X" isn't expressible. We would need to add following-sibling/preceding-sibling yet. |
In my opinion, developing a generic |
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.