Skip to content

Cached SELECT statement AST is mutated for a $var target: subsequent reuse reads the wrong type (and a first-execution data race) #6669

Closed

Description

@ruispereira

Summary

SelectExecutionPlanner.init() aliases the statement's target without copying it, and a later planner rewrite mutates that shared node in place. Because the Statement object is cached (StatementCache) and shared across executions, a SELECT ... FROM $var whose $var resolves to a type name permanently pins the cached statement (and cached plan) to the first execution's type — a subsequent execution with a different $var binding reads the wrong type. Also a concurrent-first-execution data race on the shared AST.

Where (HEAD 86a704526)

engine/src/main/java/com/arcadedb/query/sql/executor/SelectExecutionPlanner.java:

info.target = this.statement.getTarget();                       // L140 — ALIAS (no .copy())
info.whereClause = ...getWhereClause().copy();                  // L141 — every other clause IS copied
...
String typeName = info.target.getItem().getIdentifier().getStringValue();   // L823
if (typeName.startsWith("$")) {
  typeName = (String) context.getVariable(typeName);
  info.target.getItem().setIdentifier(new Identifier(typeName));            // L826 — mutates the SHARED cached AST node
}

SelectStatement builds the planner on the cached statement (new SelectExecutionPlanner(this), no copy), and a $var-only target reports isCacheable() == true.

Repro

In a script / prepared reuse under one SQL text:

  1. LET $t = "Agent"; SELECT FROM $t WHERE ... → statement S cached; L826 overwrites S's FromItem identifier to "Agent"; plan cached.
  2. Same SQL text with $t = "Queue" → the cached plan (bound to Agent) is returned, or the mutated S (identifier now literally "Agent", no longer $-prefixed) re-plans against Agent.
    Expected rows from Queue; actual rows from Agent. Silent wrong result. Concurrent first-executions also race on S's shared FromItem via the unsynchronized setIdentifier.

Suggested fix

Copy the target in init() (info.target = statement.getTarget() == null ? null : statement.getTarget().copy();) so the $var rewrite mutates a per-planning copy; or resolve $var into info only, never into the shared node; or make a $var target non-cacheable in FromItem.isCacheable().

Scope / confidence

High on the code (the target is the one clause init() doesn't copy, and L826 writes the shared node). Requires a $-variable target bound to a type-name string, reused with a different binding under the same SQL text.

Activity

  1. added this to the 26.9.1 milestone on Aug 24, 2026
  2. self-assigned this
    on Aug 24, 2026
  3. added
    in progress
    on Aug 24, 2026
  4. added 5 commits that reference this issue on Aug 24, 2026
  5. lvca commented on Aug 24, 2026

    @lvca
    Member

    Confirmed and fixed. A SELECT FROM $var query (where the target type comes from a variable) could, in rare cases, get cached against the wrong type and return results from a previous execution's type instead of the current one, especially when the same query text runs repeatedly with different variable values.

    Fixed in #6706 (merged).


    ❤️ 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.

  6. removed
    in progress
    on Aug 24, 2026
  7. added a commit that references this issue on Aug 25, 2026
  8. added theissue type on Aug 25, 2026
  9. added
    concurrency
    on Sep 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugconcurrencysql

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions