mirror of
https://github.com/GreptimeTeam/greptimedb.git
synced 2026-11-17 00:07:00 +00:00
fix(promql): check smoothing factors at the first non-empty window
Signed-off-by: Dennis Zhuang <killme2008@gmail.com>
This commit is contained in:
@@ -71,6 +71,8 @@ pub struct RangeEvaluator {
|
||||
fields: Vec<FieldSamples>,
|
||||
row: Vec<Option<f64>>,
|
||||
drops_metric_name: bool,
|
||||
/// Raised by the first non-empty window, where Prometheus validates function arguments.
|
||||
window_error: Option<String>,
|
||||
}
|
||||
|
||||
/// Prometheus `schema.IsMetadataLabel`: the labels removed with the metric name.
|
||||
@@ -100,15 +102,15 @@ impl RangeEvaluator {
|
||||
window: RangeWindow,
|
||||
function: RangeFunction,
|
||||
) -> Result<Self> {
|
||||
let mut window_error = None;
|
||||
if let RangeFunction::DoubleExponentialSmoothing { smoothing, trend } = function {
|
||||
for (name, factor) in [("smoothing", smoothing), ("trend", trend)] {
|
||||
// Prometheus rejects factors outside (0, 1); NaN passes its check.
|
||||
if factor <= 0.0 || factor >= 1.0 {
|
||||
return Err(DataFusionError::Plan(format!(
|
||||
"invalid {name} factor. Expected: 0 < factor < 1, got: {factor}"
|
||||
)));
|
||||
}
|
||||
}
|
||||
// Prometheus rejects factors outside (0, 1); NaN passes its check.
|
||||
window_error = [("smoothing", smoothing), ("trend", trend)]
|
||||
.into_iter()
|
||||
.find(|(_, factor)| *factor <= 0.0 || *factor >= 1.0)
|
||||
.map(|(name, factor)| {
|
||||
format!("invalid {name} factor. Expected: 0 < factor < 1, got: {factor}")
|
||||
});
|
||||
}
|
||||
if let RangeFunction::PredictLinear(_) = function {
|
||||
let spec = grid.spec();
|
||||
@@ -126,6 +128,7 @@ impl RangeEvaluator {
|
||||
let mut evaluator =
|
||||
Self::with_kernel(input, grid, window, function.kernel(window.offset_ms))?;
|
||||
evaluator.drops_metric_name = function.drops_metric_name();
|
||||
evaluator.window_error = window_error;
|
||||
Ok(evaluator)
|
||||
}
|
||||
|
||||
@@ -164,6 +167,7 @@ impl RangeEvaluator {
|
||||
.collect(),
|
||||
row: Vec::with_capacity(input.fields.len()),
|
||||
drops_metric_name: false,
|
||||
window_error: None,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -334,6 +338,9 @@ impl RangeEvaluator {
|
||||
state.cursor = usize::try_from(steps).map_or(grid.len(), |i| i.min(grid.len()));
|
||||
continue;
|
||||
}
|
||||
if let Some(error) = &self.window_error {
|
||||
return Err(DataFusionError::Execution(error.clone()));
|
||||
}
|
||||
|
||||
self.row.clear();
|
||||
for field in &self.fields {
|
||||
|
||||
@@ -717,14 +717,23 @@ fn rejects_double_exponential_smoothing_factors_outside_the_unit_interval() {
|
||||
offset_ms: 0,
|
||||
};
|
||||
for (smoothing, trend) in [(0.0, 0.5), (1.0, 0.5), (0.5, 0.0), (0.5, 1.5)] {
|
||||
assert!(matches!(
|
||||
let evaluator = || {
|
||||
RangeEvaluator::try_new(
|
||||
&raw_schema(1),
|
||||
grid(0, 10, 4),
|
||||
window,
|
||||
RangeFunction::DoubleExponentialSmoothing { smoothing, trend }
|
||||
),
|
||||
Err(DataFusionError::Plan(_))
|
||||
RangeFunction::DoubleExponentialSmoothing { smoothing, trend },
|
||||
)
|
||||
.unwrap()
|
||||
};
|
||||
// Like Prometheus, only a window holding a sample checks the factors.
|
||||
let outside = chunk(&[(100, vec![Some(1.0)])], 1, 0, true);
|
||||
assert!(evaluator().push(&outside).unwrap().is_none());
|
||||
evaluator().finish().unwrap();
|
||||
let inside = chunk(&[(5, vec![Some(1.0)])], 1, 0, true);
|
||||
assert!(matches!(
|
||||
evaluator().push(&inside),
|
||||
Err(DataFusionError::Execution(_))
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user