Skip to content

Commit

Permalink
fix: refactor ColumnarValue code to not use parser crate constructs
Browse files Browse the repository at this point in the history
  • Loading branch information
iajoiner committed Nov 7, 2024
1 parent b3170e3 commit 9e83101
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 41 deletions.
59 changes: 25 additions & 34 deletions crates/proof-of-sql/src/base/database/columnar_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use crate::base::{
database::{Column, ColumnOperationError, ColumnOperationResult, ColumnType, LiteralValue},
scalar::Scalar,
};
use alloc::string::ToString;
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 @@ -63,63 +63,54 @@ impl<'a, S: Scalar> ColumnarValue<'a, S> {
}

/// Applies a unary operator to a [`ColumnarValue`].
pub(crate) fn apply_boolean_unary_operator(
pub(crate) fn apply_boolean_unary_operator<F>(
&self,
op: UnaryOperator,
op: F,
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]),
)))
) -> 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: op,
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(
pub(crate) fn apply_boolean_binary_operator<F>(
&self,
rhs: &Self,
op: BinaryOperator,
op: F,
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(),
})
}
};
) -> 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_fn(
*lhs, *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_fn(lhs[i], *rhs)),
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_fn(*lhs, rhs[i])),
alloc.alloc_slice_fill_with(rhs.len(), |i| op(lhs, &rhs[i])),
))),
(
ColumnarValue::Column(Column::Boolean(lhs)),
Expand All @@ -133,11 +124,11 @@ impl<'a, S: Scalar> ColumnarValue<'a, S> {
});
}
Ok(ColumnarValue::Column(Column::Boolean(
alloc.alloc_slice_fill_with(len, |i| op_fn(lhs[i], rhs[i])),
alloc.alloc_slice_fill_with(len, |i| op(&lhs[i], &rhs[i])),
)))
}
_ => Err(ColumnOperationError::BinaryOperationInvalidColumnType {
operator: op,
operator: "Some func Fn(&bool, &bool) -> bool".to_string(),
left_type: self.column_type(),
right_type: rhs.column_type(),
}),
Expand Down
3 changes: 1 addition & 2 deletions crates/proof-of-sql/src/sql/proof_exprs/and_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use crate::{
};
use alloc::{boxed::Box, vec};
use bumpalo::Bump;
use proof_of_sql_parser::intermediate_ast::BinaryOperator;
use serde::{Deserialize, Serialize};

/// Provable logical AND expression
Expand Down Expand Up @@ -53,7 +52,7 @@ impl ProofExpr for AndExpr {
let lhs_columnar_value: ColumnarValue<'a, S> = self.lhs.result_evaluate(alloc, accessor);
let rhs_columnar_value: ColumnarValue<'a, S> = self.rhs.result_evaluate(alloc, accessor);
lhs_columnar_value
.apply_boolean_binary_operator(&rhs_columnar_value, BinaryOperator::And, alloc)
.apply_boolean_binary_operator(&rhs_columnar_value, |l, r| *l && *r, alloc)
.expect("Failed to apply boolean binary operator")
}

Expand Down
3 changes: 1 addition & 2 deletions crates/proof-of-sql/src/sql/proof_exprs/not_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use crate::{
};
use alloc::boxed::Box;
use bumpalo::Bump;
use proof_of_sql_parser::intermediate_ast::UnaryOperator;
use serde::{Deserialize, Serialize};

/// Provable logical NOT expression
Expand Down Expand Up @@ -46,7 +45,7 @@ impl ProofExpr for NotExpr {
) -> ColumnarValue<'a, S> {
let expr_columnar_value: ColumnarValue<'a, S> = self.expr.result_evaluate(alloc, accessor);
expr_columnar_value
.apply_boolean_unary_operator(UnaryOperator::Not, alloc)
.apply_boolean_unary_operator(|l| !*l, alloc)
.expect("Failed to apply boolean unary operator")
}

Expand Down
3 changes: 1 addition & 2 deletions crates/proof-of-sql/src/sql/proof_exprs/or_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use crate::{
};
use alloc::{boxed::Box, vec};
use bumpalo::Bump;
use proof_of_sql_parser::intermediate_ast::BinaryOperator;
use serde::{Deserialize, Serialize};

/// Provable logical OR expression
Expand Down Expand Up @@ -51,7 +50,7 @@ impl ProofExpr for OrExpr {
let lhs_columnar_value: ColumnarValue<'a, S> = self.lhs.result_evaluate(alloc, accessor);
let rhs_columnar_value: ColumnarValue<'a, S> = self.rhs.result_evaluate(alloc, accessor);
lhs_columnar_value
.apply_boolean_binary_operator(&rhs_columnar_value, BinaryOperator::Or, alloc)
.apply_boolean_binary_operator(&rhs_columnar_value, |l, r| *l || *r, alloc)
.expect("Failed to apply boolean binary operator")
}

Expand Down
1 change: 0 additions & 1 deletion crates/proof-of-sql/src/sql/proof_plans/empty_exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ impl ProverEvaluate for EmptyExec {
#[tracing::instrument(name = "EmptyExec::result_evaluate", level = "debug", skip_all)]
fn result_evaluate<'a, S: Scalar>(
&self,
_input_length: usize,
_alloc: &'a Bump,
_accessor: &'a dyn DataAccessor<S>,
) -> Vec<Column<'a, S>> {
Expand Down

0 comments on commit 9e83101

Please sign in to comment.