fix: label_join should work with unknown (#6714)

* fix: label_join should work with unknown

Signed-off-by: yihong0618 <zouzou0208@gmail.com>

* fix: address comments

Signed-off-by: yihong0618 <zouzou0208@gmail.com>
Co-authored-by: Jiachun Feng <jiachun_feng@proton.me>

* fix: address forget comments

Signed-off-by: yihong0618 <zouzou0208@gmail.com>

---------

Signed-off-by: yihong0618 <zouzou0208@gmail.com>
Co-authored-by: Jiachun Feng <jiachun_feng@proton.me>
This commit is contained in:
yihong
2025-08-13 11:45:40 +08:00
committed by GitHub
parent 1d84e802d8
commit 5eb491df12
3 changed files with 32 additions and 6 deletions

View File

@@ -1716,8 +1716,11 @@ impl PromPlanner {
}
"label_join" => {
let (concat_expr, dst_label) =
Self::build_concat_labels_expr(&mut other_input_exprs, query_engine_state)?;
let (concat_expr, dst_label) = Self::build_concat_labels_expr(
&mut other_input_exprs,
&self.ctx,
query_engine_state,
)?;
// Reserve the current field columns except the `dst_label`.
for value in &self.ctx.field_columns {
@@ -2000,6 +2003,7 @@ impl PromPlanner {
/// Build expr for `label_join` function
fn build_concat_labels_expr(
other_input_exprs: &mut VecDeque<DfExpr>,
ctx: &PromPlannerContext,
query_engine_state: &QueryEngineState,
) -> Result<(DfExpr, String)> {
// label_join(vector, dst_label, separator, src_label_1, src_label_2, ...)
@@ -2018,17 +2022,30 @@ impl PromPlanner {
}
.fail()?,
};
// Create a set of available columns (tag columns + field columns + time index column)
let available_columns: HashSet<&str> = ctx
.tag_columns
.iter()
.chain(ctx.field_columns.iter())
.chain(ctx.time_index_column.as_ref())
.map(|s| s.as_str())
.collect();
let src_labels = other_input_exprs
.clone()
.into_iter()
.iter()
.map(|expr| {
// Cast source label into column
// Cast source label into column or null literal
match expr {
DfExpr::Literal(ScalarValue::Utf8(Some(label))) => {
if label.is_empty() {
Ok(DfExpr::Literal(ScalarValue::Null))
} else {
} else if available_columns.contains(label.as_str()) {
// Label exists in the table schema
Ok(DfExpr::Column(Column::from_name(label)))
} else {
// Label doesn't exist, treat as empty string (null)
Ok(DfExpr::Literal(ScalarValue::Null))
}
}
other => UnexpectedPlanExprSnafu {

View File

@@ -100,6 +100,12 @@ TQL EVAL (0, 15, '5s') label_join(test{host="host1"}, "new_host", "-", "idc", "h
| 1970-01-01T00:00:15 | 7 | idc4:zone3-host1 | host1 | idc4:zone3 |
+---------------------+-----+------------------+-------+------------+
-- Should return empty result instead of error
tql eval label_join(demo_num_cpus, "new_label", "-", "instance", "job");
++
++
-- SQLNESS SORT_RESULT 3 1
TQL EVAL (0, 15, '5s') label_replace(test{host="host1"}, "new_idc", "$2", "idc", "(.*):(.*)");

View File

@@ -34,6 +34,9 @@ TQL EVAL (0, 15, '5s') label_join(test{host="host1"}, "host", "-", "");
-- SQLNESS SORT_RESULT 3 1
TQL EVAL (0, 15, '5s') label_join(test{host="host1"}, "new_host", "-", "idc", "host");
-- Should return empty result instead of error
tql eval label_join(demo_num_cpus, "new_label", "-", "instance", "job");
-- SQLNESS SORT_RESULT 3 1
TQL EVAL (0, 15, '5s') label_replace(test{host="host1"}, "new_idc", "$2", "idc", "(.*):(.*)");