Skip to content
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
41 changes: 24 additions & 17 deletions crates/executor/src/operators/aggregate/columnar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
use super::{kahan_inc, percentile_cont, quantile, Reduction};
use crate::operators::common::{row_chunks, Workspace};
use crate::operators::group_table::GroupTable;
use crate::operators::{Kind, Operator};
use crate::runtime::{Cooperative, Input, RunContext};
use crate::values::arrow_columns::{self, ColumnDepth};
use crate::values::{Batch, SchemaRef, Value};
use crate::{expressions::Expression, Error};
use crate::Error;
use ahash::{AHashMap as HashMap, AHashSet as HashSet};
use arrow::array::{Array, ArrayRef, BooleanArray, Float64Array, Int64Array};
use futures::StreamExt;
Expand Down Expand Up @@ -491,7 +492,6 @@ fn finish_measure(
states: MeasureStates,
measure: &Reduction,
dtype: &DataType,
promql: bool,
order: &[u32],
) -> Result<ArrayRef, Error> {
let values: Vec<Value> = match states {
Expand All @@ -503,7 +503,7 @@ fn finish_measure(
.iter()
.map(|&g| {
let (sum, c, seen) = sums[g as usize];
if !seen && !promql {
if !seen {
Value::Null
} else if sum.is_infinite() {
Value::Float64(sum)
Expand All @@ -516,7 +516,7 @@ fn finish_measure(
.iter()
.map(|&g| {
let average = averages[g as usize];
if average.count == 0 && !promql {
if average.count == 0 {
Value::Null
} else {
Value::Float64(average.value())
Expand All @@ -527,7 +527,7 @@ fn finish_measure(
.iter()
.map(|&g| {
let (sum, count) = sums[g as usize];
if count == 0 && !promql {
if count == 0 {
Ok(Value::Null)
} else if matches!(measure, Reduction::Avg(_)) {
Ok(Value::Float64(sum as f64 / count as f64))
Expand Down Expand Up @@ -616,7 +616,6 @@ fn finish_measure(
};
match value {
Some(value) => Value::Float64(value),
None if promql => Value::Float64(f64::NAN),
None => Value::Null,
}
})
Expand All @@ -639,18 +638,25 @@ fn finish_measure(
)
}

/// The exact `Aggregate`: the groups of `groups` in key order, each with
/// its `measures` (each over the rows its filter keeps).
/// The exact `Aggregate` `operator`: the groups of its `groups` in key
/// order, each with its `measures` (each over the rows its filter keeps).
/// With `emit_empty_global` and no groups, an empty input still has the
/// global group (SQL).
pub(super) async fn aggregate(
mut input: Input<'_, Batch>,
groups: &[usize],
measures: &[Reduction],
filters: &[Option<Expression>],
input_schema: &SchemaRef,
output: &SchemaRef,
operator: &Operator,
context: &RunContext,
) -> Result<Batch, Error> {
let promql = input_schema.has_promql_series_identity();
let Kind::Aggregate {
groups,
measures,
filters,
emit_empty_global,
} = &operator.kind
else {
unreachable!("an aggregate's run")
};
let (input_schema, output) = (&operator.inputs[0], &operator.output);
// `now()` in a filter is this run's evaluation time.
let filters: Vec<Option<_>> = filters
.iter()
Expand Down Expand Up @@ -715,8 +721,9 @@ pub(super) async fn aggregate(
workspace.set_bytes(workspace_bytes + table.memory_bytes())?;
}
}
// PromQL sums over an empty vector emit no sample.
if table.len() == 0 && groups.is_empty() && !promql {
// A global SQL aggregate has one row over no input rows; a PromQL
// aggregate over an empty vector emits no sample.
if table.len() == 0 && groups.is_empty() && *emit_empty_global {
table.add_empty_group()?;
for state in &mut states {
state.ensure_groups(1);
Expand All @@ -732,7 +739,7 @@ pub(super) async fn aggregate(
let FieldDataType::Plain(dtype) = &field.dtype else {
return Err(invalid("aggregate measures are plain"));
};
arrays.push(finish_measure(state, measure, dtype, promql, &order)?);
arrays.push(finish_measure(state, measure, dtype, &order)?);
}
Batch::from_parts(
output.clone(),
Expand Down
79 changes: 35 additions & 44 deletions crates/executor/src/operators/aggregate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ impl Operator {
}
(DataType::Float64, false)
}
// NULL for a group of NULLs, as SQL `SUM` (and for a
// global one over no row: `with_emit_empty_global`).
Reduction::Sum(i) | Reduction::Avg(i) => {
let (t, _) = plain(&input, *i)?;
let (t, nullable) = plain(&input, *i)?;
if !matches!(t, DataType::Int64 | DataType::Float64) {
return Err(invalid("numeric aggregate input required"));
}
Expand All @@ -46,8 +48,7 @@ impl Operator {
} else {
t.clone()
},
!input.has_promql_series_identity()
&& (groups.is_empty() || plain(&input, *i)?.1),
nullable,
)
}
Reduction::Quantile { column, .. } => {
Expand All @@ -63,35 +64,31 @@ impl Operator {
"a percentile requires Float64 input and a fraction in [0, 1]",
));
}
// NULL for a group with no value, as SQL `SUM`: a global
// one over no row, or a group of NULLs.
(
DataType::Float64,
groups.is_empty() || plain(&input, *column)?.1,
)
// NULL for a group of NULLs, as SQL `SUM` (and for a
// global one over no row: `with_emit_empty_global`).
(DataType::Float64, plain(&input, *column)?.1)
}
Reduction::Min(i) | Reduction::Max(i) => {
let (t, nullable) = plain(&input, *i)?;
if !ordered(t) {
return Err(invalid("ordered aggregate input required"));
}
(t.clone(), nullable || groups.is_empty())
(t.clone(), nullable)
}
Reduction::StdDev { column, population }
| Reduction::Variance { column, population } => {
let (t, nullable) = plain(&input, *column)?;
if !matches!(t, DataType::Int64 | DataType::Float64) {
return Err(invalid("numeric aggregate input required"));
}
// SQL: NULL for fewer than two values (sample) or none
// (population); a PromQL group always has a value. The
// IR's aggregate schema defines this nullability; this
// NULL for a group of NULLs and, as SQL's sample
// `STDDEV`/`VAR_SAMP`, for a group of one value (and for
// a global one over no row: `with_emit_empty_global`).
// PromQL's `stddev`/`stdvar` are population ones over
// groups that have a value, so never NULL. The IR's
// aggregate schema defines this nullability; this
// derives it again to check the declared output.
(
DataType::Float64,
!input.has_promql_series_identity()
&& (!population || groups.is_empty() || nullable),
)
(DataType::Float64, !population || nullable)
}
Reduction::PearsonCorr { left, right } => {
for column in [left, right] {
Expand All @@ -113,6 +110,7 @@ impl Operator {
groups,
measures: measures.into_iter().map(|(_, r)| r).collect(),
filters: vec![],
emit_empty_global: false,
},
inputs: vec![input],
output: schema(fields),
Expand All @@ -128,6 +126,7 @@ impl Operator {
filters: slot,
measures,
groups,
..
} = &mut self.kind
else {
return Err(invalid("measure filters need an aggregate"));
Expand All @@ -151,16 +150,7 @@ impl Operator {
if filter.dtype(&self.inputs[0])?.0 != DataType::Bool {
return Err(invalid("measure filter must be boolean"));
}
if matches!(
measures[index],
Reduction::Sum(_)
| Reduction::Avg(_)
| Reduction::Min(_)
| Reduction::Max(_)
| Reduction::Percentile { .. }
| Reduction::StdDev { .. }
| Reduction::Variance { .. }
) {
if measures[index].is_null_without_values() {
output.fields[groups.len() + index].nullable = true;
}
}
Expand Down Expand Up @@ -293,6 +283,22 @@ pub enum Reduction {
/// PromQL `group`: 1 for every group.
Group,
}
impl Reduction {
/// Whether the measure is NULL for a group with no (matching) value, as
/// SQL `SELECT sum(x) FROM t WHERE false` is NULL while `count(*)` is 0.
pub(super) fn is_null_without_values(&self) -> bool {
matches!(
self,
Reduction::Sum(_)
| Reduction::Avg(_)
| Reduction::Min(_)
| Reduction::Max(_)
| Reduction::Percentile { .. }
| Reduction::StdDev { .. }
| Reduction::Variance { .. }
)
}
}
pub(super) fn execute<'a>(
operator: &'a Operator,
mut inputs: Vec<Input<'a, Batch>>,
Expand Down Expand Up @@ -333,22 +339,7 @@ pub(super) fn execute<'a>(
results.len(),
)
}
Kind::Aggregate {
groups,
measures,
filters,
} => {
columnar::aggregate(
input,
groups,
measures,
filters,
&operator.inputs[0],
&output,
&context,
)
.await
}
Kind::Aggregate { .. } => columnar::aggregate(input, operator, &context).await,
_ => unreachable!(),
}
})
Expand Down
76 changes: 76 additions & 0 deletions crates/executor/src/operators/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,12 @@ enum Kind {
/// Per-measure row filters (SQL `FILTER (WHERE …)`); empty when none.
#[serde(default, skip_serializing_if = "Vec::is_empty")]
filters: Vec<Option<Expression>>,
/// Whether an empty input still has the global group, as a SQL
/// `SELECT count(*) FROM t WHERE false` has one row (0); a PromQL
/// `count(v)` over an empty vector has no sample. Only an aggregate
/// with no groups; `false` (no output row) by default.
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
emit_empty_global: bool,
},
SemiJoin {
keys: Vec<(usize, usize)>,
Expand All @@ -179,6 +185,11 @@ enum Kind {
/// Rows for which this is not true update no state; their group is kept.
#[serde(default, skip_serializing_if = "Option::is_none")]
filter: Option<Box<Expression>>,
/// Whether an empty input still has the global group, with an empty
/// state, as a global SQL aggregate has one row over no input rows.
/// Only a build with no groups; `false` (no output row) by default.
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
emit_empty_global: bool,
},
/// One keyed state per group whose items take a row weight, e.g. a CMS
/// with heap (built by [`Operator::per_group_keyed_summary_build`]).
Expand Down Expand Up @@ -352,6 +363,71 @@ impl Operator {
pub fn schema(&self) -> SchemaRef {
self.output.clone()
}

/// An aggregate, per-group build or merge into no groups follows SQL's
/// rule for an empty input when `emit_empty_global`: its one global
/// group still has a row, as `SELECT count(*) FROM t WHERE false`
/// answers 0 and `SELECT sum(x) FROM t WHERE false` NULL. Otherwise (the
/// default; `false` leaves the operator as it is) an empty input has no
/// row, as a PromQL `count(v)` over an empty vector has no sample.
///
/// An `Aggregate`'s SUM, AVG, MIN, MAX, percentile, standard deviation
/// and variance measures become nullable (NULL over no row). A `PerGroupSummaryBuild` outputs an empty
/// state of its own θ, and a `SummaryMerge` one of `parameters`: the θ
/// of the states it merges (the exported build node's `parameters`),
/// `None` for an exact family and for the other two.
pub fn with_emit_empty_global(
mut self,
emit_empty_global: bool,
parameters: Option<SketchParams>,
) -> Result<Self, Error> {
if !emit_empty_global {
return Ok(self);
}
match &mut self.kind {
Kind::Aggregate {
groups,
measures,
emit_empty_global,
..
} if groups.is_empty() && parameters.is_none() => {
let mut output = (*self.output).clone();
for (field, measure) in output.fields.iter_mut().zip(measures.iter()) {
if measure.is_null_without_values() {
field.nullable = true;
}
}
*emit_empty_global = true;
self.output = Arc::new(output);
}
Kind::PerGroupSummaryBuild {
groups,
emit_empty_global,
..
} if groups.is_empty() && parameters.is_none() => *emit_empty_global = true,
Kind::SummaryMerge {
state,
groups,
emit_empty_global,
parameters: slot,
} if groups.is_empty() => {
// An empty state of this family and θ can be built.
crate::StateColumnUpdater::new(
&self.inputs[0].fields[*state].dtype,
parameters.as_ref(),
)?;
*emit_empty_global = true;
*slot = parameters;
}
_ => {
return Err(invalid(
"an empty global row needs an aggregate, build or merge into no groups \
(only a merge takes the θ of its empty state)",
))
}
}
Ok(self)
}
}
impl PhysicalOperator<Batch, SchemaRef> for Operator {
fn requires_bounded_input(&self) -> bool {
Expand Down
Loading
Loading