-
Notifications
You must be signed in to change notification settings - Fork 193
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
480 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
170 changes: 170 additions & 0 deletions
170
crates/proof-of-sql/src/base/database/table_test_accessor.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
use super::{ | ||
Column, ColumnRef, ColumnType, CommitmentAccessor, DataAccessor, MetadataAccessor, | ||
SchemaAccessor, Table, TableRef, TestAccessor, | ||
}; | ||
use crate::base::{ | ||
commitment::{CommitmentEvaluationProof, VecCommitmentExt}, | ||
map::IndexMap, | ||
}; | ||
use alloc::vec::Vec; | ||
use proof_of_sql_parser::Identifier; | ||
|
||
/// A test accessor that uses [`Table`] as the underlying table type. | ||
/// Note: this is intended for testing and examples. It is not optimized for performance, so should not be used for benchmarks or production use-cases. | ||
pub struct TableTestAccessor<'a, CP: CommitmentEvaluationProof> { | ||
tables: IndexMap<TableRef, (Table<'a, CP::Scalar>, usize)>, | ||
setup: Option<CP::ProverPublicSetup<'a>>, | ||
} | ||
|
||
impl<CP: CommitmentEvaluationProof> Default for TableTestAccessor<'_, CP> { | ||
fn default() -> Self { | ||
Self { | ||
tables: IndexMap::default(), | ||
setup: None, | ||
} | ||
} | ||
} | ||
|
||
impl<CP: CommitmentEvaluationProof> Clone for TableTestAccessor<'_, CP> { | ||
fn clone(&self) -> Self { | ||
Self { | ||
tables: self.tables.clone(), | ||
setup: self.setup, | ||
} | ||
} | ||
} | ||
|
||
impl<'a, CP: CommitmentEvaluationProof> TestAccessor<CP::Commitment> for TableTestAccessor<'a, CP> { | ||
type Table = Table<'a, CP::Scalar>; | ||
|
||
fn new_empty() -> Self { | ||
TableTestAccessor::default() | ||
} | ||
|
||
fn add_table(&mut self, table_ref: TableRef, data: Self::Table, table_offset: usize) { | ||
self.tables.insert(table_ref, (data, table_offset)); | ||
} | ||
/// | ||
/// # Panics | ||
/// | ||
/// Will panic if the `table_ref` is not found in `self.tables`, indicating | ||
/// that an invalid reference was provided. | ||
fn get_column_names(&self, table_ref: TableRef) -> Vec<&str> { | ||
self.tables | ||
.get(&table_ref) | ||
.unwrap() | ||
.0 | ||
.column_names() | ||
.map(proof_of_sql_parser::Identifier::as_str) | ||
.collect() | ||
} | ||
|
||
/// | ||
/// # Panics | ||
/// | ||
/// Will panic if the `table_ref` is not found in `self.tables`, indicating that an invalid reference was provided. | ||
fn update_offset(&mut self, table_ref: TableRef, new_offset: usize) { | ||
self.tables.get_mut(&table_ref).unwrap().1 = new_offset; | ||
} | ||
} | ||
|
||
/// | ||
/// # Panics | ||
/// | ||
/// Will panic if the `column.table_ref()` is not found in `self.tables`, or if | ||
/// the `column.column_id()` is not found in the inner table for that reference, | ||
/// indicating that an invalid column reference was provided. | ||
impl<'a, CP: CommitmentEvaluationProof> DataAccessor<CP::Scalar> for TableTestAccessor<'a, CP> { | ||
fn get_column(&self, column: ColumnRef) -> Column<'a, CP::Scalar> { | ||
*self | ||
.tables | ||
.get(&column.table_ref()) | ||
.unwrap() | ||
.0 | ||
.inner_table() | ||
.get(&column.column_id()) | ||
.unwrap() | ||
} | ||
} | ||
|
||
/// | ||
/// # Panics | ||
/// | ||
/// Will panic if the `column.table_ref()` is not found in `self.tables`, or if the `column.column_id()` is not found in the inner table for that reference,indicating that an invalid column reference was provided. | ||
impl<CP: CommitmentEvaluationProof> CommitmentAccessor<CP::Commitment> | ||
for TableTestAccessor<'_, CP> | ||
{ | ||
fn get_commitment(&self, column: ColumnRef) -> CP::Commitment { | ||
let (table, offset) = self.tables.get(&column.table_ref()).unwrap(); | ||
let borrowed_column = table.inner_table().get(&column.column_id()).unwrap(); | ||
Vec::<CP::Commitment>::from_columns_with_offset( | ||
[borrowed_column], | ||
*offset, | ||
self.setup.as_ref().unwrap(), | ||
)[0] | ||
.clone() | ||
} | ||
} | ||
impl<CP: CommitmentEvaluationProof> MetadataAccessor for TableTestAccessor<'_, CP> { | ||
/// | ||
/// # Panics | ||
/// | ||
/// Will panic if the `table_ref` is not found in `self.tables`, indicating that an invalid reference was provided. | ||
fn get_length(&self, table_ref: TableRef) -> usize { | ||
self.tables.get(&table_ref).unwrap().0.num_rows() | ||
} | ||
/// | ||
/// # Panics | ||
/// | ||
/// Will panic if the `table_ref` is not found in `self.tables`, indicating that an invalid reference was provided. | ||
fn get_offset(&self, table_ref: TableRef) -> usize { | ||
self.tables.get(&table_ref).unwrap().1 | ||
} | ||
} | ||
impl<CP: CommitmentEvaluationProof> SchemaAccessor for TableTestAccessor<'_, CP> { | ||
fn lookup_column(&self, table_ref: TableRef, column_id: Identifier) -> Option<ColumnType> { | ||
Some( | ||
self.tables | ||
.get(&table_ref)? | ||
.0 | ||
.inner_table() | ||
.get(&column_id)? | ||
.column_type(), | ||
) | ||
} | ||
/// | ||
/// # Panics | ||
/// | ||
/// Will panic if the `table_ref` is not found in `self.tables`, indicating that an invalid reference was provided. | ||
fn lookup_schema(&self, table_ref: TableRef) -> Vec<(Identifier, ColumnType)> { | ||
self.tables | ||
.get(&table_ref) | ||
.unwrap() | ||
.0 | ||
.inner_table() | ||
.iter() | ||
.map(|(&id, col)| (id, col.column_type())) | ||
.collect() | ||
} | ||
} | ||
|
||
impl<'a, CP: CommitmentEvaluationProof> TableTestAccessor<'a, CP> { | ||
/// Create a new empty test accessor with the given setup. | ||
pub fn new_empty_with_setup(setup: CP::ProverPublicSetup<'a>) -> Self { | ||
let mut res = Self::new_empty(); | ||
res.setup = Some(setup); | ||
res | ||
} | ||
|
||
/// Create a new test accessor containing the provided table. | ||
pub fn new_from_table( | ||
table_ref: TableRef, | ||
table: Table<'a, CP::Scalar>, | ||
offset: usize, | ||
setup: CP::ProverPublicSetup<'a>, | ||
) -> Self { | ||
let mut res = Self::new_empty_with_setup(setup); | ||
res.add_table(table_ref, table, offset); | ||
res | ||
} | ||
} |
Oops, something went wrong.