Skip to content

Commit ff8e564

Browse files
committed
Cargo fmt and clippy. Point elastic_dsl_utilities to custom copy of opensearch_dsl.
1 parent 45f8a7a commit ff8e564

4 files changed

Lines changed: 46 additions & 41 deletions

File tree

‎Cargo.lock‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎asap-common/dependencies/rs/elastic_dsl_utilities/Cargo.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ version.workspace = true
77
serde.workspace = true
88
serde_json.workspace = true
99
chrono.workspace = true
10-
opensearch-dsl = { version = "0.3.1", path = "../../../../../opensearch-client-rs/opensearch-dsl" }
10+
opensearch-dsl = { version = "0.1.0", path = "../../../../../opensearch-dsl" }

‎asap-common/dependencies/rs/elastic_dsl_utilities/src/ast_parsing/extract_info.rs‎

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@ use opensearch_dsl::{self as dsl};
66
use serde_json;
77

88
pub fn extract_query_info(query: &str) -> Option<ElasticDSLQueryInfo> {
9-
// Main entry point for extracting relevant information from the parsed query pattern. This function would contain the core logic for traversing the AST and applying rules to determine the target field, predicates, group by specifications, and aggregation type.
10-
let search_request = parse_query_to_ast(query)?;
9+
// Main entry point for extracting relevant information from the parsed query pattern.
10+
let search_request = serde_json::from_str(query).ok()?;
1111
walk_ast_and_extract_info(&search_request)
12-
}
12+
}
1313

1414
pub fn parse_query_to_ast(query: &str) -> Option<dsl::Search> {
15-
let search_request = serde_json::from_str(query).ok()?;
16-
search_request
15+
serde_json::from_str(query).ok()?
1716
}
1817

1918
pub fn walk_ast_and_extract_info(ast: &dsl::Search) -> Option<ElasticDSLQueryInfo> {
@@ -24,15 +23,15 @@ pub fn walk_ast_and_extract_info(ast: &dsl::Search) -> Option<ElasticDSLQueryInf
2423
Some(dsl::Query::Bool(bool_query)) => {
2524
// Extract information from the bool query
2625
walk_bool_query_and_extract_info(&bool_query)
27-
},
26+
}
2827
Some(other) => {
2928
// Predicates may just be specified directly without enclosing bool context.
3029
if let Some(predicate) = extract_predicates_from_query(&other) {
3130
vec![predicate]
3231
} else {
3332
Vec::new()
3433
}
35-
},
34+
}
3635
None => Vec::new(), // Return an empty vector of predicates if no query is specified
3736
};
3837
let (target_field, aggregation_type, group_by_spec) =
@@ -71,7 +70,7 @@ fn extract_predicates_from_query(query: &dsl::Query) -> Option<Predicate> {
7170
return None; // Skip if term query value cannot be mapped to a JSON value
7271
};
7372
// Process the term query information as needed
74-
return Some(Predicate::Term {
73+
Some(Predicate::Term {
7574
field,
7675
value: term_value,
7776
})
@@ -83,21 +82,17 @@ fn extract_predicates_from_query(query: &dsl::Query) -> Option<Predicate> {
8382
let gte = range_query.gte.clone();
8483
let lte = range_query.lte.clone();
8584
// Process the range query information as needed
86-
let gte_value = gte
87-
.as_ref()
88-
.and_then(|gte_term| map_term_to_json_value(gte_term));
89-
let lte_value = lte
90-
.as_ref()
91-
.and_then(|lte_term| map_term_to_json_value(lte_term));
92-
return Some(Predicate::Range {
85+
let gte_value = gte.as_ref().and_then(map_term_to_json_value);
86+
let lte_value = lte.as_ref().and_then(map_term_to_json_value);
87+
Some(Predicate::Range {
9388
field,
9489
gte: gte_value,
9590
lte: lte_value,
9691
})
97-
},
92+
}
9893
_ => {
9994
// Handle other query types
100-
return None; // Skip unsupported query types
95+
None // Skip unsupported query types
10196
}
10297
}
10398
}
@@ -106,7 +101,7 @@ fn walk_aggregations_and_extract_info(
106101
aggregations: &dsl::Aggregations,
107102
) -> Option<(FieldName, AggregationType, Option<GroupBySpec>)> {
108103
// Traverse the aggregations in the AST and extracting relevant information. Extract the first valid aggregation type found, along with any associated group by specifications.
109-
for (_, agg) in aggregations {
104+
for agg in aggregations.values() {
110105
match agg {
111106
dsl::Aggregation::MultiTerms(terms_agg) => {
112107
// Extract information from the terms aggregation
@@ -141,7 +136,7 @@ fn walk_aggregations_and_extract_info(
141136
}
142137
other => {
143138
// Handle other aggregation types
144-
let (target_field, aggregation_type) = extract_aggregation_info(&other)?;
139+
let (target_field, aggregation_type) = extract_aggregation_info(other)?;
145140
return Some((target_field, aggregation_type, None));
146141
}
147142
}
@@ -151,8 +146,8 @@ fn walk_aggregations_and_extract_info(
151146

152147
fn find_aggregation_info(aggregations: &dsl::Aggregations) -> Option<(FieldName, AggregationType)> {
153148
// Placeholder for extracting specific information from an aggregation node
154-
for (_, agg) in aggregations {
155-
let (field, aggregation_type) = extract_aggregation_info(&agg)?;
149+
if let Some((_, agg)) = aggregations.iter().next() {
150+
let (field, aggregation_type) = extract_aggregation_info(agg)?;
156151
return Some((field, aggregation_type));
157152
}
158153
None // Return None if no relevant aggregation information is found
@@ -202,11 +197,11 @@ fn map_term_to_json_value(term: &dsl::Term) -> Option<TermValue> {
202197
let value_str = value.to_string(); // Convert the term value to a string representation
203198
Some(TermValue::String(value_str))
204199
}
205-
dsl::Term::Float32(value) => Some(TermValue::Float(value.clone() as f64)),
206-
dsl::Term::Float64(value) => Some(TermValue::Float(value.clone())),
207-
dsl::Term::PositiveNumber(value) => Some(TermValue::UnsignedInt(value.clone())),
208-
dsl::Term::NegativeNumber(value) => Some(TermValue::Int(value.clone())),
209-
dsl::Term::Boolean(value) => Some(TermValue::Boolean(value.clone())),
200+
dsl::Term::Float32(value) => Some(TermValue::Float(*value as f64)),
201+
dsl::Term::Float64(value) => Some(TermValue::Float(*value)),
202+
dsl::Term::PositiveNumber(value) => Some(TermValue::UnsignedInt(*value)),
203+
dsl::Term::NegativeNumber(value) => Some(TermValue::Int(*value)),
204+
dsl::Term::Boolean(value) => Some(TermValue::Boolean(*value)),
210205
}
211206
}
212207

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

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
use super::SimpleEngine;
66
use super::{QueryExecutionContext, QueryMetadata, QueryTimestamps};
77
use crate::engines::query_result::QueryResult;
8-
use elastic_dsl_utilities::ast_parsing::{self, ElasticDSLQueryInfo, GroupBySpec, AggregationType, Predicate};
9-
use elastic_dsl_utilities::datemath::{range_query_to_time_range};
8+
use elastic_dsl_utilities::ast_parsing::{
9+
self, AggregationType, ElasticDSLQueryInfo, GroupBySpec, Predicate,
10+
};
11+
use elastic_dsl_utilities::datemath::range_query_to_time_range;
1012
use promql_utilities::data_model::KeyByLabelNames;
1113
use promql_utilities::query_logics::enums::Statistic;
1214
use std::collections::HashMap;
@@ -55,7 +57,7 @@ impl SimpleEngine {
5557

5658
let spatial_filter = String::new(); // Placeholder - extract from query if applicable
5759

58-
// Parse time range information from first query predicate if available, otherwise default to entire history up to query_time.
60+
// Parse time range information from first query predicate if available, otherwise default to entire history up to query_time.
5961
let timestamps = self.resolve_query_time_range_elastic(query_time, query_info);
6062

6163
let query_plan = self
@@ -106,7 +108,9 @@ impl SimpleEngine {
106108
// By default, we only include grouping labels in the output for ES DSL.
107109
let query_output_labels = match &query_info.group_by_buckets {
108110
Some(GroupBySpec::Fields(fields)) => KeyByLabelNames::new(fields.clone()),
109-
Some(GroupBySpec::Filters(_)) => { return None; } // We don't support filter-based group by in ES DSL for now, so return None to indicate unsupported query pattern.
111+
Some(GroupBySpec::Filters(_)) => {
112+
return None;
113+
} // We don't support filter-based group by in ES DSL for now, so return None to indicate unsupported query pattern.
110114
None => KeyByLabelNames::empty(),
111115
};
112116

@@ -124,9 +128,9 @@ impl SimpleEngine {
124128
let mut query_kwargs = HashMap::new();
125129
if let AggregationType::Percentiles(percents) = aggregation {
126130
// Get first value from percents array since we only support one quantile argument for now.
127-
let quantile = percents.first()?;
128-
// ES percentiles are specified as values between 0 and 100, but we want to convert to 0-1 range for our internal representation.
129-
query_kwargs.insert("quantile".to_string(), (quantile / 100.0).to_string());
131+
let quantile = percents.first()?;
132+
// ES percentiles are specified as values between 0 and 100, but we want to convert to 0-1 range for our internal representation.
133+
query_kwargs.insert("quantile".to_string(), (quantile / 100.0).to_string());
130134
}
131135

132136
let metadata = QueryMetadata {
@@ -149,16 +153,22 @@ impl SimpleEngine {
149153
let mut start_timestamp: u64 = 0;
150154
let mut end_timestamp: u64 = query_time;
151155

152-
let predicate = query_info
153-
.predicates
154-
.first().clone(); // For now, we only look at the first predicate for time range information. We can extend this to support multiple predicates and more complex logic in the future.
156+
let predicate = query_info.predicates.first(); // For now, we only look at the first predicate for time range information. We can extend this to support multiple predicates and more complex logic in the future.
155157

156158
match predicate {
157-
Some(Predicate::Range { field: _, gte: _, lte: _ }) => {
159+
Some(Predicate::Range {
160+
field: _,
161+
gte: _,
162+
lte: _,
163+
}) => {
158164
// If we have a range predicate, we can try to extract time range information from it.
159165
// For now, we assume that any range predicate applies to the timestamp field, but we could add more complex logic here to determine which field is the timestamp field.
160-
debug!("Found range predicate in query, attempting to extract time range information");
161-
if let Some(resolved_range) = range_query_to_time_range(predicate.unwrap(), query_time as i64) {
166+
debug!(
167+
"Found range predicate in query, attempting to extract time range information"
168+
);
169+
if let Some(resolved_range) =
170+
range_query_to_time_range(predicate.unwrap(), query_time as i64)
171+
{
162172
debug!(
163173
"Parsed time range from range predicate: start={} end={}",
164174
resolved_range.gte_ms.unwrap_or(0),

0 commit comments

Comments
 (0)