Skip to content

[CALCITE-7632] Replace java.util.Stack with java.util.ArrayDeque in Pattern and TopDownRuleDriver#5062

Merged
mihaibudiu merged 1 commit into
apache:mainfrom
lanky228:feature/replace-stack-with-arraydeque
Jul 1, 2026
Merged

[CALCITE-7632] Replace java.util.Stack with java.util.ArrayDeque in Pattern and TopDownRuleDriver#5062
mihaibudiu merged 1 commit into
apache:mainfrom
lanky228:feature/replace-stack-with-arraydeque

Conversation

@lanky228

@lanky228 lanky228 commented Jun 30, 2026

Copy link
Copy Markdown
Contributor

Summary

Replace java.util.Stack with java.util.ArrayDeque in two locations, completing a TODO left in place since 2020.

Background

In CALCITE-4314 (commit e537246), Vladimir Sitnikov enabled Error Prone checking for the Calcite codebase. As part of that effort, he identified 2 remaining java.util.Stack usages as obsolete and added @SuppressWarnings("JdkObsolete") with // TODO: replace with Deque to suppress warnings. The commit message explicitly stated "It should be replaced with Deque", but the actual refactoring was deferred.

CALCITE-4314 was a broad static-analysis enablement issue — the Stack→Deque replacement was too granular to be tracked there specifically. This PR creates a dedicated issue (CALCITE-7632) to properly track and complete that deferred work.

Changes

  • core/src/main/java/org/apache/calcite/runtime/Pattern.java: Stack<Pattern>Deque<Pattern> in PatternBuilder.stack field (used for SQL MATCH_RECOGNIZE NFA pattern matching)
  • core/src/main/java/org/apache/calcite/plan/volcano/TopDownRuleDriver.java: Stack<Task>Deque<Task> in task queue (used for Volcano optimizer top-down Cascades-style optimization)

Also removed the corresponding @SuppressWarnings("JdkObsolete") annotations and // TODO: replace with Deque comments.

Rationale

  • java.util.Stack extends Vector — all methods are synchronized, adding unnecessary lock overhead in single-threaded contexts
  • ArrayDeque is non-synchronized, officially recommended by the Java documentation as a replacement for Stack
  • Eliminates Error Prone JdkObsolete warnings without suppression
  • Better memory expansion: ArrayDeque doubles capacity on resize vs Vector's linear growth

Testing

  • Compilation: BUILD SUCCESSFUL
  • Tests passed: AutomatonTest (9), DeterministicAutomatonTest (4), EnumerablesTest (53) — 0 failures
  • No remaining java.util.Stack references in core/src/main

Related

…attern and TopDownRuleDriver

Replace java.util.Stack (which extends Vector and provides unnecessary
synchronized access) with java.util.ArrayDeque in:
- runtime/Pattern.java: PatternBuilder.stack field
- plan/volcano/TopDownRuleDriver.java: tasks field

Also remove @SuppressWarnings("JdkObsolete") annotations and TODO
comments that requested this change.

Stack is officially discouraged by the Java documentation, which
recommends using Deque implementations like ArrayDeque instead.
ArrayDeque is faster for single-threaded use since it avoids the
synchronization overhead inherited from Vector.
@sonarqubecloud

Copy link
Copy Markdown

@mihaibudiu

Copy link
Copy Markdown
Contributor

I cannot see this issue in the JIRA, does it exist?

@mihaibudiu

Copy link
Copy Markdown
Contributor

This looks fine, but I'd like to see the issue addressed before commenting.

@lanky228

lanky228 commented Jul 1, 2026

Copy link
Copy Markdown
Contributor Author

Hi @mihaibudiu, thanks for reviewing.

The JIRA issue has been created: CALCITE-7632

Sorry for the earlier confusion — the issue was not created at the time the PR was opened.

@mihaibudiu mihaibudiu merged commit c3e3e6b into apache:main Jul 1, 2026
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants