fix(promql): let count_values group by its own value label

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>
This commit is contained in:
Dennis Zhuang
2026-10-21 11:25:01 +08:00
parent 51d7987d73
commit 686d3f5b8a
2 changed files with 42 additions and 1 deletions
+15 -1
View File
@@ -195,7 +195,21 @@ pub fn count_values(
label: &str,
grouping: Grouping<'_>,
) -> Result<Vec<Series>> {
validate_input(schema, input, grouping)?;
// `by` may list the value label, which this operator writes itself, even when an input field
// has that name; grouping by any other field name stays unsupported.
let other_labels: Vec<&str>;
let checked = match grouping {
Grouping::By(labels) => {
other_labels = labels
.iter()
.copied()
.filter(|name| *name != label)
.collect();
Grouping::By(&other_labels)
}
without => without,
};
validate_input(schema, input, checked)?;
if label.is_empty() {
return Err(DataFusionError::Execution(format!(
"invalid label name {label:?}"
+27
View File
@@ -945,3 +945,30 @@ fn quantile_without_q_at_a_point_has_no_result() {
let (start, values) = values_of(&output, 0, 128);
assert_eq!((start, values), (10, vec![Some(2.0), None, Some(2.0)]));
}
/// Listing the value label in `by` repeats what `count_values` adds to the grouping, also when an
/// input field has that name; listing another field name is still unsupported.
#[test]
fn count_values_accepts_its_own_label_in_by() {
let schema = schema(&["value"]);
let series = [
input(&schema, metric("m", &[("i", "0")]), 10, &[vec![Some(1.0)]]),
input(&schema, metric("m", &[("i", "1")]), 10, &[vec![Some(2.0)]]),
];
let rendered = |grouping| {
let mut output: Vec<_> = count_values(&schema, &series, "value", grouping)
.unwrap()
.iter()
.map(|series| (format!("{:?}", series.metric), values(series, 0)))
.collect();
output.sort_by(|a, b| a.0.cmp(&b.0));
output
};
let implicit = rendered(Grouping::By(&[]));
assert_eq!(implicit.len(), 2);
assert_eq!(implicit, rendered(Grouping::By(&["value"])));
assert!(matches!(
count_values(&schema, &series, "c", Grouping::By(&["value"])),
Err(DataFusionError::NotImplemented(_))
));
}