Skip to content

feat: EXPOSED-868 Add common table expression support - #2911

Open
Hugo Costa (hugoncosta) wants to merge 1 commit into
JetBrains:mainfrom
hugoncosta:feat/cte-support
Open

Hugo Costa (hugoncosta) wants to merge 1 commit into
JetBrains:mainfrom
hugoncosta:feat/cte-support

Conversation

@hugoncosta

Copy link
Copy Markdown
Contributor

Description

Summary of the change: Adds first-class support for ordinary and recursive Common Table Expressions (CTEs) to the Exposed DSL for both JDBC and R2DBC.

Detailed description:

  • Why: Exposed currently requires raw SQL or custom query implementations to use CTEs. This loses DSL type safety, requires manual parameter and column mapping, and makes CTE relations difficult to reuse in joins, predicates, projections, and ResultRow access.
  • What:
    • Adds asCte(), recursiveCte(), and withCtes() APIs.
    • Supports ordinary, recursive, multiple, and dependent CTEs.
    • Allows CTE relations to be selected from, joined, filtered, grouped, and ordered.
    • Supports CTE definitions backed by regular queries and set operations.
    • Preserves parameter ordering, query copying, counting, comments, and target tracking.
    • Adds dialect-specific validation and recursive CTE syntax.
    • Adds equivalent JDBC and R2DBC integration tests, including composite and JSONB projections.
    • Adds API documentation, user documentation, a design specification, and a changelog entry.
  • How:
    • Introduces CommonTableExpression as a query-backed ColumnSet with explicitly mapped output fields.
    • CTE definitions are attached to the outermost AbstractQuery and rendered as a single WITH clause in caller-supplied dependency order.
    • Query definitions are snapshotted to prevent later query mutation from changing the CTE.
    • Dialect capability flags control ordinary and recursive CTE support and whether recursive queries use WITH RECURSIVE or plain WITH.
    • Invalid schemas, duplicate names, incompatible recursive members, unsupported database versions, and nested CTE usage fail before execution.
    • MySQL 8.0+ and MariaDB 10.2+ are supported; MySQL 5 remains unsupported and is rejected before execution.
    • The initial scope is CTE-backed SELECT queries. Data-modifying CTEs, materialization hints, and nested CTE-bearing subqueries remain out of scope.

Type of Change

Please mark the relevant options with an "X":

  • Bug fix
  • New feature
  • Documentation update

Updates/remove existing public API methods:

  • Is breaking change

Affected databases:

  • MariaDB
  • [] Mysql5
  • Mysql8
  • Oracle
  • Postgres
  • Redshift
  • SqlServer
  • H2
  • SQLite

Checklist

  • Unit tests are in place
  • The build is green (including the Detekt check)
  • All public methods affected by my PR has up to date API docs
  • Documentation for my change is up to date

Related Issues

@bog-walk Chantal Loncle (bog-walk) changed the title feat: add common table expression support feat: EXPOSED-868 Add common table expression support Sep 9, 2026
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