mirror of
https://github.com/GreptimeTeam/greptimedb.git
synced 2026-11-28 13:44:03 +00:00
fix(promql): strip parentheses the native compiler does not read through
Parentheses group; they say nothing about the expression inside them, so `(rate(x[5m]))` asks for exactly what `rate(x[5m])` asks for. The compiler matched the bare shape only and refused the parenthesised one as NotImplemented, rejecting a statement on how it was typed rather than on what it means. Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>
This commit is contained in:
@@ -58,7 +58,7 @@ pub fn compile(
|
||||
input: Arc<SeriesSchema>,
|
||||
ordering: SeriesOrdering,
|
||||
) -> Result<NativeTree> {
|
||||
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<RangeFunction> {
|
||||
}
|
||||
}
|
||||
|
||||
/// 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!(
|
||||
|
||||
@@ -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"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user