Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experimental support for AVG/SUM aggregated queries operators #105

Merged
merged 1 commit into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ path = "src/lib.rs"

[dependencies]
tracing = "0.1"
gcloud-sdk = { version = "0.20", features = ["google-firestore-v1"] }
gcloud-sdk = { version = "0.20.5", features = ["google-firestore-v1"] }
tonic = { version = "0.9", features = ["tls"] }
hyper = { version ="0.14" }
struct-path = "0.2"
Expand Down
10 changes: 9 additions & 1 deletion examples/aggregated-query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ pub fn config_env_var(name: &str) -> Result<String, String> {
#[derive(Debug, Clone, Deserialize, Serialize)]
struct MyAggTestStructure {
counter: usize,
calc_sum: Option<usize>,
calc_avg: Option<usize>,
}

#[tokio::main]
Expand All @@ -30,7 +32,13 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
.fluent()
.select()
.from(TEST_COLLECTION_NAME)
.aggregate(|a| a.fields([a.field(path!(MyAggTestStructure::counter)).count()]))
.aggregate(|a| {
a.fields([
a.field(path!(MyAggTestStructure::counter)).count(),
//a.field(path!(MyAggTestStructure::calc_sum)).sum("some_num"), // Not supported/enabled yet by Firestore, exists only on proto level
//a.field(path!(MyAggTestStructure::calc_avg)).avg("some_num"),
])
})
.obj()
.query()
.await?;
Expand Down
42 changes: 40 additions & 2 deletions src/db/aggregated_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,21 @@ impl From<&FirestoreAggregation> for structured_aggregation_query::Aggregation {
#[derive(Debug, PartialEq, Clone)]
pub enum FirestoreAggregationOperator {
Count(FirestoreAggregationOperatorCount),
Sum(FirestoreAggregationOperatorSum),
Avg(FirestoreAggregationOperatorAvg),
}

impl From<&FirestoreAggregationOperator> for structured_aggregation_query::aggregation::Operator {
fn from(op: &FirestoreAggregationOperator) -> Self {
match op {
FirestoreAggregationOperator::Count(cnt) => {
structured_aggregation_query::aggregation::Operator::Count(cnt.into())
FirestoreAggregationOperator::Count(opts) => {
structured_aggregation_query::aggregation::Operator::Count(opts.into())
}
FirestoreAggregationOperator::Sum(opts) => {
structured_aggregation_query::aggregation::Operator::Sum(opts.into())
}
FirestoreAggregationOperator::Avg(opts) => {
structured_aggregation_query::aggregation::Operator::Avg(opts.into())
}
}
}
Expand All @@ -63,6 +71,36 @@ impl From<&FirestoreAggregationOperatorCount> for structured_aggregation_query::
}
}

#[derive(Debug, PartialEq, Clone, Builder)]
pub struct FirestoreAggregationOperatorSum {
pub field_name: String,
}

impl From<&FirestoreAggregationOperatorSum> for structured_aggregation_query::aggregation::Sum {
fn from(operator: &FirestoreAggregationOperatorSum) -> Self {
structured_aggregation_query::aggregation::Sum {
field: Some(structured_query::FieldReference {
field_path: operator.field_name.clone(),
}),
}
}
}

#[derive(Debug, PartialEq, Clone, Builder)]
pub struct FirestoreAggregationOperatorAvg {
pub field_name: String,
}

impl From<&FirestoreAggregationOperatorAvg> for structured_aggregation_query::aggregation::Avg {
fn from(operator: &FirestoreAggregationOperatorAvg) -> Self {
structured_aggregation_query::aggregation::Avg {
field: Some(structured_query::FieldReference {
field_path: operator.field_name.clone(),
}),
}
}
}

#[async_trait]
pub trait FirestoreAggregatedQuerySupport {
async fn aggregated_query_doc(
Expand Down
27 changes: 26 additions & 1 deletion src/fluent_api/select_aggregation_builder.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{
FirestoreAggregation, FirestoreAggregationOperator, FirestoreAggregationOperatorCount,
FirestoreAggregation, FirestoreAggregationOperator, FirestoreAggregationOperatorAvg,
FirestoreAggregationOperatorCount, FirestoreAggregationOperatorSum,
};

pub struct FirestoreAggregationBuilder {}
Expand Down Expand Up @@ -58,6 +59,30 @@ impl FirestoreAggregationFieldExpr {
),
))
}

#[inline]
pub fn sum<S>(self, sum_on_field_name: S) -> Option<FirestoreAggregation>
where
S: AsRef<str>,
{
Some(FirestoreAggregation::new(self.field_name).with_operator(
FirestoreAggregationOperator::Sum(FirestoreAggregationOperatorSum::new(
sum_on_field_name.as_ref().to_string(),
)),
))
}

#[inline]
pub fn avg<S>(self, avg_on_field_name: S) -> Option<FirestoreAggregation>
where
S: AsRef<str>,
{
Some(FirestoreAggregation::new(self.field_name).with_operator(
FirestoreAggregationOperator::Avg(FirestoreAggregationOperatorAvg::new(
avg_on_field_name.as_ref().to_string(),
)),
))
}
}

impl FirestoreAggregationExpr for FirestoreAggregation {
Expand Down