mirror of
https://github.com/GreptimeTeam/greptimedb.git
synced 2026-11-17 00:07:00 +00:00
feat(promql): run count_values in the native select node
Signed-off-by: Dennis Zhuang <killme2008@gmail.com>
This commit is contained in:
@@ -127,6 +127,9 @@ fn describe(node: &NativeNode) -> String {
|
||||
BoundSelect::Histogram(HistogramFunction::Fraction { lower, upper }) => {
|
||||
format!("histogram_fraction {lower} {upper}")
|
||||
}
|
||||
BoundSelect::CountValues { label, group } => {
|
||||
format!("count_values {label:?} {}", grouping(group))
|
||||
}
|
||||
},
|
||||
NativeExpr::Aggregate { op, .. } => format!(
|
||||
"{} {} {}",
|
||||
|
||||
@@ -445,6 +445,13 @@ fn a_selection_names_its_binding() {
|
||||
},
|
||||
r#" root Select absent {job="x"}"#,
|
||||
),
|
||||
(
|
||||
BoundSelect::CountValues {
|
||||
label: "value".into(),
|
||||
group: Grouping::Without(Arc::new(["job".into()])),
|
||||
},
|
||||
r#" root Select count_values "value" without [job]"#,
|
||||
),
|
||||
];
|
||||
for (op, line) in lines {
|
||||
let tree = NativeTree::try_new(NativeNode {
|
||||
|
||||
@@ -91,6 +91,7 @@ use datatypes::series::samples::{
|
||||
use datatypes::series::time::{EvalGrid, GridRange, GridSpan, GridSpec, TimeAxis};
|
||||
use datatypes::series::{MetricName, SeriesOrdering};
|
||||
|
||||
use crate::aggregate;
|
||||
use crate::grid_series::{
|
||||
GridSample, GridSeriesBuilder, LabelSet, label_set, merge_same_labelset, owned,
|
||||
};
|
||||
@@ -1884,6 +1885,19 @@ fn select_series(
|
||||
BoundSelect::Histogram(function) => {
|
||||
evaluate_histograms(*function, schema, &merge_same_labelset(schema, series)?)
|
||||
}
|
||||
BoundSelect::CountValues { label, group } => {
|
||||
let (by, labels) = match group {
|
||||
Grouping::By(labels) => (true, labels),
|
||||
Grouping::Without(labels) => (false, labels),
|
||||
};
|
||||
let labels: Vec<&str> = labels.iter().map(|label| &**label).collect();
|
||||
let grouping = if by {
|
||||
aggregate::Grouping::By(&labels)
|
||||
} else {
|
||||
aggregate::Grouping::Without(&labels)
|
||||
};
|
||||
aggregate::count_values(schema, &series, label, grouping)
|
||||
}
|
||||
BoundSelect::Absent { labels } => Ok(select::absent(schema, &series, labels.clone())?
|
||||
.into_iter()
|
||||
.collect()),
|
||||
@@ -1902,6 +1916,9 @@ fn select_series(
|
||||
/// - A sort: the value each series is keyed by, and the floats `sort` and `sort_desc` rebuild a
|
||||
/// series into when it holds histograms; the label sorts move the series they were given.
|
||||
/// - `absent`: a presence flag and a result value per grid point and field.
|
||||
/// - `count_values`, per row, since a gauge with distinct values makes about one group per sample:
|
||||
/// the group's label set twice (its metric and its key) with the value written into it, its
|
||||
/// count per field, and the result series it becomes.
|
||||
/// - `histogram_quantile` and `histogram_fraction`: the merge of label-set pieces before the kernel
|
||||
/// ([`merge_workspace_bytes`]); per series, its group entry and label pairs and a cursor per
|
||||
/// field; per row, the bucket it contributes and a result value per field.
|
||||
@@ -1949,6 +1966,20 @@ fn selection_workspace_bytes(op: &BoundSelect, schema: &SeriesSchema, series: &[
|
||||
},
|
||||
);
|
||||
}
|
||||
BoundSelect::CountValues { .. } => {
|
||||
let label = std::mem::size_of::<(Arc<str>, Arc<str>)>();
|
||||
// The value label at the length of a 17-digit value with sign, point and exponent;
|
||||
// the `'f'` form writes a large magnitude out in full, which this does not follow.
|
||||
let value = 24;
|
||||
let group = std::mem::size_of::<Vec<f64>>()
|
||||
+ fields * std::mem::size_of::<f64>()
|
||||
+ std::mem::size_of::<Series>()
|
||||
+ fields * std::mem::size_of::<Samples>();
|
||||
return series.iter().fold(0usize, |total, item| {
|
||||
let labels = 2 * (item.metric.tags().len() + 2) * label + value;
|
||||
total.saturating_add(item.time.len().saturating_mul(group + labels))
|
||||
});
|
||||
}
|
||||
BoundSelect::TopK { .. } | BoundSelect::LimitK { .. } | BoundSelect::LimitRatio { .. } => {}
|
||||
}
|
||||
let row = std::mem::size_of::<(usize, usize)>()
|
||||
@@ -2973,12 +3004,14 @@ impl NativeExecutionState {
|
||||
// set stay pieces: they hold disjoint points, so ranking them apart picks
|
||||
// what ranking the merged series would, and what is above treats them as
|
||||
// it treats a map's. `absent` answers with one series under the labels it
|
||||
// was bound to, and the histogram functions merge the label sets they
|
||||
// answer with, so neither hands over pieces.
|
||||
// was bound to, and the histogram functions and `count_values` answer with
|
||||
// one series per label set they make, so none of them hands over pieces.
|
||||
let whole = matches!(
|
||||
expr,
|
||||
NativeExpr::Select {
|
||||
op: BoundSelect::Absent { .. } | BoundSelect::Histogram(_),
|
||||
op: BoundSelect::Absent { .. }
|
||||
| BoundSelect::Histogram(_)
|
||||
| BoundSelect::CountValues { .. },
|
||||
..
|
||||
}
|
||||
);
|
||||
|
||||
@@ -6805,3 +6805,53 @@ fn histogram_quantile_reads_the_pieces_of_one_label_set_as_one_series() {
|
||||
.collect();
|
||||
assert_eq!(positions, [0, 1]);
|
||||
}
|
||||
|
||||
/// `count_values` counts the samples that share a value across the whole vector at a point, under
|
||||
/// a label set per value; `a` arrives in two chunks and counts at both points.
|
||||
#[test]
|
||||
fn count_values_counts_the_samples_sharing_a_value() {
|
||||
let grid = grid(0, 60_000, 2);
|
||||
let schema = evaluated_schema(Arc::clone(&grid));
|
||||
let tree = NativeTree::try_new(select(
|
||||
evaluated_input(Arc::clone(&grid)),
|
||||
BoundSelect::CountValues {
|
||||
label: "v".into(),
|
||||
group: Grouping::By(Arc::from([])),
|
||||
},
|
||||
))
|
||||
.unwrap();
|
||||
let mut execution = executor(&tree, pool(), &ExecutionPlanMetricsSet::new());
|
||||
for (job, start, value, is_last) in [
|
||||
("a", 0, 1.0, false),
|
||||
("a", 1, 1.0, true),
|
||||
("b", 0, 2.0, true),
|
||||
("c", 0, 1.0, true),
|
||||
] {
|
||||
execution
|
||||
.push_evaluated(evaluated_chunk_of(
|
||||
&schema,
|
||||
named_metric("m", job),
|
||||
start,
|
||||
vec![Some(value)],
|
||||
is_last,
|
||||
))
|
||||
.unwrap();
|
||||
}
|
||||
let mut counts: Vec<(String, Decided)> = run(&mut execution)
|
||||
.iter()
|
||||
.map(|chunk| {
|
||||
(
|
||||
chunk.series.metric.get("v").unwrap().to_string(),
|
||||
decided(std::slice::from_ref(chunk)),
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
counts.sort_by(|a, b| a.0.cmp(&b.0));
|
||||
assert_eq!(
|
||||
counts,
|
||||
[
|
||||
("1".to_string(), vec![(0, Some(2.0)), (1, Some(1.0))]),
|
||||
("2".to_string(), vec![(0, Some(1.0))]),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
@@ -226,7 +226,7 @@ pub struct BoundAggregate {
|
||||
|
||||
/// An operator over the whole input vector at each grid point: one that keeps some of the input's
|
||||
/// samples with their series' labels and values, orders the series, reports where none is present,
|
||||
/// or reads a classic histogram from its bucket series.
|
||||
/// reads a classic histogram from its bucket series, or counts the samples sharing a value.
|
||||
///
|
||||
/// Its constants are bound here, as a window's are: a `k` or a ratio only known at run time has no
|
||||
/// field.
|
||||
@@ -249,6 +249,9 @@ pub enum BoundSelect {
|
||||
/// `histogram_quantile` or `histogram_fraction`: each classic histogram is read from the bucket
|
||||
/// series that share its labels, so this too reads the whole vector at a point.
|
||||
Histogram(HistogramFunction),
|
||||
/// `count_values(label, v)`: how many samples of each group share a value, under a label set
|
||||
/// per value. It writes a label per distinct value, which an aggregate's fixed groups cannot.
|
||||
CountValues { label: Arc<str>, group: Grouping },
|
||||
}
|
||||
|
||||
/// What a node computes. Every variant holds a bound operation, never a name to resolve.
|
||||
|
||||
Reference in New Issue
Block a user