sql_splitter/
pk.rs

1//! Shared primary key hashing utilities.
2//!
3//! This module provides compact 64-bit hash representations for primary keys,
4//! used by both the `validate` and `diff` commands for memory-efficient
5//! PK tracking.
6
7use crate::parser::mysql_insert;
8use std::hash::{Hash, Hasher};
9
10/// Compact primary key hash (64-bit).
11/// Collision risk is negligible for realistic dump sizes.
12pub type PkHash = u64;
13
14/// Hash a list of PK/FK values into a compact 64-bit hash.
15/// Uses AHash for fast, high-quality hashing.
16pub fn hash_pk_values(values: &smallvec::SmallVec<[mysql_insert::PkValue; 2]>) -> PkHash {
17    let mut hasher = ahash::AHasher::default();
18
19    // Include arity (number of columns) in the hash to distinguish (1) from (1, NULL)
20    (values.len() as u8).hash(&mut hasher);
21
22    for v in values {
23        match v {
24            mysql_insert::PkValue::Int(i) => {
25                0u8.hash(&mut hasher);
26                i.hash(&mut hasher);
27            }
28            mysql_insert::PkValue::BigInt(i) => {
29                1u8.hash(&mut hasher);
30                i.hash(&mut hasher);
31            }
32            mysql_insert::PkValue::Text(s) => {
33                2u8.hash(&mut hasher);
34                s.hash(&mut hasher);
35            }
36            mysql_insert::PkValue::Null => {
37                3u8.hash(&mut hasher);
38            }
39        }
40    }
41
42    hasher.finish()
43}