fix(promql): read replacement group names with Go's Unicode classes

Go reads a group name as Unicode letters, decimal digits and _, so in
$1² the name is 1; char::is_alphanumeric also takes ² and other number
characters.

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>
This commit is contained in:
Dennis Zhuang
2026-10-17 23:22:28 +08:00
parent 64ac8a8eb8
commit 9611bb53a4
2 changed files with 7 additions and 4 deletions
+6 -4
View File
@@ -15,7 +15,7 @@
//! `label_replace` and `label_join` over evaluated series, following Prometheus
//! `evalLabelReplace`, `evalLabelJoin` and `mergeSeriesWithSameLabelset`.
use std::sync::Arc;
use std::sync::{Arc, LazyLock};
use datafusion::error::{DataFusionError, Result};
use datatypes::series::MetricName;
@@ -59,6 +59,10 @@ pub fn label_replace(
merge_same_labelset(schema, series)
}
/// Go `regexp.Expand` reads a group name as Unicode letters (L), decimal digits (Nd) and `_`,
/// narrower than `char::is_alphanumeric`.
static GROUP_NAME: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"^[\p{L}\p{Nd}_]*").unwrap());
/// Go `regexp.Expand`, whose template syntax differs from `regex::Captures::expand`: a numeric
/// reference with a leading zero, like `$01`, or of more than nine digits is a group name.
fn expand(regex: &Regex, captures: &regex::Captures<'_>, template: &str, out: &mut String) {
@@ -75,9 +79,7 @@ fn expand(regex: &Regex, captures: &regex::Captures<'_>, template: &str, out: &m
Some(body) => (true, body),
None => (false, rest),
};
let len = body
.find(|c: char| !c.is_alphanumeric() && c != '_')
.unwrap_or(body.len());
let len = GROUP_NAME.find(body).map_or(0, |name| name.end());
let name = &body[..len];
let closed = !braced || body[len..].starts_with('}');
if name.is_empty() || !closed {
+1
View File
@@ -106,6 +106,7 @@ fn replacements_expand_like_go() {
("$0", "abc-42"),
("$9", ""),
("$1234567890", ""),
("$1²", "abc²"),
] {
let mut out = String::new();
super::expand(&regex, &captures, template, &mut out);