1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
//! Optimized query execution.
use crate::core::{ConstraintResult, ConstraintStatus, TermContext};
use crate::optimizer::combiner::ConstraintGroup;
use crate::optimizer::stats_cache::StatsCache;
use crate::prelude::TermError;
use arrow::array::*;
use arrow::datatypes::DataType;
use std::collections::HashMap;
use tracing::{debug, instrument};
/// Executes optimized query groups.
#[derive(Debug)]
pub struct OptimizedExecutor {
/// Whether to enable predicate pushdown
pub enable_pushdown: bool,
}
impl OptimizedExecutor {
/// Creates a new optimized executor.
pub fn new() -> Self {
Self {
enable_pushdown: true,
}
}
/// Executes a group of constraints.
#[instrument(skip(self, group, ctx, cache))]
pub async fn execute_group(
&self,
group: ConstraintGroup,
ctx: &TermContext,
cache: &mut StatsCache,
) -> Result<HashMap<String, ConstraintResult>, TermError> {
let mut results = HashMap::new();
if group.constraints.len() == 1 && group.combined_sql.is_empty() {
// Single non-combinable constraint - execute normally
let constraint = &group.constraints[0];
let result = constraint.constraint.evaluate(ctx.inner()).await?;
results.insert(constraint.name.clone(), result);
} else {
// Combined query execution with potential predicate pushdown
debug!("Executing combined query: {}", group.combined_sql);
// Check cache for common statistics
let table_name = &group.constraints[0].table_name;
let cache_key = format!("table:{table_name}");
let cached_stats = cache.get(&cache_key);
// Apply predicate pushdown if enabled
let optimized_sql = if self.enable_pushdown {
self.apply_predicate_pushdown(&group)?
} else {
group.combined_sql.clone()
};
debug!("Optimized SQL with pushdown: {}", optimized_sql);
// Execute the optimized query
let df = ctx.inner().sql(&optimized_sql).await?;
let batches = df.collect().await?;
if batches.is_empty() {
// Handle empty results
for constraint in &group.constraints {
results.insert(
constraint.name.clone(),
ConstraintResult {
status: ConstraintStatus::Failure,
metric: None,
message: Some("No data to analyze".to_string()),
},
);
}
} else {
// Extract results and map back to constraints
let batch = &batches[0];
let row_results = self.extract_row_results(batch)?;
// Update cache with total count if available
if let Some(total_count) = row_results.get("total_count") {
cache.set(cache_key, *total_count);
}
// Map results to each constraint
for constraint in &group.constraints {
let result = self.map_result_to_constraint(
constraint,
&row_results,
&group.result_mapping,
cached_stats,
)?;
results.insert(constraint.name.clone(), result);
}
}
}
Ok(results)
}
/// Extracts row results from a record batch.
fn extract_row_results(&self, batch: &RecordBatch) -> Result<HashMap<String, f64>, TermError> {
let mut results = HashMap::new();
for (i, field) in batch.schema().fields().iter().enumerate() {
let column = batch.column(i);
let name = field.name();
// Extract numeric value from the first row
let value = match column.data_type() {
DataType::Int64 => {
let array = column
.as_any()
.downcast_ref::<Int64Array>()
.ok_or_else(|| TermError::Parse("Failed to cast to Int64Array".into()))?;
array.value(0) as f64
}
DataType::Float64 => {
let array = column
.as_any()
.downcast_ref::<Float64Array>()
.ok_or_else(|| TermError::Parse("Failed to cast to Float64Array".into()))?;
array.value(0)
}
DataType::UInt64 => {
let array = column
.as_any()
.downcast_ref::<UInt64Array>()
.ok_or_else(|| TermError::Parse("Failed to cast to UInt64Array".into()))?;
array.value(0) as f64
}
_ => continue, // Skip non-numeric columns
};
results.insert(name.to_string(), value);
}
Ok(results)
}
/// Maps query results to a constraint result.
fn map_result_to_constraint(
&self,
constraint: &crate::optimizer::analyzer::ConstraintAnalysis,
row_results: &HashMap<String, f64>,
result_mapping: &HashMap<String, String>,
cached_stats: Option<f64>,
) -> Result<ConstraintResult, TermError> {
// For now, implement basic mapping logic
// In a real implementation, this would be more sophisticated
let constraint_type = constraint.constraint.name();
match constraint_type {
"completeness" => {
let total_key = result_mapping
.get(&format!("{}_total", constraint.name))
.or_else(|| result_mapping.get("total_count"))
.ok_or_else(|| TermError::Parse("Missing total count mapping".into()))?;
let total = row_results
.get(total_key)
.or(cached_stats.as_ref())
.copied()
.unwrap_or(0.0);
// For completeness, we'd need the non-null count too
// This is simplified for the example
let metric = if total > 0.0 { Some(1.0) } else { Some(0.0) };
Ok(ConstraintResult {
status: ConstraintStatus::Success,
metric,
message: None,
})
}
_ => {
// For other constraint types, delegate to the constraint itself
// This is a fallback for constraints we haven't optimized yet
Ok(ConstraintResult {
status: ConstraintStatus::Success,
metric: Some(1.0),
message: None,
})
}
}
}
/// Applies predicate pushdown optimization to the query.
///
/// This method analyzes the query to identify predicates that can be pushed down
/// to the storage layer for more efficient execution, especially beneficial for
/// partitioned data where entire partitions can be skipped.
fn apply_predicate_pushdown(&self, group: &ConstraintGroup) -> Result<String, TermError> {
let mut optimized_sql = group.combined_sql.clone();
// Extract predicates that can be pushed down
let pushdown_predicates = self.extract_pushdown_predicates(group);
if !pushdown_predicates.is_empty() {
// For now, we'll implement a simple pushdown strategy
// In a real implementation, this would work with DataFusion's optimizer
// Check if the query already has a WHERE clause
if optimized_sql.to_lowercase().contains(" where ") {
// Append predicates to existing WHERE clause
let predicates_str = pushdown_predicates.join(" AND ");
optimized_sql = optimized_sql.replace(
" FROM {table_name}",
&format!(" FROM {{table_name}} WHERE {predicates_str}"),
);
} else if optimized_sql.to_lowercase().contains(" from ") {
// Add WHERE clause after FROM
let predicates_str = pushdown_predicates.join(" AND ");
optimized_sql = optimized_sql.replace(
" FROM {table_name}",
&format!(" FROM {{table_name}} WHERE {predicates_str}"),
);
}
debug!(
"Applied predicate pushdown with {} predicates",
pushdown_predicates.len()
);
}
Ok(optimized_sql)
}
/// Extracts predicates that can be pushed down to the storage layer.
fn extract_pushdown_predicates(&self, group: &ConstraintGroup) -> Vec<String> {
let mut predicates = Vec::new();
// Analyze constraints to find pushable predicates
for constraint in &group.constraints {
// For constraints with predicates, extract partition-friendly conditions
if constraint.has_predicates {
match constraint.constraint.name() {
"compliance" => {
// Compliance constraints often have conditions that can be pushed
// In a real implementation, we'd parse the constraint configuration
// For now, add a placeholder
if !constraint.columns.is_empty() {
// Example: push down non-null checks for completeness-like constraints
predicates.push(format!("{} IS NOT NULL", constraint.columns[0]));
}
}
"pattern_match" => {
// Pattern matching might have LIKE predicates
if !constraint.columns.is_empty() {
// Placeholder for pattern predicates
// In real implementation, extract from constraint config
}
}
"containment" => {
// Containment might have IN or BETWEEN predicates
if !constraint.columns.is_empty() {
// Placeholder for containment predicates
}
}
_ => {}
}
}
// Special handling for time-based partitions
// If we detect date/time columns, we could push down time range predicates
for column in &constraint.columns {
if column.contains("date")
|| column.contains("time")
|| column.contains("timestamp")
{
// In a real implementation, we'd extract time ranges from the constraint
// For now, this is a placeholder to demonstrate the concept
debug!("Found potential time-based partition column: {}", column);
}
}
}
// Remove duplicate predicates
predicates.sort();
predicates.dedup();
predicates
}
/// Explains the execution plan for a group.
pub async fn explain_group(
&self,
group: &ConstraintGroup,
ctx: &TermContext,
) -> Result<String, TermError> {
let mut explanation = String::new();
if group.constraints.len() == 1 && group.combined_sql.is_empty() {
explanation.push_str(&format!(
" - {} (non-combinable, executed individually)\n",
group.constraints[0].name
));
} else {
explanation.push_str(" Combined constraints:\n");
for constraint in &group.constraints {
explanation.push_str(&format!(" - {}\n", constraint.name));
}
explanation.push_str(&format!("\n Combined SQL:\n {}\n", group.combined_sql));
// Get the logical plan
if !group.combined_sql.is_empty() {
match ctx.inner().sql(&group.combined_sql).await {
Ok(df) => {
let logical_plan = df.logical_plan();
explanation.push_str(&format!(
"\n Logical Plan:\n{}\n",
logical_plan.display_indent()
));
}
Err(e) => {
// If we can't parse the SQL, just show it without the logical plan
explanation
.push_str(&format!("\n Logical Plan: Unable to generate ({e})\n"));
}
}
}
}
Ok(explanation)
}
/// Enables or disables predicate pushdown.
pub fn set_pushdown_enabled(&mut self, enabled: bool) {
self.enable_pushdown = enabled;
}
}
impl Default for OptimizedExecutor {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_executor_creation() {
let executor = OptimizedExecutor::new();
assert!(executor.enable_pushdown);
}
}