Repository navigation
[ES|QL] Extract the NDJSON row writer from the stream listener - #167027
fang-xing-esql wants to merge 6 commits into
Conversation
|
Pinging @elastic/es-analytical-engine (Team:Analytics) |
There was a problem hiding this comment.
🟢 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
NdjsonRowWriterwith 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 { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Good point! I simplified it a bit.
|
Merge this after #166646. |
Part of the stream GET work (#167013 adds the endpoints). A standalone refactor of
POST /_query/stream: it needsnothing 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/streamand the upcomingGET /_query/stream/{id}both answer with NDJSON, one line per row. This PR moves the writing of a page's rows out ofEsqlStreamResponseListener.NdjsonPageBodyPartinto a newNdjsonRowWriter, 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.over several chunks continues where the previous chunk stopped.
writeRowswrites until the chunk reaches the size hint, the page ends, or the caller's limit is reached.max_rows,max_bytes, totals or HTTP.NdjsonPageBodyPartkeeps what only the POST response knows: the response totals, ending the response when a limit is reached inside a page, and releasing the page.RowLimitconnects the two. The writer asksreached()before each row, and once more when a chunk is full, and reports each row withrowWritten(bytes). The POST part answers frommax_rowsandmax_bytesand the rows and bytes written so far.Behavior notes for the reviewer
onNextalready ends the response there before a page part is created. A new assertion inencodeChunkstates that invariant.RestEsqlStreamQueryAction.NDJSON_MEDIA_TYPEinstead 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:EsqlStreamResponseListenerTests(itsmax_rowsandmax_bytestests encode with 2 to 10 byte chunks, so pages span several chunks),RestEsqlStreamQueryActionTests, andEsqlStreamQueryIT(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 maincode 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.