Skip to content

fix(rowbinary): harden writer edge cases from #911 review - #916

Merged
peter-leonov-ch merged 3 commits into
mainfrom
fix/rowbinary-writer-review-followups
Jun 28, 2026
Merged

peter-leonov-ch merged 3 commits into
mainfrom
fix/rowbinary-writer-review-followups

Conversation

@peter-leonov-ch

Copy link
Copy Markdown
Collaborator

Summary

Follow-up PR for the RowBinary writer added in #911. Peter's own review comments on #911 were all resolved during that PR; the remaining items were the Copilot bot's review findings, which Peter left for a separate PR ("Looks good so far, gonna continue in another PR"). This addresses the three that were still live in main:

  • datetime_writer.ts — round → floor. writeDate, writeDate32 and writeDateTime used Math.round, which shifts a non-midnight Date into the next/previous day (12:00 → next day) and a sub-second DateTime up to the next second (00:00:00.600 → +1s). They now Math.floor, truncating to the calendar day / whole second — matching readDate*/readDateTime and the existing writeDateTime64 (Math.floor(getTime()/1000)). writeDate32 floors toward -inf, so pre-1970 instants land on the correct earlier day rather than rounding toward the epoch.
  • ip_writer.ts — strict IPv6 group parsing. parseIPv6 did parseInt(part, 16) & 0xffff, which silently encoded malformed groups as 0 (NaN & 0xffff === 0) and wrapped negatives like "-1" to 0xffff. It now validates each group is 1–4 hex digits and throws RangeError otherwise.
  • geo_writer.ts — validate before writing. writeGeometry wrote the 1-byte discriminant before the switch, so an out-of-range discriminant threw after advancing the sink, leaving a partial payload. The discriminant byte is now written only inside an accepted case; an unknown value throws from default before any bytes are written (mirrors writeVariant).

Two other Copilot findings on #911 are already resolved and need no change here: the WRITER_TODO.md NeedMoreSpace/BufferFull mismatch (the file was removed) and the missing CHANGELOG entry (added for #911/#915). The writeRows design comment was handled in #915.

Tests

Each fix gets a ClickHouse-independent test (encode-vs-encode for the flooring, throw-assertions for the validation), so they run without a live server and can't be masked by a reader. Full package suite: 724 passing, typecheck and prettier clean.

🤖 Generated with Claude Code

Addresses the remaining Copilot review findings on #911 (the writer
follow-ups left for a separate PR):

- datetime_writer: writeDate/writeDate32/writeDateTime now floor instead
  of Math.round, so a non-midnight Date or sub-second DateTime truncates
  to its calendar day / whole second (matching the reader and CH) rather
  than rounding up into the next day/second. Date32 floors toward -inf so
  pre-1970 instants land on the correct earlier day.
- ip_writer: parseIPv6 validates each hex group (1-4 hex digits) instead
  of `parseInt(part, 16) & 0xffff`, which silently turned invalid groups
  into 0 (NaN & 0xffff) and wrapped negatives like "-1" to 0xffff.
- geo_writer: writeGeometry validates the discriminant before writing its
  byte, so an out-of-range value throws without leaving a partial payload
  in the sink (mirrors writeVariant).

Adds CH-independent tests for each.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings June 28, 2026 22:21
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

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.

Pull request overview

Follow-up hardening for the @clickhouse/rowbinary writer introduced in #911/#915, addressing edge cases where encoding could silently produce incorrect bytes or leave partial payloads on failure.

Changes:

  • Date/time writers now truncate (Math.floor) to day/second boundaries to avoid rounding into the next day/second.
  • parseIPv6 now strictly validates IPv6 hex groups (rejecting malformed/negative/empty/overlong groups).
  • writeGeometry now validates discriminants before writing the discriminant byte to avoid partial payloads.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
skills/clickhouse-js-node-rowbinary-parser/src/datetime_writer.ts Switches date/time encoding from rounding to flooring; updates docs (review note: suggest range/NaN validation + changelog entry).
skills/clickhouse-js-node-rowbinary-parser/src/ip_writer.ts Tightens IPv6 group parsing to reject malformed groups instead of coercing/wrapping.
skills/clickhouse-js-node-rowbinary-parser/src/geo_writer.ts Moves discriminant write into valid switch cases to avoid advancing the sink on invalid discriminants.
skills/clickhouse-js-node-rowbinary-parser/tests/DateTime.write.test.ts Adds ClickHouse-independent tests asserting flooring behavior for date/time writers.
skills/clickhouse-js-node-rowbinary-parser/tests/IP.write.test.ts Adds tests for strict IPv6 validation error cases.
skills/clickhouse-js-node-rowbinary-parser/tests/Geo.write.test.ts Adds a regression test ensuring invalid geometry discriminants don’t write any bytes.

Per #916 review: the date/time leaf writers intentionally don't
runtime-validate input — matching the convention set on #911's varint
writer ("we expect the data at this level to be correct or throw at the
server side"). Document the valid-range precondition in each JSDoc
(mirroring writeUVarint) instead of adding NaN/range guards to these
hot-path writers.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
peter-leonov-ch added a commit that referenced this pull request Jun 28, 2026
Make explicit the convention applied on #911/#916: readX/writeX hot-path
functions don't runtime-validate input values — invalid input is a
programming error documented in JSDoc, and the ClickHouse server rejects
bad bytes server-side. The only checks that belong in are ones that keep
the wire protocol in sync (e.g. writeIPv6's 16-byte length) or zero-cost
parse-time helpers that never reach the server.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@codecov

codecov Bot commented Jun 28, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@peter-leonov-ch
peter-leonov-ch force-pushed the fix/rowbinary-writer-review-followups branch from bd30bcb to 0a919a0 Compare June 28, 2026 22:39
@peter-leonov-ch
peter-leonov-ch merged commit 30a0bf6 into main Jun 28, 2026
33 checks passed
@peter-leonov-ch
peter-leonov-ch deleted the fix/rowbinary-writer-review-followups branch June 28, 2026 22:44
peter-leonov-ch added a commit that referenced this pull request Jun 28, 2026
## Summary

The root
[`AGENTS.md`](https://github.com/ClickHouse/clickhouse-js/blob/main/AGENTS.md)
had grown to cover packages, examples, skills, docs and the upstream
test harness all in one file. This splits each section into a nested
`AGENTS.md` next to the code it describes, so an agent (or human) reads
the guidance closest to the files being edited:

| File | Content |
| ---- | ------- |
| `packages/AGENTS.md` | Log-message conventions, package structure,
intentional node/web duplication |
| `examples/AGENTS.md` | Example corpus layout and conventions |
| `skills/AGENTS.md` | Skill declaration requirement (`agents.skills`
array) |
| `skills/clickhouse-js-node-rowbinary-parser/AGENTS.md` | RowBinary
reader/writer test conventions **+ a new no-defensive-validation rule**
for the codecs |
| `docs/AGENTS.md` | Embedded docs guidance |
| `tests/clickhouse-test-runner/AGENTS.md` | Upstream SQL harness +
allowlist-growth strategy |

The **root `AGENTS.md`** keeps only cross-cutting guidance — the
audience note, TypeScript LSP, and the code-review section (security /
API stability / changelog rules) — plus an **index** linking to each
nested file.

## Notes

- **Relative links rewritten** for each file's new location; all link
targets verified to resolve on disk.
- The "API quality and stability" section stays at root because
[`.github/instructions/review.instructions.md`](https://github.com/ClickHouse/clickhouse-js/blob/main/.github/instructions/review.instructions.md)
links to it by name.
- The new **no-defensive-validation rule** for the RowBinary codecs
(don't runtime-validate input in hot-path `readX`/`writeX`; document the
precondition in JSDoc and let the server reject bad bytes — with narrow
exceptions for wire-framing checks and zero-cost parse helpers) lands in
the rowbinary skill's `AGENTS.md`. It codifies the convention applied in
#911/#916.

Docs-only; no code or test changes.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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