Skip to content

The #7774 parenthesis fix renames unaliased projections: SELECT (name).asString() FROM T is now the column "(name).asString()" #7896

Closed

Description

@rspereiratech

Summary

Regression introduced by PR #7845 (commit 5f4ee25c0c, "Fix issues #7774, #7773, #7766, #7765"), in the part that fixes #7774.

#7774 was that a method call on a NULL literal was silently discarded: SQLASTBuilder.visitNullBaseExpr never built the modifier chain, so null.ifNull('default') answered bare null. That half is correctly fixed. The second half of the fix changed BaseExpression.rendersParentheses() to also return true whenever the expression carries a modifier, so that a re-rendered (42).asString() does not become the unparseable 42.asString().

But rendersParentheses() sits on the projection-naming path: the text an expression renders to is the name an unaliased projection column gets. Adding modifier != null to the condition therefore renamed every unaliased projection that parenthesises an expression carrying a method or selector chain.

Before: SELECT (name).asString() FROM T returned a column called name.asString().
Now: it returns a column called (name).asString(). A client that reads result.getProperty("name.asString()") gets null after upgrading.

The method's own javadoc, written in the same file, warns about exactly this hazard two paragraphs above the change: "printing them would change the name every unaliased projection written that way has always had".

The parentheses are only load-bearing when the inner atom cannot legally carry a modifier on its own, which is a NUMBER, NULL or a boolean literal (42.asString() does not parse). For an identifier, a string literal, a function call, a map or array literal or a CASE block, X.method() parses exactly as (X).method() and the parentheses could be dropped as they were before.

Where (HEAD 06b033d)

engine/src/main/java/com/arcadedb/query/sql/parser/BaseExpression.java:134-151

  /**
   * Whether the written parentheses have to be rendered back, which is only where dropping them could change what the
   * text means: around a COMPOUND arithmetic expression, whose operators would otherwise be re-associated by the
   * surrounding precedence - {@code (1 + 2) * 3} flattened to {@code 1 + 2 * 3} is 7 rather than 9.
   * <p>
   * Parentheses around anything that already renders as one atom - an identifier, a literal, a function call, a map or
   * array literal, a CASE block - carry no meaning, and printing them would change the name every unaliased projection
   * written that way has always had ({@code SELECT (name) FROM V} is a column called {@code name}).
   * <p>
   * Also load-bearing when this expression itself carries a {@link #modifier} chain: most literal grammar
   * alternatives have no bare {@code modifier*} form of their own (only {@code NULL modifier*} does), so
   * {@code (42).asString()} needs the parentheses back or it re-renders as the unparseable {@code 42.asString()}
   * (issue #7774).
   */
  private boolean rendersParentheses() {
    return parenthesized && expression != null
        && (modifier != null || expression.mathExpression != null && !expression.mathExpression.operators.isEmpty());
  }

Before the PR the body read:

    return parenthesized && expression != null && expression.mathExpression != null
        && !expression.mathExpression.operators.isEmpty();

The single caller, engine/src/main/java/com/arcadedb/query/sql/parser/BaseExpression.java:119-127, is the render that names the column:

    else if (expression != null) {
      final boolean parentheses = rendersParentheses();
      if (parentheses)
        builder.append('(');
      expression.toString(params, builder);
      if (parentheses)
        builder.append(')');

Repro

One document type T with a single record {name: 'bob'}, then the column names each unaliased projection produces:

SELECT (name).asString() FROM T
   columns = [(name).asString()]   -> {"(name).asString()":"bob"}
SELECT name.asString() FROM T
   columns = [name.asString()]   -> {"name.asString()":"bob"}
SELECT ('a').append('b') FROM T
   columns = [('a').append('b')]   -> {"('a').append('b')":"ab"}
SELECT (null).ifNull('d') FROM T
   columns = [(NULL).ifNull('d')]   -> {"(NULL).ifNull('d')":"d"}
SELECT (42).asString() FROM T
   columns = [(42).asString()]   -> {"(42).asString()":"42"}
SELECT (name) FROM T
   columns = [name]   -> {"name":"bob"}

Line 1 is the regression: before the PR that column was named name.asString(), the same as line 2. Line 6 shows the unchanged case the javadoc describes, and line 5 shows the one case where the parentheses genuinely have to come back.

Why the PR's own test did not catch it

engine/src/test/java/com/arcadedb/query/sql/method/Issue7774NullModifierChainTest.java:57-73:

  @Test
  void reRenderingAParenthesizedNullModifierChainStaysReparseableAndKeepsTheSameAnswer() {
    final String rendered = renderStatement("SELECT (null).ifNull('default') AS r");
    // The written parentheses must survive the round trip: they carry the modifier chain now that a bare
    // NULL.ifNull(...) is itself meaningful, so dropping them must not change what the text means.
    assertThat(rendered).containsIgnoringCase("(null)");
    assertThat(scalar(rendered)).isEqualTo("default");
  }

  @Test
  void reRenderingAParenthesizedNumericLiteralModifierChainStaysReparseable() {
    // NUMBER has no bare `modifier*` grammar alternative of its own (only NULL does), so the parentheses are the
    // only thing standing between a valid statement and a syntax error once it is rendered back to text.
    final String rendered = renderStatement("SELECT (42).asString() AS r");
    assertThat(rendered).contains("(42)");
    assertThat(scalar(rendered)).isEqualTo("42");
  }

Two reasons it is blind to this. Every statement in the file uses an explicit AS r alias, so the projection-naming path the change affects is never exercised. And both re-render tests use the two atoms that genuinely need the parentheses, NULL and a NUMBER literal, never an identifier, a string literal or a function call, which are exactly the atoms where the new parentheses are gratuitous.

Suggested fix

Restrict the new disjunct to the atoms that cannot carry a modifier unparenthesised, which is all (42).asString() needs. Something of the shape:

  private boolean rendersParentheses() {
    return parenthesized && expression != null
        && ((modifier != null && innerAtomCannotCarryAModifierUnparenthesised())
            || expression.mathExpression != null && !expression.mathExpression.operators.isEmpty());
  }

where the new predicate asks whether the parenthesised expression bottoms out in a NUMBER, NULL or a boolean literal, that is the alternatives with no bare modifier* form in the grammar. An identifier, string literal, function call, map/array literal or CASE block then renders unparenthesised again and keeps its historical column name.

The two existing tests in Issue7774NullModifierChainTest continue to pass under that condition. Worth adding alongside them: an unaliased SELECT (name).asString() FROM T asserting the column is named name.asString(), and the same for a string literal and a function call.

Scope / confidence

Reproduced at HEAD 06b033d against a freshly built engine.

  • Blast radius: any client that reads an unaliased projection by name, where the projection was written with redundant parentheses around an identifier, string literal, function call, map or array literal or CASE block followed by a method or selector chain. Silent: the query still succeeds and returns the right value, just under a different key.
  • copy() carries parenthesized and the modifier (BaseExpression.copy()), so there is no separate copy-related hole here.
  • Not affected: aliased projections, parenthesised compound arithmetic (which rendered its parentheses before the PR too), and (42).asString() / (null).ifNull(...), which are the cases the change was for.
  • Falsified if the project decides the rendered text of a parenthesised expression should include the parentheses and the column-name change is acceptable. That would contradict the method's own javadoc and would want a release note, since it is a silent rename of result keys.

Activity

  1. added
    engine
    on Sep 18, 2026
  2. added this to the 26.10.1 milestone on Sep 18, 2026
  3. added
    severity:major
    on Sep 19, 2026
  4. added
    in progress
    on Sep 21, 2026
  5. self-assigned this
    on Sep 21, 2026
  6. added 2 commits that reference this issue on Sep 21, 2026
  7. lvca commented on Sep 21, 2026

    @lvca
    Member

    Fixed in 26.10.1.

    An unaliased projection is named after the text it is written as, and a recent fix had started keeping redundant parentheses in that text. SELECT (name).asString() FROM T returned a column called (name).asString() instead of name.asString(), so reading the result by its usual key gave back nothing.

    Parentheses are now kept only where dropping them would change what the query means, which is around a number, NULL, a boolean or a RID. Everywhere else they go back to being ignored, and the column name is what it always was.


    ❤️ Thanks for helping make ArcadeDB better! ArcadeDB is free and open source, sustained by its community. If it's useful to you or your company, please consider becoming a sponsor to keep development going.

  8. removed
    in progress
    on Sep 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugengineseverity:majorsql

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions