Skip to content

Commit

Permalink
feat: modify ProofExpr::result_evaluate to return ColumnarValue a…
Browse files Browse the repository at this point in the history
…nd remove `table_length` as arg
  • Loading branch information
iajoiner committed Nov 5, 2024
1 parent 0d42213 commit ef6b931
Show file tree
Hide file tree
Showing 36 changed files with 374 additions and 221 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/lint-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ jobs:
# files: lcov.info
# fail_ci_if_error: true

# Run cargo fmt --all -- --config imports_granularity=Crate,group_imports=One --check
# Run cargo fmt --all -- --check
format:
name: Format
runs-on: ubuntu-latest
Expand All @@ -194,7 +194,7 @@ jobs:
curl https://sh.rustup.rs -sSf | bash -s -- -y --profile minimal && source ~/.cargo/env
rustup component add rustfmt
- name: Run cargo fmt
run: cargo fmt --all -- --config imports_granularity=Crate,group_imports=One --check
run: cargo fmt --all -- --check

udeps:
name: Unused Dependencies
Expand Down
93 changes: 92 additions & 1 deletion crates/proof-of-sql/src/base/database/columnar_value.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::base::{
database::{Column, ColumnType, LiteralValue},
database::{Column, ColumnOperationError, ColumnOperationResult, ColumnType, LiteralValue},
scalar::Scalar,
};
use bumpalo::Bump;
use proof_of_sql_parser::intermediate_ast::{BinaryOperator, UnaryOperator};
use snafu::Snafu;

/// The result of evaluating an expression.
Expand Down Expand Up @@ -37,6 +38,14 @@ impl<'a, S: Scalar> ColumnarValue<'a, S> {
}
}

/// Get default length of the [`ColumnarValue`]
pub fn default_length(&self) -> usize {
match self {
Self::Column(column) => column.len(),
Self::Literal(_) => 1,
}
}

/// Converts the [`ColumnarValue`] to a [`Column`]
pub fn into_column(
&self,
Expand All @@ -59,6 +68,88 @@ impl<'a, S: Scalar> ColumnarValue<'a, S> {
}
}
}

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

/// Applies a binary operator to two [`ColumnarValue`]s.
pub(crate) fn apply_boolean_binary_operator(
&self,
rhs: &Self,
op: BinaryOperator,
alloc: &'a Bump,
) -> ColumnOperationResult<ColumnarValue<'a, S>> {
let op_fn = match op {
BinaryOperator::And => |lhs, rhs| lhs && rhs,
BinaryOperator::Or => |lhs, rhs| lhs || rhs,
_ => {
return Err(ColumnOperationError::BinaryOperationInvalidColumnType {
operator: op,
left_type: self.column_type(),
right_type: rhs.column_type(),
})
}
};
match (self, rhs) {
(
ColumnarValue::Literal(LiteralValue::Boolean(lhs)),
ColumnarValue::Literal(LiteralValue::Boolean(rhs)),
) => Ok(ColumnarValue::Literal(LiteralValue::Boolean(op_fn(
*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_fn(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_fn(*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_fn(lhs[i], rhs[i])),
)))
}
_ => Err(ColumnOperationError::BinaryOperationInvalidColumnType {
operator: op,
left_type: self.column_type(),
right_type: rhs.column_type(),
}),
}
}
}

#[cfg(test)]
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 @@ -55,7 +55,6 @@ pub trait ProverEvaluate<S: Scalar> {
/// Evaluate the query and modify `FirstRoundBuilder` to track the result of the query.
fn result_evaluate<'a>(
&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<S: Scalar> ProverEvaluate<S> for TrivialTestProofPlan {
fn result_evaluate<'a>(
&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<S: Scalar> ProverEvaluate<S> for SquareTestProofPlan {
fn result_evaluate<'a>(
&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<S: Scalar> ProverEvaluate<S> for DoubleSquareTestProofPlan {
fn result_evaluate<'a>(
&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<S: Scalar> ProverEvaluate<S> for ChallengeTestProofPlan {
fn result_evaluate<'a>(
&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<S: Scalar> ProverEvaluate<S> for EmptyTestQueryExpr {
fn result_evaluate<'a>(
&self,
_input_length: usize,
alloc: &'a Bump,
_accessor: &'a dyn DataAccessor<S>,
) -> Vec<Column<'a, S>> {
Expand Down
26 changes: 13 additions & 13 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 Down Expand Up @@ -54,22 +57,19 @@ impl<C: Commitment> ProofExpr<C> for AddSubtractExpr<C> {

fn result_evaluate<'a>(
&self,
table_length: usize,
alloc: &'a Bump,
accessor: &'a dyn DataAccessor<C::Scalar>,
) -> Column<'a, C::Scalar> {
let lhs_column: Column<'a, C::Scalar> =
self.lhs.result_evaluate(table_length, alloc, accessor);
let rhs_column: Column<'a, C::Scalar> =
self.rhs.result_evaluate(table_length, alloc, accessor);
Column::Scalar(add_subtract_columns(
lhs_column,
rhs_column,
) -> ColumnarValue<'a, C::Scalar> {
let lhs: ColumnarValue<'a, C::Scalar> = self.lhs.result_evaluate(alloc, accessor);
let rhs: ColumnarValue<'a, C::Scalar> = 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
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
base::{
commitment::InnerProductProof,
database::{owned_table_utility::*, Column, OwnedTableTestAccessor},
database::{owned_table_utility::*, Column, ColumnarValue, OwnedTableTestAccessor},
scalar::Curve25519Scalar,
},
sql::{
Expand Down Expand Up @@ -317,11 +317,11 @@ fn we_can_compute_the_correct_output_of_an_add_subtract_expr_using_result_evalua
subtract(column(t, "a", &accessor), const_bigint(1)),
);
let alloc = Bump::new();
let res = add_subtract_expr.result_evaluate(4, &alloc, &accessor);
let res = add_subtract_expr.result_evaluate(&alloc, &accessor);
let expected_res_scalar = [0, 2, 2, 4]
.iter()
.map(|v| Curve25519Scalar::from(*v))
.collect::<Vec<_>>();
let expected_res = Column::Scalar(&expected_res_scalar);
let expected_res = ColumnarValue::Column(Column::Scalar(&expected_res_scalar));
assert_eq!(res, expected_res);
}
9 changes: 5 additions & 4 deletions crates/proof-of-sql/src/sql/proof_exprs/aggregate_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use super::{DynProofExpr, ProofExpr};
use crate::{
base::{
commitment::Commitment,
database::{Column, ColumnRef, ColumnType, CommitmentAccessor, DataAccessor},
database::{
Column, ColumnRef, ColumnType, ColumnarValue, CommitmentAccessor, DataAccessor,
},
map::IndexSet,
proof::ProofError,
},
Expand Down Expand Up @@ -45,11 +47,10 @@ impl<C: Commitment> ProofExpr<C> for AggregateExpr<C> {
#[tracing::instrument(name = "AggregateExpr::result_evaluate", level = "debug", skip_all)]
fn result_evaluate<'a>(
&self,
table_length: usize,
alloc: &'a Bump,
accessor: &'a dyn DataAccessor<C::Scalar>,
) -> Column<'a, C::Scalar> {
self.expr.result_evaluate(table_length, alloc, accessor)
) -> ColumnarValue<'a, C::Scalar> {
self.expr.result_evaluate(alloc, accessor)
}

#[tracing::instrument(name = "AggregateExpr::prover_evaluate", level = "debug", skip_all)]
Expand Down
22 changes: 12 additions & 10 deletions crates/proof-of-sql/src/sql/proof_exprs/and_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use super::{DynProofExpr, ProofExpr};
use crate::{
base::{
commitment::Commitment,
database::{Column, ColumnRef, ColumnType, CommitmentAccessor, DataAccessor},
database::{
Column, ColumnRef, ColumnType, ColumnarValue, CommitmentAccessor, DataAccessor,
},
map::IndexSet,
proof::ProofError,
},
Expand All @@ -11,6 +13,7 @@ use crate::{
use alloc::{boxed::Box, vec};
use bumpalo::Bump;
use num_traits::One;
use proof_of_sql_parser::intermediate_ast::BinaryOperator;
use serde::{Deserialize, Serialize};

/// Provable logical AND expression
Expand Down Expand Up @@ -44,17 +47,16 @@ impl<C: Commitment> ProofExpr<C> for AndExpr<C> {
#[tracing::instrument(name = "AndExpr::result_evaluate", level = "debug", skip_all)]
fn result_evaluate<'a>(
&self,
table_length: usize,
alloc: &'a Bump,
accessor: &'a dyn DataAccessor<C::Scalar>,
) -> Column<'a, C::Scalar> {
let lhs_column: Column<'a, C::Scalar> =
self.lhs.result_evaluate(table_length, alloc, accessor);
let rhs_column: Column<'a, C::Scalar> =
self.rhs.result_evaluate(table_length, alloc, accessor);
let lhs = lhs_column.as_boolean().expect("lhs is not boolean");
let rhs = rhs_column.as_boolean().expect("rhs is not boolean");
Column::Boolean(alloc.alloc_slice_fill_with(table_length, |i| lhs[i] && rhs[i]))
) -> ColumnarValue<'a, C::Scalar> {
let lhs_columnar_value: ColumnarValue<'a, C::Scalar> =
self.lhs.result_evaluate(alloc, accessor);
let rhs_columnar_value: ColumnarValue<'a, C::Scalar> =
self.rhs.result_evaluate(alloc, accessor);
lhs_columnar_value
.apply_boolean_binary_operator(&rhs_columnar_value, BinaryOperator::And, alloc)
.expect("Failed to apply boolean binary operator")
}

#[tracing::instrument(name = "AndExpr::prover_evaluate", level = "debug", skip_all)]
Expand Down
6 changes: 3 additions & 3 deletions crates/proof-of-sql/src/sql/proof_exprs/and_expr_test.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
base::{
commitment::InnerProductProof,
database::{owned_table_utility::*, Column, OwnedTableTestAccessor},
database::{owned_table_utility::*, Column, ColumnarValue, OwnedTableTestAccessor},
},
sql::{
proof::{exercise_verification, VerifiableQueryResult},
Expand Down Expand Up @@ -160,7 +160,7 @@ fn we_can_compute_the_correct_output_of_an_and_expr_using_result_evaluate() {
equal(column(t, "d", &accessor), const_varchar("t")),
);
let alloc = Bump::new();
let res = and_expr.result_evaluate(4, &alloc, &accessor);
let expected_res = Column::Boolean(&[false, true, false, false]);
let res = and_expr.result_evaluate(&alloc, &accessor);
let expected_res = ColumnarValue::Column(Column::Boolean(&[false, true, false, false]));
assert_eq!(res, expected_res);
}
11 changes: 6 additions & 5 deletions crates/proof-of-sql/src/sql/proof_exprs/column_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ use super::ProofExpr;
use crate::{
base::{
commitment::Commitment,
database::{Column, ColumnField, ColumnRef, ColumnType, CommitmentAccessor, DataAccessor},
database::{
Column, ColumnField, ColumnRef, ColumnType, ColumnarValue, CommitmentAccessor,
DataAccessor,
},
map::IndexSet,
proof::ProofError,
},
Expand Down Expand Up @@ -62,13 +65,11 @@ impl<C: Commitment> ProofExpr<C> for ColumnExpr<C> {
/// add the result to the [`FirstRoundBuilder`](crate::sql::proof::FirstRoundBuilder)
fn result_evaluate<'a>(
&self,
table_length: usize,
_alloc: &'a Bump,
accessor: &'a dyn DataAccessor<C::Scalar>,
) -> Column<'a, C::Scalar> {
) -> ColumnarValue<'a, C::Scalar> {
let column = accessor.get_column(self.column_ref);
assert_eq!(column.len(), table_length);
column
ColumnarValue::Column(column)
}

/// Given the selected rows (as a slice of booleans), evaluate the column expression and
Expand Down
Loading

0 comments on commit ef6b931

Please sign in to comment.