Skip to content

fix: req_sketch truncated k to 8 bits - #552

Open
rustyconover wants to merge 2 commits into
apache:masterfrom
rustyconover:fix/req-k-8-bit-truncation
Open

rustyconover wants to merge 2 commits into
apache:masterfrom
rustyconover:fix/req-k-8-bit-truncation

Conversation

@rustyconover

Copy link
Copy Markdown

req_sketch's constructor computes k_ with std::max<uint8_t>, so any k of 256 or more wraps around:

req_sketch<float>(256).get_k();  // 4
req_sketch<float>(500).get_k();  // 244
req_sketch<float>(1024).get_k(); // 4

Everything else already treats k as 16 bits: the constructor parameter, the k_ member and the serialized format. The documented range (and the Java implementation's check) is [4, 1024]. This changes the one std::max<uint8_t> to std::max<uint16_t>; odd values still round down.

Added a test, req sketch: k above 255. It checks k of 256, 500 and 1024, an odd k (1001 → 1000), and that k = 1024 survives serialization in estimation mode. Without the fix it fails with 4 == 256. With it, all REQ tests pass.

We found this through the DuckDB DataSketches extension, where users asking for K = 1024 were silently getting K = 4.

🤖 Generated with Claude Code

The constructor computed k_ with std::max<uint8_t>, so any k of 256 or more
wrapped around: req_sketch<float>(256).get_k() returned 4 and
req_sketch<float>(500).get_k() returned 244. k is a uint16_t everywhere else
(the parameter, the member and the serialized format), and the documented
range is [4, 1024], so use std::max<uint16_t>.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Comment thread req/include/req_sketch_impl.hpp
Validate the original constructor argument before allocating compactors so k = 1025 cannot round down into the supported range. Preserve existing lower-bound clamping and odd-k rounding, and document the constructor behavior.

Cover both accuracy modes, the upper boundary, values above the limit, and the maximum uint16_t value. Verified the new regression fails before the fix and all 17 CTest targets pass on the EC2 ARM64 host with GCC 14.2.1.

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants