Skip to content

Commit

Permalink
refactor: add AliasedProvableExprPlan (#35)
Browse files Browse the repository at this point in the history
# Rationale for this change
In new versions of `GroupByExpr` tuple `(ProvableExprPlan<C>,
Identifier)` is extremely common. We may as well add a struct for this.
<!--
Why are you proposing this change? If this is already explained clearly
in the linked Jira ticket then this section is not needed.
Explaining clearly why changes are proposed helps reviewers understand
your changes and offer better suggestions for fixes.
-->

# What changes are included in this PR?
- add `AliasedProvableExprPlan`
- one instance of minor typo fix
<!--
There is no need to duplicate the description in the ticket here but it
is sometimes worth providing a summary of the individual changes in this
PR.
-->

# Are these changes tested?
Existing tests should pass
<!--
We typically require tests for all PRs in order to:
1. Prevent the code from being accidentally broken by subsequent changes
2. Serve as another way to document the expected behavior of the code

If tests are not included in your PR, please explain why (for example,
are they covered by existing tests)?
-->
  • Loading branch information
iajoiner authored Jun 27, 2024
1 parent e69e4f7 commit ad91a39
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 118 deletions.
29 changes: 13 additions & 16 deletions crates/proof-of-sql/src/sql/ast/add_subtract_expr_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@ fn we_can_prove_a_typical_add_subtract_query() {
vec![
col_expr_plan(t, "a", &accessor),
col_expr_plan(t, "c", &accessor),
(
add(column(t, "b", &accessor), const_bigint(4)),
"res".parse().unwrap(),
),
aliased_plan(add(column(t, "b", &accessor), const_bigint(4)), "res"),
col_expr_plan(t, "d", &accessor),
],
tab(t),
Expand Down Expand Up @@ -72,15 +69,15 @@ fn we_can_prove_a_typical_add_subtract_query_with_decimals() {
let ast = dense_filter(
vec![
col_expr_plan(t, "a", &accessor),
(
aliased_plan(
add(
add(
add(column(t, "a", &accessor), column(t, "b", &accessor)),
column(t, "c", &accessor),
),
const_decimal75(2, 1, 4),
),
"c".parse().unwrap(),
"c",
),
col_expr_plan(t, "d", &accessor),
],
Expand Down Expand Up @@ -124,9 +121,9 @@ fn result_expr_can_overflow() {
let t = "sxt.t".parse().unwrap();
let accessor = OwnedTableTestAccessor::<InnerProductProof>::new_from_table(t, data, 0, ());
let ast: ProofPlan<RistrettoPoint> = dense_filter(
vec![(
vec![aliased_plan(
add(column(t, "a", &accessor), column(t, "b", &accessor)),
"c".parse().unwrap(),
"c",
)],
tab(t),
equal(column(t, "b", &accessor), const_bigint(1)),
Expand All @@ -149,9 +146,9 @@ fn overflow_in_nonselected_rows_doesnt_error_out() {
let t = "sxt.t".parse().unwrap();
let accessor = OwnedTableTestAccessor::<InnerProductProof>::new_from_table(t, data, 0, ());
let ast: ProofPlan<RistrettoPoint> = dense_filter(
vec![(
vec![aliased_plan(
add(column(t, "a", &accessor), column(t, "b", &accessor)),
"c".parse().unwrap(),
"c",
)],
tab(t),
equal(column(t, "b", &accessor), const_bigint(0)),
Expand Down Expand Up @@ -197,13 +194,13 @@ fn result_expr_can_overflow_more() {
let accessor = OwnedTableTestAccessor::<InnerProductProof>::new_from_table(t, data, 0, ());
let ast: ProofPlan<RistrettoPoint> = dense_filter(
vec![
(
aliased_plan(
add(column(t, "a", &accessor), column(t, "b", &accessor)),
"c".parse().unwrap(),
"c",
),
(
aliased_plan(
subtract(column(t, "a", &accessor), column(t, "b", &accessor)),
"d".parse().unwrap(),
"d",
),
],
tab(t),
Expand Down Expand Up @@ -251,12 +248,12 @@ fn test_random_tables_with_given_offset(offset: usize) {
let ast = dense_filter(
vec![
col_expr_plan(t, "d", &accessor),
(
aliased_plan(
subtract(
add(column(t, "a", &accessor), column(t, "c", &accessor)),
const_int128(4),
),
"f".parse().unwrap(),
"f",
),
],
tab(t),
Expand Down
11 changes: 11 additions & 0 deletions crates/proof-of-sql/src/sql/ast/aliased_provable_expr_plan.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use super::ProvableExprPlan;
use crate::base::commitment::Commitment;
use proof_of_sql_parser::Identifier;
use serde::{Deserialize, Serialize};

/// A `ProvableExprPlan` with an alias.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct AliasedProvableExprPlan<C: Commitment> {
pub expr: ProvableExprPlan<C>,
pub alias: Identifier,
}
31 changes: 14 additions & 17 deletions crates/proof-of-sql/src/sql/ast/dense_filter_expr.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use super::{
dense_filter_util::{fold_columns, fold_vals},
filter_columns,
provable_expr_plan::ProvableExprPlan,
ProvableExpr, TableExpr,
filter_columns, AliasedProvableExprPlan, ProvableExpr, ProvableExprPlan, TableExpr,
};
use crate::{
base::{
Expand All @@ -23,7 +21,6 @@ use crate::{
use bumpalo::Bump;
use core::iter::repeat_with;
use num_traits::{One, Zero};
use proof_of_sql_parser::Identifier;
use serde::{Deserialize, Serialize};
use std::{collections::HashSet, marker::PhantomData};

Expand All @@ -35,7 +32,7 @@ use std::{collections::HashSet, marker::PhantomData};
/// This differs from the [`FilterExpr`] in that the result is not a sparse table.
#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct OstensibleDenseFilterExpr<C: Commitment, H: ProverHonestyMarker> {
pub(super) aliased_results: Vec<(ProvableExprPlan<C>, Identifier)>,
pub(super) aliased_results: Vec<AliasedProvableExprPlan<C>>,
pub(super) table: TableExpr,
pub(super) where_clause: ProvableExprPlan<C>,
phantom: PhantomData<H>,
Expand All @@ -44,7 +41,7 @@ pub struct OstensibleDenseFilterExpr<C: Commitment, H: ProverHonestyMarker> {
impl<C: Commitment, H: ProverHonestyMarker> OstensibleDenseFilterExpr<C, H> {
/// Creates a new dense_filter expression.
pub fn new(
aliased_results: Vec<(ProvableExprPlan<C>, Identifier)>,
aliased_results: Vec<AliasedProvableExprPlan<C>>,
table: TableExpr,
where_clause: ProvableExprPlan<C>,
) -> Self {
Expand All @@ -68,7 +65,7 @@ where
) -> Result<(), ProofError> {
self.where_clause.count(builder)?;
for aliased_expr in self.aliased_results.iter() {
aliased_expr.0.count(builder)?;
aliased_expr.expr.count(builder)?;
builder.count_result_columns(1);
}
builder.count_intermediate_mles(2);
Expand Down Expand Up @@ -99,7 +96,7 @@ where
let columns_evals = Vec::from_iter(
self.aliased_results
.iter()
.map(|(expr, _)| expr.verifier_evaluate(builder, accessor))
.map(|aliased_expr| aliased_expr.expr.verifier_evaluate(builder, accessor))
.collect::<Result<Vec<_>, _>>()?,
);
// 3. indexes
Expand Down Expand Up @@ -128,15 +125,15 @@ where
fn get_column_result_fields(&self) -> Vec<ColumnField> {
self.aliased_results
.iter()
.map(|(expr, alias)| ColumnField::new(*alias, expr.data_type()))
.map(|aliased_expr| ColumnField::new(aliased_expr.alias, aliased_expr.expr.data_type()))
.collect()
}

fn get_column_references(&self) -> HashSet<ColumnRef> {
let mut columns = HashSet::new();

for (col, _) in self.aliased_results.iter() {
col.get_column_references(&mut columns);
for aliased_expr in self.aliased_results.iter() {
aliased_expr.expr.get_column_references(&mut columns);
}

self.where_clause.get_column_references(&mut columns);
Expand Down Expand Up @@ -165,11 +162,11 @@ impl<C: Commitment> ProverEvaluate<C::Scalar> for DenseFilterExpr<C> {
.expect("selection is not boolean");

// 2. columns
let columns = Vec::from_iter(
self.aliased_results
.iter()
.map(|(expr, _)| expr.result_evaluate(builder.table_length(), alloc, accessor)),
);
let columns = Vec::from_iter(self.aliased_results.iter().map(|aliased_expr| {
aliased_expr
.expr
.result_evaluate(builder.table_length(), alloc, accessor)
}));
// Compute filtered_columns and indexes
let (filtered_columns, result_len) = filter_columns(alloc, &columns, selection);
// 3. set indexes
Expand Down Expand Up @@ -200,7 +197,7 @@ impl<C: Commitment> ProverEvaluate<C::Scalar> for DenseFilterExpr<C> {
let columns = Vec::from_iter(
self.aliased_results
.iter()
.map(|(expr, _)| expr.prover_evaluate(builder, alloc, accessor)),
.map(|aliased_expr| aliased_expr.expr.prover_evaluate(builder, alloc, accessor)),
);
// Compute filtered_columns and indexes
let (filtered_columns, result_len) = filter_columns(alloc, &columns, selection);
Expand Down
27 changes: 12 additions & 15 deletions crates/proof-of-sql/src/sql/ast/dense_filter_expr_test.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use crate::{
base::{database::owned_table_utility::*, math::decimal::Precision},
sql::ast::{
test_utility::{and, not, or},
ProvableExprPlan,
},
sql::ast::{test_utility::*, ProvableExprPlan},
};
use crate::{
base::{
Expand Down Expand Up @@ -46,21 +43,21 @@ fn we_can_correctly_fetch_the_query_result_schema() {
let b = Identifier::try_new("b").unwrap();
let provable_ast = DenseFilterExpr::<RistrettoPoint>::new(
vec![
(
aliased_plan(
ProvableExprPlan::Column(ColumnExpr::new(ColumnRef::new(
table_ref,
a,
ColumnType::BigInt,
))),
a,
"a",
),
(
aliased_plan(
ProvableExprPlan::Column(ColumnExpr::new(ColumnRef::new(
table_ref,
b,
ColumnType::BigInt,
))),
b,
"b",
),
],
TableExpr { table_ref },
Expand Down Expand Up @@ -98,21 +95,21 @@ fn we_can_correctly_fetch_all_the_referenced_columns() {
let f = Identifier::try_new("f").unwrap();
let provable_ast = DenseFilterExpr::new(
vec![
(
aliased_plan(
ProvableExprPlan::Column(ColumnExpr::new(ColumnRef::new(
table_ref,
a,
ColumnType::BigInt,
))),
a,
"a",
),
(
aliased_plan(
ProvableExprPlan::Column(ColumnExpr::new(ColumnRef::new(
table_ref,
f,
ColumnType::BigInt,
))),
f,
"f",
),
],
TableExpr { table_ref },
Expand Down Expand Up @@ -438,10 +435,10 @@ fn we_can_prove_a_dense_filter() {
col_expr_plan(t, "c", &accessor),
col_expr_plan(t, "d", &accessor),
col_expr_plan(t, "e", &accessor),
(const_int128(105), "const".parse().unwrap()),
(
aliased_plan(const_int128(105), "const"),
aliased_plan(
equal(column(t, "b", &accessor), column(t, "c", &accessor)),
"bool".parse().unwrap(),
"bool",
),
],
tab(t),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ impl ProverEvaluate<Curve25519Scalar> for DishonestDenseFilterExpr<RistrettoPoin
.as_boolean()
.expect("selection is not boolean");
// 2. columns
let columns = Vec::from_iter(
self.aliased_results
.iter()
.map(|(expr, _)| expr.result_evaluate(builder.table_length(), alloc, accessor)),
);
let columns = Vec::from_iter(self.aliased_results.iter().map(|aliased_expr| {
aliased_expr
.expr
.result_evaluate(builder.table_length(), alloc, accessor)
}));
// Compute filtered_columns and indexes
let (filtered_columns, result_len) = filter_columns(alloc, &columns, selection);
let filtered_columns = tamper_column(alloc, filtered_columns);
Expand Down Expand Up @@ -87,7 +87,7 @@ impl ProverEvaluate<Curve25519Scalar> for DishonestDenseFilterExpr<RistrettoPoin
let columns = Vec::from_iter(
self.aliased_results
.iter()
.map(|(expr, _)| expr.prover_evaluate(builder, alloc, accessor)),
.map(|aliased_expr| aliased_expr.expr.prover_evaluate(builder, alloc, accessor)),
);
// Compute filtered_columns and indexes
let (filtered_columns, result_len) = filter_columns(alloc, &columns, selection);
Expand Down
3 changes: 3 additions & 0 deletions crates/proof-of-sql/src/sql/ast/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
//! This module proves provable ASTs.
mod aliased_provable_expr_plan;
pub(crate) use aliased_provable_expr_plan::AliasedProvableExprPlan;

mod filter_result_expr;
pub(crate) use filter_result_expr::FilterResultExpr;

Expand Down
Loading

0 comments on commit ad91a39

Please sign in to comment.