mirror of
https://github.com/GreptimeTeam/greptimedb.git
synced 2026-12-05 08:50:58 +00:00
test(promql): drop tests that repeat other coverage and table-drive the rest (#10855)
* test(promql): drop native compile tests the executor tests already cover Executing a compiled tree for bool/filter comparisons, negation and sum aggregates repeated the kernel tests in promql::native::execute, while the compile-side binding is pinned by the compile tests. Fold single-row duplicates into the existing tables and add a helper for the repeated compile_selectors call. Signed-off-by: Dennis Zhuang <killme2008@gmail.com> * test(promql): drop planner tests repeated by sqlness and fold copies into tables Subquery window execution cases already pinned by subquery.sql and subquery_at_modifier.sql are removed; the remaining cases form one table. scatter_time_filter_many_evaluation_points only planned what scatter_time_filter.sql executes. Tests that differed only in data are merged into table-driven tests. Signed-off-by: Dennis Zhuang <killme2008@gmail.com> * test(promql): keep split and empty input in the compiled sum over rate test Signed-off-by: Dennis Zhuang <killme2008@gmail.com> * test(promql): split a bare-selector sum series and check the final histogram binary column type Signed-off-by: Dennis Zhuang <killme2008@gmail.com> --------- Signed-off-by: Dennis Zhuang <killme2008@gmail.com>
This commit is contained in:
1 parent
38621747e1
commit
2d4c29b5ec
2 files changed
+451
-1359
No files matched your search
@@ -42,7 +42,7 @@ use promql_parser::parser::{
|
||||
};
|
||||
|
||||
use super::{
|
||||
CompiledInput, SelectorRequirement, SelectorTiming, compile, compile_selectors,
|
||||
CompiledInput, CompiledTree, SelectorRequirement, SelectorTiming, compile, compile_selectors,
|
||||
compile_statement, validate_inputs,
|
||||
};
|
||||
|
||||
@@ -91,6 +91,16 @@ fn raw_schema_with_fields(count: usize) -> Arc<SeriesSchema> {
|
||||
})
|
||||
}
|
||||
|
||||
/// Compiles `query` with every selector bound to [`raw_schema`] and read as floats.
|
||||
fn compile_float_selectors(query: &str) -> datafusion::error::Result<CompiledTree> {
|
||||
compile_selectors(
|
||||
&eval_stmt(query),
|
||||
&|_| Ok(raw_schema()),
|
||||
SeriesOrdering::KeyThenTime,
|
||||
Some(FieldKind::Float),
|
||||
)
|
||||
}
|
||||
|
||||
/// A statement over 100 seconds in 5-second steps, which is 21 grid points.
|
||||
fn eval_stmt(query: &str) -> EvalStmt {
|
||||
eval_stmt_over(query, Duration::from_secs(100))
|
||||
@@ -422,43 +432,6 @@ fn a_quantile_outside_the_unit_interval_is_bound_as_given() {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_parenthesised_quantile_binds_as_written() {
|
||||
let tree = compile(
|
||||
&eval_stmt("quantile_over_time((0.9), x[5m])"),
|
||||
raw_schema(),
|
||||
SeriesOrdering::KeyThenTime,
|
||||
)
|
||||
.expect("a parenthesised literal is the same quantile");
|
||||
|
||||
let (_, op) = root_window(&tree);
|
||||
assert_eq!(op.function, RangeFunction::QuantileOverTime(0.9));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_parenthesised_selector_binds_as_written() {
|
||||
let bare = compile(
|
||||
&eval_stmt("quantile_over_time(0.9, x[5m])"),
|
||||
raw_schema(),
|
||||
SeriesOrdering::KeyThenTime,
|
||||
)
|
||||
.expect("the bare selector binds");
|
||||
|
||||
for query in [
|
||||
"quantile_over_time(0.9, (x[5m]))",
|
||||
"quantile_over_time((0.9), (x[5m]))",
|
||||
"(quantile_over_time(0.9, (x[5m])))",
|
||||
] {
|
||||
let tree = compile(&eval_stmt(query), raw_schema(), SeriesOrdering::KeyThenTime)
|
||||
.unwrap_or_else(|error| panic!("`{query}` was refused: {error}"));
|
||||
assert_eq!(
|
||||
tree.to_string(),
|
||||
bare.to_string(),
|
||||
"`{query}` did not compile to the tree `quantile_over_time(0.9, x[5m])` does"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_parameter_folded_from_an_expression_binds_its_value() {
|
||||
// `1 - 0.1` is `0.9` bit for bit in IEEE754, and `-(0.9)` is the negative of the same number:
|
||||
@@ -621,19 +594,6 @@ fn an_offset_on_a_bare_selector_keeps_its_sign() {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_anchored_instant_selector_binds_its_anchor() {
|
||||
let tree = compile(
|
||||
&eval_stmt("x @ 100"),
|
||||
raw_schema(),
|
||||
SeriesOrdering::KeyThenTime,
|
||||
)
|
||||
.unwrap();
|
||||
let (_, op) = root_instant(&tree);
|
||||
assert_eq!(op.at_ms, Some(100_000));
|
||||
assert_eq!(op.offset_ms, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn anchored_instant_selector_binds_scan_bounds() {
|
||||
for (query, at_ms, offset_ms, bounds) in [
|
||||
@@ -643,16 +603,11 @@ fn anchored_instant_selector_binds_scan_bounds() {
|
||||
60_000,
|
||||
(1_169_000, 1_174_000, false),
|
||||
),
|
||||
("x @ 100", Some(100_000), 0, (95_000, 100_000, false)),
|
||||
("x @ start()", Some(0), 0, (-5_000, 0, false)),
|
||||
("x @ end()", Some(100_000), 0, (95_000, 100_000, false)),
|
||||
] {
|
||||
let compiled = compile_selectors(
|
||||
&eval_stmt(query),
|
||||
&|_| Ok(raw_schema()),
|
||||
SeriesOrdering::KeyThenTime,
|
||||
Some(FieldKind::Float),
|
||||
)
|
||||
.unwrap();
|
||||
let compiled = compile_float_selectors(query).unwrap();
|
||||
let (_, op) = root_instant(&compiled.tree);
|
||||
assert_eq!(op.at_ms, at_ms, "{query}");
|
||||
assert_eq!(op.offset_ms, offset_ms, "{query}");
|
||||
@@ -667,15 +622,18 @@ fn anchored_instant_selector_binds_scan_bounds() {
|
||||
#[test]
|
||||
fn an_anchor_on_a_window_resolves_to_absolute_milliseconds() {
|
||||
let cases = [
|
||||
("rate(x[5m] @ 1234)", 1_234_000),
|
||||
("rate(x[5m] @ 1.234)", 1_234),
|
||||
("rate(x[5m] @ 1234)", 1_234_000, 0),
|
||||
("rate(x[5m] @ 1.234)", 1_234, 0),
|
||||
// Prometheus allows a pre-epoch anchor.
|
||||
("rate(x[5m] @ -1.5)", -1_500),
|
||||
("rate(x[5m] @ start())", 0),
|
||||
("rate(x[5m] @ end())", 100_000),
|
||||
("rate(x[5m] @ -1.5)", -1_500, 0),
|
||||
("rate(x[5m] @ start())", 0, 0),
|
||||
("rate(x[5m] @ end())", 100_000, 0),
|
||||
("rate(x[5m] @ 1.234 offset 1m)", 1_234, 60_000),
|
||||
("rate(x[5m] @ 1234 offset 1m)", 1_234_000, 60_000),
|
||||
("rate(x[5m] @ 1.234 offset -1m)", 1_234, -60_000),
|
||||
];
|
||||
|
||||
for (query, anchor_ms) in cases {
|
||||
for (query, anchor_ms, offset_ms) in cases {
|
||||
let tree = compile(&eval_stmt(query), raw_schema(), SeriesOrdering::KeyThenTime).unwrap();
|
||||
let (_, op) = root_window(&tree);
|
||||
assert_eq!(
|
||||
@@ -683,7 +641,10 @@ fn an_anchor_on_a_window_resolves_to_absolute_milliseconds() {
|
||||
Some(anchor_ms),
|
||||
"`{query}` bound the wrong anchor"
|
||||
);
|
||||
assert_eq!(op.window.offset_ms, 0, "`{query}` invented an offset");
|
||||
assert_eq!(
|
||||
op.window.offset_ms, offset_ms,
|
||||
"`{query}` bound the wrong offset"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -707,26 +668,6 @@ fn end_resolves_to_the_statement_end_not_the_last_grid_point() {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_anchor_and_an_offset_are_bound_apart() {
|
||||
let cases = [
|
||||
("rate(x[5m] @ 1.234 offset 1m)", 1_234, 60_000),
|
||||
("rate(x[5m] @ 1234 offset 1m)", 1_234_000, 60_000),
|
||||
("rate(x[5m] @ 1.234 offset -1m)", 1_234, -60_000),
|
||||
];
|
||||
|
||||
for (query, anchor_ms, offset_ms) in cases {
|
||||
let tree = compile(&eval_stmt(query), raw_schema(), SeriesOrdering::KeyThenTime).unwrap();
|
||||
let (_, op) = root_window(&tree);
|
||||
assert_eq!(
|
||||
op.window.anchor_ms,
|
||||
Some(anchor_ms),
|
||||
"`{query}` lost the anchor"
|
||||
);
|
||||
assert_eq!(op.window.offset_ms, offset_ms, "`{query}` lost the offset");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_numeric_edges_of_a_statement_are_bound_as_given() {
|
||||
enum Expected {
|
||||
@@ -990,25 +931,39 @@ fn the_display_of_a_tree_the_batch_added_to_is_the_query() {
|
||||
|
||||
#[test]
|
||||
fn parentheses_around_the_accepted_shape_are_transparent() {
|
||||
let bare = compile(
|
||||
&eval_stmt("rate(x[5m])"),
|
||||
raw_schema(),
|
||||
SeriesOrdering::KeyThenTime,
|
||||
)
|
||||
.unwrap();
|
||||
for query in [
|
||||
"(rate(x[5m]))",
|
||||
"rate((x[5m]))",
|
||||
"rate(((x[5m])))",
|
||||
"((rate((x[5m]))))",
|
||||
] {
|
||||
let tree = compile(&eval_stmt(query), raw_schema(), SeriesOrdering::KeyThenTime)
|
||||
.unwrap_or_else(|error| panic!("`{query}` was refused: {error}"));
|
||||
assert_eq!(
|
||||
tree.to_string(),
|
||||
bare.to_string(),
|
||||
"`{query}` did not compile to the tree `rate(x[5m])` does"
|
||||
);
|
||||
let cases: [(&str, &[&str]); 2] = [
|
||||
(
|
||||
"rate(x[5m])",
|
||||
&[
|
||||
"(rate(x[5m]))",
|
||||
"rate((x[5m]))",
|
||||
"rate(((x[5m])))",
|
||||
"((rate((x[5m]))))",
|
||||
],
|
||||
),
|
||||
(
|
||||
"quantile_over_time(0.9, x[5m])",
|
||||
&[
|
||||
"quantile_over_time((0.9), x[5m])",
|
||||
"quantile_over_time(0.9, (x[5m]))",
|
||||
"quantile_over_time((0.9), (x[5m]))",
|
||||
"(quantile_over_time(0.9, (x[5m])))",
|
||||
],
|
||||
),
|
||||
];
|
||||
for (bare, queries) in cases {
|
||||
let bare_tree = compile(&eval_stmt(bare), raw_schema(), SeriesOrdering::KeyThenTime)
|
||||
.unwrap()
|
||||
.to_string();
|
||||
for query in queries {
|
||||
let tree = compile(&eval_stmt(query), raw_schema(), SeriesOrdering::KeyThenTime)
|
||||
.unwrap_or_else(|error| panic!("`{query}` was refused: {error}"));
|
||||
assert_eq!(
|
||||
tree.to_string(),
|
||||
bare_tree,
|
||||
"`{query}` did not compile to the tree `{bare}` does"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
@@ -1380,6 +1335,14 @@ fn a_constant_operand_binds_the_value_it_folds_to() {
|
||||
("x * 2 ^ 3", 8.0),
|
||||
("x * -0.5", -0.5),
|
||||
("x * (1 - 0.1)", 0.9),
|
||||
// Unfolded, a `Binary` of two constants passes tree validation and fails only at execution.
|
||||
("x * (1 < bool 2)", 1.0),
|
||||
("x * (1 > bool 2)", 0.0),
|
||||
("x * (1 == bool 1)", 1.0),
|
||||
("x * (1 != bool 1)", 0.0),
|
||||
// NaN satisfies only `!=`.
|
||||
("x * (0 / 0 == bool 0)", 0.0),
|
||||
("x * (0 / 0 != bool 0)", 1.0),
|
||||
];
|
||||
|
||||
for (query, value) in cases {
|
||||
@@ -1402,13 +1365,8 @@ fn a_constant_operand_binds_the_value_it_folds_to() {
|
||||
/// Two vectors bind independently, while the compatibility entry refuses to hide the two inputs.
|
||||
#[test]
|
||||
fn a_two_vector_operation_binds_one_input_per_selector() {
|
||||
let compiled = compile_selectors(
|
||||
&eval_stmt("a + b"),
|
||||
&|_| Ok(raw_schema()),
|
||||
SeriesOrdering::KeyThenTime,
|
||||
Some(FieldKind::Float),
|
||||
)
|
||||
.unwrap_or_else(|error| panic!("`a + b` was refused: {error}"));
|
||||
let compiled = compile_float_selectors("a + b")
|
||||
.unwrap_or_else(|error| panic!("`a + b` was refused: {error}"));
|
||||
assert_eq!(compiled.inputs.len(), 2);
|
||||
assert_eq!(compiled.inputs[0].requirement.metric.as_deref(), Some("a"));
|
||||
assert_eq!(compiled.inputs[1].requirement.metric.as_deref(), Some("b"));
|
||||
@@ -1503,6 +1461,16 @@ fn vector_binary_matching_binds_labels_cardinality_fills_and_bool() {
|
||||
Some(2.0),
|
||||
false,
|
||||
),
|
||||
(
|
||||
"a / ignoring(instance) group_left(host) b",
|
||||
false,
|
||||
vec!["instance"],
|
||||
"many-to-one",
|
||||
vec!["host"],
|
||||
None,
|
||||
None,
|
||||
false,
|
||||
),
|
||||
(
|
||||
"a > bool ignoring(zone) b",
|
||||
false,
|
||||
@@ -1514,13 +1482,8 @@ fn vector_binary_matching_binds_labels_cardinality_fills_and_bool() {
|
||||
true,
|
||||
),
|
||||
] {
|
||||
let compiled = compile_selectors(
|
||||
&eval_stmt(query),
|
||||
&|_| Ok(raw_schema()),
|
||||
SeriesOrdering::KeyThenTime,
|
||||
Some(FieldKind::Float),
|
||||
)
|
||||
.unwrap_or_else(|error| panic!("`{query}` was refused: {error}"));
|
||||
let compiled = compile_float_selectors(query)
|
||||
.unwrap_or_else(|error| panic!("`{query}` was refused: {error}"));
|
||||
let (_, _, matching) = binary_parts(compiled.tree.root());
|
||||
assert_eq!(matching_labels(&matching.labels), (on, labels), "{query}");
|
||||
assert_eq!(
|
||||
@@ -1537,23 +1500,6 @@ fn vector_binary_matching_binds_labels_cardinality_fills_and_bool() {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn vector_binary_matching_keeps_ignoring_and_group_left_separate() {
|
||||
let compiled = compile_selectors(
|
||||
&eval_stmt("a / ignoring(instance) group_left(host) b"),
|
||||
&|_| Ok(raw_schema()),
|
||||
SeriesOrdering::KeyThenTime,
|
||||
Some(FieldKind::Float),
|
||||
)
|
||||
.unwrap();
|
||||
let (_, _, matching) = binary_parts(compiled.tree.root());
|
||||
assert_eq!(matching_labels(&matching.labels), (false, vec!["instance"]));
|
||||
assert_eq!(
|
||||
matching_cardinality(&matching.cardinality),
|
||||
("many-to-one", vec!["host"])
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn set_operators_bind_their_operator_and_labels() {
|
||||
for (query, op, on, labels) in [
|
||||
@@ -1566,13 +1512,8 @@ fn set_operators_bind_their_operator_and_labels() {
|
||||
vec!["pod"],
|
||||
),
|
||||
] {
|
||||
let compiled = compile_selectors(
|
||||
&eval_stmt(query),
|
||||
&|_| Ok(raw_schema()),
|
||||
SeriesOrdering::KeyThenTime,
|
||||
Some(FieldKind::Float),
|
||||
)
|
||||
.unwrap_or_else(|error| panic!("`{query}` was refused: {error}"));
|
||||
let compiled = compile_float_selectors(query)
|
||||
.unwrap_or_else(|error| panic!("`{query}` was refused: {error}"));
|
||||
assert_eq!(compiled.inputs.len(), 2, "{query}");
|
||||
let (left, right, bound) = set_parts(compiled.tree.root());
|
||||
assert_eq!(bound.op, op, "{query}");
|
||||
@@ -1658,13 +1599,8 @@ fn info_binds_its_info_metric_after_the_base() {
|
||||
false,
|
||||
),
|
||||
] {
|
||||
let compiled = compile_selectors(
|
||||
&eval_stmt(query),
|
||||
&|_| Ok(raw_schema()),
|
||||
SeriesOrdering::KeyThenTime,
|
||||
Some(FieldKind::Float),
|
||||
)
|
||||
.unwrap_or_else(|error| panic!("`{query}` was refused: {error}"));
|
||||
let compiled = compile_float_selectors(query)
|
||||
.unwrap_or_else(|error| panic!("`{query}` was refused: {error}"));
|
||||
let [base_input, info_input] = &compiled.inputs[..] else {
|
||||
panic!("{query}: {} inputs", compiled.inputs.len());
|
||||
};
|
||||
@@ -1760,13 +1696,8 @@ fn info_selects_its_info_series_at_the_shared_reference_time() {
|
||||
Some(30_000),
|
||||
),
|
||||
] {
|
||||
let compiled = compile_selectors(
|
||||
&eval_stmt(query),
|
||||
&|_| Ok(raw_schema()),
|
||||
SeriesOrdering::KeyThenTime,
|
||||
Some(FieldKind::Float),
|
||||
)
|
||||
.unwrap_or_else(|error| panic!("`{query}` was refused: {error}"));
|
||||
let compiled = compile_float_selectors(query)
|
||||
.unwrap_or_else(|error| panic!("`{query}` was refused: {error}"));
|
||||
let info = &compiled.inputs.last().unwrap().requirement;
|
||||
assert_eq!(info.metric.as_deref(), Some("target_info"), "{query}");
|
||||
assert_eq!((info.offset_ms, info.at_ms), (offset_ms, at_ms), "{query}");
|
||||
@@ -1883,14 +1814,9 @@ fn a_statement_that_reads_no_data_compiles_without_input() {
|
||||
#[test]
|
||||
fn a_varying_scalar_is_broadcast_where_it_was_written() {
|
||||
let compile_tree = |query: &str| {
|
||||
compile_selectors(
|
||||
&eval_stmt(query),
|
||||
&|_| Ok(raw_schema()),
|
||||
SeriesOrdering::KeyThenTime,
|
||||
Some(FieldKind::Float),
|
||||
)
|
||||
.unwrap_or_else(|error| panic!("`{query}` was refused: {error}"))
|
||||
.tree
|
||||
compile_float_selectors(query)
|
||||
.unwrap_or_else(|error| panic!("`{query}` was refused: {error}"))
|
||||
.tree
|
||||
};
|
||||
let tree = compile_tree("x > bool time()");
|
||||
let (left, right, matching) = binary_parts(tree.root());
|
||||
@@ -1909,13 +1835,8 @@ fn a_varying_scalar_is_broadcast_where_it_was_written() {
|
||||
#[test]
|
||||
fn a_math_function_with_a_varying_parameter_binds_its_parameters_as_scalars() {
|
||||
let compiled = |query: &str| {
|
||||
compile_selectors(
|
||||
&eval_stmt(query),
|
||||
&|_| Ok(raw_schema()),
|
||||
SeriesOrdering::KeyThenTime,
|
||||
Some(FieldKind::Float),
|
||||
)
|
||||
.unwrap_or_else(|error| panic!("`{query}` was refused: {error}"))
|
||||
compile_float_selectors(query)
|
||||
.unwrap_or_else(|error| panic!("`{query}` was refused: {error}"))
|
||||
};
|
||||
let parts = |tree: &NativeTree| match &tree.root().expr {
|
||||
NativeExpr::ParameterizedMap {
|
||||
@@ -1964,14 +1885,7 @@ fn a_math_function_with_a_varying_parameter_binds_its_parameters_as_scalars() {
|
||||
/// Prometheus reads a `quantile` φ at every step.
|
||||
#[test]
|
||||
fn a_quantile_reads_its_argument_per_point() {
|
||||
let compiled = |query: &str| {
|
||||
compile_selectors(
|
||||
&eval_stmt(query),
|
||||
&|_| Ok(raw_schema()),
|
||||
SeriesOrdering::KeyThenTime,
|
||||
Some(FieldKind::Float),
|
||||
)
|
||||
};
|
||||
let compiled = |query: &str| compile_float_selectors(query);
|
||||
let per_point = |query: &str| match &compiled(query)
|
||||
.unwrap_or_else(|error| panic!("`{query}` was refused: {error}"))
|
||||
.tree
|
||||
@@ -2075,41 +1989,31 @@ fn a_call_of_two_scalars_binds_them_as_a_scalar_map() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_scalar_function_over_a_vector_is_refused() {
|
||||
let stmt = with_call_arguments("max_of(1, 2)", |args| {
|
||||
*args[0] = a_bare_selector();
|
||||
});
|
||||
let error = compile(&stmt, raw_schema(), SeriesOrdering::KeyThenTime)
|
||||
.expect_err("`max_of(x, 2)` compiled, and it should not have");
|
||||
assert!(
|
||||
matches!(error, DataFusionError::NotImplemented(_)),
|
||||
"the refusal did not report NotImplemented, so the statement would not fall back: {error}"
|
||||
);
|
||||
let message = error.to_string();
|
||||
assert!(message.contains("a call to `max_of`"), "{message}");
|
||||
assert!(
|
||||
message.contains("where the function takes scalars"),
|
||||
"the refusal did not say what the function takes: {message}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_scalar_function_with_one_argument_is_refused() {
|
||||
let stmt = with_call_arguments("max_of(1, 2)", |args| {
|
||||
args.pop();
|
||||
});
|
||||
let error = compile(&stmt, raw_schema(), SeriesOrdering::KeyThenTime)
|
||||
.expect_err("`max_of(1)` compiled, and it should not have");
|
||||
assert!(
|
||||
matches!(error, DataFusionError::NotImplemented(_)),
|
||||
"the refusal did not report NotImplemented: {error}"
|
||||
);
|
||||
let message = error.to_string();
|
||||
assert!(message.contains("a call to `max_of`"), "{message}");
|
||||
assert!(
|
||||
message.contains("where it takes two scalars"),
|
||||
"the refusal did not name the arguments it takes: {message}"
|
||||
);
|
||||
fn a_scalar_function_over_the_wrong_arguments_is_refused() {
|
||||
type Edit = fn(&mut Vec<Box<PromExpr>>);
|
||||
let cases: [(Edit, &str); 2] = [
|
||||
(
|
||||
|args| *args[0] = a_bare_selector(),
|
||||
"where the function takes scalars",
|
||||
),
|
||||
(
|
||||
|args| {
|
||||
args.pop();
|
||||
},
|
||||
"where it takes two scalars",
|
||||
),
|
||||
];
|
||||
for (edit, expected) in cases {
|
||||
let stmt = with_call_arguments("max_of(1, 2)", edit);
|
||||
let error = compile(&stmt, raw_schema(), SeriesOrdering::KeyThenTime).expect_err(expected);
|
||||
assert!(
|
||||
matches!(error, DataFusionError::NotImplemented(_)),
|
||||
"the refusal did not report NotImplemented, so the statement would not fall back: {error}"
|
||||
);
|
||||
let message = error.to_string();
|
||||
assert!(message.contains("a call to `max_of`"), "{message}");
|
||||
assert!(message.contains(expected), "{message}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -2181,34 +2085,6 @@ fn a_modifier_inside_a_folded_call_argument_is_refused() {
|
||||
}
|
||||
}
|
||||
|
||||
/// Unfolded, a `Binary` of two constants passes tree validation and fails only at execution.
|
||||
#[test]
|
||||
fn a_bool_comparison_of_two_constants_folds_to_its_number() {
|
||||
for (query, folded) in [
|
||||
("x * (1 < bool 2)", 1.0),
|
||||
("x * (1 > bool 2)", 0.0),
|
||||
("x * (1 == bool 1)", 1.0),
|
||||
("x * (1 != bool 1)", 0.0),
|
||||
// NaN satisfies only `!=`.
|
||||
("x * (0 / 0 == bool 0)", 0.0),
|
||||
("x * (0 / 0 != bool 0)", 1.0),
|
||||
] {
|
||||
let tree = compile(&eval_stmt(query), raw_schema(), SeriesOrdering::KeyThenTime)
|
||||
.unwrap_or_else(|error| panic!("`{query}` was refused: {error}"));
|
||||
let (left, right, _) = binary_parts(tree.root());
|
||||
let scalar_side = if constant(left).is_some() {
|
||||
left
|
||||
} else {
|
||||
right
|
||||
};
|
||||
assert_eq!(
|
||||
constant(scalar_side),
|
||||
Some(folded),
|
||||
"`{query}` did not fold its comparison to {folded}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// The parser refuses `1 > 2`, so the `bool` flag is cleared on a parsed tree.
|
||||
#[test]
|
||||
fn a_comparison_of_two_constants_without_bool_is_refused() {
|
||||
@@ -2693,29 +2569,6 @@ fn a_compiled_constant_on_the_left_keeps_the_operand_order() {
|
||||
assert_ne!(divided, other, "`100 / x` and `x / 100` agree");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_compiled_bool_comparison_reports_one_and_zero() {
|
||||
let (timestamps, _) = samples_at_every_grid_point();
|
||||
// Half the values are negative, so the comparison decides both ways.
|
||||
let values = (1..=21)
|
||||
.map(|value| Some(f64::from(value) - 10.0))
|
||||
.collect();
|
||||
let out = execute_compiled("x > bool 0", vec![raw_x(×tamps, values, true)]);
|
||||
|
||||
let expected: Vec<(usize, f64)> = (0..21)
|
||||
.map(|index| {
|
||||
let value = index as f64 + 1.0 - 10.0;
|
||||
(index, f64::from(u8::from(value > 0.0)))
|
||||
})
|
||||
.collect();
|
||||
assert_eq!(rows(&out), expected, "`x > bool 0` did not report 0/1");
|
||||
assert_eq!(
|
||||
executed_name(&out),
|
||||
None,
|
||||
"`x > bool 0` kept the metric name"
|
||||
);
|
||||
}
|
||||
|
||||
/// Constant comparisons fold before the executor runs the vector operation.
|
||||
#[test]
|
||||
fn a_compiled_bool_constant_comparison_runs_with_vector_arithmetic() {
|
||||
@@ -2748,38 +2601,6 @@ fn a_compiled_bool_constant_comparison_runs_with_vector_arithmetic() {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_compiled_filtering_comparison_keeps_the_values_that_passed() {
|
||||
let (timestamps, _) = samples_at_every_grid_point();
|
||||
let values = (1..=21)
|
||||
.map(|value| Some(f64::from(value) - 10.0))
|
||||
.collect();
|
||||
let out = execute_compiled("x > 0", vec![raw_x(×tamps, values, true)]);
|
||||
|
||||
let expected: Vec<(usize, f64)> = (0..21)
|
||||
.filter_map(|index| {
|
||||
let value = index as f64 + 1.0 - 10.0;
|
||||
(value > 0.0).then_some((index, value))
|
||||
})
|
||||
.collect();
|
||||
assert_eq!(rows(&out), expected, "`x > 0` did not filter the vector");
|
||||
assert_eq!(
|
||||
executed_name(&out),
|
||||
Some("x".to_string()),
|
||||
"a filtering comparison keeps the labels it read"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_compiled_negation_negates_every_value() {
|
||||
let (timestamps, values) = samples_at_every_grid_point();
|
||||
let out = execute_compiled("-x", vec![raw_x(×tamps, values, true)]);
|
||||
let expected: Vec<(usize, f64)> = (0..21)
|
||||
.map(|index| (index, -(index as f64 + 1.0)))
|
||||
.collect();
|
||||
assert_eq!(rows(&out), expected, "`-x` did not negate every value");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_compiled_negation_of_a_window_negates_the_window_result() {
|
||||
let (timestamps, values) = samples_at_every_grid_point();
|
||||
@@ -4045,7 +3866,8 @@ fn aggregate_results(output: &[EvaluatedChunk]) -> Vec<AggSeries> {
|
||||
results
|
||||
}
|
||||
|
||||
/// Pushes `chunks` into the tree's only input, ends it, and collects what the root reports.
|
||||
/// Pushes `chunks` into the tree's only input, ends it, and collects what the root reports. The
|
||||
/// aggregate at the root must not answer before the input ends.
|
||||
fn run_aggregate_tree(tree: &NativeTree, chunks: Vec<RawSeriesChunk>) -> Vec<AggSeries> {
|
||||
let pool: Arc<dyn MemoryPool> = Arc::new(GreedyMemoryPool::new(1 << 26));
|
||||
let state = NativeExecutionState::try_new(tree, pool, None).unwrap();
|
||||
@@ -4054,6 +3876,10 @@ fn run_aggregate_tree(tree: &NativeTree, chunks: Vec<RawSeriesChunk>) -> Vec<Agg
|
||||
execution
|
||||
.push_raw(InputId::new(0), chunk)
|
||||
.unwrap_or_else(|error| panic!("a source chunk was refused: {error}"));
|
||||
assert!(
|
||||
execution.next_chunk().unwrap().is_none(),
|
||||
"the aggregate answered before its input ended"
|
||||
);
|
||||
}
|
||||
let output = execution
|
||||
.finish()
|
||||
@@ -4071,119 +3897,6 @@ fn agg_timestamps_0_to_100() -> Vec<i64> {
|
||||
(0..21).map(|index| index * 5_000).collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_compiled_sum_drops_every_label_and_adds_its_group() {
|
||||
let tree = compile(
|
||||
&eval_stmt("sum(x)"),
|
||||
raw_schema(),
|
||||
SeriesOrdering::KeyThenTime,
|
||||
)
|
||||
.unwrap();
|
||||
let schema = raw_schema();
|
||||
let timestamps = agg_timestamps_0_to_100();
|
||||
let chunks = vec![
|
||||
agg_raw_chunk(
|
||||
&schema,
|
||||
b"a",
|
||||
agg_metric("x", &[("job", "api"), ("instance", "a")]),
|
||||
×tamps,
|
||||
&(1..=21).map(f64::from).collect::<Vec<_>>(),
|
||||
),
|
||||
agg_raw_chunk(
|
||||
&schema,
|
||||
b"b",
|
||||
agg_metric("x", &[("job", "db"), ("instance", "b")]),
|
||||
×tamps,
|
||||
&(1..=21).map(|value| (2 * value) as f64).collect::<Vec<_>>(),
|
||||
),
|
||||
];
|
||||
|
||||
let output = run_aggregate_tree(&tree, chunks);
|
||||
// `3` at the first point, three more at each of the twenty after it: the two members summed.
|
||||
let values: Vec<(usize, Option<f64>)> = agg_points_0_to_100()
|
||||
.into_iter()
|
||||
.map(|position| (position, Some(3.0 * (position as f64 + 1.0))))
|
||||
.collect();
|
||||
assert_eq!(output, vec![(None, Vec::new(), values)]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_compiled_sum_aggregates_series_chunks_after_input_ends() {
|
||||
let schema = raw_schema();
|
||||
let timestamps = agg_timestamps_0_to_100();
|
||||
let values: Vec<f64> = (1..=21).map(f64::from).collect();
|
||||
let cases = [
|
||||
("sum(x)", None, Vec::new()),
|
||||
(
|
||||
"sum by(job)(x)",
|
||||
None,
|
||||
vec![("job".to_string(), "api".to_string())],
|
||||
),
|
||||
(
|
||||
"sum without(instance)(x)",
|
||||
None,
|
||||
vec![("job".to_string(), "api".to_string())],
|
||||
),
|
||||
];
|
||||
|
||||
for (query, name, tags) in cases {
|
||||
let tree = compile(&eval_stmt(query), raw_schema(), SeriesOrdering::KeyThenTime).unwrap();
|
||||
let metric = agg_metric("x", &[("job", "api"), ("instance", "a")]);
|
||||
let chunks = vec![
|
||||
agg_raw_chunk_with_last(
|
||||
&schema,
|
||||
b"a",
|
||||
metric.clone(),
|
||||
×tamps[..10],
|
||||
&values[..10],
|
||||
false,
|
||||
),
|
||||
agg_raw_chunk_with_last(
|
||||
&schema,
|
||||
b"a",
|
||||
metric,
|
||||
×tamps[10..],
|
||||
&values[10..],
|
||||
true,
|
||||
),
|
||||
];
|
||||
let pool: Arc<dyn MemoryPool> = Arc::new(GreedyMemoryPool::new(1 << 26));
|
||||
let state = NativeExecutionState::try_new(&tree, pool, None).unwrap();
|
||||
let mut execution = NativeExecutor::new(state);
|
||||
for chunk in chunks {
|
||||
execution.push_raw(InputId::new(0), chunk).unwrap();
|
||||
assert!(
|
||||
execution.next_chunk().unwrap().is_none(),
|
||||
"`{query}` produced output before the whole input ended"
|
||||
);
|
||||
}
|
||||
let output = execution.finish().unwrap();
|
||||
let results = aggregate_results(&output);
|
||||
let expected_points = values
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(position, value)| (position, Some(*value)))
|
||||
.collect();
|
||||
assert_eq!(
|
||||
results,
|
||||
vec![(name.map(str::to_string), tags, expected_points)],
|
||||
"`{query}`"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_compiled_aggregate_over_empty_input_produces_no_groups() {
|
||||
for query in ["sum(x)", "sum by(job)(x)", "sum without(instance)(x)"] {
|
||||
let tree = compile(&eval_stmt(query), raw_schema(), SeriesOrdering::KeyThenTime).unwrap();
|
||||
assert_eq!(
|
||||
run_aggregate_tree(&tree, Vec::new()),
|
||||
Vec::new(),
|
||||
"`{query}` invented a group for empty input"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Members cover different points, so each group sums a subset that changes per point. No sample
|
||||
/// sits exactly one lookback behind a grid point, so the boundary convention does not matter.
|
||||
#[test]
|
||||
@@ -4196,15 +3909,18 @@ fn a_compiled_sum_by_groups_its_members_and_sums_each_group() {
|
||||
.unwrap();
|
||||
let schema = raw_schema();
|
||||
let all = agg_timestamps_0_to_100();
|
||||
let metric_a = agg_metric("x", &[("job", "api"), ("instance", "a")]);
|
||||
let chunks = vec![
|
||||
// The whole grid, holding one.
|
||||
agg_raw_chunk(
|
||||
// The whole grid, holding one, in two chunks.
|
||||
agg_raw_chunk_with_last(
|
||||
&schema,
|
||||
b"a",
|
||||
agg_metric("x", &[("job", "api"), ("instance", "a")]),
|
||||
&all,
|
||||
&[1.0; 21],
|
||||
metric_a.clone(),
|
||||
&all[..10],
|
||||
&[1.0; 10],
|
||||
false,
|
||||
),
|
||||
agg_raw_chunk_with_last(&schema, b"a", metric_a, &all[10..], &[1.0; 11], true),
|
||||
// One sample halfway between grid points 0 and 1, so only grid point 1 selects it.
|
||||
agg_raw_chunk(
|
||||
&schema,
|
||||
@@ -4338,13 +4054,30 @@ fn a_compiled_sum_over_a_rate_groups_the_window_it_reads() {
|
||||
let schema = raw_schema();
|
||||
let timestamps: Vec<i64> = (0..=80).map(|index| index * 5_000).collect();
|
||||
let values: Vec<f64> = (0..=80).map(|index| f64::from(index) + 1.0).collect();
|
||||
assert_eq!(
|
||||
run_aggregate_tree(&tree, Vec::new()),
|
||||
Vec::new(),
|
||||
"empty input invented a group"
|
||||
);
|
||||
// Series `a` arrives in two chunks, so the window carries its state across the split before
|
||||
// handing the series to the aggregate.
|
||||
let metric_a = agg_metric("x", &[("job", "api"), ("instance", "a")]);
|
||||
let chunks = vec![
|
||||
agg_raw_chunk(
|
||||
agg_raw_chunk_with_last(
|
||||
&schema,
|
||||
b"a",
|
||||
agg_metric("x", &[("job", "api"), ("instance", "a")]),
|
||||
×tamps,
|
||||
&values,
|
||||
metric_a.clone(),
|
||||
×tamps[..41],
|
||||
&values[..41],
|
||||
false,
|
||||
),
|
||||
agg_raw_chunk_with_last(
|
||||
&schema,
|
||||
b"a",
|
||||
metric_a,
|
||||
×tamps[41..],
|
||||
&values[41..],
|
||||
true,
|
||||
),
|
||||
agg_raw_chunk(
|
||||
&schema,
|
||||
@@ -4384,74 +4117,6 @@ fn a_compiled_sum_over_a_rate_groups_the_window_it_reads() {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_compiled_sum_over_a_rate_aggregates_series_chunks_after_input_ends() {
|
||||
let statement = EvalStmt {
|
||||
expr: promql_parser::parser::parse("sum by(job)(rate(x[5m]))").unwrap(),
|
||||
start: UNIX_EPOCH + Duration::from_secs(300),
|
||||
end: UNIX_EPOCH + Duration::from_secs(400),
|
||||
interval: Duration::from_secs(5),
|
||||
lookback_delta: Duration::from_secs(5),
|
||||
};
|
||||
let tree = compile(&statement, raw_schema(), SeriesOrdering::KeyThenTime).unwrap();
|
||||
let schema = raw_schema();
|
||||
let timestamps: Vec<i64> = (0..=80).map(|index| index * 5_000).collect();
|
||||
let values: Vec<f64> = (0..=80).map(|index| f64::from(index) + 1.0).collect();
|
||||
let metric = agg_metric("x", &[("job", "api"), ("instance", "a")]);
|
||||
let chunks = [
|
||||
agg_raw_chunk_with_last(
|
||||
&schema,
|
||||
b"a",
|
||||
metric.clone(),
|
||||
×tamps[..41],
|
||||
&values[..41],
|
||||
false,
|
||||
),
|
||||
agg_raw_chunk_with_last(
|
||||
&schema,
|
||||
b"a",
|
||||
metric,
|
||||
×tamps[41..],
|
||||
&values[41..],
|
||||
true,
|
||||
),
|
||||
];
|
||||
let pool: Arc<dyn MemoryPool> = Arc::new(GreedyMemoryPool::new(1 << 26));
|
||||
let state = NativeExecutionState::try_new(&tree, pool, None).unwrap();
|
||||
let mut execution = NativeExecutor::new(state);
|
||||
for chunk in chunks {
|
||||
execution.push_raw(InputId::new(0), chunk).unwrap();
|
||||
assert!(
|
||||
execution.next_chunk().unwrap().is_none(),
|
||||
"the rate aggregate produced output before the whole input ended"
|
||||
);
|
||||
}
|
||||
let output = execution.finish().unwrap();
|
||||
assert_eq!(
|
||||
aggregate_results(&output),
|
||||
vec![(
|
||||
None,
|
||||
vec![("job".to_string(), "api".to_string())],
|
||||
(0..21)
|
||||
.map(|position| (position, Some(0.19999999999999998)))
|
||||
.collect(),
|
||||
)]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_compiled_sum_over_a_rate_with_empty_input_produces_no_groups() {
|
||||
let statement = EvalStmt {
|
||||
expr: promql_parser::parser::parse("sum by(job)(rate(x[5m]))").unwrap(),
|
||||
start: UNIX_EPOCH + Duration::from_secs(300),
|
||||
end: UNIX_EPOCH + Duration::from_secs(400),
|
||||
interval: Duration::from_secs(5),
|
||||
lookback_delta: Duration::from_secs(5),
|
||||
};
|
||||
let tree = compile(&statement, raw_schema(), SeriesOrdering::KeyThenTime).unwrap();
|
||||
assert_eq!(run_aggregate_tree(&tree, Vec::new()), Vec::new());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn label_functions_bind_their_change_into_a_map() {
|
||||
let cases = [
|
||||
@@ -4542,13 +4207,8 @@ fn timestamp_anchored_selector_retains_offset_and_ordinary_selection_keeps_it()
|
||||
(-15_000, -10_000),
|
||||
),
|
||||
] {
|
||||
let compiled = compile_selectors(
|
||||
&eval_stmt(query),
|
||||
&|_| Ok(raw_schema()),
|
||||
SeriesOrdering::KeyThenTime,
|
||||
Some(FieldKind::Float),
|
||||
)
|
||||
.unwrap_or_else(|error| panic!("{query}: {error}"));
|
||||
let compiled =
|
||||
compile_float_selectors(query).unwrap_or_else(|error| panic!("{query}: {error}"));
|
||||
let requirement = &compiled.inputs[0].requirement;
|
||||
assert_eq!(requirement.offset_ms, expected_offset, "{query}");
|
||||
assert_eq!(requirement.at_ms, expected_anchor, "{query}");
|
||||
@@ -4701,13 +4361,7 @@ fn subquery_selector_bounds_use_inner_grid_and_selector_offset() {
|
||||
(840_000, 1_170_000, false),
|
||||
),
|
||||
] {
|
||||
let compiled = compile_selectors(
|
||||
&eval_stmt(query),
|
||||
&|_| Ok(raw_schema()),
|
||||
SeriesOrdering::KeyThenTime,
|
||||
Some(FieldKind::Float),
|
||||
)
|
||||
.unwrap_or_else(|e| panic!("{query}: {e}"));
|
||||
let compiled = compile_float_selectors(query).unwrap_or_else(|e| panic!("{query}: {e}"));
|
||||
let req = &compiled.inputs[0].requirement;
|
||||
assert_eq!(
|
||||
(req.grid_start_ms, req.grid_end_ms),
|
||||
@@ -4739,22 +4393,10 @@ fn selector_binding_callback_error_is_returned_once() {
|
||||
#[test]
|
||||
fn validate_inputs_rejects_misbound_occurrences_and_accepts_scalar_wrappers() {
|
||||
for query in ["x * 2", "2 * x"] {
|
||||
let compiled = compile_selectors(
|
||||
&eval_stmt(query),
|
||||
&|_| Ok(raw_schema()),
|
||||
SeriesOrdering::KeyThenTime,
|
||||
Some(FieldKind::Float),
|
||||
)
|
||||
.unwrap();
|
||||
let compiled = compile_float_selectors(query).unwrap();
|
||||
validate_inputs(compiled.tree.root(), &compiled.inputs).unwrap();
|
||||
}
|
||||
let compiled = compile_selectors(
|
||||
&eval_stmt("x * 2"),
|
||||
&|_| Ok(raw_schema()),
|
||||
SeriesOrdering::KeyThenTime,
|
||||
Some(FieldKind::Float),
|
||||
)
|
||||
.unwrap();
|
||||
let compiled = compile_float_selectors("x * 2").unwrap();
|
||||
let original = compiled.inputs[0].clone();
|
||||
|
||||
for records in [Vec::new(), vec![original.clone(), original.clone()]] {
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user