From e4556ce12b3b7d4863380262c4e830e7ad6dc977 Mon Sep 17 00:00:00 2001 From: Ruihang Xia Date: Thu, 17 Apr 2025 22:01:21 +0800 Subject: [PATCH] fix: label values potential panic (#5921) Signed-off-by: Ruihang Xia --- src/frontend/src/instance/promql.rs | 8 ++------ tests-integration/src/test_util.rs | 9 +++++++++ tests-integration/tests/http.rs | 8 ++++++++ 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/frontend/src/instance/promql.rs b/src/frontend/src/instance/promql.rs index 1dca064982..f3f276bdd9 100644 --- a/src/frontend/src/instance/promql.rs +++ b/src/frontend/src/instance/promql.rs @@ -161,15 +161,11 @@ impl Instance { let mut results = Vec::with_capacity(batches.iter().map(|b| b.num_rows()).sum()); for batch in batches { - // Only one column the results, ensured by `prometheus::label_values_matchers_to_plan`. + // Only one column in results, ensured by `prometheus::label_values_matchers_to_plan`. let names = batch.column(0); for i in 0..names.len() { - let Value::String(name) = names.get(i) else { - unreachable!(); - }; - - results.push(name.into_string()); + results.push(names.get(i).to_string()); } } diff --git a/tests-integration/src/test_util.rs b/tests-integration/src/test_util.rs index b842c9412c..ee68b8d715 100644 --- a/tests-integration/src/test_util.rs +++ b/tests-integration/src/test_util.rs @@ -503,6 +503,9 @@ pub async fn setup_test_prom_app_with_frontend( let sql = "INSERT INTO demo_metrics(idc, val, ts) VALUES ('idc1', 1.1, 0), ('idc2', 2.1, 600000)"; run_sql(sql, &instance).await; + // insert a row with empty label + let sql = "INSERT INTO demo_metrics(val, ts) VALUES (1.1, 0)"; + run_sql(sql, &instance).await; // build physical table let sql = "CREATE TABLE phy2 (ts timestamp(9) time index, val double, host string primary key) engine=metric with ('physical_metric_table' = '')"; @@ -512,6 +515,12 @@ pub async fn setup_test_prom_app_with_frontend( let sql = "INSERT INTO demo_metrics_with_nanos(idc, val, ts) VALUES ('idc1', 1.1, 0)"; run_sql(sql, &instance).await; + // a mito table with non-prometheus compatible values + let sql = "CREATE TABLE mito (ts timestamp(9) time index, val double, host bigint primary key) engine=mito"; + run_sql(sql, &instance).await; + let sql = "INSERT INTO mito(host, val, ts) VALUES (1, 1.1, 0)"; + run_sql(sql, &instance).await; + let http_opts = HttpOptions { addr: format!("127.0.0.1:{}", ports::get_port()), ..Default::default() diff --git a/tests-integration/tests/http.rs b/tests-integration/tests/http.rs index 8ef27cdbf0..9e369e654d 100644 --- a/tests-integration/tests/http.rs +++ b/tests-integration/tests/http.rs @@ -765,6 +765,13 @@ pub async fn test_prom_http_api(store_type: StorageType) { assert!(prom_resp.error.is_none()); assert!(prom_resp.error_type.is_none()); + // query non-string value + let res = client + .get("/v1/prometheus/api/v1/label/host/values?match[]=mito") + .send() + .await; + assert_eq!(res.status(), StatusCode::OK); + // query `__name__` without match[] // create a physical table and a logical table let res = client @@ -794,6 +801,7 @@ pub async fn test_prom_http_api(store_type: StorageType) { "demo_metrics".to_string(), "demo_metrics_with_nanos".to_string(), "logic_table".to_string(), + "mito".to_string(), "numbers".to_string() ]) );