Skip to content

[ES|QL] Extract the NDJSON row writer from the stream listener - #167027

Open
fang-xing-esql wants to merge 6 commits into
elastic:mainfrom
fang-xing-esql:stream-ndjson-2a-row-writer
Open

fang-xing-esql wants to merge 6 commits into
elastic:mainfrom
fang-xing-esql:stream-ndjson-2a-row-writer

Conversation

@fang-xing-esql

@fang-xing-esql fang-xing-esql commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

Part of the stream GET work (#167013 adds the endpoints). A standalone refactor of POST /_query/stream: it needs
nothing from #167013 and can be reviewed on its own. This is the first step to resolve https://github.com/elastic/esql-planning/issues/2771

What this does

POST /_query/stream and the upcoming GET /_query/stream/{id} both answer with NDJSON, one line per row. This PR moves the writing of a page's rows out of EsqlStreamResponseListener.NdjsonPageBodyPart into a new NdjsonRowWriter, so both endpoints can write a row the same way.

The stream POST now writes its rows through NdjsonRowWriter. Its output does not change.

How the work is split

  • NdjsonRowWriter (new) writes the rows of one page, each as a JSON array followed by a newline.
    • It builds one value converter per column, once, and keeps its position in the page, so a page that is written
      over several chunks continues where the previous chunk stopped.
    • writeRows writes until the chunk reaches the size hint, the page ends, or the caller's limit is reached.
    • It does not own the page and knows nothing about max_rows, max_bytes, totals or HTTP.
  • NdjsonPageBodyPart keeps what only the POST response knows: the response totals, ending the response when a limit is reached inside a page, and releasing the page.
  • RowLimit connects the two. The writer asks reached() before each row, and once more when a chunk is full, and reports each row with rowWritten(bytes). The POST part answers from max_rows and max_bytes and the rows and bytes written so far.

Behavior notes for the reviewer

  • The old loop checked the limits after each row when rows remained. The writer checks before each row, plus once when a chunk fills. These are the same check at the same points, except before the first row of a page, and onNext already ends the response there before a page part is created. A new assertion in encodeChunk states that invariant.
  • The listener's three parts now return RestEsqlStreamQueryAction.NDJSON_MEDIA_TYPE instead of a private copy of the same string.

Tests

  • NdjsonRowWriterTests (new, 13 tests) compares the exact lines written, on a circuit-breaking block factory that fails a test if a page is not released. It covers:
    • the line format: column order, nulls, multivalued fields, dates in the given zone, dropped null columns, values with newlines, quotes and Unicode staying on one line, and a page without rows;
    • a page written over several chunks, including a size hint smaller than one row;
    • limits: stopping in the middle of a page, a limit reached exactly when a chunk is full, a limit reached by the last row of a page, and a limit already reached before the first row.
  • Unchanged and passing: EsqlStreamResponseListenerTests (its max_rows and max_bytes tests encode with 2 to 10 byte chunks, so pages span several chunks), RestEsqlStreamQueryActionTests, and EsqlStreamQueryIT (47 tests), which is the evidence that the POST output is the same.

Size

516 insertions and 80 deletions: NdjsonRowWriter (157), its tests (328), and the listener change. Most of the main
code is moved, not new.

Follow-up

A second PR shares the first and last line of the response between the two endpoints, stacked on this one.

@elasticsearchmachine elasticsearchmachine added the Team:Analytics Meta label for analytical engine team (ESQL/Aggs/Geo) label Nov 22, 2026
@elasticsearchmachine

Copy link
Copy Markdown
Collaborator

Pinging @elastic/es-analytical-engine (Team:Analytics)

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues were identified for the reusable NDJSON row serialization extraction.

No open issues 🎉

What changed in this PR

Extracts reusable NDJSON row serialization from the ES|QL stream POST listener, preparing for the upcoming stream GET endpoint while preserving POST output.

Changes:

  • Adds NdjsonRowWriter with resumable chunk writing, cached column converters, and caller-controlled limits.
  • Keeps response totals, limit handling, and page ownership in the listener, and reuses the shared NDJSON media type.
  • Adds 13 tests covering formatting, chunk boundaries, limits, and page release.
File Description
x-pack/​plugin/​esql/​src/​test/​java/​org/​elasticsearch/​xpack/​esql/​action/​NdjsonRowWriterTests.java Tests row formatting, chunk continuation, and limit behavior.
x-pack/​plugin/​esql/​src/​main/​java/​org/​elasticsearch/​xpack/​esql/​action/​NdjsonRowWriter.java Introduces the reusable, stateful NDJSON row writer.
x-pack/​plugin/​esql/​src/​main/​java/​org/​elasticsearch/​xpack/​esql/​action/​EsqlStreamResponseListener.java Delegates row writing while retaining response accounting and page cleanup.

Review effort: Balanced

static final byte[] NEWLINE = "\n".getBytes(StandardCharsets.UTF_8);

/** Lets the caller stop the rows before the page ends, and count what was written. */
interface RowLimit {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does RowLimit track counts through a rowWritten(bytes) callback? rowsInChunk and bytesInChunk always equal writeRows's return value and chunkStream.size(), which the writer already knows, and the listener has to reset them before every call. Would it be simpler for the writer to pass the counts in, as in boolean reached(int rowsInChunk, long bytesInChunk)? That would turn RowLimit into a plain check and remove the reset, which a future caller like the GET endpoint could forget.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! I simplified it a bit.

@fang-xing-esql

Copy link
Copy Markdown
Member Author

Merge this after #166646.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

:Analytics/ES|QL AKA ESQL >non-issue Team:Analytics Meta label for analytical engine team (ESQL/Aggs/Geo) v9.7.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants