Skip to content

Commit e327e6c

Browse files
some more refactor
1 parent fc0be7e commit e327e6c

2 files changed

Lines changed: 41 additions & 26 deletions

File tree

‎asap-common/dependencies/rs/sql_utilities/src/ast_matching/sqlpattern_matcher.rs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,14 @@ impl SQLQuery {
7373
}
7474

7575
/// The outer (spatial / single) query's data — always `query_data[0]`.
76-
pub fn outer_data(&self) -> &SQLQueryData {
77-
&self.query_data[0]
76+
pub fn outer_data(&self) -> Option<&SQLQueryData> {
77+
self.query_data.first()
7878
}
7979

8080
/// The inner (temporal) query's data for nested queries — always `query_data[1]`.
8181
/// Only valid for `OneTemporalOneSpatial` patterns.
82-
pub fn inner_data(&self) -> &SQLQueryData {
83-
&self.query_data[1]
82+
pub fn inner_data(&self) -> Option<&SQLQueryData> {
83+
self.query_data.get(1)
8484
}
8585
}
8686

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

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -395,13 +395,21 @@ impl SimpleEngine {
395395
) -> u64 {
396396
match query_pattern_type {
397397
QueryPatternType::OnlyTemporal => {
398-
let scrape_intervals =
399-
match_result.outer_data().time_info.clone().get_duration() as u64;
398+
let scrape_intervals = match_result
399+
.outer_data()
400+
.expect("OnlyTemporal pattern guarantees outer_data is present")
401+
.time_info
402+
.clone()
403+
.get_duration() as u64;
400404
end_timestamp - (scrape_intervals * self.prometheus_scrape_interval * 1000)
401405
}
402406
QueryPatternType::OneTemporalOneSpatial => {
403-
let scrape_intervals =
404-
match_result.inner_data().time_info.clone().get_duration() as u64;
407+
let scrape_intervals = match_result
408+
.inner_data()
409+
.expect("OneTemporalOneSpatial pattern guarantees inner_data is present")
410+
.time_info
411+
.clone()
412+
.get_duration() as u64;
405413
end_timestamp - (scrape_intervals * self.prometheus_scrape_interval * 1000)
406414
}
407415
QueryPatternType::OnlySpatial => {
@@ -1627,12 +1635,15 @@ impl SimpleEngine {
16271635
match_result: &SQLQuery,
16281636
query_pattern_type: QueryPatternType,
16291637
) -> QueryRequirements {
1630-
let query_data = match_result.outer_data();
1638+
let query_data = match_result
1639+
.outer_data()
1640+
.expect("build_query_requirements_sql called on valid SQLQuery");
16311641
let metric = query_data.metric.clone();
16321642

16331643
let statistic_name = match query_pattern_type {
16341644
QueryPatternType::OneTemporalOneSpatial => match_result
16351645
.inner_data()
1646+
.expect("OneTemporalOneSpatial pattern guarantees inner_data is present")
16361647
.aggregation_info
16371648
.get_name()
16381649
.to_lowercase(),
@@ -1650,8 +1661,12 @@ impl SimpleEngine {
16501661
Some(scrape_intervals * self.prometheus_scrape_interval * 1000)
16511662
}
16521663
QueryPatternType::OneTemporalOneSpatial => {
1653-
let scrape_intervals =
1654-
match_result.inner_data().time_info.clone().get_duration() as u64;
1664+
let scrape_intervals = match_result
1665+
.inner_data()
1666+
.expect("OneTemporalOneSpatial pattern guarantees inner_data is present")
1667+
.time_info
1668+
.clone()
1669+
.get_duration() as u64;
16551670
Some(scrape_intervals * self.prometheus_scrape_interval * 1000)
16561671
}
16571672
};
@@ -1845,7 +1860,7 @@ impl SimpleEngine {
18451860
// so we need to use the inner (temporal) query's time_info to compute query_time
18461861
let query_time = match query_pattern_type {
18471862
QueryPatternType::OneTemporalOneSpatial => {
1848-
let inner_time_info = &match_result.inner_data().time_info;
1863+
let inner_time_info = &match_result.inner_data()?.time_info;
18491864
Self::convert_query_time_to_data_time(
18501865
inner_time_info.get_start() + inner_time_info.get_duration(),
18511866
)
@@ -1876,17 +1891,17 @@ impl SimpleEngine {
18761891
// Potentially change SQLQueryType
18771892
1 => {
18781893
// For non-nested queries, output associated labels
1879-
let labels = &match_result.outer_data().labels;
1894+
let labels = &match_result.outer_data()?.labels;
18801895

18811896
KeyByLabelNames::new(labels.clone().into_iter().collect())
18821897
}
18831898
2 => {
18841899
// Extract spatial aggregation output labels using AST-based approach
1885-
let temporal_labels = &match_result.inner_data().labels;
1886-
let spatial_labels = &match_result.outer_data().labels;
1900+
let temporal_labels = &match_result.inner_data()?.labels;
1901+
let spatial_labels = &match_result.outer_data()?.labels;
18871902

1888-
let temporal_aggregation = &match_result.inner_data().aggregation_info;
1889-
let spatial_aggregation = &match_result.outer_data().aggregation_info;
1903+
let temporal_aggregation = &match_result.inner_data()?.aggregation_info;
1904+
let spatial_aggregation = &match_result.outer_data()?.aggregation_info;
18901905

18911906
match self.sql_get_is_collapsable(temporal_aggregation, spatial_aggregation) {
18921907
// If false: get all labels, which are all temporal labels. If true, get only spatial labels
@@ -1905,23 +1920,23 @@ impl SimpleEngine {
19051920
QueryPatternType::OnlyTemporal => {
19061921
// Use the temporal aggregation (first subquery)
19071922
match_result
1908-
.outer_data()
1923+
.outer_data()?
19091924
.aggregation_info
19101925
.get_name()
19111926
.to_lowercase()
19121927
}
19131928
QueryPatternType::OneTemporalOneSpatial => {
19141929
// Use the temporal aggregation (second subquery contains temporal)
19151930
match_result
1916-
.inner_data()
1931+
.inner_data()?
19171932
.aggregation_info
19181933
.get_name()
19191934
.to_lowercase()
19201935
}
19211936
QueryPatternType::OnlySpatial => {
19221937
// Use the spatial aggregation (first subquery)
19231938
match_result
1924-
.outer_data()
1939+
.outer_data()?
19251940
.aggregation_info
19261941
.get_name()
19271942
.to_lowercase()
@@ -1966,11 +1981,11 @@ impl SimpleEngine {
19661981
.find_compatible_aggregation(&requirements)?
19671982
};
19681983

1969-
let metric = &match_result.outer_data().metric;
1984+
let metric = &match_result.outer_data()?.metric;
19701985

19711986
let spatial_filter = if query_pattern_type == QueryPatternType::OneTemporalOneSpatial {
19721987
match_result
1973-
.outer_data()
1988+
.outer_data()?
19741989
.labels
19751990
.iter()
19761991
.cloned()
@@ -2055,7 +2070,7 @@ impl SimpleEngine {
20552070
// Output labels are the GROUP BY columns (subset of all labels)
20562071
let query_output_labels = KeyByLabelNames::new(
20572072
match_result
2058-
.outer_data()
2073+
.outer_data()?
20592074
.labels
20602075
.clone()
20612076
.into_iter()
@@ -2064,7 +2079,7 @@ impl SimpleEngine {
20642079

20652080
// Get the statistic from the aggregation
20662081
let statistic_name = match_result
2067-
.outer_data()
2082+
.outer_data()?
20682083
.aggregation_info
20692084
.get_name()
20702085
.to_lowercase();
@@ -2088,7 +2103,7 @@ impl SimpleEngine {
20882103
// Calculate timestamps - similar to OnlyTemporal
20892104
let end_timestamp =
20902105
self.validate_and_align_end_timestamp(query_time, QueryPatternType::OnlyTemporal);
2091-
let scrape_intervals = match_result.outer_data().time_info.get_duration() as u64;
2106+
let scrape_intervals = match_result.outer_data()?.time_info.get_duration() as u64;
20922107
let start_timestamp =
20932108
end_timestamp - (scrape_intervals * self.prometheus_scrape_interval * 1000);
20942109

@@ -2116,7 +2131,7 @@ impl SimpleEngine {
21162131
self.streaming_config
21172132
.find_compatible_aggregation(&requirements)?
21182133
};
2119-
let metric = &match_result.outer_data().metric;
2134+
let metric = &match_result.outer_data()?.metric;
21202135

21212136
self.build_sql_execution_context_tail(
21222137
metric,

0 commit comments

Comments
 (0)