diff --git a/crates/proof-of-sql/src/base/commitment/column_commitments.rs b/crates/proof-of-sql/src/base/commitment/column_commitments.rs index 305dce265..e7a881ac0 100644 --- a/crates/proof-of-sql/src/base/commitment/column_commitments.rs +++ b/crates/proof-of-sql/src/base/commitment/column_commitments.rs @@ -60,7 +60,9 @@ impl ColumnCommitments { 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, diff --git a/crates/proof-of-sql/src/base/database/column.rs b/crates/proof-of-sql/src/base/database/column.rs index 2b7a0e2a7..e8f9d9e40 100644 --- a/crates/proof-of-sql/src/base/database/column.rs +++ b/crates/proof-of-sql/src/base/database/column.rs @@ -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, diff --git a/crates/proof-of-sql/src/base/database/table_ref.rs b/crates/proof-of-sql/src/base/database/table_ref.rs index 488f108ec..8501286f2 100644 --- a/crates/proof-of-sql/src/base/database/table_ref.rs +++ b/crates/proof-of-sql/src/base/database/table_ref.rs @@ -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 { + 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 { + self.object_name.0.get(1).cloned() } /// Returns the underlying resource id of the table @@ -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) diff --git a/crates/proof-of-sql/src/base/database/table_utility.rs b/crates/proof-of-sql/src/base/database/table_utility.rs index 25458bac3..64cde69a9 100644 --- a/crates/proof-of-sql/src/base/database/table_utility.rs +++ b/crates/proof-of-sql/src/base/database/table_utility.rs @@ -139,8 +139,7 @@ pub fn borrowed_int( /// use bumpalo::Bump; /// use proof_of_sql::base::{database::table_utility::*, scalar::Curve25519Scalar}; /// let alloc = Bump::new(); -/// let result = table::([ -/// borrowed_bigint("a", [1, 2, 3], &alloc), +/// let result = table::([/// borrowed_bigint("a", [1, 2, 3], &alloc), /// ]); /// ``` diff --git a/crates/proof-of-sql/src/evm_compatibility/serialize_query_expr.rs b/crates/proof-of-sql/src/evm_compatibility/serialize_query_expr.rs index 5a97ed2d2..a6ebb6578 100644 --- a/crates/proof-of-sql/src/evm_compatibility/serialize_query_expr.rs +++ b/crates/proof-of-sql/src/evm_compatibility/serialize_query_expr.rs @@ -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( 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 18a17f2e5..cc36cfb8f 100644 --- a/crates/proof-of-sql/src/sql/proof/query_proof.rs +++ b/crates/proof-of-sql/src/sql/proof/query_proof.rs @@ -357,7 +357,7 @@ impl QueryProof { ); let one_eval_map: IndexMap = 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, 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 317f6d4ec..e3ccf581c 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 @@ -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 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 7cebedb0c..7c35c0294 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 @@ -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 { diff --git a/crates/proof-of-sql/src/sql/proof_exprs/test_utility.rs b/crates/proof-of-sql/src/sql/proof_exprs/test_utility.rs index baa8b44e5..9016423f4 100644 --- a/crates/proof-of-sql/src/sql/proof_exprs/test_utility.rs +++ b/crates/proof-of-sql/src/sql/proof_exprs/test_utility.rs @@ -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>(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>( + 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 @@ -125,8 +131,10 @@ pub fn const_decimal75>(precision: u8, scale: i8, val: T) -> DynPr )) } -pub fn tab(tab: TableRef) -> TableExpr { - TableExpr { table_ref: tab } +pub fn tab>(tab: S) -> TableExpr { + TableExpr { + table_ref: tab.into(), + } } /// # Panics @@ -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>( + 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(), } } @@ -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>( + 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>( + tab: S, names: &[(&str, &str)], accessor: &impl SchemaAccessor, ) -> Vec { + 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>( + tab: S, names: &[&str], accessor: &impl SchemaAccessor, ) -> Vec { + 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>( + 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 { +pub fn cols_expr>( + tab: S, + names: &[&str], + accessor: &impl SchemaAccessor, +) -> Vec { + let tab = tab.into(); names .iter() - .map(|name| col_expr(tab, name, accessor)) + .map(|name| col_expr(tab.clone(), name, accessor)) .collect() } 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 4bbfabe2b..f677e3def 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 @@ -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::>();