Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion crates/proof-of-sql/benches/bench_append_rows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use proof_of_sql::{
commitment::TableCommitment,
database::{
owned_table_utility::{
bigint, boolean, decimal75, int, int128, owned_table, scalar, smallint,
bigint, boolean, decimal75, int, tinyint, int128, owned_table, scalar, smallint,
timestamptz, varchar,
},
OwnedTable,
Expand Down Expand Up @@ -76,6 +76,7 @@ pub fn generate_random_owned_table<S: Scalar>(
"boolean",
"int128",
"scalar",
"tinyint",
"varchar",
"decimal75",
"smallint",
Expand Down Expand Up @@ -103,6 +104,10 @@ pub fn generate_random_owned_table<S: Scalar>(
identifier.deref(),
vec![generate_random_u64_array(); num_rows],
)),
"tinyint" => colums.push(tinyint(
identifier.deref(),
vec![rng.gen::<i8>(); num_rows],
)),
"varchar" => columns.push(varchar(identifier.deref(), gen_rnd_str(num_rows))),
"decimal75" => columns.push(decimal75(
identifier.deref(),
Expand Down
46 changes: 45 additions & 1 deletion crates/proof-of-sql/src/base/commitment/column_bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ pub enum ColumnBounds {
NoOrder,
/// The bounds of a SmallInt column.
SmallInt(Bounds<i16>),
/// The bounds of a TinyInt column.
TinyInt(Bounds<i8>),
/// The bounds of an Int column.
Int(Bounds<i32>),
/// The bounds of a BigInt column.
Expand All @@ -224,6 +226,7 @@ impl ColumnBounds {
pub fn from_column(column: &CommittableColumn) -> ColumnBounds {
match column {
CommittableColumn::SmallInt(ints) => ColumnBounds::SmallInt(Bounds::from_iter(*ints)),
CommittableColumn::TinyInt(ints) => ColumnBounds::TinyInt(Bounds::from_iter(*ints)),
CommittableColumn::Int(ints) => ColumnBounds::Int(Bounds::from_iter(*ints)),
CommittableColumn::BigInt(ints) => ColumnBounds::BigInt(Bounds::from_iter(*ints)),
CommittableColumn::Int128(ints) => ColumnBounds::Int128(Bounds::from_iter(*ints)),
Expand All @@ -247,6 +250,9 @@ impl ColumnBounds {
(ColumnBounds::SmallInt(bounds_a), ColumnBounds::SmallInt(bounds_b)) => {
Ok(ColumnBounds::SmallInt(bounds_a.union(bounds_b)))
}
(ColumnBounds::TinyInt(bounds_a), ColumnBounds::TinyInt(bounds_b)) => {
Ok(ColumnBounds::TinyInt(bounds_a.union(bounds_b)))
}
(ColumnBounds::Int(bounds_a), ColumnBounds::Int(bounds_b)) => {
Ok(ColumnBounds::Int(bounds_a.union(bounds_b)))
}
Expand Down Expand Up @@ -279,6 +285,9 @@ impl ColumnBounds {
(ColumnBounds::Int(bounds_a), ColumnBounds::Int(bounds_b)) => {
Ok(ColumnBounds::Int(bounds_a.difference(bounds_b)))
}
(ColumnBounds::TinyInt(bounds_a), ColumnBounds::TinyInt(bounds_b)) => {
Ok(ColumnBounds::TinyInt(bounds_a.difference(bounds_b)))
}
(ColumnBounds::BigInt(bounds_a), ColumnBounds::BigInt(bounds_b)) => {
Ok(ColumnBounds::BigInt(bounds_a.difference(bounds_b)))
}
Expand All @@ -298,6 +307,8 @@ impl ColumnBounds {

#[cfg(test)]
mod tests {
use core::time;

use super::*;
use crate::base::{database::OwnedColumn, math::decimal::Precision, scalar::Curve25519Scalar};
use alloc::{string::String, vec};
Expand Down Expand Up @@ -522,6 +533,14 @@ mod tests {
ColumnBounds::BigInt(Bounds::Sharp(BoundsInner { min: 0, max: 3 }))
);

let tinyint_column = OwnedColumn::<Curve25519Scalar>::TinyInt([1, 2, 3, 1, 0].to_vec());
let committable_tinyint_column = CommittableColumn::from(&tinyint_column);
let tinyint_column_bounds = ColumnBounds::from_column(&committable_tinyint_column);
assert_eq!(
tinyint_column_bounds,
ColumnBounds::TinyInt(Bounds::Sharp(BoundsInner { min: 0, max: 3 }))
);

let int128_column = OwnedColumn::<Curve25519Scalar>::Int128([1, 2, 3, 1, 0].to_vec());
let committable_int128_column = CommittableColumn::from(&int128_column);
let int128_column_bounds = ColumnBounds::from_column(&committable_int128_column);
Expand Down Expand Up @@ -574,7 +593,13 @@ mod tests {
int_a.try_union(int_b).unwrap(),
ColumnBounds::Int(Bounds::Sharp(BoundsInner { min: 1, max: 6 }))
);


let tinyint_a = ColumnBounds::TinyInt(Bounds::Sharp(BoundsInner { min: 1, max: 3 }));
let tinyint_b = ColumnBounds::TinyInt(Bounds::Sharp(BoundsInner { min: 4, max: 6 }));
assert_eq!(
TinyInt_a.try_union(tinyint_b).unwrap(),
ColumnBounds::TinyInt(Bounds::Sharp(BoundsInner { min: 1, max: 6 }))
);
let bigint_a = ColumnBounds::BigInt(Bounds::Sharp(BoundsInner { min: 1, max: 3 }));
let bigint_b = ColumnBounds::BigInt(Bounds::Sharp(BoundsInner { min: 4, max: 6 }));
assert_eq!(
Expand Down Expand Up @@ -609,6 +634,7 @@ mod tests {
fn we_cannot_union_mismatched_column_bounds() {
let no_order = ColumnBounds::NoOrder;
let smallint = ColumnBounds::SmallInt(Bounds::Sharp(BoundsInner { min: -5, max: 5 }));
let tinyint = ColumnBounds::TinyInt(Bounds::Sharp(BoundsInner { min: -3, max: 4}));
let int = ColumnBounds::Int(Bounds::Sharp(BoundsInner { min: -10, max: 10 }));
let bigint = ColumnBounds::BigInt(Bounds::Sharp(BoundsInner { min: 1, max: 3 }));
let int128 = ColumnBounds::Int128(Bounds::Sharp(BoundsInner { min: 4, max: 6 }));
Expand All @@ -620,6 +646,7 @@ mod tests {
(int, "Int"),
(bigint, "BigInt"),
(int128, "Int128"),
(tinyint, "TinyInt"),
(timestamp, "Timestamp"),
];

Expand Down Expand Up @@ -648,6 +675,10 @@ mod tests {
let smallint_b = ColumnBounds::SmallInt(Bounds::Empty);
assert_eq!(smallint_a.try_difference(smallint_b).unwrap(), smallint_a);

let tinyint_a = ColumnBounds::TinyInt(Bounds::Sharp(BoundsInner { min: 1, max: 3 }));
let tinyint_b = ColumnBounds::TinyInt(Bounds::Empty);
assert_eq!(smallint_a.try_difference(tinyint_b).unwrap(), tinyint_a);

let int_a = ColumnBounds::Int(Bounds::Sharp(BoundsInner { min: 1, max: 3 }));
let int_b = ColumnBounds::Int(Bounds::Empty);
assert_eq!(int_a.try_difference(int_b).unwrap(), int_a);
Expand Down Expand Up @@ -678,6 +709,7 @@ mod tests {
let int128 = ColumnBounds::Int128(Bounds::Sharp(BoundsInner { min: 4, max: 6 }));
let timestamp = ColumnBounds::TimestampTZ(Bounds::Sharp(BoundsInner { min: 4, max: 6 }));
let smallint = ColumnBounds::SmallInt(Bounds::Sharp(BoundsInner { min: 1, max: 3 }));
let tinyint = ColumnBounds::TinyInt(Bounds::Sharp(BoundsInner { min: 2, max: 4}));

assert!(no_order.try_difference(bigint).is_err());
assert!(bigint.try_difference(no_order).is_err());
Expand All @@ -690,5 +722,17 @@ mod tests {

assert!(smallint.try_difference(timestamp).is_err());
assert!(timestamp.try_difference(smallint).is_err());

assert!(tinyint.try_difference(smallint).is_err());
assert!(smallint.try_difference(tinyint).is_err());

assert!(tinyint.try_difference(bigint).is_err());
assert!(bigint.try_difference(tinyint).is_err());

assert!(tinyint.try_difference(int128).is_err());
assert!(int128.try_difference(tinyint).is_err());

assert!(tinyint.try_difference(timestamp).is_err());
assert!(timestamp.try_difference(tinyint).is_err());
}
}
118 changes: 116 additions & 2 deletions crates/proof-of-sql/src/base/commitment/column_commitment_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ impl ColumnCommitmentMetadata {
(ColumnType::SmallInt, ColumnBounds::SmallInt(_))
| (ColumnType::Int, ColumnBounds::Int(_))
| (ColumnType::BigInt, ColumnBounds::BigInt(_))
| (ColumnType::TinyInt, ColumnBounds::TinyInt(_))
| (ColumnType::Int128, ColumnBounds::Int128(_))
| (ColumnType::TimestampTZ(_, _), ColumnBounds::TimestampTZ(_))
| (
Expand Down Expand Up @@ -81,6 +82,10 @@ impl ColumnCommitmentMetadata {
BoundsInner::try_new(i64::MIN, i64::MAX)
.expect("i64::MIN and i64::MAX are valid bounds for BigInt"),
)),
ColumnType::TinyInt => ColumnBounds::TinyInt(super::Bounds::Bounded(
BoundsInner::try_new(i8::MIN, i8::MAX)
.expect("i64::MIN and i64::MAX are valid bounds for TinyInt"),
)),
ColumnType::TimestampTZ(_, _) => ColumnBounds::TimestampTZ(super::Bounds::Bounded(
BoundsInner::try_new(i64::MIN, i64::MAX)
.expect("i64::MIN and i64::MAX are valid bounds for TimeStamp"),
Expand Down Expand Up @@ -215,6 +220,17 @@ mod tests {
}
);

assert_eq!(
ColumnCommitmentMetadata::try_new(
ColumnType::TinyInt,
ColumnBounds::TinyInt(Bounds::Empty)
)
.unwrap(),
ColumnCommitmentMetadata {
column_type: ColumnType::TinyInt,
bounds: ColumnBounds::TinyInt(Bounds::Empty)
}
);
assert_eq!(
ColumnCommitmentMetadata::try_new(ColumnType::Boolean, ColumnBounds::NoOrder,).unwrap(),
ColumnCommitmentMetadata {
Expand Down Expand Up @@ -440,6 +456,18 @@ mod tests {
panic!("Bounds constructed from nonempty BigInt column should be ColumnBounds::SmallInt(Bounds::Sharp(_))");
}

let tinyint_column = OwnedColumn::<Curve25519Scalar>::TinyInt([1, 2, 3, 1, 0].to_vec());
let committable_tinyint_column = CommittableColumn::from(&tinyint_column);
let tinyint_metadata = ColumnCommitmentMetadata::from_column(&committable_tinyint_column);
assert_eq!(tinyint.column_type(), &ColumnType::TinyInt);
if let ColumnBounds::TinyInt(Bounds::Sharp(bounds)) = tinyint_metadata.bounds() {
assert_eq!(bounds.min(), &0);
assert_eq!(bounds.max(), &3);
} else {
panic!("Bounds constructed from nonempty BigInt column should be ColumnBounds::TinyInt(Bounds::Sharp(_))");
}


let int128_column = OwnedColumn::<Curve25519Scalar>::Int128([].to_vec());
let committable_int128_column = CommittableColumn::from(&int128_column);
let int128_metadata = ColumnCommitmentMetadata::from_column(&committable_int128_column);
Expand Down Expand Up @@ -509,6 +537,18 @@ mod tests {
smallint_metadata_c
);

let ints = [1, 2, 3, 1, 0];
let tinyint_column_a = CommittableColumn::TinyInt(&ints[..2]);
let tinyint_metadata_a = ColumnCommitmentMetadata::from_column(&tinyint_column_a);
let tinyint_column_b = CommittableColumn::TinyInt(&ints[2..]);
let tinyint_metadata_b = ColumnCommitmentMetadata::from_column(&tinyint_column_b);
let tinyint_column_c = CommittableColumn::TinyInt(&ints);
let tinyint_metadata_c = ColumnCommitmentMetadata::from_column(&tinyint_column_c);
assert_eq!(
tinyint_metadata_a.try_union(tinyint_metadata_b).unwrap(),
tinyint_metadata_c
);

let ints = [1, 2, 3, 1, 0];
let int_column_a = CommittableColumn::Int(&ints[..2]);
let int_metadata_a = ColumnCommitmentMetadata::from_column(&int_column_a);
Expand Down Expand Up @@ -647,8 +687,8 @@ mod tests {
fn we_can_difference_smallint_matching_metadata() {
// Ordered case
let smallints = [1, 2, 3, 1, 0];
let smallint_column_a = CommittableColumn::SmallInt(&smallints[..2]);
let smallint_metadata_a = ColumnCommitmentMetadata::from_column(&smallint_column_a);
let smallint_column_a = CommittableColumn::SmallInt(&s[..2]);
let smallint_metadata_a = ColumnCommitmentMetadata::from_column(&_column_a);
let smallint_column_b = CommittableColumn::SmallInt(&smallints);
let smallint_metadata_b = ColumnCommitmentMetadata::from_column(&smallint_column_b);

Expand Down Expand Up @@ -679,6 +719,39 @@ mod tests {
smallint_metadata_empty
);
}
fn we_can_difference_tinyint_matching_metadata() {
// Ordered case
let tinyints = [1, 2, 3, 1, 0];
let tinyint_column_a = CommittableColumn::TinyInt(&tinyints[..2]);
let tinyint_metadata_a = ColumnCommitmentMetadata::from_column(&tinyint_column_a);
let tinyint_column_b = CommittableColumn::TinyInt(&tinyints);
let tinyint_metadata_b = ColumnCommitmentMetadata::from_column(&tinyint_column_b);

let b_difference_a = tinyint_metadata_b.try_difference(tinyint_metadata_a).unwrap();
assert_eq!(b_difference_a.column_type, ColumnType::TinyInt);
if let ColumnBounds::TinyInt(Bounds::Bounded(bounds)) = b_difference_a.bounds() {
assert_eq!(bounds.min(), &0);
assert_eq!(bounds.max(), &3);
} else {
panic!("difference of overlapping bounds should be Bounded");
}

let tinyint_column_empty = CommittableColumn::TinyInt(&[]);
let tinyint_metadata_empty = ColumnCommitmentMetadata::from_column(&tinyint_column_empty);

assert_eq!(
tinyint_metadata_b
.try_difference(tinyint_metadata_empty)
.unwrap(),
tinyint_metadata_b
);
assert_eq!(
tinyint_metadata_empty
.try_difference(tinyint_metadata_b)
.unwrap(),
tinyint_metadata_empty
);
}

#[test]
fn we_can_difference_int_matching_metadata() {
Expand Down Expand Up @@ -729,6 +802,10 @@ mod tests {
column_type: ColumnType::SmallInt,
bounds: ColumnBounds::SmallInt(Bounds::Empty),
};
let tinyint_metadata = ColumnCommitmentMetadata {
column_type: ColumnType::TinyInt,
bounds: ColumnBounds::TinyInt(Bounds::Empty),
};
let int_metadata = ColumnCommitmentMetadata {
column_type: ColumnType::Int,
bounds: ColumnBounds::Int(Bounds::Empty),
Expand Down Expand Up @@ -758,6 +835,25 @@ mod tests {
assert!(smallint_metadata.try_union(boolean_metadata).is_err());
assert!(boolean_metadata.try_union(smallint_metadata).is_err());

assert!(tinyint_metadata.try_union(scalar_metadata).is_err());
assert!(scalar_metadata.try_union(tinyint_metadata).is_err());

assert!(tinyint_metadata.try_union(int_metadata).is_err());
assert!(int_metadata.try_union(tinyint_metadata).is_err());

assert!(tinyint_metadata.try_union(bigint_metadata).is_err());
assert!(bigint_metadata.try_union(tinyint_metadata).is_err());

assert!(tinyint_metadata.try_union(smallint_metadata).is_err());
assert!(smallint_metadata.try_union(tinyint_metadata).is_err());

assert!(tinyint_metadata.try_union(boolean_metadata).is_err());
assert!(boolean_metadata.try_union(tinyint_metadata).is_err());

assert!(tinyint_metadata.try_union(varchar_metadata).is_err());
assert!(varchar_metadata.try_union(tinyint_metadata).is_err());


assert!(int_metadata.try_union(scalar_metadata).is_err());
assert!(scalar_metadata.try_union(int_metadata).is_err());

Expand Down Expand Up @@ -845,6 +941,24 @@ mod tests {
assert!(boolean_metadata.try_difference(scalar_metadata).is_err());
assert!(scalar_metadata.try_difference(boolean_metadata).is_err());

assert!(tinyint_metadata.try_difference(scalar_metadata).is_err());
assert!(scalar_metadata.try_difference(tinyint_metadata).is_err());

assert!(tinyint_metadata.try_difference(int_metadata).is_err());
assert!(int_metadata.try_difference(tinyint_metadata).is_err());

assert!(tinyint_metadata.try_difference(bigint_metadata).is_err());
assert!(bigint_metadata.try_difference(tinyint_metadata).is_err());

assert!(tinyint_metadata.try_difference(smallint_metadata).is_err());
assert!(smallint_metadata.try_difference(tinyint_metadata).is_err());

assert!(tinyint_metadata.try_difference(boolean_metadata).is_err());
assert!(boolean_metadata.try_difference(tinyint_metadata).is_err());

assert!(tinyint_metadata.try_difference(varchar_metadata).is_err());
assert!(varchar_metadata.try_difference(tinyint_metadata).is_err());

let different_decimal75_metadata = ColumnCommitmentMetadata {
column_type: ColumnType::Decimal75(Precision::new(75).unwrap(), 0),
bounds: ColumnBounds::Int128(Bounds::Empty),
Expand Down
Loading