Skip to content

Commit

Permalink
add few fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
varshith257 committed Dec 24, 2024
1 parent 79fc62f commit 4eddcd1
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ impl<C: Commitment> ColumnCommitments<C> {
ColumnCommitmentMetadataMap::from_column_fields_with_max_bounds(columns);
let commitments = columns
.iter()
.map(|c| accessor.get_commitment(ColumnRef::new(table.clone(), c.name(), c.data_type())))
.map(|c| {
accessor.get_commitment(ColumnRef::new(table.clone(), c.name(), c.data_type()))
})
.collect();
ColumnCommitments {
commitments,
Expand Down
8 changes: 4 additions & 4 deletions crates/proof-of-sql/src/base/database/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,16 +491,16 @@ impl Display for ColumnType {

/// Reference of a SQL column
#[derive(Debug, PartialEq, Eq, Clone, Hash, Serialize, Deserialize)]
pub struct ColumnRef {
pub struct ColumnRef<'a> {
column_id: Ident,
table_ref: TableRef,
table_ref: &'a TableRef,
column_type: ColumnType,
}

impl ColumnRef {
impl<'a> ColumnRef<'a> {
/// Create a new `ColumnRef` from a table, column identifier and column type
#[must_use]
pub fn new(table_ref: TableRef, column_id: Ident, column_type: ColumnType) -> Self {
pub fn new(table_ref: &'a TableRef, column_id: Ident, column_type: ColumnType) -> Self {
Self {
column_id,
table_ref,
Expand Down
18 changes: 13 additions & 5 deletions crates/proof-of-sql/src/base/database/table_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,21 @@ impl TableRef {
/// Creates a new table reference from a resource id
#[must_use]
pub fn new(object_name: ObjectName) -> Self {
Self { object_name: object_name.clone() }
Self {
object_name: object_name.clone(),
}
}

/// Returns the identifier of the schema
#[must_use]
pub fn schema_id(&self) -> Ident {
self.object_name.0.get(0).clone()
pub fn schema_id(&self) -> Option<Ident> {
self.object_name.0.get(0).cloned()
}

/// Returns the identifier of the table
#[must_use]
pub fn table_id(&self) -> Ident {
self.object_name.0.get(1).clone()
pub fn table_id(&self) -> Option<Ident> {
self.object_name.0.get(1).cloned()
}

/// Returns the underlying resource id of the table
Expand All @@ -47,6 +49,12 @@ impl FromStr for TableRef {
}
}

impl From<&str> for TableRef {
fn from(s: &str) -> Self {
TableRef::new(object_name_from(s))
}
}

impl Display for TableRef {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
self.object_name.clone().fmt(f)
Expand Down
3 changes: 1 addition & 2 deletions crates/proof-of-sql/src/base/database/table_utility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,7 @@ pub fn borrowed_int<S: Scalar>(
/// use bumpalo::Bump;
/// use proof_of_sql::base::{database::table_utility::*, scalar::Curve25519Scalar};
/// let alloc = Bump::new();
/// let result = table::<Curve25519Scalar>([
/// borrowed_bigint("a", [1, 2, 3], &alloc),
/// let result = table::<Curve25519Scalar>([/// borrowed_bigint("a", [1, 2, 3], &alloc),
/// ]);
/// ```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,17 @@ mod tests {
let identifier_b = "b".into();
let identifier_alias = "alias".into();

let column_ref_a = ColumnRef::new(table_ref, identifier_a, ColumnType::BigInt);
let column_ref_b = ColumnRef::new(table_ref, identifier_b, ColumnType::BigInt);
let column_ref_a = ColumnRef::new(&table_ref, identifier_a, ColumnType::BigInt);
let column_ref_b = ColumnRef::new(&table_ref, identifier_b, ColumnType::BigInt);

let plan = DynProofPlan::Filter(FilterExec::new(
vec![AliasedDynProofExpr {
expr: DynProofExpr::Column(ColumnExpr::new(column_ref_b)),
alias: identifier_alias,
}],
TableExpr { table_ref },
TableExpr {
table_ref: &table_ref,
},
DynProofExpr::Equals(EqualsExpr::new(
Box::new(DynProofExpr::Column(ColumnExpr::new(column_ref_a))),
Box::new(DynProofExpr::Literal(LiteralExpr::new(
Expand Down
2 changes: 1 addition & 1 deletion crates/proof-of-sql/src/sql/proof/query_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ impl<CP: CommitmentEvaluationProof> QueryProof<CP> {
);
let one_eval_map: IndexMap<TableRef, CP::Scalar> = table_length_map
.iter()
.map(|(table_ref, length)| (**table_ref, sumcheck_evaluations.one_evaluations[length]))
.map(|(table_ref, length)| (table_ref, sumcheck_evaluations.one_evaluations[length]))
.collect();
let mut builder = VerificationBuilder::new(
min_row_num,
Expand Down
2 changes: 1 addition & 1 deletion crates/proof-of-sql/src/sql/proof/query_proof_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ use crate::{
sql::proof::{FirstRoundBuilder, QueryData, SumcheckSubpolynomialType},
};
use bumpalo::Bump;
use proof_of_sql_parser::sqlparser::object_name_from;
use serde::Serialize;
use sqlparser::ast::Ident;
use proof_of_sql_parser::sqlparser::object_name_from;

/// Type to allow us to prove and verify an artificial polynomial where we prove
/// that every entry in the result is zero
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ use crate::{
sql::proof::{FirstRoundBuilder, QueryData},
};
use bumpalo::Bump;
use serde::Serialize;
use proof_of_sql_parser::sqlparser::object_name_from;
use serde::Serialize;

#[derive(Debug, Serialize, Default)]
pub(super) struct EmptyTestQueryExpr {
Expand Down
70 changes: 47 additions & 23 deletions crates/proof-of-sql/src/sql/proof_exprs/test_utility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,25 @@ use crate::base::{
use proof_of_sql_parser::intermediate_ast::AggregationOperator;
use sqlparser::ast::Ident;

pub fn col_ref(tab: TableRef, name: &str, accessor: &impl SchemaAccessor) -> ColumnRef {
pub fn col_ref<S: Into<TableRef>>(tab: S, name: &str, accessor: &impl SchemaAccessor) -> ColumnRef {
let table_ref = tab.into();
let name: Ident = name.into();
let type_col = accessor.lookup_column(tab, name.clone()).unwrap();
let type_col = accessor.lookup_column(tab.clone(), name.clone()).unwrap();
ColumnRef::new(tab, name, type_col)
}

/// # Panics
/// Panics if:
/// - `accessor.lookup_column()` returns `None`, indicating the column is not found.
pub fn column(tab: TableRef, name: &str, accessor: &impl SchemaAccessor) -> DynProofExpr {
pub fn column<S: Into<TableRef>>(
tab: S,
name: &str,
accessor: &impl SchemaAccessor,
) -> DynProofExpr {
let table_ref = tab.into();
let name: Ident = name.into();
let type_col = accessor.lookup_column(tab, name.clone()).unwrap();
DynProofExpr::Column(ColumnExpr::new(ColumnRef::new(tab, name, type_col)))
let type_col = accessor.lookup_column(tab.clone(), name.clone()).unwrap();
DynProofExpr::Column(ColumnExpr::new(ColumnRef::new(tab.clone(), name, type_col)))
}

/// # Panics
Expand Down Expand Up @@ -125,8 +131,10 @@ pub fn const_decimal75<T: Into<I256>>(precision: u8, scale: i8, val: T) -> DynPr
))
}

pub fn tab(tab: TableRef) -> TableExpr {
TableExpr { table_ref: tab }
pub fn tab<S: Into<TableRef>>(tab: S) -> TableExpr {
TableExpr {
table_ref: tab.into(),
}
}

/// # Panics
Expand All @@ -143,14 +151,15 @@ pub fn aliased_plan(expr: DynProofExpr, alias: &str) -> AliasedDynProofExpr {
/// Panics if:
/// - `old_name.parse()` or `new_name.parse()` fails to parse the provided column names.
/// - `col_ref()` fails to find the referenced column, leading to a panic in the column accessor.
pub fn aliased_col_expr_plan(
tab: TableRef,
pub fn aliased_col_expr_plan<S: Into<TableRef>>(
tab: S,
old_name: &str,
new_name: &str,
accessor: &impl SchemaAccessor,
) -> AliasedDynProofExpr {
let tab = tab.into();
AliasedDynProofExpr {
expr: DynProofExpr::Column(ColumnExpr::new(col_ref(tab, old_name, accessor))),
expr: DynProofExpr::Column(ColumnExpr::new(col_ref(tab.clone(), old_name, accessor))),
alias: new_name.into(),
}
}
Expand All @@ -159,47 +168,62 @@ pub fn aliased_col_expr_plan(
/// Panics if:
/// - `name.parse()` fails to parse the provided column name.
/// - `col_ref()` fails to find the referenced column, leading to a panic in the column accessor.
pub fn col_expr_plan(
tab: TableRef,
pub fn col_expr_plan<S: Into<TableRef>>(
tab: S,
name: &str,
accessor: &impl SchemaAccessor,
) -> AliasedDynProofExpr {
let tab = tab.into();
AliasedDynProofExpr {
expr: DynProofExpr::Column(ColumnExpr::new(col_ref(tab, name, accessor))),
expr: DynProofExpr::Column(ColumnExpr::new(col_ref(tab.clone(), name, accessor))),
alias: name.into(),
}
}

pub fn aliased_cols_expr_plan(
tab: TableRef,
pub fn aliased_cols_expr_plan<S: Into<TableRef>>(
tab: S,
names: &[(&str, &str)],
accessor: &impl SchemaAccessor,
) -> Vec<AliasedDynProofExpr> {
let tab = tab.into();
names
.iter()
.map(|(old_name, new_name)| aliased_col_expr_plan(tab, old_name, new_name, accessor))
.map(|(old_name, new_name)| {
aliased_col_expr_plan(tab.clone(), old_name, new_name, accessor)
})
.collect()
}

pub fn cols_expr_plan(
tab: TableRef,
pub fn cols_expr_plan<S: Into<TableRef>>(
tab: S,
names: &[&str],
accessor: &impl SchemaAccessor,
) -> Vec<AliasedDynProofExpr> {
let tab = tab.into();
names
.iter()
.map(|name| col_expr_plan(tab, name, accessor))
.map(|name| col_expr_plan(tab.clone(), name, accessor))
.collect()
}

pub fn col_expr(tab: TableRef, name: &str, accessor: &impl SchemaAccessor) -> ColumnExpr {
ColumnExpr::new(col_ref(tab, name, accessor))
pub fn col_expr<S: Into<TableRef>>(
tab: S,
name: &str,
accessor: &impl SchemaAccessor,
) -> ColumnExpr {
let tab = tab.into();
ColumnExpr::new(col_ref(tab.clone(), name, accessor))
}

pub fn cols_expr(tab: TableRef, names: &[&str], accessor: &impl SchemaAccessor) -> Vec<ColumnExpr> {
pub fn cols_expr<S: Into<TableRef>>(
tab: S,
names: &[&str],
accessor: &impl SchemaAccessor,
) -> Vec<ColumnExpr> {
let tab = tab.into();
names
.iter()
.map(|name| col_expr(tab, name, accessor))
.map(|name| col_expr(tab.clone(), name, accessor))
.collect()
}

Expand Down
3 changes: 2 additions & 1 deletion crates/proof-of-sql/src/sql/proof_plans/table_exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ impl ProofPlan for TableExec {
.schema
.iter()
.map(|field| {
let column_ref = ColumnRef::new(self.table_ref.clone(), field.name(), field.data_type());
let column_ref =
ColumnRef::new(self.table_ref.clone(), field.name(), field.data_type());
*accessor.get(&column_ref).expect("Column does not exist")
})
.collect::<Vec<_>>();
Expand Down

0 comments on commit 4eddcd1

Please sign in to comment.