From 49478947c71facb32aaab211b6057e047ac60618 Mon Sep 17 00:00:00 2001 From: Ian Joiner <14581281+iajoiner@users.noreply.github.com> Date: Wed, 13 Nov 2024 16:42:00 -0500 Subject: [PATCH] refactor!: replace `DataAccessor` in `ProofPlan` with `&IndexMap>` --- .../src/base/database/accessor.rs | 2 +- .../proof-of-sql/src/sql/proof/proof_plan.rs | 6 +- .../proof-of-sql/src/sql/proof/query_proof.rs | 30 ++++-- .../src/sql/proof/query_proof_test.rs | 54 ++++++----- .../sql/proof/verifiable_query_result_test.rs | 8 +- .../src/sql/proof_plans/dyn_proof_plan.rs | 2 +- .../src/sql/proof_plans/empty_exec.rs | 8 +- .../src/sql/proof_plans/filter_exec.rs | 31 +++---- .../src/sql/proof_plans/filter_exec_test.rs | 92 +++++++++++-------- .../filter_exec_test_dishonest_prover.rs | 36 ++++---- .../src/sql/proof_plans/group_by_exec.rs | 33 +++---- .../src/sql/proof_plans/projection_exec.rs | 28 +++--- .../sql/proof_plans/projection_exec_test.rs | 70 ++++++++------ .../src/sql/proof_plans/table_exec.rs | 16 +++- 14 files changed, 227 insertions(+), 189 deletions(-) diff --git a/crates/proof-of-sql/src/base/database/accessor.rs b/crates/proof-of-sql/src/base/database/accessor.rs index d9d7dcb50..79f2cd3c9 100644 --- a/crates/proof-of-sql/src/base/database/accessor.rs +++ b/crates/proof-of-sql/src/base/database/accessor.rs @@ -90,7 +90,7 @@ pub trait DataAccessor: MetadataAccessor { /// Creates a new [`Table`] from a [`TableRef`] and [`ColumnRef`]s. /// /// Columns are retrieved from the [`DataAccessor`] using the provided [`TableRef`] and [`ColumnRef`]s. - /// The only reason why [`table_ref` is needed is because `column_refs` can be empty. + /// The only reason why [`table_ref`] is needed is because [`column_refs`] can be empty. /// # Panics /// Column length mismatches can occur in theory. In practice, this should not happen. fn get_table(&self, table_ref: TableRef, column_refs: &IndexSet) -> Table { diff --git a/crates/proof-of-sql/src/sql/proof/proof_plan.rs b/crates/proof-of-sql/src/sql/proof/proof_plan.rs index edfe1f371..0a21b6556 100644 --- a/crates/proof-of-sql/src/sql/proof/proof_plan.rs +++ b/crates/proof-of-sql/src/sql/proof/proof_plan.rs @@ -1,6 +1,6 @@ use super::{CountBuilder, FinalRoundBuilder, FirstRoundBuilder, VerificationBuilder}; use crate::base::{ - database::{ColumnField, ColumnRef, DataAccessor, OwnedTable, Table, TableRef}, + database::{ColumnField, ColumnRef, OwnedTable, Table, TableRef}, map::{IndexMap, IndexSet}, proof::ProofError, scalar::Scalar, @@ -39,7 +39,7 @@ pub trait ProverEvaluate { fn result_evaluate<'a, S: Scalar>( &self, alloc: &'a Bump, - accessor: &'a dyn DataAccessor, + table_map: &IndexMap>, ) -> Table<'a, S>; /// Evaluate the query and modify `FirstRoundBuilder` to form the query's proof. @@ -55,7 +55,7 @@ pub trait ProverEvaluate { &self, builder: &mut FinalRoundBuilder<'a, S>, alloc: &'a Bump, - accessor: &'a dyn DataAccessor, + table_map: &IndexMap>, ) -> Table<'a, S>; } diff --git a/crates/proof-of-sql/src/sql/proof/query_proof.rs b/crates/proof-of-sql/src/sql/proof/query_proof.rs index 5df8c68b9..d6f4346dc 100644 --- a/crates/proof-of-sql/src/sql/proof/query_proof.rs +++ b/crates/proof-of-sql/src/sql/proof/query_proof.rs @@ -6,8 +6,10 @@ use crate::{ base::{ bit::BitDistribution, commitment::CommitmentEvaluationProof, - database::{CommitmentAccessor, DataAccessor, MetadataAccessor, TableRef}, - map::IndexMap, + database::{ + ColumnRef, CommitmentAccessor, DataAccessor, MetadataAccessor, Table, TableRef, + }, + map::{IndexMap, IndexSet}, math::log2_up, polynomial::{compute_evaluation_vector, CompositePolynomialInfo}, proof::{Keccak256Transcript, ProofError, Transcript}, @@ -75,8 +77,22 @@ impl QueryProof { let alloc = Bump::new(); + let total_col_refs = expr.get_column_references(); + let table_map: IndexMap> = expr + .get_table_references() + .into_iter() + .map(|table_ref| { + let col_refs: IndexSet = total_col_refs + .iter() + .filter(|col_ref| col_ref.table_ref() == table_ref) + .cloned() + .collect(); + (table_ref, accessor.get_table(table_ref, &col_refs)) + }) + .collect(); + // Evaluate query result - let provable_result = expr.result_evaluate(&alloc, accessor).into(); + let provable_result = expr.result_evaluate(&alloc, &table_map).into(); // Prover First Round let mut first_round_builder = FirstRoundBuilder::new(); @@ -99,11 +115,11 @@ impl QueryProof { let mut builder = FinalRoundBuilder::new(range_length, num_sumcheck_variables, post_result_challenges); - expr.get_column_references().into_iter().for_each(|col| { - builder.produce_anchored_mle(accessor.get_column(col)); - }); + for col_ref in total_col_refs { + builder.produce_anchored_mle(accessor.get_column(col_ref)); + } - expr.final_round_evaluate(&mut builder, &alloc, accessor); + expr.final_round_evaluate(&mut builder, &alloc, &table_map); let num_sumcheck_variables = builder.num_sumcheck_variables(); diff --git a/crates/proof-of-sql/src/sql/proof/query_proof_test.rs b/crates/proof-of-sql/src/sql/proof/query_proof_test.rs index 15ffba158..66e0e7ad6 100644 --- a/crates/proof-of-sql/src/sql/proof/query_proof_test.rs +++ b/crates/proof-of-sql/src/sql/proof/query_proof_test.rs @@ -7,8 +7,8 @@ use crate::{ database::{ owned_table_utility::{bigint, owned_table}, table_utility::*, - ColumnField, ColumnRef, ColumnType, DataAccessor, OwnedTable, OwnedTableTestAccessor, - Table, TableRef, + ColumnField, ColumnRef, ColumnType, OwnedTable, OwnedTableTestAccessor, Table, + TableRef, }, map::{indexset, IndexMap, IndexSet}, proof::ProofError, @@ -17,6 +17,7 @@ use crate::{ sql::proof::{FirstRoundBuilder, QueryData, SumcheckSubpolynomialType}, }; use bumpalo::Bump; +use proof_of_sql_parser::Identifier; use serde::Serialize; /// Type to allow us to prove and verify an artificial polynomial where we prove @@ -44,7 +45,7 @@ impl ProverEvaluate for TrivialTestProofPlan { fn result_evaluate<'a, S: Scalar>( &self, alloc: &'a Bump, - _accessor: &'a dyn DataAccessor, + _table_map: &IndexMap>, ) -> Table<'a, S> { let col = vec![self.column_fill_value; self.length]; table([borrowed_bigint("a1", col, alloc)]) @@ -56,7 +57,7 @@ impl ProverEvaluate for TrivialTestProofPlan { &self, builder: &mut FinalRoundBuilder<'a, S>, alloc: &'a Bump, - _accessor: &'a dyn DataAccessor, + _table_map: &IndexMap>, ) -> Table<'a, S> { let col = alloc.alloc_slice_fill_copy(self.length, self.column_fill_value); builder.produce_intermediate_mle(col as &[_]); @@ -217,7 +218,7 @@ impl ProverEvaluate for SquareTestProofPlan { fn result_evaluate<'a, S: Scalar>( &self, alloc: &'a Bump, - _accessor: &'a dyn DataAccessor, + _table_map: &IndexMap>, ) -> Table<'a, S> { table([borrowed_bigint("a1", self.res, alloc)]) } @@ -228,13 +229,14 @@ impl ProverEvaluate for SquareTestProofPlan { &self, builder: &mut FinalRoundBuilder<'a, S>, alloc: &'a Bump, - accessor: &'a dyn DataAccessor, + table_map: &IndexMap>, ) -> Table<'a, S> { - let x = accessor.get_column(ColumnRef::new( - "sxt.test".parse().unwrap(), - "x".parse().unwrap(), - ColumnType::BigInt, - )); + let x = *table_map + .get(&TableRef::new("sxt.test".parse().unwrap())) + .unwrap() + .inner_table() + .get(&"x".parse::().unwrap()) + .unwrap(); let res: &[_] = alloc.alloc_slice_copy(&self.res); builder.produce_intermediate_mle(res); builder.produce_sumcheck_subpolynomial( @@ -393,7 +395,7 @@ impl ProverEvaluate for DoubleSquareTestProofPlan { fn result_evaluate<'a, S: Scalar>( &self, alloc: &'a Bump, - _accessor: &'a dyn DataAccessor, + _table_map: &IndexMap>, ) -> Table<'a, S> { table([borrowed_bigint("a1", self.res, alloc)]) } @@ -404,13 +406,14 @@ impl ProverEvaluate for DoubleSquareTestProofPlan { &self, builder: &mut FinalRoundBuilder<'a, S>, alloc: &'a Bump, - accessor: &'a dyn DataAccessor, + table_map: &IndexMap>, ) -> Table<'a, S> { - let x = accessor.get_column(ColumnRef::new( - "sxt.test".parse().unwrap(), - "x".parse().unwrap(), - ColumnType::BigInt, - )); + let x = *table_map + .get(&TableRef::new("sxt.test".parse().unwrap())) + .unwrap() + .inner_table() + .get(&"x".parse::().unwrap()) + .unwrap(); let res: &[_] = alloc.alloc_slice_copy(&self.res); let z: &[_] = alloc.alloc_slice_copy(&self.z); builder.produce_intermediate_mle(z); @@ -599,7 +602,7 @@ impl ProverEvaluate for ChallengeTestProofPlan { fn result_evaluate<'a, S: Scalar>( &self, alloc: &'a Bump, - _accessor: &'a dyn DataAccessor, + _table_map: &IndexMap>, ) -> Table<'a, S> { table([borrowed_bigint("a1", [9, 25], alloc)]) } @@ -612,13 +615,14 @@ impl ProverEvaluate for ChallengeTestProofPlan { &self, builder: &mut FinalRoundBuilder<'a, S>, alloc: &'a Bump, - accessor: &'a dyn DataAccessor, + table_map: &IndexMap>, ) -> Table<'a, S> { - let x = accessor.get_column(ColumnRef::new( - "sxt.test".parse().unwrap(), - "x".parse().unwrap(), - ColumnType::BigInt, - )); + let x = *table_map + .get(&TableRef::new("sxt.test".parse().unwrap())) + .unwrap() + .inner_table() + .get(&"x".parse::().unwrap()) + .unwrap(); let res: &[_] = alloc.alloc_slice_copy(&[9, 25]); let alpha = builder.consume_post_result_challenge(); let _beta = builder.consume_post_result_challenge(); diff --git a/crates/proof-of-sql/src/sql/proof/verifiable_query_result_test.rs b/crates/proof-of-sql/src/sql/proof/verifiable_query_result_test.rs index 194da80e9..0900ae1ff 100644 --- a/crates/proof-of-sql/src/sql/proof/verifiable_query_result_test.rs +++ b/crates/proof-of-sql/src/sql/proof/verifiable_query_result_test.rs @@ -8,8 +8,8 @@ use crate::{ database::{ owned_table_utility::{bigint, owned_table}, table_utility::*, - ColumnField, ColumnRef, ColumnType, DataAccessor, OwnedTable, OwnedTableTestAccessor, - Table, TableRef, + ColumnField, ColumnRef, ColumnType, OwnedTable, OwnedTableTestAccessor, Table, + TableRef, }, map::{indexset, IndexMap, IndexSet}, proof::ProofError, @@ -29,7 +29,7 @@ impl ProverEvaluate for EmptyTestQueryExpr { fn result_evaluate<'a, S: Scalar>( &self, alloc: &'a Bump, - _accessor: &'a dyn DataAccessor, + _table_map: &IndexMap>, ) -> Table<'a, S> { let zeros = vec![0_i64; self.length]; table_with_row_count( @@ -42,7 +42,7 @@ impl ProverEvaluate for EmptyTestQueryExpr { &self, builder: &mut FinalRoundBuilder<'a, S>, alloc: &'a Bump, - _accessor: &'a dyn DataAccessor, + _table_map: &IndexMap>, ) -> Table<'a, S> { let zeros = vec![0_i64; self.length]; let res: &[_] = alloc.alloc_slice_copy(&zeros); diff --git a/crates/proof-of-sql/src/sql/proof_plans/dyn_proof_plan.rs b/crates/proof-of-sql/src/sql/proof_plans/dyn_proof_plan.rs index 11acca0ea..e402708aa 100644 --- a/crates/proof-of-sql/src/sql/proof_plans/dyn_proof_plan.rs +++ b/crates/proof-of-sql/src/sql/proof_plans/dyn_proof_plan.rs @@ -1,7 +1,7 @@ use super::{EmptyExec, FilterExec, GroupByExec, ProjectionExec, TableExec}; use crate::{ base::{ - database::{ColumnField, ColumnRef, DataAccessor, OwnedTable, Table, TableRef}, + database::{ColumnField, ColumnRef, OwnedTable, Table, TableRef}, map::{IndexMap, IndexSet}, proof::ProofError, scalar::Scalar, diff --git a/crates/proof-of-sql/src/sql/proof_plans/empty_exec.rs b/crates/proof-of-sql/src/sql/proof_plans/empty_exec.rs index a06420c7b..9f5dd8ed9 100644 --- a/crates/proof-of-sql/src/sql/proof_plans/empty_exec.rs +++ b/crates/proof-of-sql/src/sql/proof_plans/empty_exec.rs @@ -1,8 +1,6 @@ use crate::{ base::{ - database::{ - ColumnField, ColumnRef, DataAccessor, OwnedTable, Table, TableOptions, TableRef, - }, + database::{ColumnField, ColumnRef, OwnedTable, Table, TableOptions, TableRef}, map::{IndexMap, IndexSet}, proof::ProofError, scalar::Scalar, @@ -68,7 +66,7 @@ impl ProverEvaluate for EmptyExec { fn result_evaluate<'a, S: Scalar>( &self, _alloc: &'a Bump, - _accessor: &'a dyn DataAccessor, + _table_map: &IndexMap>, ) -> Table<'a, S> { // Create an empty table with one row Table::<'a, S>::try_new_with_options(IndexMap::default(), TableOptions::new(Some(1))) @@ -83,7 +81,7 @@ impl ProverEvaluate for EmptyExec { &self, _builder: &mut FinalRoundBuilder<'a, S>, _alloc: &'a Bump, - _accessor: &'a dyn DataAccessor, + _table_map: &IndexMap>, ) -> Table<'a, S> { // Create an empty table with one row Table::<'a, S>::try_new_with_options(IndexMap::default(), TableOptions::new(Some(1))) diff --git a/crates/proof-of-sql/src/sql/proof_plans/filter_exec.rs b/crates/proof-of-sql/src/sql/proof_plans/filter_exec.rs index c76af3eaa..910897732 100644 --- a/crates/proof-of-sql/src/sql/proof_plans/filter_exec.rs +++ b/crates/proof-of-sql/src/sql/proof_plans/filter_exec.rs @@ -2,8 +2,8 @@ use super::{fold_columns, fold_vals}; use crate::{ base::{ database::{ - filter_util::filter_columns, Column, ColumnField, ColumnRef, DataAccessor, OwnedTable, - Table, TableOptions, TableRef, + filter_util::filter_columns, Column, ColumnField, ColumnRef, OwnedTable, Table, + TableOptions, TableRef, }, map::{IndexMap, IndexSet}, proof::ProofError, @@ -140,12 +140,13 @@ impl ProverEvaluate for FilterExec { fn result_evaluate<'a, S: Scalar>( &self, alloc: &'a Bump, - accessor: &'a dyn DataAccessor, + table_map: &IndexMap>, ) -> Table<'a, S> { - let column_refs = self.get_column_references(); - let used_table = accessor.get_table(self.table.table_ref, &column_refs); + let table = table_map + .get(&self.table.table_ref) + .expect("Table not found"); // 1. selection - let selection_column: Column<'a, S> = self.where_clause.result_evaluate(alloc, &used_table); + let selection_column: Column<'a, S> = self.where_clause.result_evaluate(alloc, table); let selection = selection_column .as_boolean() .expect("selection is not boolean"); @@ -155,7 +156,7 @@ impl ProverEvaluate for FilterExec { let columns: Vec<_> = self .aliased_results .iter() - .map(|aliased_expr| aliased_expr.expr.result_evaluate(alloc, &used_table)) + .map(|aliased_expr| aliased_expr.expr.result_evaluate(alloc, table)) .collect(); // Compute filtered_columns and indexes @@ -180,14 +181,14 @@ impl ProverEvaluate for FilterExec { &self, builder: &mut FinalRoundBuilder<'a, S>, alloc: &'a Bump, - accessor: &'a dyn DataAccessor, + table_map: &IndexMap>, ) -> Table<'a, S> { - let column_refs = self.get_column_references(); - let used_table = accessor.get_table(self.table.table_ref, &column_refs); + let table = table_map + .get(&self.table.table_ref) + .expect("Table not found"); // 1. selection let selection_column: Column<'a, S> = - self.where_clause - .prover_evaluate(builder, alloc, &used_table); + self.where_clause.prover_evaluate(builder, alloc, table); let selection = selection_column .as_boolean() .expect("selection is not boolean"); @@ -197,11 +198,7 @@ impl ProverEvaluate for FilterExec { let columns: Vec<_> = self .aliased_results .iter() - .map(|aliased_expr| { - aliased_expr - .expr - .prover_evaluate(builder, alloc, &used_table) - }) + .map(|aliased_expr| aliased_expr.expr.prover_evaluate(builder, alloc, table)) .collect(); // Compute filtered_columns let (filtered_columns, result_len) = filter_columns(alloc, &columns, selection); diff --git a/crates/proof-of-sql/src/sql/proof_plans/filter_exec_test.rs b/crates/proof-of-sql/src/sql/proof_plans/filter_exec_test.rs index 3c240fb67..ed9dc8db2 100644 --- a/crates/proof-of-sql/src/sql/proof_plans/filter_exec_test.rs +++ b/crates/proof-of-sql/src/sql/proof_plans/filter_exec_test.rs @@ -2,10 +2,11 @@ use super::{test_utility::*, FilterExec}; use crate::{ base::{ database::{ - owned_table_utility::*, ColumnField, ColumnRef, ColumnType, LiteralValue, OwnedTable, - OwnedTableTestAccessor, TableRef, TestAccessor, + owned_table_utility::*, table_utility::*, ColumnField, ColumnRef, ColumnType, + LiteralValue, OwnedTable, OwnedTableTestAccessor, TableRef, TableTestAccessor, + TestAccessor, }, - map::{IndexMap, IndexSet}, + map::{indexmap, IndexMap, IndexSet}, math::decimal::Precision, scalar::Curve25519Scalar, }, @@ -177,15 +178,19 @@ fn we_can_prove_and_get_the_correct_result_from_a_basic_filter() { #[test] fn we_can_get_an_empty_result_from_a_basic_filter_on_an_empty_table_using_result_evaluate() { - let data = owned_table([ - bigint("a", [0; 0]), - bigint("b", [0; 0]), - int128("c", [0; 0]), - varchar("d", [""; 0]), - scalar("e", [0; 0]), + let alloc = Bump::new(); + let data = table([ + borrowed_bigint("a", [0; 0], &alloc), + borrowed_bigint("b", [0; 0], &alloc), + borrowed_int128("c", [0; 0], &alloc), + borrowed_varchar("d", [""; 0], &alloc), + borrowed_scalar("e", [0; 0], &alloc), ]); let t = "sxt.t".parse().unwrap(); - let mut accessor = OwnedTableTestAccessor::::new_empty_with_setup(()); + let table_map = indexmap! { + t => data.clone() + }; + let mut accessor = TableTestAccessor::::new_empty_with_setup(()); accessor.add_table(t, data, 0); let where_clause: DynProofExpr = equal(column(t, "a", &accessor), const_int128(999)); let expr = filter( @@ -193,7 +198,7 @@ fn we_can_get_an_empty_result_from_a_basic_filter_on_an_empty_table_using_result tab(t), where_clause, ); - let alloc = Bump::new(); + let mut builder = FirstRoundBuilder::new(); expr.first_round_evaluate(&mut builder); let fields = &[ @@ -206,7 +211,7 @@ fn we_can_get_an_empty_result_from_a_basic_filter_on_an_empty_table_using_result ), ]; let res: OwnedTable = - ProvableQueryResult::from(expr.result_evaluate(&alloc, &accessor)) + ProvableQueryResult::from(expr.result_evaluate(&alloc, &table_map)) .to_owned_table(fields) .unwrap(); let expected: OwnedTable = owned_table([ @@ -221,15 +226,19 @@ fn we_can_get_an_empty_result_from_a_basic_filter_on_an_empty_table_using_result #[test] fn we_can_get_an_empty_result_from_a_basic_filter_using_result_evaluate() { - let data = owned_table([ - bigint("a", [1, 4, 5, 2, 5]), - bigint("b", [1, 2, 3, 4, 5]), - int128("c", [1, 2, 3, 4, 5]), - varchar("d", ["1", "2", "3", "4", "5"]), - scalar("e", [1, 2, 3, 4, 5]), + let alloc = Bump::new(); + let data = table([ + borrowed_bigint("a", [1, 4, 5, 2, 5], &alloc), + borrowed_bigint("b", [1, 2, 3, 4, 5], &alloc), + borrowed_int128("c", [1, 2, 3, 4, 5], &alloc), + borrowed_varchar("d", ["1", "2", "3", "4", "5"], &alloc), + borrowed_scalar("e", [1, 2, 3, 4, 5], &alloc), ]); let t = "sxt.t".parse().unwrap(); - let mut accessor = OwnedTableTestAccessor::::new_empty_with_setup(()); + let table_map = indexmap! { + t => data.clone() + }; + let mut accessor = TableTestAccessor::::new_empty_with_setup(()); accessor.add_table(t, data, 0); let where_clause: DynProofExpr = equal(column(t, "a", &accessor), const_int128(999)); let expr = filter( @@ -237,7 +246,6 @@ fn we_can_get_an_empty_result_from_a_basic_filter_using_result_evaluate() { tab(t), where_clause, ); - let alloc = Bump::new(); let mut builder = FirstRoundBuilder::new(); expr.first_round_evaluate(&mut builder); let fields = &[ @@ -250,7 +258,7 @@ fn we_can_get_an_empty_result_from_a_basic_filter_using_result_evaluate() { ), ]; let res: OwnedTable = - ProvableQueryResult::from(expr.result_evaluate(&alloc, &accessor)) + ProvableQueryResult::from(expr.result_evaluate(&alloc, &table_map)) .to_owned_table(fields) .unwrap(); let expected: OwnedTable = owned_table([ @@ -265,24 +273,27 @@ fn we_can_get_an_empty_result_from_a_basic_filter_using_result_evaluate() { #[test] fn we_can_get_no_columns_from_a_basic_filter_with_no_selected_columns_using_result_evaluate() { - let data = owned_table([ - bigint("a", [1, 4, 5, 2, 5]), - bigint("b", [1, 2, 3, 4, 5]), - int128("c", [1, 2, 3, 4, 5]), - varchar("d", ["1", "2", "3", "4", "5"]), - scalar("e", [1, 2, 3, 4, 5]), + let alloc = Bump::new(); + let data = table([ + borrowed_bigint("a", [1, 4, 5, 2, 5], &alloc), + borrowed_bigint("b", [1, 2, 3, 4, 5], &alloc), + borrowed_int128("c", [1, 2, 3, 4, 5], &alloc), + borrowed_varchar("d", ["1", "2", "3", "4", "5"], &alloc), + borrowed_scalar("e", [1, 2, 3, 4, 5], &alloc), ]); let t = "sxt.t".parse().unwrap(); - let mut accessor = OwnedTableTestAccessor::::new_empty_with_setup(()); + let table_map = indexmap! { + t => data.clone() + }; + let mut accessor = TableTestAccessor::::new_empty_with_setup(()); accessor.add_table(t, data, 0); let where_clause: DynProofExpr = equal(column(t, "a", &accessor), const_int128(5)); let expr = filter(cols_expr_plan(t, &[], &accessor), tab(t), where_clause); - let alloc = Bump::new(); let mut builder = FirstRoundBuilder::new(); expr.first_round_evaluate(&mut builder); let fields = &[]; let res: OwnedTable = - ProvableQueryResult::from(expr.result_evaluate(&alloc, &accessor)) + ProvableQueryResult::from(expr.result_evaluate(&alloc, &table_map)) .to_owned_table(fields) .unwrap(); let expected = OwnedTable::try_new(IndexMap::default()).unwrap(); @@ -291,15 +302,19 @@ fn we_can_get_no_columns_from_a_basic_filter_with_no_selected_columns_using_resu #[test] fn we_can_get_the_correct_result_from_a_basic_filter_using_result_evaluate() { - let data = owned_table([ - bigint("a", [1, 4, 5, 2, 5]), - bigint("b", [1, 2, 3, 4, 5]), - int128("c", [1, 2, 3, 4, 5]), - varchar("d", ["1", "2", "3", "4", "5"]), - scalar("e", [1, 2, 3, 4, 5]), + let alloc = Bump::new(); + let data = table([ + borrowed_bigint("a", [1, 4, 5, 2, 5], &alloc), + borrowed_bigint("b", [1, 2, 3, 4, 5], &alloc), + borrowed_int128("c", [1, 2, 3, 4, 5], &alloc), + borrowed_varchar("d", ["1", "2", "3", "4", "5"], &alloc), + borrowed_scalar("e", [1, 2, 3, 4, 5], &alloc), ]); let t = "sxt.t".parse().unwrap(); - let mut accessor = OwnedTableTestAccessor::::new_empty_with_setup(()); + let table_map = indexmap! { + t => data.clone() + }; + let mut accessor = TableTestAccessor::::new_empty_with_setup(()); accessor.add_table(t, data, 0); let where_clause: DynProofExpr = equal(column(t, "a", &accessor), const_int128(5)); let expr = filter( @@ -307,7 +322,6 @@ fn we_can_get_the_correct_result_from_a_basic_filter_using_result_evaluate() { tab(t), where_clause, ); - let alloc = Bump::new(); let mut builder = FirstRoundBuilder::new(); expr.first_round_evaluate(&mut builder); let fields = &[ @@ -320,7 +334,7 @@ fn we_can_get_the_correct_result_from_a_basic_filter_using_result_evaluate() { ), ]; let res: OwnedTable = - ProvableQueryResult::from(expr.result_evaluate(&alloc, &accessor)) + ProvableQueryResult::from(expr.result_evaluate(&alloc, &table_map)) .to_owned_table(fields) .unwrap(); let expected: OwnedTable = owned_table([ diff --git a/crates/proof-of-sql/src/sql/proof_plans/filter_exec_test_dishonest_prover.rs b/crates/proof-of-sql/src/sql/proof_plans/filter_exec_test_dishonest_prover.rs index c22f02065..f2a2dcd02 100644 --- a/crates/proof-of-sql/src/sql/proof_plans/filter_exec_test_dishonest_prover.rs +++ b/crates/proof-of-sql/src/sql/proof_plans/filter_exec_test_dishonest_prover.rs @@ -2,16 +2,17 @@ use super::{filter_exec::prove_filter, OstensibleFilterExec}; use crate::{ base::{ database::{ - filter_util::*, owned_table_utility::*, Column, DataAccessor, OwnedTableTestAccessor, - Table, TableOptions, TestAccessor, + filter_util::*, owned_table_utility::*, Column, OwnedTableTestAccessor, Table, + TableOptions, TableRef, TestAccessor, }, + map::IndexMap, proof::ProofError, scalar::Scalar, }, sql::{ proof::{ - FinalRoundBuilder, FirstRoundBuilder, ProofPlan, ProverEvaluate, ProverHonestyMarker, - QueryError, VerifiableQueryResult, + FinalRoundBuilder, FirstRoundBuilder, ProverEvaluate, ProverHonestyMarker, QueryError, + VerifiableQueryResult, }, proof_exprs::{ test_utility::{cols_expr_plan, column, const_int128, equal, tab}, @@ -36,12 +37,13 @@ impl ProverEvaluate for DishonestFilterExec { fn result_evaluate<'a, S: Scalar>( &self, alloc: &'a Bump, - accessor: &'a dyn DataAccessor, + table_map: &IndexMap>, ) -> Table<'a, S> { - let column_refs = self.get_column_references(); - let used_table = accessor.get_table(self.table.table_ref, &column_refs); + let table = table_map + .get(&self.table.table_ref) + .expect("Table not found"); // 1. selection - let selection_column: Column<'a, S> = self.where_clause.result_evaluate(alloc, &used_table); + let selection_column: Column<'a, S> = self.where_clause.result_evaluate(alloc, table); let selection = selection_column .as_boolean() .expect("selection is not boolean"); @@ -50,7 +52,7 @@ impl ProverEvaluate for DishonestFilterExec { let columns: Vec<_> = self .aliased_results .iter() - .map(|aliased_expr| aliased_expr.expr.result_evaluate(alloc, &used_table)) + .map(|aliased_expr| aliased_expr.expr.result_evaluate(alloc, table)) .collect(); // Compute filtered_columns let (filtered_columns, _) = filter_columns(alloc, &columns, selection); @@ -79,14 +81,14 @@ impl ProverEvaluate for DishonestFilterExec { &self, builder: &mut FinalRoundBuilder<'a, S>, alloc: &'a Bump, - accessor: &'a dyn DataAccessor, + table_map: &IndexMap>, ) -> Table<'a, S> { - let column_refs = self.get_column_references(); - let used_table = accessor.get_table(self.table.table_ref, &column_refs); + let table = table_map + .get(&self.table.table_ref) + .expect("Table not found"); // 1. selection let selection_column: Column<'a, S> = - self.where_clause - .prover_evaluate(builder, alloc, &used_table); + self.where_clause.prover_evaluate(builder, alloc, table); let selection = selection_column .as_boolean() .expect("selection is not boolean"); @@ -95,11 +97,7 @@ impl ProverEvaluate for DishonestFilterExec { let columns: Vec<_> = self .aliased_results .iter() - .map(|aliased_expr| { - aliased_expr - .expr - .prover_evaluate(builder, alloc, &used_table) - }) + .map(|aliased_expr| aliased_expr.expr.prover_evaluate(builder, alloc, table)) .collect(); // Compute filtered_columns let (filtered_columns, result_len) = filter_columns(alloc, &columns, selection); diff --git a/crates/proof-of-sql/src/sql/proof_plans/group_by_exec.rs b/crates/proof-of-sql/src/sql/proof_plans/group_by_exec.rs index 706a9a108..25050af45 100644 --- a/crates/proof-of-sql/src/sql/proof_plans/group_by_exec.rs +++ b/crates/proof-of-sql/src/sql/proof_plans/group_by_exec.rs @@ -5,7 +5,7 @@ use crate::{ group_by_util::{ aggregate_columns, compare_indexes_by_owned_columns, AggregatedColumns, }, - Column, ColumnField, ColumnRef, ColumnType, DataAccessor, OwnedTable, Table, TableRef, + Column, ColumnField, ColumnRef, ColumnType, OwnedTable, Table, TableRef, }, map::{IndexMap, IndexSet}, proof::ProofError, @@ -199,12 +199,13 @@ impl ProverEvaluate for GroupByExec { fn result_evaluate<'a, S: Scalar>( &self, alloc: &'a Bump, - accessor: &'a dyn DataAccessor, + table_map: &IndexMap>, ) -> Table<'a, S> { - let column_refs = self.get_column_references(); - let used_table = accessor.get_table(self.table.table_ref, &column_refs); + let table = table_map + .get(&self.table.table_ref) + .expect("Table not found"); // 1. selection - let selection_column: Column<'a, S> = self.where_clause.result_evaluate(alloc, &used_table); + let selection_column: Column<'a, S> = self.where_clause.result_evaluate(alloc, table); let selection = selection_column .as_boolean() @@ -214,12 +215,12 @@ impl ProverEvaluate for GroupByExec { let group_by_columns = self .group_by_exprs .iter() - .map(|expr| expr.result_evaluate(alloc, &used_table)) + .map(|expr| expr.result_evaluate(alloc, table)) .collect::>(); let sum_columns = self .sum_expr .iter() - .map(|aliased_expr| aliased_expr.expr.result_evaluate(alloc, &used_table)) + .map(|aliased_expr| aliased_expr.expr.result_evaluate(alloc, table)) .collect::>(); // Compute filtered_columns let AggregatedColumns { @@ -254,14 +255,14 @@ impl ProverEvaluate for GroupByExec { &self, builder: &mut FinalRoundBuilder<'a, S>, alloc: &'a Bump, - accessor: &'a dyn DataAccessor, + table_map: &IndexMap>, ) -> Table<'a, S> { - let column_refs = self.get_column_references(); - let used_table = accessor.get_table(self.table.table_ref, &column_refs); + let table = table_map + .get(&self.table.table_ref) + .expect("Table not found"); // 1. selection let selection_column: Column<'a, S> = - self.where_clause - .prover_evaluate(builder, alloc, &used_table); + self.where_clause.prover_evaluate(builder, alloc, table); let selection = selection_column .as_boolean() .expect("selection is not boolean"); @@ -270,16 +271,12 @@ impl ProverEvaluate for GroupByExec { let group_by_columns = self .group_by_exprs .iter() - .map(|expr| expr.prover_evaluate(builder, alloc, &used_table)) + .map(|expr| expr.prover_evaluate(builder, alloc, table)) .collect::>(); let sum_columns = self .sum_expr .iter() - .map(|aliased_expr| { - aliased_expr - .expr - .prover_evaluate(builder, alloc, &used_table) - }) + .map(|aliased_expr| aliased_expr.expr.prover_evaluate(builder, alloc, table)) .collect::>(); // 3. Compute filtered_columns let AggregatedColumns { diff --git a/crates/proof-of-sql/src/sql/proof_plans/projection_exec.rs b/crates/proof-of-sql/src/sql/proof_plans/projection_exec.rs index 464307be1..be9c71f97 100644 --- a/crates/proof-of-sql/src/sql/proof_plans/projection_exec.rs +++ b/crates/proof-of-sql/src/sql/proof_plans/projection_exec.rs @@ -1,8 +1,6 @@ use crate::{ base::{ - database::{ - ColumnField, ColumnRef, DataAccessor, OwnedTable, Table, TableOptions, TableRef, - }, + database::{ColumnField, ColumnRef, OwnedTable, Table, TableOptions, TableRef}, map::{IndexMap, IndexSet}, proof::ProofError, scalar::Scalar, @@ -90,18 +88,19 @@ impl ProverEvaluate for ProjectionExec { fn result_evaluate<'a, S: Scalar>( &self, alloc: &'a Bump, - accessor: &'a dyn DataAccessor, + table_map: &IndexMap>, ) -> Table<'a, S> { - let column_refs = self.get_column_references(); - let used_table = accessor.get_table(self.table.table_ref, &column_refs); + let table = table_map + .get(&self.table.table_ref) + .expect("Table not found"); Table::<'a, S>::try_from_iter_with_options( self.aliased_results.iter().map(|aliased_expr| { ( aliased_expr.alias, - aliased_expr.expr.result_evaluate(alloc, &used_table), + aliased_expr.expr.result_evaluate(alloc, table), ) }), - TableOptions::new(Some(accessor.get_length(self.table.table_ref))), + TableOptions::new(Some(table.num_rows())), ) .expect("Failed to create table from iterator") } @@ -118,21 +117,20 @@ impl ProverEvaluate for ProjectionExec { &self, builder: &mut FinalRoundBuilder<'a, S>, alloc: &'a Bump, - accessor: &'a dyn DataAccessor, + table_map: &IndexMap>, ) -> Table<'a, S> { - let column_refs = self.get_column_references(); - let used_table = accessor.get_table(self.table.table_ref, &column_refs); + let table = table_map + .get(&self.table.table_ref) + .expect("Table not found"); // 1. Evaluate result expressions let res = Table::<'a, S>::try_from_iter_with_options( self.aliased_results.iter().map(|aliased_expr| { ( aliased_expr.alias, - aliased_expr - .expr - .prover_evaluate(builder, alloc, &used_table), + aliased_expr.expr.prover_evaluate(builder, alloc, table), ) }), - TableOptions::new(Some(accessor.get_length(self.table.table_ref))), + TableOptions::new(Some(table.num_rows())), ) .expect("Failed to create table from iterator"); // 2. Produce MLEs diff --git a/crates/proof-of-sql/src/sql/proof_plans/projection_exec_test.rs b/crates/proof-of-sql/src/sql/proof_plans/projection_exec_test.rs index 1780b4dea..9dc14c8c4 100644 --- a/crates/proof-of-sql/src/sql/proof_plans/projection_exec_test.rs +++ b/crates/proof-of-sql/src/sql/proof_plans/projection_exec_test.rs @@ -2,10 +2,10 @@ use super::{test_utility::*, DynProofPlan, ProjectionExec}; use crate::{ base::{ database::{ - owned_table_utility::*, ColumnField, ColumnRef, ColumnType, OwnedTable, - OwnedTableTestAccessor, TableRef, TestAccessor, + owned_table_utility::*, table_utility::*, ColumnField, ColumnRef, ColumnType, + OwnedTable, OwnedTableTestAccessor, TableRef, TableTestAccessor, TestAccessor, }, - map::{IndexMap, IndexSet}, + map::{indexmap, IndexMap, IndexSet}, math::decimal::Precision, scalar::Curve25519Scalar, }, @@ -155,19 +155,23 @@ fn we_can_prove_and_get_the_correct_result_from_a_nontrivial_projection() { #[test] fn we_can_get_an_empty_result_from_a_basic_projection_on_an_empty_table_using_result_evaluate() { - let data = owned_table([ - bigint("a", [0; 0]), - bigint("b", [0; 0]), - int128("c", [0; 0]), - varchar("d", [""; 0]), - scalar("e", [0; 0]), + let alloc = Bump::new(); + let data = table([ + borrowed_bigint("a", [0; 0], &alloc), + borrowed_bigint("b", [0; 0], &alloc), + borrowed_int128("c", [0; 0], &alloc), + borrowed_varchar("d", [""; 0], &alloc), + borrowed_scalar("e", [0; 0], &alloc), ]); let t = "sxt.t".parse().unwrap(); - let mut accessor = OwnedTableTestAccessor::::new_empty_with_setup(()); + let table_map = indexmap! { + t => data.clone() + }; + let mut accessor = TableTestAccessor::::new_empty_with_setup(()); accessor.add_table(t, data, 0); let expr: DynProofPlan = projection(cols_expr_plan(t, &["b", "c", "d", "e"], &accessor), tab(t)); - let alloc = Bump::new(); + let mut builder = FirstRoundBuilder::new(); expr.first_round_evaluate(&mut builder); let fields = &[ @@ -180,7 +184,7 @@ fn we_can_get_an_empty_result_from_a_basic_projection_on_an_empty_table_using_re ), ]; let res: OwnedTable = - ProvableQueryResult::from(expr.result_evaluate(&alloc, &accessor)) + ProvableQueryResult::from(expr.result_evaluate(&alloc, &table_map)) .to_owned_table(fields) .unwrap(); let expected: OwnedTable = owned_table([ @@ -195,23 +199,26 @@ fn we_can_get_an_empty_result_from_a_basic_projection_on_an_empty_table_using_re #[test] fn we_can_get_no_columns_from_a_basic_projection_with_no_selected_columns_using_result_evaluate() { - let data = owned_table([ - bigint("a", [1, 4, 5, 2, 5]), - bigint("b", [1, 2, 3, 4, 5]), - int128("c", [1, 2, 3, 4, 5]), - varchar("d", ["1", "2", "3", "4", "5"]), - scalar("e", [1, 2, 3, 4, 5]), + let alloc = Bump::new(); + let data = table([ + borrowed_bigint("a", [1, 4, 5, 2, 5], &alloc), + borrowed_bigint("b", [1, 2, 3, 4, 5], &alloc), + borrowed_int128("c", [1, 2, 3, 4, 5], &alloc), + borrowed_varchar("d", ["1", "2", "3", "4", "5"], &alloc), + borrowed_scalar("e", [1, 2, 3, 4, 5], &alloc), ]); let t = "sxt.t".parse().unwrap(); - let mut accessor = OwnedTableTestAccessor::::new_empty_with_setup(()); + let table_map = indexmap! { + t => data.clone() + }; + let mut accessor = TableTestAccessor::::new_empty_with_setup(()); accessor.add_table(t, data, 0); let expr: DynProofPlan = projection(cols_expr_plan(t, &[], &accessor), tab(t)); - let alloc = Bump::new(); let mut builder = FirstRoundBuilder::new(); expr.first_round_evaluate(&mut builder); let fields = &[]; let res: OwnedTable = - ProvableQueryResult::from(expr.result_evaluate(&alloc, &accessor)) + ProvableQueryResult::from(expr.result_evaluate(&alloc, &table_map)) .to_owned_table(fields) .unwrap(); let expected = OwnedTable::try_new(IndexMap::default()).unwrap(); @@ -220,15 +227,19 @@ fn we_can_get_no_columns_from_a_basic_projection_with_no_selected_columns_using_ #[test] fn we_can_get_the_correct_result_from_a_basic_projection_using_result_evaluate() { - let data = owned_table([ - bigint("a", [1, 4, 5, 2, 5]), - bigint("b", [1, 2, 3, 4, 5]), - int128("c", [1, 2, 3, 4, 5]), - varchar("d", ["1", "2", "3", "4", "5"]), - scalar("e", [1, 2, 3, 4, 5]), + let alloc = Bump::new(); + let data = table([ + borrowed_bigint("a", [1, 4, 5, 2, 5], &alloc), + borrowed_bigint("b", [1, 2, 3, 4, 5], &alloc), + borrowed_int128("c", [1, 2, 3, 4, 5], &alloc), + borrowed_varchar("d", ["1", "2", "3", "4", "5"], &alloc), + borrowed_scalar("e", [1, 2, 3, 4, 5], &alloc), ]); let t = "sxt.t".parse().unwrap(); - let mut accessor = OwnedTableTestAccessor::::new_empty_with_setup(()); + let table_map = indexmap! { + t => data.clone() + }; + let mut accessor = TableTestAccessor::::new_empty_with_setup(()); accessor.add_table(t, data, 0); let expr: DynProofPlan = projection( vec![ @@ -242,7 +253,6 @@ fn we_can_get_the_correct_result_from_a_basic_projection_using_result_evaluate() ], tab(t), ); - let alloc = Bump::new(); let mut builder = FirstRoundBuilder::new(); expr.first_round_evaluate(&mut builder); let fields = &[ @@ -255,7 +265,7 @@ fn we_can_get_the_correct_result_from_a_basic_projection_using_result_evaluate() ), ]; let res: OwnedTable = - ProvableQueryResult::from(expr.result_evaluate(&alloc, &accessor)) + ProvableQueryResult::from(expr.result_evaluate(&alloc, &table_map)) .to_owned_table(fields) .unwrap(); let expected: OwnedTable = owned_table([ diff --git a/crates/proof-of-sql/src/sql/proof_plans/table_exec.rs b/crates/proof-of-sql/src/sql/proof_plans/table_exec.rs index 7c34cc5a2..2132fcff9 100644 --- a/crates/proof-of-sql/src/sql/proof_plans/table_exec.rs +++ b/crates/proof-of-sql/src/sql/proof_plans/table_exec.rs @@ -1,6 +1,6 @@ use crate::{ base::{ - database::{ColumnField, ColumnRef, DataAccessor, OwnedTable, Table, TableRef}, + database::{ColumnField, ColumnRef, OwnedTable, Table, TableRef}, map::{indexset, IndexMap, IndexSet}, proof::ProofError, scalar::Scalar, @@ -72,9 +72,12 @@ impl ProverEvaluate for TableExec { fn result_evaluate<'a, S: Scalar>( &self, _alloc: &'a Bump, - accessor: &'a dyn DataAccessor, + table_map: &IndexMap>, ) -> Table<'a, S> { - accessor.get_table(self.table_ref, &self.get_column_references()) + table_map + .get(&self.table_ref) + .expect("Table not found") + .clone() } fn first_round_evaluate(&self, _builder: &mut FirstRoundBuilder) {} @@ -85,9 +88,12 @@ impl ProverEvaluate for TableExec { &self, builder: &mut FinalRoundBuilder<'a, S>, alloc: &'a Bump, - accessor: &'a dyn DataAccessor, + table_map: &IndexMap>, ) -> Table<'a, S> { - let table = accessor.get_table(self.table_ref, &self.get_column_references()); + let table = table_map + .get(&self.table_ref) + .expect("Table not found") + .clone(); for column in table.columns() { builder.produce_intermediate_mle(column.as_scalar(alloc)); }