From 08d0f31865f8d3d44fd41524221b38dc2fef46e9 Mon Sep 17 00:00:00 2001 From: Ruihang Xia Date: Wed, 9 Apr 2025 19:53:25 +0800 Subject: [PATCH] perf: avoid duplicate computation in promql (#5863) Signed-off-by: Ruihang Xia --- src/common/macro/src/range_fn.rs | 2 +- src/promql/src/functions/extrapolate_rate.rs | 2 +- src/promql/src/functions/holt_winters.rs | 2 +- src/promql/src/functions/idelta.rs | 2 +- src/promql/src/functions/predict_linear.rs | 2 +- src/promql/src/functions/quantile.rs | 2 +- src/promql/src/functions/quantile_aggr.rs | 2 +- src/promql/src/functions/round.rs | 2 +- .../common/tql-explain-analyze/analyze.result | 26 +++++++++++++++++++ .../common/tql-explain-analyze/analyze.sql | 8 ++++++ 10 files changed, 42 insertions(+), 8 deletions(-) diff --git a/src/common/macro/src/range_fn.rs b/src/common/macro/src/range_fn.rs index bf415240d9..b995e12789 100644 --- a/src/common/macro/src/range_fn.rs +++ b/src/common/macro/src/range_fn.rs @@ -139,7 +139,7 @@ fn build_struct( Self::name(), Self::input_type(), Self::return_type(), - Volatility::Immutable, + Volatility::Volatile, Arc::new(Self::calc) as _, ) } diff --git a/src/promql/src/functions/extrapolate_rate.rs b/src/promql/src/functions/extrapolate_rate.rs index 6ced7c9d4d..8977eaf083 100644 --- a/src/promql/src/functions/extrapolate_rate.rs +++ b/src/promql/src/functions/extrapolate_rate.rs @@ -77,7 +77,7 @@ impl ExtrapolatedRate IDelta { Self::name(), Self::input_type(), Self::return_type(), - Volatility::Immutable, + Volatility::Volatile, Arc::new(Self::calc) as _, ) } diff --git a/src/promql/src/functions/predict_linear.rs b/src/promql/src/functions/predict_linear.rs index b64e1c1066..4b945cabbb 100644 --- a/src/promql/src/functions/predict_linear.rs +++ b/src/promql/src/functions/predict_linear.rs @@ -55,7 +55,7 @@ impl PredictLinear { Self::name(), input_types, DataType::Float64, - Volatility::Immutable, + Volatility::Volatile, Arc::new(move |input: &_| Self::new(t).predict_linear(input)) as _, ) } diff --git a/src/promql/src/functions/quantile.rs b/src/promql/src/functions/quantile.rs index 4900ab4453..f975a76cf4 100644 --- a/src/promql/src/functions/quantile.rs +++ b/src/promql/src/functions/quantile.rs @@ -51,7 +51,7 @@ impl QuantileOverTime { Self::name(), input_types, DataType::Float64, - Volatility::Immutable, + Volatility::Volatile, Arc::new(move |input: &_| Self::new(quantile).quantile_over_time(input)) as _, ) } diff --git a/src/promql/src/functions/quantile_aggr.rs b/src/promql/src/functions/quantile_aggr.rs index e19cfeb21a..2f8d9edd9d 100644 --- a/src/promql/src/functions/quantile_aggr.rs +++ b/src/promql/src/functions/quantile_aggr.rs @@ -45,7 +45,7 @@ pub fn quantile_udaf(q: f64) -> Arc { vec![DataType::Float64], // Output type: the φ-quantile Arc::new(DataType::Float64), - Volatility::Immutable, + Volatility::Volatile, // Create the accumulator Arc::new(move |_| Ok(Box::new(QuantileAccumulator::new(q)))), // Intermediate state types diff --git a/src/promql/src/functions/round.rs b/src/promql/src/functions/round.rs index d52f84f261..d1c9d318d8 100644 --- a/src/promql/src/functions/round.rs +++ b/src/promql/src/functions/round.rs @@ -48,7 +48,7 @@ impl Round { Self::name(), Self::input_type(), Self::return_type(), - Volatility::Immutable, + Volatility::Volatile, Arc::new(move |input: &_| Self::new(nearest).calc(input)) as _, ) } diff --git a/tests/cases/standalone/common/tql-explain-analyze/analyze.result b/tests/cases/standalone/common/tql-explain-analyze/analyze.result index 9a25774e34..af1a3d4fce 100644 --- a/tests/cases/standalone/common/tql-explain-analyze/analyze.result +++ b/tests/cases/standalone/common/tql-explain-analyze/analyze.result @@ -125,6 +125,32 @@ TQL ANALYZE (0, 10, '5s') test; |_|_| Total rows: 0_| +-+-+-+ +-- SQLNESS REPLACE (metrics.*) REDACTED +-- SQLNESS REPLACE (RoundRobinBatch.*) REDACTED +-- SQLNESS REPLACE (-+) - +-- SQLNESS REPLACE (\s\s+) _ +-- SQLNESS REPLACE (peers.*) REDACTED +-- SQLNESS REPLACE region=\d+\(\d+,\s+\d+\) region=REDACTED +TQL ANALYZE (0, 10, '5s') rate(test[10s]); + ++-+-+-+ +| stage | node | plan_| ++-+-+-+ +| 0_| 0_|_CoalesceBatchesExec: target_batch_size=8192 REDACTED +|_|_|_FilterExec: prom_rate(j_range,i,j)@1 IS NOT NULL REDACTED +|_|_|_ProjectionExec: expr=[j@1 as j, prom_rate(j_range@4, i@0, j@1) as prom_rate(j_range,i,j), k@2 as k, l@3 as l] REDACTED +|_|_|_PromRangeManipulateExec: req range=[0..10000], interval=[5000], eval range=[10000], time index=[j] REDACTED +|_|_|_PromSeriesNormalizeExec: offset=[0], time index=[j], filter NaN: [true] REDACTED +|_|_|_PromSeriesDivideExec: tags=["k", "l"] REDACTED +|_|_|_MergeScanExec: REDACTED +|_|_|_| +| 1_| 0_|_SeqScan: region=REDACTED, partition_count=0 (0 memtable ranges, 0 file 0 ranges), distribution=PerSeries REDACTED +|_|_|_| +| 1_| 1_|_SeqScan: region=REDACTED, partition_count=0 (0 memtable ranges, 0 file 0 ranges), distribution=PerSeries REDACTED +|_|_|_| +|_|_| Total rows: 0_| ++-+-+-+ + drop table test; Affected Rows: 0 diff --git a/tests/cases/standalone/common/tql-explain-analyze/analyze.sql b/tests/cases/standalone/common/tql-explain-analyze/analyze.sql index 0fb3613746..99de9a0a9d 100644 --- a/tests/cases/standalone/common/tql-explain-analyze/analyze.sql +++ b/tests/cases/standalone/common/tql-explain-analyze/analyze.sql @@ -55,4 +55,12 @@ CREATE TABLE test(i DOUBLE, j TIMESTAMP TIME INDEX, k STRING, l STRING, PRIMARY -- SQLNESS REPLACE region=\d+\(\d+,\s+\d+\) region=REDACTED TQL ANALYZE (0, 10, '5s') test; +-- SQLNESS REPLACE (metrics.*) REDACTED +-- SQLNESS REPLACE (RoundRobinBatch.*) REDACTED +-- SQLNESS REPLACE (-+) - +-- SQLNESS REPLACE (\s\s+) _ +-- SQLNESS REPLACE (peers.*) REDACTED +-- SQLNESS REPLACE region=\d+\(\d+,\s+\d+\) region=REDACTED +TQL ANALYZE (0, 10, '5s') rate(test[10s]); + drop table test;