Skip to content

Commit 60b2224

Browse files
refactor(query-engine): dedup controller-pattern matching loop in promql.rs (#515)
1 parent 430fef6 commit 60b2224

1 file changed

Lines changed: 27 additions & 33 deletions

File tree

  • asap-query-engine/src/engines/simple_engine

asap-query-engine/src/engines/simple_engine/promql.rs

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,30 @@ impl SimpleEngine {
174174
.cloned()
175175
}
176176

177+
/// Scans `self.controller_patterns` for the first `PromQLPattern` that matches
178+
/// `ast`, returning its `QueryPatternType` and match result. `query` is used
179+
/// only for debug logging.
180+
fn find_matching_controller_pattern(
181+
&self,
182+
ast: &promql_parser::parser::Expr,
183+
query: &str,
184+
) -> Option<(QueryPatternType, PromQLMatchResult)> {
185+
for (pattern_type, patterns) in &self.controller_patterns {
186+
for pattern in patterns {
187+
debug!(
188+
"Trying pattern type: {:?} for query: {}",
189+
pattern_type, query
190+
);
191+
let match_result = pattern.matches(ast);
192+
debug!("Match result: {:?}", match_result);
193+
if match_result.matches {
194+
return Some((*pattern_type, match_result));
195+
}
196+
}
197+
}
198+
None
199+
}
200+
177201
/// Variant of `build_query_execution_context_promql` that accepts a pre-parsed
178202
/// AST node and a pre-found `QueryConfig`, avoiding redundant parsing and lookup.
179203
pub fn build_query_execution_context_from_ast(
@@ -184,21 +208,8 @@ impl SimpleEngine {
184208
) -> Option<QueryExecutionContext> {
185209
let query_time = Self::convert_query_time_to_data_time(time);
186210

187-
let mut found_match = None;
188-
for (pattern_type, patterns) in &self.controller_patterns {
189-
for pattern in patterns {
190-
let match_result = pattern.matches(arm_ast);
191-
if match_result.matches {
192-
found_match = Some((*pattern_type, match_result));
193-
break;
194-
}
195-
}
196-
if found_match.is_some() {
197-
break;
198-
}
199-
}
200-
201-
let (query_pattern_type, match_result) = found_match?;
211+
let (query_pattern_type, match_result) =
212+
self.find_matching_controller_pattern(arm_ast, &query_config.query)?;
202213

203214
let agg_info = self
204215
.get_aggregation_id_info(query_config)
@@ -984,24 +995,7 @@ impl SimpleEngine {
984995

985996
let pattern_match_start_time = Instant::now();
986997

987-
let mut found_match = None;
988-
for (pattern_type, patterns) in &self.controller_patterns {
989-
for pattern in patterns {
990-
debug!(
991-
"Trying pattern type: {:?} for query: {}",
992-
pattern_type, query
993-
);
994-
let match_result = pattern.matches(&ast);
995-
debug!("Match result: {:?}", match_result);
996-
if match_result.matches {
997-
found_match = Some((*pattern_type, match_result));
998-
break;
999-
}
1000-
}
1001-
if found_match.is_some() {
1002-
break;
1003-
}
1004-
}
998+
let found_match = self.find_matching_controller_pattern(&ast, &query);
1005999

10061000
let (query_pattern_type, match_result) = match found_match {
10071001
Some((pt, result)) => {

0 commit comments

Comments
 (0)