Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: initial support for timestamp #12

Merged
merged 17 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ bytemuck = {version = "1.14.2" }
byte-slice-cast = { version = "1.2.1" }
clap = { version = "4.5.4" }
criterion = { version = "0.5.1" }
chrono-tz = {version = "0.9.0", features = ["serde"]}
curve25519-dalek = { version = "4", features = ["rand_core"] }
derive_more = { version = "0.99" }
dyn_partial_eq = { version = "0.1.2" }
Expand Down
1 change: 1 addition & 0 deletions crates/proof-of-sql/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ bumpalo = { workspace = true, features = ["collections"] }
bytemuck = { workspace = true }
byte-slice-cast = { workspace = true }
curve25519-dalek = { workspace = true, features = ["serde"] }
chrono-tz = {workspace = true, features = ["serde"]}
derive_more = { workspace = true }
dyn_partial_eq = { workspace = true }
hashbrown = { workspace = true }
Expand Down
8 changes: 8 additions & 0 deletions 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 {
BigInt(Bounds<i64>),
/// The bounds of an Int128 column.
Int128(Bounds<i128>),
/// The bounds of a Timestamp column.
Timestamp(Bounds<i64>),
}

impl ColumnBounds {
Expand All @@ -219,6 +221,9 @@ impl ColumnBounds {
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)),
CommittableColumn::Timestamp(_, _, times) => {
ColumnBounds::Timestamp(Bounds::from_iter(*times))
}
CommittableColumn::Boolean(_)
| CommittableColumn::Decimal75(_, _, _)
| CommittableColumn::Scalar(_)
Expand All @@ -241,6 +246,9 @@ impl ColumnBounds {
(ColumnBounds::BigInt(bounds_a), ColumnBounds::BigInt(bounds_b)) => {
Ok(ColumnBounds::BigInt(bounds_a.union(bounds_b)))
}
(ColumnBounds::Timestamp(bounds_a), ColumnBounds::Timestamp(bounds_b)) => {
Ok(ColumnBounds::Timestamp(bounds_a.union(bounds_b)))
}
(ColumnBounds::Int128(bounds_a), ColumnBounds::Int128(bounds_b)) => {
Ok(ColumnBounds::Int128(bounds_a.union(bounds_b)))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ impl ColumnCommitmentMetadata {
| (ColumnType::Int, ColumnBounds::Int(_))
| (ColumnType::BigInt, ColumnBounds::BigInt(_))
| (ColumnType::Int128, ColumnBounds::Int128(_))
| (ColumnType::Timestamp(_, _), ColumnBounds::Timestamp(_))
| (
ColumnType::Boolean
| ColumnType::VarChar
Expand Down Expand Up @@ -72,6 +73,10 @@ impl ColumnCommitmentMetadata {
BoundsInner::try_new(i64::MIN, i64::MAX)
.expect("i64::MIN and i64::MAX are valid bounds for BigInt"),
)),
ColumnType::Timestamp(_, _) => ColumnBounds::Timestamp(super::Bounds::Bounded(
BoundsInner::try_new(i64::MIN, i64::MAX)
.expect("i64::MIN and i64::MAX are valid bounds for TimeStamp"),
)),
ColumnType::Int128 => ColumnBounds::Int128(super::Bounds::Bounded(
BoundsInner::try_new(i128::MIN, i128::MAX)
.expect("i128::MIN and i128::MAX are valid bounds for Int128"),
Expand Down
11 changes: 11 additions & 0 deletions crates/proof-of-sql/src/base/commitment/committable_column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::base::{
math::decimal::Precision,
ref_into::RefInto,
scalar::Scalar,
time::timestamp::{ProofsTimeUnit, ProofsTimeZone},
};
#[cfg(feature = "blitzar")]
use blitzar::sequence::Sequence;
Expand Down Expand Up @@ -37,6 +38,8 @@ pub enum CommittableColumn<'a> {
Scalar(Vec<[u64; 4]>),
/// Column of limbs for committing to scalars, hashed from a VarChar column.
VarChar(Vec<[u64; 4]>),
/// Borrowed Timestamp column, mapped to `i64`.
Timestamp(ProofsTimeUnit, ProofsTimeZone, &'a [i64]),
}

impl<'a> CommittableColumn<'a> {
Expand All @@ -51,6 +54,7 @@ impl<'a> CommittableColumn<'a> {
CommittableColumn::Scalar(col) => col.len(),
CommittableColumn::VarChar(col) => col.len(),
CommittableColumn::Boolean(col) => col.len(),
CommittableColumn::Timestamp(_, _, col) => col.len(),
}
}

Expand Down Expand Up @@ -78,6 +82,7 @@ impl<'a> From<&CommittableColumn<'a>> for ColumnType {
CommittableColumn::Scalar(_) => ColumnType::Scalar,
CommittableColumn::VarChar(_) => ColumnType::VarChar,
CommittableColumn::Boolean(_) => ColumnType::Boolean,
CommittableColumn::Timestamp(tu, tz, _) => ColumnType::Timestamp(*tu, *tz),
}
}
}
Expand All @@ -99,6 +104,7 @@ impl<'a, S: Scalar> From<&Column<'a, S>> for CommittableColumn<'a> {
let as_limbs: Vec<_> = scalars.iter().map(RefInto::<[u64; 4]>::ref_into).collect();
CommittableColumn::VarChar(as_limbs)
}
Column::Timestamp(tu, tz, times) => CommittableColumn::Timestamp(*tu, *tz, times),
}
}
}
Expand Down Expand Up @@ -128,6 +134,7 @@ impl<'a, S: Scalar> From<&'a OwnedColumn<S>> for CommittableColumn<'a> {
.map(Into::<[u64; 4]>::into)
.collect(),
),
OwnedColumn::Timestamp(_, _, times) => (times as &[_]).into(),
}
}
}
Expand All @@ -142,11 +149,14 @@ impl<'a> From<&'a [i32]> for CommittableColumn<'a> {
CommittableColumn::Int(value)
}
}

// TODO: make sure this does not conflict with TimeStamp
impl<'a> From<&'a [i64]> for CommittableColumn<'a> {
fn from(value: &'a [i64]) -> Self {
CommittableColumn::BigInt(value)
}
}

impl<'a> From<&'a [i128]> for CommittableColumn<'a> {
fn from(value: &'a [i128]) -> Self {
CommittableColumn::Int128(value)
Expand Down Expand Up @@ -175,6 +185,7 @@ impl<'a, 'b> From<&'a CommittableColumn<'b>> for Sequence<'a> {
CommittableColumn::Scalar(limbs) => Sequence::from(limbs),
CommittableColumn::VarChar(limbs) => Sequence::from(limbs),
CommittableColumn::Boolean(bools) => Sequence::from(*bools),
CommittableColumn::Timestamp(_, _, times) => Sequence::from(*times),
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@ use crate::{
base::{
database::Column,
math::decimal::Precision,
scalar::{Curve25519Scalar, Scalar},
scalar::Scalar,
time::timestamp::{ProofsTimeUnit, ProofsTimeZone},
},
sql::parse::ConversionError,
};
use arrow::{
array::{
Array, ArrayRef, BooleanArray, Decimal128Array, Decimal256Array, Int16Array, Int32Array,
Int64Array, StringArray,
Int64Array, StringArray, TimestampMicrosecondArray, TimestampMillisecondArray,
TimestampNanosecondArray, TimestampSecondArray,
},
datatypes::{i256, DataType},
datatypes::{i256, DataType, TimeUnit as ArrowTimeUnit},
};
use bumpalo::Bump;
use std::ops::Range;
Expand All @@ -36,6 +38,9 @@ pub enum ArrowArrayToColumnConversionError {
/// Variant for conversion errors
#[error("conversion error: {0}")]
ConversionError(#[from] ConversionError),
/// Variant for timezone conversion errors, i.e. invalid timezone
#[error("Timezone conversion failed: {0}")]
TimezoneConversionError(String),
}

/// This trait is used to provide utility functions to convert ArrayRefs into proof types (Column, Scalars, etc.)
Expand All @@ -48,7 +53,7 @@ pub trait ArrayRefExt {
#[cfg(feature = "blitzar")]
fn to_curve25519_scalars(
&self,
) -> Result<Vec<Curve25519Scalar>, ArrowArrayToColumnConversionError>;
) -> Result<Vec<crate::base::scalar::Curve25519Scalar>, ArrowArrayToColumnConversionError>;

/// Convert an ArrayRef into a Proof of SQL Column type
///
Expand Down Expand Up @@ -76,7 +81,7 @@ impl ArrayRefExt for ArrayRef {
#[cfg(feature = "blitzar")]
fn to_curve25519_scalars(
&self,
) -> Result<Vec<Curve25519Scalar>, ArrowArrayToColumnConversionError> {
) -> Result<Vec<crate::base::scalar::Curve25519Scalar>, ArrowArrayToColumnConversionError> {
if self.null_count() != 0 {
return Err(ArrowArrayToColumnConversionError::ArrayContainsNulls);
}
Expand Down Expand Up @@ -131,6 +136,24 @@ impl ArrayRefExt for ArrayRef {
})
.collect()
}),
DataType::Timestamp(time_unit, _) => match time_unit {
ArrowTimeUnit::Second => self
.as_any()
.downcast_ref::<TimestampSecondArray>()
.map(|array| array.values().iter().map(|v| Ok((*v).into())).collect()),
ArrowTimeUnit::Millisecond => self
.as_any()
.downcast_ref::<TimestampMillisecondArray>()
.map(|array| array.values().iter().map(|v| Ok((*v).into())).collect()),
ArrowTimeUnit::Microsecond => self
.as_any()
.downcast_ref::<TimestampMicrosecondArray>()
.map(|array| array.values().iter().map(|v| Ok((*v).into())).collect()),
ArrowTimeUnit::Nanosecond => self
.as_any()
.downcast_ref::<TimestampNanosecondArray>()
.map(|array| array.values().iter().map(|v| Ok((*v).into())).collect()),
},
_ => None,
};

Expand Down Expand Up @@ -251,6 +274,61 @@ impl ArrayRefExt for ArrayRef {
))
}
}
// Handle all possible TimeStamp TimeUnit instances
DataType::Timestamp(time_unit, tz) => match time_unit {
ArrowTimeUnit::Second => {
if let Some(array) = self.as_any().downcast_ref::<TimestampSecondArray>() {
Ok(Column::Timestamp(
ProofsTimeUnit::Second,
ProofsTimeZone::try_from(tz.clone())?,
array.values(),
))
} else {
Err(ArrowArrayToColumnConversionError::UnsupportedType(
self.data_type().clone(),
))
}
}
ArrowTimeUnit::Millisecond => {
if let Some(array) = self.as_any().downcast_ref::<TimestampMillisecondArray>() {
Ok(Column::Timestamp(
ProofsTimeUnit::Millisecond,
ProofsTimeZone::try_from(tz.clone())?,
array.values(),
))
} else {
Err(ArrowArrayToColumnConversionError::UnsupportedType(
self.data_type().clone(),
))
}
}
ArrowTimeUnit::Microsecond => {
if let Some(array) = self.as_any().downcast_ref::<TimestampMicrosecondArray>() {
Ok(Column::Timestamp(
ProofsTimeUnit::Microsecond,
ProofsTimeZone::try_from(tz.clone())?,
array.values(),
))
} else {
Err(ArrowArrayToColumnConversionError::UnsupportedType(
self.data_type().clone(),
))
}
}
ArrowTimeUnit::Nanosecond => {
if let Some(array) = self.as_any().downcast_ref::<TimestampNanosecondArray>() {
Ok(Column::Timestamp(
ProofsTimeUnit::Nanosecond,
ProofsTimeZone::try_from(tz.clone())?,
array.values(),
))
} else {
Err(ArrowArrayToColumnConversionError::UnsupportedType(
self.data_type().clone(),
))
}
}
},
DataType::Utf8 => {
if let Some(array) = self.as_any().downcast_ref::<StringArray>() {
let vals = alloc
Expand Down Expand Up @@ -283,7 +361,7 @@ impl ArrayRefExt for ArrayRef {
mod tests {

use super::*;
use crate::proof_primitive::dory::DoryScalar;
use crate::{base::scalar::Curve25519Scalar, proof_primitive::dory::DoryScalar};
use arrow::array::Decimal256Builder;
use std::{str::FromStr, sync::Arc};

Expand Down
Loading
Loading