Skip to content

Commit

Permalink
Merge pull request #2884 from finos/min-max-datetime
Browse files Browse the repository at this point in the history
Add `min` and `max` aggregates for `date` and `datetime` columns
  • Loading branch information
texodus authored Dec 21, 2024
2 parents 5c864a0 + bcccadf commit 2373396
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 7 deletions.
12 changes: 6 additions & 6 deletions cpp/perspective/src/cpp/sparse_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1544,31 +1544,31 @@ t_stree::update_agg_table(
t_tscalar dst_scalar = dst->get_scalar(dst_ridx);
old_value.set(dst_scalar);
auto pkeys = get_pkeys(nidx);
std::vector<double> values;
std::vector<t_tscalar> values;
read_column_from_gstate(
gstate,
expression_master_table,
spec.get_dependencies()[0].name(),
pkeys,
values,
true
values
);

new_value.set(*std::max_element(values.begin(), values.end()));
dst->set_scalar(dst_ridx, new_value);
} break;
case AGGTYPE_MIN: {
t_tscalar dst_scalar = dst->get_scalar(dst_ridx);
old_value.set(dst_scalar);
auto pkeys = get_pkeys(nidx);
std::vector<double> values;
std::vector<t_tscalar> values;
read_column_from_gstate(
gstate,
expression_master_table,
spec.get_dependencies()[0].name(),
pkeys,
values,
true
values
);

new_value.set(*std::min_element(values.begin(), values.end()));
dst->set_scalar(dst_ridx, new_value);
} break;
Expand Down
24 changes: 23 additions & 1 deletion rust/perspective-client/src/rust/config/aggregates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,10 +272,32 @@ const NUMBER_AGGREGATES: &[SingleAggregate] = &[
SingleAggregate::Var,
];

const DATETIME_AGGREGATES: &[SingleAggregate] = &[
SingleAggregate::Any,
SingleAggregate::Avg,
SingleAggregate::Count,
SingleAggregate::DistinctCount,
SingleAggregate::Dominant,
SingleAggregate::First,
SingleAggregate::High,
SingleAggregate::Low,
SingleAggregate::Max,
SingleAggregate::Min,
SingleAggregate::LastByIndex,
SingleAggregate::Last,
SingleAggregate::Median,
SingleAggregate::Unique,
];

impl proto::ColumnType {
pub fn aggregates_iter(&self) -> Box<dyn Iterator<Item = Aggregate>> {
match self {
Self::Boolean | Self::Date | Self::Datetime | Self::String => Box::new(
Self::Date | Self::Datetime => Box::new(
DATETIME_AGGREGATES
.iter()
.map(|x| Aggregate::SingleAggregate(*x)),
),
Self::Boolean | Self::String => Box::new(
STRING_AGGREGATES
.iter()
.map(|x| Aggregate::SingleAggregate(*x)),
Expand Down
74 changes: 74 additions & 0 deletions rust/perspective-js/test/js/pivots.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,80 @@ const std = (nums) => {
await table.delete();
});

test("min", async () => {
const data = {
w: [1.5, 2.5, 3.5, 4.5],
x: [1, 2, 3, 4],
y: ["a", "b", "a", "b"],
z: [
new Date(1555126035065),
new Date(1555126035065),
new Date(1555026035065),
new Date(1555026035065),
],
};

const table = await perspective.table(data);
const view = await table.view({
aggregates: {
w: "min",
x: "min",
y: "min",
z: "min",
},
group_by: ["y"],
});

const cols = await view.to_columns();
expect(cols).toEqual({
__ROW_PATH__: [[], ["a"], ["b"]],
w: [1.5, 1.5, 2.5],
x: [1, 1, 2],
y: ["a", "a", "b"],
z: [1555026035065, 1555026035065, 1555026035065],
});

await view.delete();
await table.delete();
});

test("max", async () => {
const data = {
w: [1.5, 2.5, 3.5, 4.5],
x: [1, 2, 3, 4],
y: ["a", "b", "a", "b"],
z: [
new Date(1555126035065),
new Date(1555126035065),
new Date(1555026035065),
new Date(1555026035065),
],
};

const table = await perspective.table(data);
const view = await table.view({
aggregates: {
w: "max",
x: "max",
y: "max",
z: "max",
},
group_by: ["y"],
});

const cols = await view.to_columns();
expect(cols).toEqual({
__ROW_PATH__: [[], ["a"], ["b"]],
w: [4.5, 3.5, 4.5],
x: [4, 3, 4],
y: ["b", "a", "b"],
z: [1555126035065, 1555126035065, 1555126035065],
});

await view.delete();
await table.delete();
});

test("high with count and partial update", async () => {
const table = await perspective.table(
{
Expand Down

0 comments on commit 2373396

Please sign in to comment.