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

refactor!: modify ProofExpr::result_evaluate to return ColumnarValue and remove table_length as arg #357

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::base::{database::ColumnType, math::decimal::DecimalError};
use alloc::string::String;
use core::result::Result;
use proof_of_sql_parser::intermediate_ast::{BinaryOperator, UnaryOperator};
use snafu::Snafu;

/// Errors from operations on columns.
Expand All @@ -19,8 +18,8 @@ pub enum ColumnOperationError {
/// Incorrect `ColumnType` in binary operations
#[snafu(display("{operator:?}(lhs: {left_type:?}, rhs: {right_type:?}) is not supported"))]
BinaryOperationInvalidColumnType {
/// `BinaryOperator` that caused the error
operator: BinaryOperator,
/// Binary operator that caused the error
operator: String,
/// `ColumnType` of left operand
left_type: ColumnType,
/// `ColumnType` of right operand
Expand All @@ -30,8 +29,8 @@ pub enum ColumnOperationError {
/// Incorrect `ColumnType` in unary operations
#[snafu(display("{operator:?}(operand: {operand_type:?}) is not supported"))]
UnaryOperationInvalidColumnType {
/// `UnaryOperator` that caused the error
operator: UnaryOperator,
/// Unary operator that caused the error
operator: String,
/// `ColumnType` of the operand
operand_type: ColumnType,
},
Expand Down
80 changes: 39 additions & 41 deletions crates/proof-of-sql/src/base/database/column_type_operation.rs

Large diffs are not rendered by default.

76 changes: 75 additions & 1 deletion crates/proof-of-sql/src/base/database/columnar_value.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::base::{
database::{Column, ColumnType, LiteralValue},
database::{Column, ColumnOperationError, ColumnOperationResult, ColumnType, LiteralValue},
scalar::Scalar,
};
use alloc::string::ToString;
use bumpalo::Bump;
use snafu::Snafu;

Expand Down Expand Up @@ -60,6 +61,79 @@ impl<'a, S: Scalar> ColumnarValue<'a, S> {
}
}
}

/// Applies a unary operator to a [`ColumnarValue`].
pub(crate) fn apply_boolean_unary_operator<F>(
&self,
op: F,
alloc: &'a Bump,
) -> ColumnOperationResult<ColumnarValue<'a, S>>
where
F: Fn(&bool) -> bool,
{
match self {
ColumnarValue::Literal(LiteralValue::Boolean(value)) => {
Ok(ColumnarValue::Literal(LiteralValue::Boolean(op(value))))
}
ColumnarValue::Column(Column::Boolean(column)) => Ok(ColumnarValue::Column(
Column::Boolean(alloc.alloc_slice_fill_with(column.len(), |i| op(&column[i]))),
)),
_ => Err(ColumnOperationError::UnaryOperationInvalidColumnType {
operator: "Some func Fn(&bool) -> bool".to_string(),
operand_type: self.column_type(),
}),
}
}

/// Applies a binary operator to two [`ColumnarValue`]s.
pub(crate) fn apply_boolean_binary_operator<F>(
&self,
rhs: &Self,
op: F,
alloc: &'a Bump,
) -> ColumnOperationResult<ColumnarValue<'a, S>>
where
F: Fn(&bool, &bool) -> bool,
{
match (self, rhs) {
(
ColumnarValue::Literal(LiteralValue::Boolean(lhs)),
ColumnarValue::Literal(LiteralValue::Boolean(rhs)),
) => Ok(ColumnarValue::Literal(LiteralValue::Boolean(op(lhs, rhs)))),
(
ColumnarValue::Column(Column::Boolean(lhs)),
ColumnarValue::Literal(LiteralValue::Boolean(rhs)),
) => Ok(ColumnarValue::Column(Column::Boolean(
alloc.alloc_slice_fill_with(lhs.len(), |i| op(&lhs[i], rhs)),
))),
(
ColumnarValue::Literal(LiteralValue::Boolean(lhs)),
ColumnarValue::Column(Column::Boolean(rhs)),
) => Ok(ColumnarValue::Column(Column::Boolean(
alloc.alloc_slice_fill_with(rhs.len(), |i| op(lhs, &rhs[i])),
))),
(
ColumnarValue::Column(Column::Boolean(lhs)),
ColumnarValue::Column(Column::Boolean(rhs)),
) => {
let len = lhs.len();
if len != rhs.len() {
return Err(ColumnOperationError::DifferentColumnLength {
len_a: len,
len_b: rhs.len(),
});
}
Ok(ColumnarValue::Column(Column::Boolean(
alloc.alloc_slice_fill_with(len, |i| op(&lhs[i], &rhs[i])),
)))
}
_ => Err(ColumnOperationError::BinaryOperationInvalidColumnType {
operator: "Some func Fn(&bool, &bool) -> bool".to_string(),
left_type: self.column_type(),
right_type: rhs.column_type(),
}),
}
}
}

#[cfg(test)]
Expand Down
22 changes: 11 additions & 11 deletions crates/proof-of-sql/src/base/database/owned_column_operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ use crate::base::{
},
scalar::Scalar,
};
use alloc::string::ToString;
use core::ops::{Add, Div, Mul, Sub};
use proof_of_sql_parser::intermediate_ast::{BinaryOperator, UnaryOperator};

impl<S: Scalar> OwnedColumn<S> {
/// Element-wise NOT operation for a column
pub fn element_wise_not(&self) -> ColumnOperationResult<Self> {
match self {
Self::Boolean(values) => Ok(Self::Boolean(slice_not(values))),
_ => Err(ColumnOperationError::UnaryOperationInvalidColumnType {
operator: UnaryOperator::Not,
operator: "NOT".to_string(),
operand_type: self.column_type(),
}),
}
Expand All @@ -42,7 +42,7 @@ impl<S: Scalar> OwnedColumn<S> {
match (self, rhs) {
(Self::Boolean(lhs), Self::Boolean(rhs)) => Ok(Self::Boolean(slice_and(lhs, rhs))),
_ => Err(ColumnOperationError::BinaryOperationInvalidColumnType {
operator: BinaryOperator::And,
operator: "AND".to_string(),
left_type: self.column_type(),
right_type: rhs.column_type(),
}),
Expand All @@ -60,7 +60,7 @@ impl<S: Scalar> OwnedColumn<S> {
match (self, rhs) {
(Self::Boolean(lhs), Self::Boolean(rhs)) => Ok(Self::Boolean(slice_or(lhs, rhs))),
_ => Err(ColumnOperationError::BinaryOperationInvalidColumnType {
operator: BinaryOperator::Or,
operator: "OR".to_string(),
left_type: self.column_type(),
right_type: rhs.column_type(),
}),
Expand Down Expand Up @@ -242,7 +242,7 @@ impl<S: Scalar> OwnedColumn<S> {
todo!("Implement equality check for TimeStampTZ")
}
_ => Err(ColumnOperationError::BinaryOperationInvalidColumnType {
operator: BinaryOperator::Equal,
operator: "=".to_string(),
left_type: self.column_type(),
right_type: rhs.column_type(),
}),
Expand Down Expand Up @@ -423,7 +423,7 @@ impl<S: Scalar> OwnedColumn<S> {
todo!("Implement inequality check for TimeStampTZ")
}
_ => Err(ColumnOperationError::BinaryOperationInvalidColumnType {
operator: BinaryOperator::LessThanOrEqual,
operator: "<=".to_string(),
left_type: self.column_type(),
right_type: rhs.column_type(),
}),
Expand Down Expand Up @@ -604,7 +604,7 @@ impl<S: Scalar> OwnedColumn<S> {
todo!("Implement inequality check for TimeStampTZ")
}
_ => Err(ColumnOperationError::BinaryOperationInvalidColumnType {
operator: BinaryOperator::GreaterThanOrEqual,
operator: ">=".to_string(),
left_type: self.column_type(),
right_type: rhs.column_type(),
}),
Expand Down Expand Up @@ -798,7 +798,7 @@ impl<S: Scalar> Add for OwnedColumn<S> {
Ok(Self::Decimal75(new_precision, new_scale, new_values))
}
_ => Err(ColumnOperationError::BinaryOperationInvalidColumnType {
operator: BinaryOperator::Add,
operator: "+".to_string(),
left_type: self.column_type(),
right_type: rhs.column_type(),
}),
Expand Down Expand Up @@ -996,7 +996,7 @@ impl<S: Scalar> Sub for OwnedColumn<S> {
Ok(Self::Decimal75(new_precision, new_scale, new_values))
}
_ => Err(ColumnOperationError::BinaryOperationInvalidColumnType {
operator: BinaryOperator::Subtract,
operator: "-".to_string(),
left_type: self.column_type(),
right_type: rhs.column_type(),
}),
Expand Down Expand Up @@ -1194,7 +1194,7 @@ impl<S: Scalar> Mul for OwnedColumn<S> {
Ok(Self::Decimal75(new_precision, new_scale, new_values))
}
_ => Err(ColumnOperationError::BinaryOperationInvalidColumnType {
operator: BinaryOperator::Multiply,
operator: "*".to_string(),
left_type: self.column_type(),
right_type: rhs.column_type(),
}),
Expand Down Expand Up @@ -1392,7 +1392,7 @@ impl<S: Scalar> Div for OwnedColumn<S> {
Ok(Self::Decimal75(new_precision, new_scale, new_values))
}
_ => Err(ColumnOperationError::BinaryOperationInvalidColumnType {
operator: BinaryOperator::Division,
operator: "/".to_string(),
left_type: self.column_type(),
right_type: rhs.column_type(),
}),
Expand Down
10 changes: 2 additions & 8 deletions crates/proof-of-sql/src/base/database/slice_decimal_operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use alloc::vec::Vec;
use core::{cmp::Ordering, fmt::Debug};
use num_bigint::BigInt;
use num_traits::Zero;
use proof_of_sql_parser::intermediate_ast::BinaryOperator;
/// Check whether a numerical slice is equal to a decimal one.
///
/// Note that we do not check for length equality here.
Expand Down Expand Up @@ -273,8 +272,7 @@ where
T0: Copy,
T1: Copy,
{
let new_column_type =
try_add_subtract_column_types(left_column_type, right_column_type, BinaryOperator::Add)?;
let new_column_type = try_add_subtract_column_types(left_column_type, right_column_type)?;
let new_precision_value = new_column_type
.precision_value()
.expect("numeric columns have precision");
Expand Down Expand Up @@ -332,11 +330,7 @@ where
T0: Copy,
T1: Copy,
{
let new_column_type = try_add_subtract_column_types(
left_column_type,
right_column_type,
BinaryOperator::Subtract,
)?;
let new_column_type = try_add_subtract_column_types(left_column_type, right_column_type)?;
let new_precision_value = new_column_type
.precision_value()
.expect("numeric columns have precision");
Expand Down
7 changes: 2 additions & 5 deletions crates/proof-of-sql/src/sql/parse/query_context_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,12 +305,9 @@ pub(crate) fn type_check_binary_operation(
| (ColumnType::TimestampTZ(_, _), ColumnType::TimestampTZ(_, _))
)
}
BinaryOperator::Add => {
try_add_subtract_column_types(*left_dtype, *right_dtype, BinaryOperator::Add).is_ok()
}
BinaryOperator::Add => try_add_subtract_column_types(*left_dtype, *right_dtype).is_ok(),
BinaryOperator::Subtract => {
try_add_subtract_column_types(*left_dtype, *right_dtype, BinaryOperator::Subtract)
.is_ok()
try_add_subtract_column_types(*left_dtype, *right_dtype).is_ok()
}
BinaryOperator::Multiply => try_multiply_column_types(*left_dtype, *right_dtype).is_ok(),
BinaryOperator::Division => left_dtype.is_numeric() && right_dtype.is_numeric(),
Expand Down
1 change: 0 additions & 1 deletion crates/proof-of-sql/src/sql/proof/proof_plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ pub trait ProverEvaluate {
/// Evaluate the query and modify `FirstRoundBuilder` to track the result of the query.
fn result_evaluate<'a, S: Scalar>(
&self,
input_length: usize,
alloc: &'a Bump,
accessor: &'a dyn DataAccessor<S>,
) -> Vec<Column<'a, S>>;
Expand Down
2 changes: 1 addition & 1 deletion crates/proof-of-sql/src/sql/proof/query_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl<CP: CommitmentEvaluationProof> QueryProof<CP> {
let alloc = Bump::new();

// Evaluate query result
let result_cols = expr.result_evaluate(table_length, &alloc, accessor);
let result_cols = expr.result_evaluate(&alloc, accessor);
let output_length = result_cols.first().map_or(0, Column::len);
let provable_result = ProvableQueryResult::new(output_length as u64, &result_cols);

Expand Down
4 changes: 0 additions & 4 deletions crates/proof-of-sql/src/sql/proof/query_proof_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ impl Default for TrivialTestProofPlan {
impl ProverEvaluate for TrivialTestProofPlan {
fn result_evaluate<'a, S: Scalar>(
&self,
_input_length: usize,
alloc: &'a Bump,
_accessor: &'a dyn DataAccessor<S>,
) -> Vec<Column<'a, S>> {
Expand Down Expand Up @@ -203,7 +202,6 @@ impl Default for SquareTestProofPlan {
impl ProverEvaluate for SquareTestProofPlan {
fn result_evaluate<'a, S: Scalar>(
&self,
_table_length: usize,
alloc: &'a Bump,
_accessor: &'a dyn DataAccessor<S>,
) -> Vec<Column<'a, S>> {
Expand Down Expand Up @@ -388,7 +386,6 @@ impl Default for DoubleSquareTestProofPlan {
impl ProverEvaluate for DoubleSquareTestProofPlan {
fn result_evaluate<'a, S: Scalar>(
&self,
_input_length: usize,
alloc: &'a Bump,
_accessor: &'a dyn DataAccessor<S>,
) -> Vec<Column<'a, S>> {
Expand Down Expand Up @@ -603,7 +600,6 @@ struct ChallengeTestProofPlan {}
impl ProverEvaluate for ChallengeTestProofPlan {
fn result_evaluate<'a, S: Scalar>(
&self,
_input_length: usize,
_alloc: &'a Bump,
_accessor: &'a dyn DataAccessor<S>,
) -> Vec<Column<'a, S>> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ pub(super) struct EmptyTestQueryExpr {
impl ProverEvaluate for EmptyTestQueryExpr {
fn result_evaluate<'a, S: Scalar>(
&self,
_input_length: usize,
alloc: &'a Bump,
_accessor: &'a dyn DataAccessor<S>,
) -> Vec<Column<'a, S>> {
Expand Down
32 changes: 14 additions & 18 deletions crates/proof-of-sql/src/sql/proof_exprs/add_subtract_expr.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use super::{add_subtract_columns, scale_and_add_subtract_eval, DynProofExpr, ProofExpr};
use super::{
add_subtract_columnar_values, add_subtract_columns, scale_and_add_subtract_eval, DynProofExpr,
ProofExpr,
};
use crate::{
base::{
commitment::Commitment,
database::{
try_add_subtract_column_types, Column, ColumnRef, ColumnType, CommitmentAccessor,
DataAccessor,
try_add_subtract_column_types, Column, ColumnRef, ColumnType, ColumnarValue,
CommitmentAccessor, DataAccessor,
},
map::IndexSet,
proof::ProofError,
Expand All @@ -14,7 +17,6 @@ use crate::{
};
use alloc::boxed::Box;
use bumpalo::Bump;
use proof_of_sql_parser::intermediate_ast::BinaryOperator;
use serde::{Deserialize, Serialize};

/// Provable numerical `+` / `-` expression
Expand Down Expand Up @@ -44,31 +46,25 @@ impl ProofExpr for AddSubtractExpr {
}

fn data_type(&self) -> ColumnType {
let operator = if self.is_subtract {
BinaryOperator::Subtract
} else {
BinaryOperator::Add
};
try_add_subtract_column_types(self.lhs.data_type(), self.rhs.data_type(), operator)
try_add_subtract_column_types(self.lhs.data_type(), self.rhs.data_type())
.expect("Failed to add/subtract column types")
}

fn result_evaluate<'a, S: Scalar>(
&self,
table_length: usize,
alloc: &'a Bump,
accessor: &'a dyn DataAccessor<S>,
) -> Column<'a, S> {
let lhs_column: Column<'a, S> = self.lhs.result_evaluate(table_length, alloc, accessor);
let rhs_column: Column<'a, S> = self.rhs.result_evaluate(table_length, alloc, accessor);
Column::Scalar(add_subtract_columns(
lhs_column,
rhs_column,
) -> ColumnarValue<'a, S> {
let lhs: ColumnarValue<'a, S> = self.lhs.result_evaluate(alloc, accessor);
let rhs: ColumnarValue<'a, S> = self.rhs.result_evaluate(alloc, accessor);
add_subtract_columnar_values(
lhs,
rhs,
self.lhs.data_type().scale().unwrap_or(0),
self.rhs.data_type().scale().unwrap_or(0),
alloc,
self.is_subtract,
))
)
}

#[tracing::instrument(
Expand Down
Loading
Loading