Skip to content

Commit

Permalink
feat: add TableTestAccessor
Browse files Browse the repository at this point in the history
  • Loading branch information
iajoiner committed Nov 12, 2024
1 parent 0e3d2d8 commit b7c419f
Show file tree
Hide file tree
Showing 3 changed files with 480 additions and 0 deletions.
5 changes: 5 additions & 0 deletions crates/proof-of-sql/src/base/database/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ pub use owned_table_test_accessor::OwnedTableTestAccessor;
#[cfg(all(test, feature = "blitzar"))]
mod owned_table_test_accessor_test;

mod table_test_accessor;
pub use table_test_accessor::TableTestAccessor;
#[cfg(all(test, feature = "blitzar"))]
mod table_test_accessor_test;

/// TODO: add docs
pub(crate) mod filter_util;
#[cfg(test)]
Expand Down
170 changes: 170 additions & 0 deletions crates/proof-of-sql/src/base/database/table_test_accessor.rs
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
}
}
Loading

0 comments on commit b7c419f

Please sign in to comment.