Skip to content

Commit c683591

Browse files
committed
save
1 parent a955d50 commit c683591

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

pgdog/src/backend/pool/connection/aggregate.rs

+20
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ impl Grouping {
3030
}
3131

3232
/// The aggregate accumulator.
33+
///
34+
/// This transfors disttributed aggregate functions
35+
/// into a single value.
3336
#[derive(Debug)]
3437
struct Accumulator<'a> {
3538
target: &'a AggregateTarget,
@@ -54,6 +57,7 @@ impl<'a> Accumulator<'a> {
5457
.collect()
5558
}
5659

60+
/// Transform COUNT(*), MIN, MAX, etc., from multiple shards into a single value.
5761
fn accumulate(&mut self, row: &DataRow, rd: &RowDescription) -> Result<(), Error> {
5862
let column = row.get_column(self.target.column(), rd)?;
5963
if let Some(column) = column {
@@ -77,6 +81,13 @@ impl<'a> Accumulator<'a> {
7781
self.datum = column.value;
7882
}
7983
}
84+
AggregateFunction::Sum => {
85+
if !self.datum.is_null() {
86+
self.datum = self.datum.clone() + column.value;
87+
} else {
88+
self.datum = column.value;
89+
}
90+
}
8091
_ => (),
8192
}
8293
}
@@ -122,6 +133,15 @@ impl<'a> Aggregates<'a> {
122133

123134
let mut rows = VecDeque::new();
124135
for (grouping, accumulator) in self.mappings {
136+
//
137+
// Aggregate rules in Postgres dictate that the only
138+
// columns present in the row are either:
139+
//
140+
// 1. part of the GROUP BY, which means they are
141+
// stored in the grouping
142+
// 2. are aggregate functions, which means they
143+
// are stored in the accunmulator
144+
//
125145
let mut row = DataRow::new();
126146
for (idx, datum) in grouping.columns {
127147
row.insert(idx, datum);

pgdog/src/frontend/router/parser/aggregate.rs

+6
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ pub enum AggregateFunction {
2626
Max,
2727
Min,
2828
Avg,
29+
Sum,
2930
}
3031

3132
#[derive(Debug, Clone, PartialEq, Default)]
@@ -82,6 +83,11 @@ impl Aggregate {
8283
});
8384
}
8485

86+
"sum" => targets.push(AggregateTarget {
87+
column: idx,
88+
function: AggregateFunction::Max,
89+
}),
90+
8591
_ => {}
8692
}
8793
}

0 commit comments

Comments
 (0)