Skip to content

feat: add tuple sketch implementation#152

Merged
tisonkun merged 5 commits into
apache:mainfrom
ariesdevil:tuple-sketch
Jul 23, 2026
Merged

feat: add tuple sketch implementation#152
tisonkun merged 5 commits into
apache:mainfrom
ariesdevil:tuple-sketch

Conversation

@ariesdevil

@ariesdevil ariesdevil commented Jul 19, 2026

Copy link
Copy Markdown
Contributor

Summary

Implement the Tuple sketch behind a new tuple feature (built on top of theta). A Tuple sketch extends the Theta sketch by attaching a user-defined summary to every retained key, merging summaries on duplicate keys and during set operations.

Summary behavior is injected externally via policies (C++-style) rather than baked into the type.

Note

This implementation is highly inspired by the C++ version.

Comment thread datasketches/src/tuple/sketch.rs Outdated
Comment thread datasketches/src/tuple/sketch.rs Outdated
Comment thread datasketches/src/tuple/hash_table.rs Outdated
Comment thread datasketches/src/tuple/hash_table.rs
Comment thread datasketches/src/tuple/hash_table.rs Outdated
Comment thread datasketches/src/tuple/hash_table.rs Outdated
Comment thread datasketches/src/tuple/policy.rs Outdated
Comment thread datasketches/src/tuple/policy.rs Outdated
@ariesdevil

Copy link
Copy Markdown
Contributor Author

@ZENOTME Fixed, PTAL again.

Comment thread datasketches/src/tuple/serde.rs Outdated
Comment on lines +88 to +120
mod private {
/// Sealed helper describing fixed-width little-endian encoding of a primitive.
pub trait LeBytes: Copy {
/// Number of bytes in the little-endian encoding.
const WIDTH: usize;
/// Appends the little-endian bytes of `self` to `out`.
fn write_le(self, out: &mut Vec<u8>);
/// Reads the value from exactly `WIDTH` leading bytes of `bytes`.
fn read_le(bytes: &[u8]) -> Self;
}

macro_rules! impl_le_bytes {
($($t:ty),* $(,)?) => {
$(
impl LeBytes for $t {
const WIDTH: usize = std::mem::size_of::<$t>();

fn write_le(self, out: &mut Vec<u8>) {
out.extend_from_slice(&self.to_le_bytes());
}

fn read_le(bytes: &[u8]) -> Self {
let mut buf = [0u8; std::mem::size_of::<$t>()];
buf.copy_from_slice(&bytes[..std::mem::size_of::<$t>()]);
<$t>::from_le_bytes(buf)
}
}
)*
};
}

impl_le_bytes!(u32, u64, i32, i64, f32, f64);
}

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 we need this? The SketchSlice and SketchBytes in the codec module should implement all these functionalities. Or we should add new utilities there instead of randomly add some code in dedicated skecthes modules.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

SketchBytes / SketchSlice only offer per-type concrete methods (write_u64_le, read_f32_le, ...), but PrimitiveSummarySerde is a generic impl<S> SummarySerde<S> — it needs a trait to dispatch on the type's LE width and encoding, so the abstraction itself can't go away.
Moved LeBytes to codec/primitive.rs as a pub(crate) utility instead of hiding it in the tuple module; frequencies' impl_primitive! could migrate to it later if desired.

@tisonkun tisonkun left a comment

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.

Generally LGTM. Comments inline.

tisonkun added 4 commits July 23, 2026 14:53
Signed-off-by: tison <wander4096@gmail.com>
Signed-off-by: tison <wander4096@gmail.com>
Signed-off-by: tison <wander4096@gmail.com>
Signed-off-by: tison <wander4096@gmail.com>

@tisonkun tisonkun left a comment

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.

LGTM.

I pushed several commits to do post fine tune.

I'm going to submit a new PR to rework the Builder interface. Basically, no need for several construction point and avoid some strange interface change.

@tisonkun
tisonkun enabled auto-merge (squash) July 23, 2026 07:36
Comment thread datasketches/src/lib.rs
#[cfg(feature = "theta")]
pub mod theta;
#[cfg(any(feature = "theta", feature = "tuple"))]
#[allow(dead_code)] // some utilities are only used for certain sketches

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.

To be reviewed once the full tuple sketch implementation landed.

@ariesdevil

Copy link
Copy Markdown
Contributor Author

Thx for your patient review, let's move on.

@tisonkun
tisonkun merged commit 81ec1cb into apache:main Jul 23, 2026
10 checks passed
@ariesdevil
ariesdevil deleted the tuple-sketch branch July 23, 2026 08:03
@tisonkun

Copy link
Copy Markdown
Member

I'm going to submit a new PR to rework the Builder interface. Basically, no need for several construction point and avoid some strange interface change.

See #158

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.

3 participants