mirror of
https://github.com/GreptimeTeam/greptimedb.git
synced 2026-12-04 08:25:29 +00:00
refactor(mito-codec): remove sparse key size compatibility bypass (#10871)
Apply the 16 MiB limit when sparse keys are re-encoded and use the standard codec in reconstruction paths. Signed-off-by: evenyag <realevenyag@gmail.com>
This commit is contained in:
@@ -59,7 +59,6 @@ use crate::row_converter::{CompositeValues, PrimaryKeyCodec, PrimaryKeyFilter};
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct SparsePrimaryKeyCodec {
|
||||
inner: Arc<SparsePrimaryKeyCodecInner>,
|
||||
preserve_labels: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
@@ -308,7 +307,6 @@ impl SparsePrimaryKeyCodec {
|
||||
label_field: SortField::new(ConcreteDataType::string_datatype()),
|
||||
columns: Some(columns),
|
||||
}),
|
||||
preserve_labels: false,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -338,7 +336,6 @@ impl SparsePrimaryKeyCodec {
|
||||
label_field: SortField::new(ConcreteDataType::string_datatype()),
|
||||
columns: None,
|
||||
}),
|
||||
preserve_labels: false,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -349,13 +346,6 @@ impl SparsePrimaryKeyCodec {
|
||||
codec
|
||||
}
|
||||
|
||||
/// Reconstructs existing keys during reads without omitting stored labels.
|
||||
#[must_use]
|
||||
pub fn for_existing_keys(mut self) -> Self {
|
||||
self.preserve_labels = true;
|
||||
self
|
||||
}
|
||||
|
||||
/// Returns the field of the given column id.
|
||||
fn get_field(&self, column_id: ColumnId) -> Option<&SortField> {
|
||||
if let Some(columns) = &self.inner.columns
|
||||
@@ -381,8 +371,7 @@ impl SparsePrimaryKeyCodec {
|
||||
}
|
||||
|
||||
if let Some(field) = self.get_field(column_id) {
|
||||
if !self.preserve_labels
|
||||
&& let ValueRef::String(value) = &value
|
||||
if let ValueRef::String(value) = &value
|
||||
&& encoded_label_len(value.len())
|
||||
> MAX_SPARSE_KEY_BYTES.saturating_sub(buffer.len())
|
||||
{
|
||||
@@ -419,7 +408,7 @@ impl SparsePrimaryKeyCodec {
|
||||
for (tag_column_id, tag_value) in row {
|
||||
let value_len = tag_value.len();
|
||||
let encoded_len = encoded_label_len(value_len);
|
||||
if !self.preserve_labels && encoded_len > max_end.saturating_sub(buffer.len()) {
|
||||
if encoded_len > max_end.saturating_sub(buffer.len()) {
|
||||
break;
|
||||
}
|
||||
buffer.reserve(encoded_len);
|
||||
@@ -1083,24 +1072,6 @@ mod tests {
|
||||
assert!(decoded.get(&3).is_some());
|
||||
assert!(decoded.get(&1).is_none());
|
||||
assert!(decoded.get(&2).is_none());
|
||||
|
||||
let mut existing = Vec::new();
|
||||
codec
|
||||
.clone()
|
||||
.for_existing_keys()
|
||||
.encode_raw(
|
||||
42,
|
||||
7,
|
||||
labels.map(|(id, value)| (id, value.as_bytes())),
|
||||
&mut existing,
|
||||
)
|
||||
.unwrap();
|
||||
assert!(existing.len() > MAX_SPARSE_KEY_BYTES);
|
||||
assert!(existing.starts_with(&raw));
|
||||
assert_eq!(
|
||||
codec.decode_sparse(&existing).unwrap().get(&2),
|
||||
Some(&Value::from("suffix"))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -28,7 +28,6 @@ use datatypes::data_type::ConcreteDataType;
|
||||
use datatypes::prelude::DataType;
|
||||
use datatypes::value::Value;
|
||||
use datatypes::vectors::{Helper, VectorRef};
|
||||
use mito_codec::row_converter::sparse::SparsePrimaryKeyCodec;
|
||||
use mito_codec::row_converter::{
|
||||
CompositeValues, PrimaryKeyCodec, SortField, build_primary_key_codec,
|
||||
build_primary_key_codec_with_fields,
|
||||
@@ -423,12 +422,7 @@ impl FlatRewritePrimaryKey {
|
||||
if expect.primary_key_encoding == actual.primary_key_encoding {
|
||||
return None;
|
||||
}
|
||||
let codec: Arc<dyn PrimaryKeyCodec> =
|
||||
if expect.primary_key_encoding == PrimaryKeyEncoding::Sparse {
|
||||
Arc::new(SparsePrimaryKeyCodec::new(expect).for_existing_keys())
|
||||
} else {
|
||||
build_primary_key_codec(expect)
|
||||
};
|
||||
let codec = build_primary_key_codec(expect);
|
||||
let old_codec = build_primary_key_codec(actual);
|
||||
|
||||
Some(FlatRewritePrimaryKey {
|
||||
@@ -580,12 +574,8 @@ impl FlatCompatPrimaryKey {
|
||||
debug_assert!(!fields.is_empty());
|
||||
|
||||
// Create converter to append values.
|
||||
let converter: Arc<dyn PrimaryKeyCodec> =
|
||||
if expect.primary_key_encoding == PrimaryKeyEncoding::Sparse {
|
||||
Arc::new(SparsePrimaryKeyCodec::with_fields(fields).for_existing_keys())
|
||||
} else {
|
||||
build_primary_key_codec_with_fields(expect.primary_key_encoding, fields.into_iter())
|
||||
};
|
||||
let converter =
|
||||
build_primary_key_codec_with_fields(expect.primary_key_encoding, fields.into_iter());
|
||||
|
||||
Ok(Self {
|
||||
rewriter,
|
||||
|
||||
@@ -461,9 +461,9 @@ impl ColumnarPreparedSource {
|
||||
})
|
||||
.collect::<Result<Vec<_>>>()?;
|
||||
// Identity is the exact canonical sparse key. `tsid` is a hash, so rows
|
||||
// that differ only in labels are different series and must both survive:
|
||||
// re-encoding the artifact's own tags reproduces the SST key byte for byte.
|
||||
let codec = SparsePrimaryKeyCodec::schemaless().for_existing_keys();
|
||||
// that differ only in labels are different series and must both survive.
|
||||
// Re-encoding the stored tags reconstructs keys within the sparse key limit.
|
||||
let codec = SparsePrimaryKeyCodec::schemaless();
|
||||
let mut offsets = mito_codec::row_converter::SparseOffsetsCache::new();
|
||||
let mut identity = Vec::new();
|
||||
let mut previous: Option<Vec<u8>> = None;
|
||||
|
||||
@@ -305,7 +305,7 @@ impl ColumnarReader {
|
||||
.map(|tag| (tag.name.as_str(), tag.column_id))
|
||||
.collect();
|
||||
let mut metrics = ColumnarReadMetrics::default();
|
||||
let codec = SparsePrimaryKeyCodec::schemaless().for_existing_keys();
|
||||
let codec = SparsePrimaryKeyCodec::schemaless();
|
||||
let mut offsets = SparseOffsetsCache::new();
|
||||
let mut encoded = Vec::new();
|
||||
while let Some((area, page)) = pages.try_next().await? {
|
||||
|
||||
Reference in New Issue
Block a user