use super::*;
use std::ops::Bound;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RelationalKeyRange {
pub lower: Bound<RelationalKey>,
pub upper: Bound<RelationalKey>,
}
pub type RelationalPrimaryKeyRange = RelationalKeyRange;
impl RelationalKeyRange {
pub fn from_predicate(
columns: &[String],
predicate: &RelationalPredicate,
) -> Result<Option<Self>, RelationalError> {
key_range(columns, predicate, None)
}
pub fn from_prefix_bounds(
prefix: RelationalKey,
lower: Option<(&RelationalValue, bool)>,
upper: Option<(&RelationalValue, bool)>,
) -> Result<Self, RelationalError> {
prefix_interval(prefix, lower, upper, None)
}
pub fn is_empty(&self) -> bool {
match (&self.lower, &self.upper) {
(Bound::Included(left), Bound::Included(right)) => left > right,
(
Bound::Included(left) | Bound::Excluded(left),
Bound::Included(right) | Bound::Excluded(right),
) => left >= right,
_ => false,
}
}
}
pub(super) fn equality<'a>(
predicate: &'a RelationalPredicate,
column: &str,
) -> Option<&'a RelationalValue> {
match predicate {
RelationalPredicate::And(left, right) => {
equality(left, column).or_else(|| equality(right, column))
}
RelationalPredicate::Compare {
column: name,
op: RelationalComparisonOp::Eq,
value,
} if name == column && !matches!(value, RelationalValue::Null) => Some(value),
_ => None,
}
}
fn visit_comparisons<'a>(
predicate: &'a RelationalPredicate,
column: &str,
visit: &mut impl FnMut(RelationalComparisonOp, &'a RelationalValue),
) {
match predicate {
RelationalPredicate::And(left, right) => {
visit_comparisons(left, column, visit);
visit_comparisons(right, column, visit);
}
RelationalPredicate::Compare {
column: name,
op,
value,
} if name == column && !matches!(value, RelationalValue::Null) => visit(*op, value),
_ => {}
}
}
fn copy_key<'a>(
values: impl Iterator<Item = &'a RelationalValue>,
count: usize,
allocations: Option<&crate::background::CheckpointDecodeContext>,
) -> Result<RelationalKey, RelationalError> {
collect_results_with_allocations(
values.map(|value| match allocations {
Some(context) => value.as_ref().to_owned_value_with_allocations(context),
None => Ok(value.clone()),
}),
count,
allocations,
)
.map(RelationalKey)
}
fn prefix_successor(
mut key: RelationalKey,
allocations: Option<&crate::background::CheckpointDecodeContext>,
) -> Result<Option<RelationalKey>, RelationalError> {
while let Some(value) = key.0.last_mut() {
let advanced = match value {
RelationalValue::Boolean(value) => {
let advanced = !*value;
*value = true;
advanced
}
RelationalValue::BigInt(value) => match value.checked_add(1) {
Some(next) => {
*value = next;
true
}
None => false,
},
RelationalValue::Timestamp(value) | RelationalValue::TimestampTz(value)
if value.get() < hawdb_core::sql_timestamp::max_micros() =>
{
*value = (value.get() + 1).into();
true
}
RelationalValue::Timestamp(_) | RelationalValue::TimestampTz(_) => false,
RelationalValue::Decimal(value) => match value.next_up() {
Some(next) => {
*value = next;
true
}
None => false,
},
RelationalValue::DoublePrecision(value) => {
let bits = value.to_bits();
if bits == i64::MAX as u64 {
false
} else {
let next = if bits == 1 << 63 {
0
} else if bits >> 63 == 1 {
bits - 1
} else {
bits + 1
};
*value = f64::from_bits(next);
true
}
}
RelationalValue::Text(value) => {
if let Some(context) = allocations {
let bytes = append_zero_with_allocations(value.as_bytes(), context)?;
*value = String::from_utf8(bytes).expect("NUL preserves valid UTF-8");
} else {
value.push('\0');
}
true
}
RelationalValue::Bytea(value) => {
if let Some(context) = allocations {
*value = append_zero_with_allocations(value, context)?;
} else {
value.push(0);
}
true
}
RelationalValue::Uuid(value) => match value.as_u128().checked_add(1) {
Some(next) => {
*value = Uuid::from_u128(next);
true
}
None => false,
},
RelationalValue::Null | RelationalValue::Vector(_) | RelationalValue::Overflow(_) => {
return Err(RelationalError::Schema(
"unsupported primary-key boundary value".into(),
));
}
};
if advanced {
return Ok(Some(key));
}
key.0.pop();
}
Ok(None)
}
fn append_zero_with_allocations(
value: &[u8],
context: &crate::background::CheckpointDecodeContext,
) -> Result<Vec<u8>, RelationalError> {
let capacity = value.len().checked_add(1).ok_or_else(|| {
RelationalError::Admission("primary-key boundary capacity overflow".into())
})?;
let mut next = context
.vector_capacity(capacity)
.map_err(RelationalError::from_admission)?;
next.extend_from_slice(value);
next.push(0);
Ok(next)
}
pub(super) fn mutation_primary_key_range(
schema: &RelationalTableSchema,
predicate: &RelationalPredicate,
allocations: Option<&crate::background::CheckpointDecodeContext>,
) -> Result<Option<RelationalPrimaryKeyRange>, RelationalError> {
key_range(&schema.primary_key, predicate, allocations)
}
fn key_range(
columns: &[String],
predicate: &RelationalPredicate,
allocations: Option<&crate::background::CheckpointDecodeContext>,
) -> Result<Option<RelationalKeyRange>, RelationalError> {
let prefix_len = columns
.iter()
.take_while(|column| equality(predicate, column).is_some())
.count();
let prefix = copy_key(
columns
.iter()
.take(prefix_len)
.map(|column| equality(predicate, column).expect("proven equality prefix")),
prefix_len,
allocations,
)?;
if prefix_len == columns.len() && prefix_len != 0 {
return Ok(Some(RelationalPrimaryKeyRange {
lower: Bound::Included(copy_key(prefix.0.iter(), prefix_len, allocations)?),
upper: Bound::Included(prefix),
}));
}
let mut lower = None;
let mut upper = None;
if let Some(column) = columns.get(prefix_len) {
visit_comparisons(predicate, column, &mut |op, value| match op {
RelationalComparisonOp::Gt | RelationalComparisonOp::Gte => {
let inclusive = op == RelationalComparisonOp::Gte;
if lower.is_none_or(|(old, included)| {
value > old || value == old && !inclusive && included
}) {
lower = Some((value, inclusive));
}
}
RelationalComparisonOp::Lt | RelationalComparisonOp::Lte => {
let inclusive = op == RelationalComparisonOp::Lte;
if upper.is_none_or(|(old, included)| {
value < old || value == old && !inclusive && included
}) {
upper = Some((value, inclusive));
}
}
_ => {}
});
}
if prefix_len == 0 && lower.is_none() && upper.is_none() {
return Ok(None);
}
prefix_interval(prefix, lower, upper, allocations).map(Some)
}
fn prefix_interval(
prefix: RelationalKey,
lower: Option<(&RelationalValue, bool)>,
upper: Option<(&RelationalValue, bool)>,
allocations: Option<&crate::background::CheckpointDecodeContext>,
) -> Result<RelationalKeyRange, RelationalError> {
let prefix_len = prefix.0.len();
let prefix_upper = if prefix_len == 0 {
Bound::Unbounded
} else {
prefix_successor(
copy_key(prefix.0.iter(), prefix_len, allocations)?,
allocations,
)?
.map_or(Bound::Unbounded, Bound::Excluded)
};
let lower = if let Some((value, inclusive)) = lower {
let key = copy_key(
prefix.0.iter().chain(std::iter::once(value)),
prefix_len + 1,
allocations,
)?;
if inclusive {
Bound::Included(key)
} else if let Some(next) = prefix_successor(key, allocations)? {
Bound::Included(next)
} else {
let maximum = copy_key(
prefix.0.iter().chain(std::iter::once(value)),
prefix_len + 1,
allocations,
)?;
return Ok(RelationalPrimaryKeyRange {
lower: Bound::Excluded(copy_key(maximum.0.iter(), maximum.0.len(), allocations)?),
upper: Bound::Included(maximum),
});
}
} else if prefix_len == 0 {
Bound::Unbounded
} else {
Bound::Included(copy_key(prefix.0.iter(), prefix_len, allocations)?)
};
let upper = if let Some((value, inclusive)) = upper {
let key = copy_key(
prefix.0.iter().chain(std::iter::once(value)),
prefix_len + 1,
allocations,
)?;
if inclusive {
prefix_successor(key, allocations)?.map_or(Bound::Unbounded, Bound::Excluded)
} else {
Bound::Excluded(key)
}
} else {
prefix_upper
};
Ok(RelationalPrimaryKeyRange { lower, upper })
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn prefix_bounds_on_a_middle_column_cover_exactly_the_matching_longer_keys() {
let texts = ["", "\0", "a", "a\0", "b"];
let ints = [i64::MIN, -1, 0, 1, i64::MAX];
let mut keys = Vec::new();
for a in ints {
for b in texts {
for c in ints {
keys.push(RelationalKey(vec![
RelationalValue::BigInt(a),
RelationalValue::Text(b.into()),
RelationalValue::BigInt(c),
]));
}
}
}
let rows = RelationalRowPages::from_map(
keys.iter()
.map(|key| (key.clone(), RelationalRow::new(key.0.clone())))
.collect(),
);
let holds = |value: &RelationalValue, bound: Option<(&RelationalValue, bool)>, lower| {
bound.is_none_or(|(bound, inclusive)| match (lower, inclusive) {
(true, true) => value >= bound,
(true, false) => value > bound,
(false, true) => value <= bound,
(false, false) => value < bound,
})
};
let text_values = texts.map(|text| RelationalValue::Text(text.into()));
let int_values = ints.map(RelationalValue::BigInt);
for prefix_len in 0..3 {
let next_values: &[RelationalValue] = if prefix_len == 1 {
&text_values
} else {
&int_values
};
let prefixes: Vec<Vec<RelationalValue>> = match prefix_len {
0 => vec![vec![]],
1 => int_values.iter().map(|a| vec![a.clone()]).collect(),
_ => int_values
.iter()
.flat_map(|a| text_values.iter().map(move |b| vec![a.clone(), b.clone()]))
.collect(),
};
let bounds = std::iter::once(None)
.chain(
next_values
.iter()
.flat_map(|value| [Some((value, true)), Some((value, false))]),
)
.collect::<Vec<_>>();
for prefix in &prefixes {
for lower in &bounds {
for upper in &bounds {
let range = RelationalKeyRange::from_prefix_bounds(
RelationalKey(prefix.clone()),
*lower,
*upper,
)
.unwrap();
let expected = rows
.iter()
.filter(|(key, _)| {
key.0[..prefix_len] == prefix[..]
&& holds(&key.0[prefix_len], *lower, true)
&& holds(&key.0[prefix_len], *upper, false)
})
.collect::<Vec<_>>();
let actual = if range.is_empty() {
Vec::new()
} else {
rows.range(Some(range.clone())).collect()
};
assert_eq!(actual, expected, "{prefix:?} {lower:?} {upper:?} {range:?}");
}
}
}
}
}
fn timestamp_domain(variant: fn(i128) -> RelationalValue) -> Vec<RelationalValue> {
let (min, max) = (
hawdb_core::sql_timestamp::min_micros(),
hawdb_core::sql_timestamp::max_micros(),
);
[
min,
min + 1,
-1,
0,
1,
i128::from(i64::MAX) - 1,
i128::from(i64::MAX),
i128::from(i64::MAX) + 1,
max - 1,
max,
]
.into_iter()
.map(variant)
.collect()
}
#[test]
fn scalar_prefix_bounds_preserve_predicate_truth_and_encoded_key_order() {
let domains = [
[
"-99999999999999999999999999999999999999",
"-1.01",
"-1.00",
"-1e-38",
"0.00",
"1e-38",
"1.00",
"1.0000000000000000000000000000000000001",
"1.01",
"99999999999999999999999999999999999999",
]
.into_iter()
.map(|value| RelationalValue::Decimal(value.parse().unwrap()))
.collect(),
vec![
RelationalValue::Boolean(false),
RelationalValue::Boolean(true),
],
[i64::MIN, -1, 0, 1, i64::MAX]
.into_iter()
.map(RelationalValue::BigInt)
.collect(),
[
u64::MAX,
0xfff8_0000_0000_0001,
f64::NEG_INFINITY.to_bits(),
(-1.0_f64).to_bits(),
(-0.0_f64).to_bits(),
0,
1,
1.0_f64.to_bits(),
f64::INFINITY.to_bits(),
0x7ff8_0000_0000_0001,
i64::MAX as u64,
]
.into_iter()
.map(|bits| RelationalValue::DoublePrecision(f64::from_bits(bits)))
.collect(),
["", "\0", "a", "a\0", "a\0\0", "aa", "é", "\u{10ffff}"]
.into_iter()
.map(|value| RelationalValue::Text(value.into()))
.collect(),
[
vec![],
vec![0],
vec![0, 0],
vec![1],
vec![255],
vec![255, 0],
]
.into_iter()
.map(RelationalValue::Bytea)
.collect(),
[0, 1, u128::MAX - 1, u128::MAX]
.into_iter()
.map(|value| RelationalValue::Uuid(Uuid::from_u128(value)))
.collect(),
timestamp_domain(|value| RelationalValue::Timestamp(value.into())),
timestamp_domain(|value| RelationalValue::TimestampTz(value.into())),
];
for domain in domains {
let schema = RelationalTableSchema {
name: "t".into(),
columns: vec![
RelationalColumnSchema {
name: "a".into(),
scalar_type: domain[0].scalar_type().unwrap(),
nullable: false,
default: None,
},
RelationalColumnSchema {
name: "b".into(),
scalar_type: RelationalScalarType::BigInt,
nullable: false,
default: None,
},
],
primary_key: vec!["a".into(), "b".into()],
unique_constraints: vec![],
foreign_keys: vec![],
indexes: vec![],
materialized_view: None,
};
let rows = RelationalRowPages::from_map(
domain
.iter()
.flat_map(|value| {
[i64::MIN, 0, i64::MAX].into_iter().map(move |tail| {
let values = vec![value.clone(), RelationalValue::BigInt(tail)];
(RelationalKey(values.clone()), RelationalRow::new(values))
})
})
.collect(),
);
for value in &domain {
for op in [
RelationalComparisonOp::Eq,
RelationalComparisonOp::Lt,
RelationalComparisonOp::Lte,
RelationalComparisonOp::Gt,
RelationalComparisonOp::Gte,
] {
let predicate = RelationalPredicate::Compare {
column: "a".into(),
op,
value: value.clone(),
};
let range = mutation_primary_key_range(&schema, &predicate, None)
.unwrap()
.unwrap();
let expected: Vec<_> = rows
.iter()
.filter(|(_, row)| {
predicate_truth(&schema, row, &predicate).unwrap() == Some(true)
})
.collect();
let actual: Vec<_> = rows.range(Some(range.clone())).collect();
assert_eq!(
actual, expected,
"value={value:?}, op={op:?}, range={range:?}"
);
if !range.is_empty() {
for (key, row) in rows.iter() {
let encoded = encode_relational_primary_key(key).unwrap();
let after_lower = match &range.lower {
Bound::Included(lower) => {
encoded >= encode_relational_primary_key(lower).unwrap()
}
Bound::Excluded(lower) => {
encoded > encode_relational_primary_key(lower).unwrap()
}
Bound::Unbounded => true,
};
let before_upper = match &range.upper {
Bound::Included(upper) => {
encoded <= encode_relational_primary_key(upper).unwrap()
}
Bound::Excluded(upper) => {
encoded < encode_relational_primary_key(upper).unwrap()
}
Bound::Unbounded => true,
};
assert_eq!(
after_lower && before_upper,
predicate_truth(&schema, row, &predicate).unwrap() == Some(true)
);
}
}
}
}
}
}
}