Skip to content

Commit

Permalink
fixup!: CI
Browse files Browse the repository at this point in the history
  • Loading branch information
varshith257 committed Jan 12, 2025
1 parent 1bc4808 commit f7884a6
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 83 deletions.
134 changes: 77 additions & 57 deletions crates/proof-of-sql/src/base/database/column.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use super::{OwnedColumn, TableRef};
use crate::{
alloc::string::ToString,
base::{
math::{decimal::Precision, i256::I256},
scalar::{Scalar, ScalarExt},
slice_ops::slice_cast_with,
},
sql::parse::ConversionError,
};
use alloc::vec::Vec;
use alloc::{format, string::String, vec::Vec};
use bumpalo::Bump;
use core::{
fmt,
Expand All @@ -16,6 +17,7 @@ use core::{
};
use proof_of_sql_parser::posql_time::PoSQLTimeUnit;
use serde::{Deserialize, Serialize};
use snafu::Snafu;
use sqlparser::ast::{DataType, ExactNumberInfo, Expr as SqlExpr, Ident, TimezoneInfo, Value};

/// Represents a read-only view of a column in an in-memory,
Expand Down Expand Up @@ -119,7 +121,9 @@ impl<'a, S: Scalar> Column<'a, S> {
SqlExpr::Value(Value::Number(value, _)) => {
let n = value
.parse::<i64>()
.map_err(|_| ColumnError::InvalidNumberFormat(value.clone()))?;
.map_err(|_| ColumnError::InvalidNumberFormat {
value: value.clone(),
})?;

if let Ok(n_i8) = i8::try_from(n) {
Ok(Column::TinyInt(alloc.alloc_slice_fill_copy(length, n_i8)))
Expand All @@ -142,8 +146,10 @@ impl<'a, S: Scalar> Column<'a, S> {
SqlExpr::TypedString { data_type, value } => match data_type {
// Decimal values
DataType::Decimal(ExactNumberInfo::PrecisionAndScale(precision, scale)) => {
let i256_value = I256::from_string(value)
.map_err(|_| ColumnError::InvalidDecimal(value.clone()))?;
let i256_value =
I256::from_string(value).map_err(|_| ColumnError::InvalidDecimal {
value: value.clone(),
})?;
let precision_u8 =
u8::try_from(*precision).expect("Precision must fit into u8");
let scale_i8 = i8::try_from(*scale).expect("Scale must fit into i8");
Expand All @@ -161,37 +167,49 @@ impl<'a, S: Scalar> Column<'a, S> {
DataType::Timestamp(Some(precision), tz) => {
let time_unit =
PoSQLTimeUnit::from_precision(*precision).unwrap_or(PoSQLTimeUnit::Second);
let timestamp_value = value
.parse::<i64>()
.map_err(|_| ColumnError::InvalidNumberFormat(value.clone()))?;
let timestamp_value =
value
.parse::<i64>()
.map_err(|_| ColumnError::InvalidNumberFormat {
value: value.clone(),
})?;
Ok(Column::TimestampTZ(
time_unit,
*tz,
alloc.alloc_slice_fill_copy(length, timestamp_value),
))
}
// DataType::Custom(_, _) if data_type.to_string() == "scalar" => {
// let scalar_str = value
// .strip_prefix("scalar:")
// .ok_or_else(|| ColumnError::InvalidScalarFormat(value.clone()))?;
// let limbs: Vec<u64> = scalar_str
// .split(',')
// .map(|x| {
// x.parse::<u64>()
// .map_err(|_| ColumnError::InvalidScalarFormat(value.clone()))
// })
// .collect::<Result<Vec<u64>, ColumnError>>()?;
// if limbs.len() != 4 {
// return Err(ColumnError::InvalidScalarFormat(value.clone()));
// }
// Ok(Column::Scalar(alloc.alloc_slice_fill_copy(
// length,
// Scalar::from([limbs[0], limbs[1], limbs[2], limbs[3]]),
// )))
// }
_ => Err(ColumnError::UnsupportedDataType(format!("{data_type:?}"))),
DataType::Custom(_, _) if data_type.to_string() == "scalar" => {
let scalar_str = value.strip_prefix("scalar:").ok_or_else(|| {
ColumnError::InvalidScalarFormat {
value: value.clone(),
}
})?;
let limbs: Vec<u64> = scalar_str
.split(',')
.map(|x| {
x.parse::<u64>()
.map_err(|_| ColumnError::InvalidScalarFormat {
value: value.clone(),
})
})
.collect::<Result<Vec<u64>, ColumnError>>()?;
if limbs.len() != 4 {
return Err(ColumnError::InvalidScalarFormat {
value: value.clone(),
});
}
Ok(Column::Scalar(
alloc.alloc_slice_fill_copy(length, value.clone().into()),
))
}
_ => Err(ColumnError::UnsupportedDataType {
data_type: format!("{expr:?}"),
}),
},
_ => Err(ColumnError::UnsupportedDataType(format!("{expr:?}"))),
_ => Err(ColumnError::UnsupportedDataType {
data_type: format!("{expr:?}"),
}),
}
}

Expand Down Expand Up @@ -339,45 +357,47 @@ impl<'a, S: Scalar> Column<'a, S> {
}

/// Represents errors that can occur while working with columns.
#[derive(Debug, PartialEq, Eq)]
#[derive(Snafu, Debug, PartialEq, Eq)]
pub enum ColumnError {
#[snafu(display("Invalid number format: {value}"))]
/// Error for invalid number format.
InvalidNumberFormat(String),
InvalidNumberFormat {
/// The invalid number value that caused the error.
value: String,
},

#[snafu(display("Unsupported data type: {data_type}"))]
/// Error for unsupported data types.
UnsupportedDataType(String),
UnsupportedDataType {
/// The unsupported data type as a string.
data_type: String,
},

#[snafu(display("Scalar parsing error: {value}"))]
/// Error for scalar parsing failures.
ScalarParsingError(String),
InvalidScalarFormat {
/// The scalar value that caused the parsing error.
value: String,
},

#[snafu(display("Invalid decimal format: {value}"))]
/// Error for invalid decimal format.
InvalidDecimal(String),
/// Error for column operation conversion issues.
ConversionError(ConversionError),
}
InvalidDecimal {
/// The invalid decimal value that caused the error.
value: String,
},

impl std::fmt::Display for ColumnError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ColumnError::InvalidNumberFormat(value) => {
write!(f, "Invalid number format: {value}")
}
ColumnError::UnsupportedDataType(data_type) => {
write!(f, "Unsupported data type: {data_type}")
}
ColumnError::ScalarParsingError(value) => {
write!(f, "Scalar parsing error: {value}")
}
ColumnError::InvalidDecimal(value) => {
write!(f, "Invalid decimal format: {value}")
}
ColumnError::ConversionError(err) => write!(f, "Conversion error: {err}"),
}
}
#[snafu(display("Conversion error: {source}"))]
/// Error for column operation conversion issues.
ConversionError {
/// The underlying conversion error that occurred.
source: ConversionError,
},
}

impl std::error::Error for ColumnError {}

impl From<ConversionError> for ColumnError {
fn from(error: ConversionError) -> Self {
ColumnError::ConversionError(error)
ColumnError::ConversionError { source: error }
}
}

Expand Down
1 change: 1 addition & 0 deletions crates/proof-of-sql/src/base/database/expr_utility.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use alloc::{boxed::Box, vec};
use proof_of_sql_parser::{intermediate_ast::Literal, sqlparser::SqlAliasedResultExpr};
use sqlparser::ast::{
BinaryOperator, Expr, Function, FunctionArg, FunctionArgExpr, Ident, ObjectName, UnaryOperator,
Expand Down
24 changes: 8 additions & 16 deletions crates/proof-of-sql/src/base/database/literal_value.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
use crate::base::{
database::ColumnType,
math::{decimal::Precision, i256::I256},
scalar::Scalar,
use crate::{
alloc::string::ToString,
base::{
database::ColumnType,
math::{decimal::Precision, i256::I256},
scalar::Scalar,
},
};
use alloc::vec::Vec;
use proof_of_sql_parser::posql_time::PoSQLTimeUnit;
use sqlparser::ast::{DataType, ExactNumberInfo, Expr, Value};

Expand Down Expand Up @@ -72,18 +76,6 @@ impl ToScalar for Expr {
.unwrap_or_else(|_| panic!("Invalid number: {n}"))
.into(),
Expr::Value(Value::SingleQuotedString(s)) => s.into(),
// Expr::TypedString { data_type, value }
// if matches!(data_type, DataType::Decimal(_)) =>
// {
// let bigint_value = match num_bigint::BigInt::parse_bytes(value.as_bytes(), 10) {
// Some(bigint) => bigint,
// // Will be handled later
// None => panic!("Failed to parse '{}' as a decimal", value),
// };

// let i256_value = I256::from_num_bigint(&bigint_value);
// i256_value.into_scalar()
// }
Expr::TypedString { data_type, value } if data_type.to_string() == "scalar" => {
let scalar_str = value.strip_prefix("scalar:").unwrap();
let limbs: Vec<u64> = scalar_str
Expand Down
3 changes: 1 addition & 2 deletions crates/proof-of-sql/src/base/math/i256.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::base::scalar::Scalar;
use alloc::vec::Vec;
use alloc::{fmt, format, string::String, vec::Vec};
use ark_ff::BigInteger;
use serde::{Deserialize, Serialize};
use std::fmt;

/// A 256-bit data type with some conversions implemented that interpret it as a signed integer.
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
base::{database::ColumnRef, map::IndexMap, math::i256::I256},
sql::proof_exprs::{ColumnExpr, DynProofExpr, ProofExpr},
};
use alloc::{boxed::Box, format, string::ToString, vec::Vec};
use alloc::{boxed::Box, format, string::ToString, vec, vec::Vec};
use proof_of_sql_parser::posql_time::PoSQLTimeUnit;
use sqlparser::ast::{
BinaryOperator, DataType, ExactNumberInfo, Expr, FunctionArg, FunctionArgExpr, Ident,
Expand Down
5 changes: 4 additions & 1 deletion crates/proof-of-sql/src/sql/proof_exprs/aggregation.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use alloc::{
fmt,
fmt::{Display, Formatter},
};
use serde::{Deserialize, Serialize};
use std::fmt::{self, Display, Formatter};

/// Aggregation operators
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Hash, Clone, Copy)]
Expand Down
10 changes: 5 additions & 5 deletions crates/proof-of-sql/src/sql/proof_exprs/comparison_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
},
sql::parse::{type_check_binary_operation, ConversionError, ConversionResult},
};
use alloc::string::ToString;
use alloc::{format, string::ToString, vec};
use bumpalo::Bump;
use core::cmp::{max, Ordering};
use sqlparser::ast::{BinaryOperator, DataType, Expr as SqlExpr, ObjectName};
Expand Down Expand Up @@ -164,7 +164,7 @@ pub(crate) fn scale_and_subtract_columnar_value<'a, S: Scalar>(
(ColumnarValue::Column(lhs), ColumnarValue::Column(rhs)) => {
Ok(ColumnarValue::Column(Column::Scalar(
scale_and_subtract(alloc, lhs, rhs, lhs_scale, rhs_scale, is_equal)
.map_err(ColumnError::ConversionError)?,
.map_err(|err| ColumnError::ConversionError { source: err })?,
)))
}
(ColumnarValue::Literal(lhs), ColumnarValue::Column(rhs)) => {
Expand All @@ -177,7 +177,7 @@ pub(crate) fn scale_and_subtract_columnar_value<'a, S: Scalar>(
rhs_scale,
is_equal,
)
.map_err(ColumnError::ConversionError)?,
.map_err(|err| ColumnError::ConversionError { source: err })?,
)))
}
(ColumnarValue::Column(lhs), ColumnarValue::Literal(rhs)) => {
Expand All @@ -190,13 +190,13 @@ pub(crate) fn scale_and_subtract_columnar_value<'a, S: Scalar>(
rhs_scale,
is_equal,
)
.map_err(ColumnError::ConversionError)?,
.map_err(|err| ColumnError::ConversionError { source: err })?,
)))
}
(ColumnarValue::Literal(lhs), ColumnarValue::Literal(rhs)) => {
let result_scalar =
scale_and_subtract_literal::<S>(&lhs, &rhs, lhs_scale, rhs_scale, is_equal)
.map_err(ColumnError::ConversionError)?;
.map_err(|err| ColumnError::ConversionError { source: err })?;
Ok(ColumnarValue::Literal(SqlExpr::TypedString {
data_type: DataType::Custom(ObjectName(vec![]), vec![]),
value: format!("scalar:{result_scalar}"),
Expand Down
2 changes: 1 addition & 1 deletion crates/proof-of-sql/src/sql/proof_exprs/dyn_proof_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::{
proof::{FinalRoundBuilder, VerificationBuilder},
},
};
use alloc::{boxed::Box, string::ToString};
use alloc::{boxed::Box, format, string::ToString};
use bumpalo::Bump;
use core::fmt::Debug;
use proof_of_sql_parser::intermediate_ast::AggregationOperator;
Expand Down
1 change: 1 addition & 0 deletions crates/proof-of-sql/src/sql/proof_exprs/numerical_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::base::{
database::{literal_value::ToScalar, Column, ColumnError, ColumnarValue},
scalar::{Scalar, ScalarExt},
};
use alloc::{format, vec};
use bumpalo::Bump;
use core::cmp::Ordering;
use sqlparser::ast::{DataType, Expr as SqlExpr, ObjectName};
Expand Down

0 comments on commit f7884a6

Please sign in to comment.