diff --git a/src/query/src/promql/native_compile.rs b/src/query/src/promql/native_compile.rs index 4268f87ad82..af069c3a685 100644 --- a/src/query/src/promql/native_compile.rs +++ b/src/query/src/promql/native_compile.rs @@ -58,7 +58,7 @@ pub fn compile( input: Arc, ordering: SeriesOrdering, ) -> Result { - let PromExpr::Call(Call { func, args }) = &stmt.expr else { + let PromExpr::Call(Call { func, args }) = unparen(&stmt.expr) else { return Err(not_implemented(&format!("the expression `{}`", stmt.expr))); }; let Some(function) = range_function(func.name) else { @@ -74,7 +74,7 @@ pub fn compile( args.args.len() ))); }; - let PromExpr::MatrixSelector(MatrixSelector { vs, range }) = argument.as_ref() else { + let PromExpr::MatrixSelector(MatrixSelector { vs, range }) = unparen(argument) else { return Err(not_implemented(&format!( "a call to `{}` whose argument is `{}`", func.name, argument @@ -228,6 +228,18 @@ fn range_function(name: &str) -> Option { } } +/// The expression a parenthesised one wraps, however many parentheses were written. +/// +/// Parentheses group, so they say nothing about the expression inside them: `rate((x[5m]))` asks +/// for exactly what `rate(x[5m])` asks for. Refusing it would reject a statement on the way it was +/// typed rather than on what it means. +fn unparen(mut expr: &PromExpr) -> &PromExpr { + while let PromExpr::Paren(paren) = expr { + expr = &paren.expr; + } + expr +} + /// A rejection of a shape outside [`SUPPORTED`]. fn not_implemented(found: &str) -> DataFusionError { DataFusionError::NotImplemented(format!( diff --git a/src/query/src/promql/native_compile/tests.rs b/src/query/src/promql/native_compile/tests.rs index b30a4e5be36..95179fa0996 100644 --- a/src/query/src/promql/native_compile/tests.rs +++ b/src/query/src/promql/native_compile/tests.rs @@ -349,3 +349,30 @@ fn the_display_of_a_compiled_tree_is_the_tree() { .join("\n") ); } + +/// Parentheses group; they do not change what a statement asks for. A compiler that refused them +/// would reject a query on how it was typed rather than on what it means, and a statement written +/// with them is otherwise exactly the shape the compiler accepts. +#[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" + ); + } +}