Skip to content
59 changes: 58 additions & 1 deletion crates/executor/src/expressions/columnar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
//! distinct combination of dictionary keys, in order of first appearance,
//! so a label matcher is evaluated once per series, not once per sample.
//! Otherwise it runs once per row over the decoded columns it reads.
//! - **Per dictionary entry:** a series identity less some labels
//! (`sum without (instance)`'s [`Expression::LabelIdentity`]) over a
//! dictionary column rewrites each dictionary text once, as text, and
//! keeps the column's keys.
//!
//! Errors are those of row-by-row evaluation: vectorized operations cannot
//! fail, and the per-input path visits inputs in row order.
Expand Down Expand Up @@ -737,10 +741,58 @@ fn evaluate_per_key_combination(
Ok((results, indices))
}

/// Column `column` of `batch`, a dictionary of series identities, less the
/// labels `excluding`: each dictionary text rewritten once by
/// [`series_identity_without`](crate::physical_planner::promql_rows::series_identity_without)
/// into a new dictionary over the same keys, with no decoded [`Value`]. Two
/// texts can rewrite to one identity (`{"instance":"1","job":"a"}` and
/// `{"instance":"2","job":"a"}` less `instance`), so the new dictionary may
/// repeat a text; its readers compare texts, not keys (the group table
/// resolves each entry by its text). `None` when the column is not a
/// dictionary, when the dictionary is much larger than the batch (a slice
/// of a large batch), or when a text is malformed, so the per-row path
/// reads only the rows' entries and reports its error.
fn identity_without_per_entry(
batch: &Batch,
column: usize,
excluding: &[String],
) -> Option<ArrayRef> {
let dictionary = batch
.column(column)
.as_any()
.downcast_ref::<DictionaryArray<UInt32Type>>()?;
let texts = dictionary
.values()
.as_any()
.downcast_ref::<arrow::array::StringArray>()?;
if !arrow_columns::dense_dictionary(texts.len(), batch.num_rows()) {
return None;
}
let mut rewritten =
arrow::array::StringBuilder::with_capacity(texts.len(), texts.value_data().len());
for entry in 0..texts.len() {
if texts.is_null(entry) {
rewritten.append_null();
} else {
rewritten.append_value(
crate::physical_planner::promql_rows::series_identity_without(
texts.value(entry),
excluding,
)
.ok()?,
);
}
}
let rewritten =
DictionaryArray::try_new(dictionary.keys().clone(), Arc::new(rewritten.finish()))
.expect("the keys index the same number of texts");
Some(Arc::new(rewritten))
}

fn expression_columns(expression: &Expression, columns: &mut Vec<usize>) {
use Expression as E;
match expression {
E::Column(c) | E::ExactFloat64(c) => columns.push(*c),
E::Column(c) | E::ExactFloat64(c) | E::FloatText(c) => columns.push(*c),
E::LabelSet { column, .. } | E::Label { column, .. } | E::LabelIdentity { column, .. } => {
columns.push(*column)
}
Expand Down Expand Up @@ -779,6 +831,11 @@ impl Expression {
if let Some((vector, _)) = lower_expression(self, batch.schema()) {
return evaluate_vector(&vector, batch)?.into_array(batch.num_rows());
}
if let Expression::LabelIdentity { column, excluding } = self {
if let Some(array) = identity_without_per_entry(batch, *column, excluding) {
return Ok(array);
}
}
let mut columns = Vec::new();
expression_columns(self, &mut columns);
evaluate_per_input(batch, &distinct(columns), dtype, |row| self.evaluate(row))
Expand Down
70 changes: 57 additions & 13 deletions crates/executor/src/expressions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ pub enum Expression {
column: usize,
name: String,
},
/// Canonical encoding of a label map less `excluding`, identical to
/// `promql_rows::encode_series_identity`.
/// Canonical encoding of a label map, or of a series identity, less
/// `excluding`, identical to `promql_rows::encode_series_identity`.
LabelIdentity {
column: usize,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
Expand All @@ -85,6 +85,12 @@ pub enum Expression {
value: Value,
dtype: DataType,
},
/// A Float64 column's value as text, as Go's
/// `strconv.FormatFloat(v, 'f', -1, 64)` writes it (PromQL
/// `count_values`): the shortest decimal that reads back as `v`,
/// without exponent; `NaN`, `+Inf` and `-Inf`. `2.5` is `"2.5"`, `1e21`
/// is `"1000000000000000000000"` and `-0.0` is `"-0"`.
FloatText(usize),
Negate(Box<Expression>),
Arithmetic {
op: ArithmeticOpKind,
Expand Down Expand Up @@ -132,6 +138,7 @@ impl Expression {
}
Column(_)
| ExactFloat64(_)
| FloatText(_)
| LabelSet { .. }
| Label { .. }
| LabelIdentity { .. }
Expand All @@ -158,6 +165,7 @@ impl Expression {
}
Column(_)
| ExactFloat64(_)
| FloatText(_)
| LabelSet { .. }
| Label { .. }
| LabelIdentity { .. }
Expand Down Expand Up @@ -248,6 +256,15 @@ impl Expression {
}
Ok((expected, false))
}
FloatText(column) => {
if plain(input, *column)? != (&DataType::Float64, false) {
return Err(invalid("float text requires a non-null Float64"));
}
Ok((DataType::Utf8, false))
}
LabelIdentity { column, .. } if plain(input, *column)? == (&DataType::Utf8, false) => {
Ok((DataType::Utf8, false))
}
Label { column, .. } | LabelIdentity { column, .. } => {
if plain(input, *column)?
!= (
Expand Down Expand Up @@ -410,21 +427,48 @@ impl Expression {
}
Value::Utf8(found.unwrap_or_else(|| "".into()))
}
LabelIdentity { column, excluding } => {
let Value::Map(entries) = &row[*column] else {
return Err(invalid("label identity requires a map"));
FloatText(column) => {
let Value::Float64(value) = row[*column] else {
return Err(invalid("float text requires a Float64"));
};
Value::Utf8(
if value.is_nan() {
"NaN".into()
} else if value == f64::INFINITY {
"+Inf".into()
} else if value == f64::NEG_INFINITY {
"-Inf".into()
} else {
value.to_string()
}
.into(),
)
}
LabelIdentity { column, excluding } => {
let mut labels = std::collections::BTreeMap::new();
for (key, value) in entries.iter() {
let (Value::Utf8(key), Value::Utf8(value)) = (key, value) else {
return Err(invalid("label identity requires Utf8 entries"));
};
if excluding.iter().any(|label| label.as_str() == key.as_ref()) {
continue;
match &row[*column] {
Value::Utf8(identity) => {
return Ok(Value::Utf8(
crate::physical_planner::promql_rows::series_identity_without(
identity, excluding,
)?
.into(),
));
}
if labels.insert(key.to_string(), value.to_string()).is_some() {
return Err(invalid("duplicate label name"));
Value::Map(entries) => {
for (key, value) in entries.iter() {
let (Value::Utf8(key), Value::Utf8(value)) = (key, value) else {
return Err(invalid("label identity requires Utf8 entries"));
};
if excluding.iter().any(|label| label.as_str() == key.as_ref()) {
continue;
}
if labels.insert(key.to_string(), value.to_string()).is_some() {
return Err(invalid("duplicate label name"));
}
}
}
_ => return Err(invalid("label identity requires a map or series identity")),
}
Value::Utf8(
crate::physical_planner::promql_rows::encode_series_identity(&labels)?.into(),
Expand Down
Loading
Loading