Skip to content

Commit

Permalink
feat: add Table::columns(), ProvableQueryResult::new_from_table t…
Browse files Browse the repository at this point in the history
…o simplify the code
  • Loading branch information
iajoiner committed Nov 14, 2024
1 parent 513d374 commit c1f78c3
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 71 deletions.
4 changes: 4 additions & 0 deletions crates/proof-of-sql/src/base/database/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ impl<'a, S: Scalar> Table<'a, S> {
pub fn column_names(&self) -> impl Iterator<Item = &Identifier> {
self.table.keys()
}
/// Returns the columns of this table as an Iterator
pub fn columns(&self) -> impl Iterator<Item = &Column<'a, S>> {
self.table.values()
}
}

// Note: we modify the default PartialEq for IndexMap to also check for column ordering.
Expand Down
9 changes: 8 additions & 1 deletion crates/proof-of-sql/src/sql/proof/provable_query_result.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{decode_and_convert, decode_multiple_elements, ProvableResultColumn, QueryError};
use crate::base::{
database::{Column, ColumnField, ColumnType, OwnedColumn, OwnedTable},
database::{Column, ColumnField, ColumnType, OwnedColumn, OwnedTable, Table},
polynomial::compute_evaluation_vector,
scalar::Scalar,
};
Expand Down Expand Up @@ -55,6 +55,13 @@ impl ProvableQueryResult {
}
}

/// Form intermediate query result from a table
#[must_use]
pub fn new_from_table<S: Scalar>(table: &Table<S>) -> Self {
let columns = table.columns().copied().collect::<Vec<_>>();
Self::new(table.num_rows() as u64, &columns)
}

/// Form intermediate query result from index rows and result columns
/// # Panics
///
Expand Down
8 changes: 1 addition & 7 deletions crates/proof-of-sql/src/sql/proof/query_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,7 @@ impl<CP: CommitmentEvaluationProof> QueryProof<CP> {

// Evaluate query result
let result_table = expr.result_evaluate(&alloc, accessor);
let result_cols = result_table
.inner_table()
.values()
.cloned()
.collect::<Vec<_>>();
let provable_result =
ProvableQueryResult::new(result_table.num_rows() as u64, &result_cols);
let provable_result = ProvableQueryResult::new_from_table(&result_table);

// Prover First Round
let mut first_round_builder = FirstRoundBuilder::new();
Expand Down
48 changes: 12 additions & 36 deletions crates/proof-of-sql/src/sql/proof_plans/filter_exec_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,6 @@ fn we_can_get_an_empty_result_from_a_basic_filter_on_an_empty_table_using_result
);
let alloc = Bump::new();
let result_table = expr.result_evaluate(&alloc, &accessor);
let result_cols = result_table
.inner_table()
.values()
.cloned()
.collect::<Vec<_>>();
let mut builder = FirstRoundBuilder::new();
expr.first_round_evaluate(&mut builder);
let fields = &[
Expand All @@ -211,10 +206,9 @@ fn we_can_get_an_empty_result_from_a_basic_filter_on_an_empty_table_using_result
ColumnType::Decimal75(Precision::new(75).unwrap(), 0),
),
];
let res: OwnedTable<Curve25519Scalar> =
ProvableQueryResult::new(result_table.num_rows() as u64, &result_cols)
.to_owned_table(fields)
.unwrap();
let res: OwnedTable<Curve25519Scalar> = ProvableQueryResult::new_from_table(&result_table)
.to_owned_table(fields)
.unwrap();
let expected: OwnedTable<Curve25519Scalar> = owned_table([
bigint("b", [0; 0]),
int128("c", [0; 0]),
Expand Down Expand Up @@ -245,11 +239,6 @@ fn we_can_get_an_empty_result_from_a_basic_filter_using_result_evaluate() {
);
let alloc = Bump::new();
let result_table = expr.result_evaluate(&alloc, &accessor);
let result_cols = result_table
.inner_table()
.values()
.cloned()
.collect::<Vec<_>>();
let mut builder = FirstRoundBuilder::new();
expr.first_round_evaluate(&mut builder);
let fields = &[
Expand All @@ -261,10 +250,9 @@ fn we_can_get_an_empty_result_from_a_basic_filter_using_result_evaluate() {
ColumnType::Decimal75(Precision::new(1).unwrap(), 0),
),
];
let res: OwnedTable<Curve25519Scalar> =
ProvableQueryResult::new(result_table.num_rows() as u64, &result_cols)
.to_owned_table(fields)
.unwrap();
let res: OwnedTable<Curve25519Scalar> = ProvableQueryResult::new_from_table(&result_table)
.to_owned_table(fields)
.unwrap();
let expected: OwnedTable<Curve25519Scalar> = owned_table([
bigint("b", [0; 0]),
int128("c", [0; 0]),
Expand All @@ -291,18 +279,12 @@ fn we_can_get_no_columns_from_a_basic_filter_with_no_selected_columns_using_resu
let expr = filter(cols_expr_plan(t, &[], &accessor), tab(t), where_clause);
let alloc = Bump::new();
let result_table = expr.result_evaluate(&alloc, &accessor);
let result_cols = result_table
.inner_table()
.values()
.cloned()
.collect::<Vec<_>>();
let mut builder = FirstRoundBuilder::new();
expr.first_round_evaluate(&mut builder);
let fields = &[];
let res: OwnedTable<Curve25519Scalar> =
ProvableQueryResult::new(result_table.num_rows() as u64, &result_cols)
.to_owned_table(fields)
.unwrap();
let res: OwnedTable<Curve25519Scalar> = ProvableQueryResult::new_from_table(&result_table)
.to_owned_table(fields)
.unwrap();
let expected = OwnedTable::try_new(IndexMap::default()).unwrap();
assert_eq!(res, expected);
}
Expand All @@ -327,11 +309,6 @@ fn we_can_get_the_correct_result_from_a_basic_filter_using_result_evaluate() {
);
let alloc = Bump::new();
let result_table = expr.result_evaluate(&alloc, &accessor);
let result_cols = result_table
.inner_table()
.values()
.cloned()
.collect::<Vec<_>>();
let mut builder = FirstRoundBuilder::new();
expr.first_round_evaluate(&mut builder);
let fields = &[
Expand All @@ -343,10 +320,9 @@ fn we_can_get_the_correct_result_from_a_basic_filter_using_result_evaluate() {
ColumnType::Decimal75(Precision::new(1).unwrap(), 0),
),
];
let res: OwnedTable<Curve25519Scalar> =
ProvableQueryResult::new(result_table.num_rows() as u64, &result_cols)
.to_owned_table(fields)
.unwrap();
let res: OwnedTable<Curve25519Scalar> = ProvableQueryResult::new_from_table(&result_table)
.to_owned_table(fields)
.unwrap();
let expected: OwnedTable<Curve25519Scalar> = owned_table([
bigint("b", [3, 5]),
int128("c", [3, 5]),
Expand Down
36 changes: 9 additions & 27 deletions crates/proof-of-sql/src/sql/proof_plans/projection_exec_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,6 @@ fn we_can_get_an_empty_result_from_a_basic_projection_on_an_empty_table_using_re
projection(cols_expr_plan(t, &["b", "c", "d", "e"], &accessor), tab(t));
let alloc = Bump::new();
let result_table = expr.result_evaluate(&alloc, &accessor);
let result_cols = result_table
.inner_table()
.values()
.cloned()
.collect::<Vec<_>>();
let mut builder = FirstRoundBuilder::new();
expr.first_round_evaluate(&mut builder);
let fields = &[
Expand All @@ -185,10 +180,9 @@ fn we_can_get_an_empty_result_from_a_basic_projection_on_an_empty_table_using_re
ColumnType::Decimal75(Precision::new(75).unwrap(), 0),
),
];
let res: OwnedTable<Curve25519Scalar> =
ProvableQueryResult::new(result_table.num_rows() as u64, &result_cols)
.to_owned_table(fields)
.unwrap();
let res: OwnedTable<Curve25519Scalar> = ProvableQueryResult::new_from_table(&result_table)
.to_owned_table(fields)
.unwrap();
let expected: OwnedTable<Curve25519Scalar> = owned_table([
bigint("b", [0; 0]),
int128("c", [0; 0]),
Expand All @@ -214,18 +208,12 @@ fn we_can_get_no_columns_from_a_basic_projection_with_no_selected_columns_using_
let expr: DynProofPlan = projection(cols_expr_plan(t, &[], &accessor), tab(t));
let alloc = Bump::new();
let result_table = expr.result_evaluate(&alloc, &accessor);
let result_cols = result_table
.inner_table()
.values()
.cloned()
.collect::<Vec<_>>();
let mut builder = FirstRoundBuilder::new();
expr.first_round_evaluate(&mut builder);
let fields = &[];
let res: OwnedTable<Curve25519Scalar> =
ProvableQueryResult::new(result_table.num_rows() as u64, &result_cols)
.to_owned_table(fields)
.unwrap();
let res: OwnedTable<Curve25519Scalar> = ProvableQueryResult::new_from_table(&result_table)
.to_owned_table(fields)
.unwrap();
let expected = OwnedTable::try_new(IndexMap::default()).unwrap();
assert_eq!(res, expected);
}
Expand Down Expand Up @@ -256,11 +244,6 @@ fn we_can_get_the_correct_result_from_a_basic_projection_using_result_evaluate()
);
let alloc = Bump::new();
let result_table = expr.result_evaluate(&alloc, &accessor);
let result_cols = result_table
.inner_table()
.values()
.cloned()
.collect::<Vec<_>>();
let mut builder = FirstRoundBuilder::new();
expr.first_round_evaluate(&mut builder);
let fields = &[
Expand All @@ -272,10 +255,9 @@ fn we_can_get_the_correct_result_from_a_basic_projection_using_result_evaluate()
ColumnType::Decimal75(Precision::new(1).unwrap(), 0),
),
];
let res: OwnedTable<Curve25519Scalar> =
ProvableQueryResult::new(result_table.num_rows() as u64, &result_cols)
.to_owned_table(fields)
.unwrap();
let res: OwnedTable<Curve25519Scalar> = ProvableQueryResult::new_from_table(&result_table)
.to_owned_table(fields)
.unwrap();
let expected: OwnedTable<Curve25519Scalar> = owned_table([
bigint("b", [2, 3, 4, 5, 6]),
int128("prod", [1, 4, 9, 16, 25]),
Expand Down
4 changes: 4 additions & 0 deletions crates/proof-of-sql/src/sql/proof_plans/table_exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ impl TableExec {
}

/// Returns the entire table.
///
/// # Panics
/// Panics if columns from the accessor have different lengths.
/// In practice, this should never happen.
fn get_table<'a, S: Scalar>(&self, accessor: &'a dyn DataAccessor<S>) -> Table<'a, S> {
Table::<'a, S>::try_from_iter(self.schema.iter().map(|field| {
(
Expand Down

0 comments on commit c1f78c3

Please sign in to comment.