lgK must be between 4 and 21 inclusive: ThetaSketch.Builder setLgK validation error
0 reputation · 06 Sept 2023, 07:15 UTC
0 reputation · 06 Sept 2023, 07:15 UTC
The goal is to use a Theta Sketch to estimate distinct element counts and derive page totals for paginating large datasets, while deciding whether to fix the lgK parameter globally for all queries or to adapt it per query based on observed cardinality.
Constraints include the library’s validation that lgK must be between 4 and 21 inclusive, throwing an IllegalArgumentException with the message “lgK must be between 4 and 21 inclusive” when the value is out of range, and the prohibition against merging sketches with different lgK values. Because the sketch’s estimate is probabilistic, page boundaries may shift between queries unless the same sketch configuration is reused.
lgK globally versus adapting it per query in terms of memory usage, accuracy, and pagination consistency?lgK affect the theoretical error bound and the resulting page‑count estimates?lgK validation and merge constraints?33775 reputation · 06 Sept 2023, 15:10 UTC
The lgK value you pass to ThetaSketch.Builder.setLgK() is the only knob that controls the sketch’s memory usage, relative error, and the stability of page boundaries. The library enforces that lgK be between 4 and 21 inclusive; values outside this range throw an IllegalArgumentException with the message "lgK must be between 4 and 21 inclusive".
k = 2^lgK slots regardless of the actual cardinality. A fixed lgK of 20 uses 1,048,576 slots, which is about 8 MiB for 64‑bit longs.1/√k. With lgK = 20 the error is about 0.3 %; with lgK = 10 it is about 3 %.The error bound for a ThetaSketch is approximately 1/√k. Changing lgK from 4 to 21 reduces the bound from about 25 % to 2 %. The page‑count estimate is directly proportional to the sketch estimate, so tighter error bounds translate into more stable page totals.
To give a more concrete lgK recommendation, could you share the maximum expected distinct count for the largest query you plan to paginate? Knowing that number will let us compute the exact lgK that satisfies a target error bound while staying within your memory limits.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.