-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# Rationale for this change We need to add `ProjectionExpr` both as a faster and simpler alternative to `DenseFilterExpr` when there is no `WHERE` clause and to provide one mechanism to further generalize `GROUP BY` i.e. adding a `ProjectionExpr` after `GroupByExpr` without changing `GroupByExpr` itself. <!-- Why are you proposing this change? If this is already explained clearly in the linked Jira ticket then this section is not needed. Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes. --> # What changes are included in this PR? Add `ProjectionExpr`. <!-- There is no need to duplicate the description in the ticket here but it is sometimes worth providing a summary of the individual changes in this PR. --> # Are these changes tested? Yes. <!-- We typically require tests for all PRs in order to: 1. Prevent the code from being accidentally broken by subsequent changes 2. Serve as another way to document the expected behavior of the code If tests are not included in your PR, please explain why (for example, are they covered by existing tests)? -->
- Loading branch information
Showing
7 changed files
with
507 additions
and
3 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
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
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,125 @@ | ||
use super::{AliasedProvableExprPlan, ProvableExpr, TableExpr}; | ||
use crate::{ | ||
base::{ | ||
commitment::Commitment, | ||
database::{ | ||
ColumnField, ColumnRef, CommitmentAccessor, DataAccessor, MetadataAccessor, OwnedTable, | ||
}, | ||
proof::ProofError, | ||
}, | ||
sql::proof::{ | ||
CountBuilder, Indexes, ProofBuilder, ProofExpr, ProverEvaluate, ResultBuilder, | ||
VerificationBuilder, | ||
}, | ||
}; | ||
use bumpalo::Bump; | ||
use core::iter::repeat_with; | ||
use serde::{Deserialize, Serialize}; | ||
use std::collections::HashSet; | ||
|
||
/// Provable expressions for queries of the form | ||
/// ```ignore | ||
/// SELECT <result_expr1>, ..., <result_exprN> FROM <table> | ||
/// ``` | ||
#[derive(Debug, PartialEq, Serialize, Deserialize)] | ||
pub struct ProjectionExpr<C: Commitment> { | ||
pub(super) aliased_results: Vec<AliasedProvableExprPlan<C>>, | ||
pub(super) table: TableExpr, | ||
} | ||
|
||
impl<C: Commitment> ProjectionExpr<C> { | ||
/// Creates a new projection expression. | ||
pub fn new(aliased_results: Vec<AliasedProvableExprPlan<C>>, table: TableExpr) -> Self { | ||
Self { | ||
aliased_results, | ||
table, | ||
} | ||
} | ||
} | ||
|
||
impl<C: Commitment> ProofExpr<C> for ProjectionExpr<C> { | ||
fn count( | ||
&self, | ||
builder: &mut CountBuilder, | ||
_accessor: &dyn MetadataAccessor, | ||
) -> Result<(), ProofError> { | ||
for aliased_expr in self.aliased_results.iter() { | ||
aliased_expr.expr.count(builder)?; | ||
builder.count_result_columns(1); | ||
} | ||
Ok(()) | ||
} | ||
|
||
fn get_length(&self, accessor: &dyn MetadataAccessor) -> usize { | ||
accessor.get_length(self.table.table_ref) | ||
} | ||
|
||
fn get_offset(&self, accessor: &dyn MetadataAccessor) -> usize { | ||
accessor.get_offset(self.table.table_ref) | ||
} | ||
|
||
#[allow(unused_variables)] | ||
fn verifier_evaluate( | ||
&self, | ||
builder: &mut VerificationBuilder<C>, | ||
accessor: &dyn CommitmentAccessor<C>, | ||
_result: Option<&OwnedTable<C::Scalar>>, | ||
) -> Result<(), ProofError> { | ||
self.aliased_results | ||
.iter() | ||
.map(|aliased_expr| aliased_expr.expr.verifier_evaluate(builder, accessor)) | ||
.collect::<Result<Vec<_>, _>>()?; | ||
let _columns_evals = Vec::from_iter( | ||
repeat_with(|| builder.consume_result_mle()).take(self.aliased_results.len()), | ||
); | ||
Ok(()) | ||
} | ||
|
||
fn get_column_result_fields(&self) -> Vec<ColumnField> { | ||
self.aliased_results | ||
.iter() | ||
.map(|aliased_expr| ColumnField::new(aliased_expr.alias, aliased_expr.expr.data_type())) | ||
.collect() | ||
} | ||
|
||
fn get_column_references(&self) -> HashSet<ColumnRef> { | ||
let mut columns = HashSet::new(); | ||
self.aliased_results.iter().for_each(|aliased_expr| { | ||
aliased_expr.expr.get_column_references(&mut columns); | ||
}); | ||
columns | ||
} | ||
} | ||
|
||
impl<C: Commitment> ProverEvaluate<C::Scalar> for ProjectionExpr<C> { | ||
#[tracing::instrument(name = "ProjectionExpr::result_evaluate", level = "debug", skip_all)] | ||
fn result_evaluate<'a>( | ||
&self, | ||
builder: &mut ResultBuilder<'a>, | ||
alloc: &'a Bump, | ||
accessor: &'a dyn DataAccessor<C::Scalar>, | ||
) { | ||
let columns = Vec::from_iter(self.aliased_results.iter().map(|aliased_expr| { | ||
aliased_expr | ||
.expr | ||
.result_evaluate(builder.table_length(), alloc, accessor) | ||
})); | ||
builder.set_result_indexes(Indexes::Dense(0..(builder.table_length() as u64))); | ||
for col in columns { | ||
builder.produce_result_column(col); | ||
} | ||
} | ||
|
||
#[tracing::instrument(name = "ProjectionExpr::prover_evaluate", level = "debug", skip_all)] | ||
#[allow(unused_variables)] | ||
fn prover_evaluate<'a>( | ||
&self, | ||
builder: &mut ProofBuilder<'a, C::Scalar>, | ||
alloc: &'a Bump, | ||
accessor: &'a dyn DataAccessor<C::Scalar>, | ||
) { | ||
self.aliased_results.iter().for_each(|aliased_expr| { | ||
aliased_expr.expr.prover_evaluate(builder, alloc, accessor); | ||
}); | ||
} | ||
} |
Oops, something went wrong.