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
use std::collections::{BTreeMap, HashMap};
use rsigma_eval::pipeline::state::PipelineState;
use rsigma_parser::*;
use crate::error::{ConvertError, Result};
impl super::PostgresBackend {
/// Format a field name for use in a SELECT column list.
///
/// Inspired by the pySigma Athena backend's `_format_select_field`:
/// - Expressions containing parentheses (function calls) pass through unchanged
/// - `field as alias` is split and both sides are quoted independently
/// - Plain field names are quoted via `field_expr`
pub(super) fn format_select_field(&self, field: &str) -> Result<String> {
if field == "*" {
return Ok("*".to_string());
}
if field.contains('(') && field.contains(')') {
return Ok(field.to_string());
}
if let Some((expr, alias)) = field.split_once(" as ") {
let quoted_expr = self.field_expr(expr.trim())?;
let quoted_alias = self.field_expr(alias.trim())?;
return Ok(format!("{quoted_expr} AS {quoted_alias}"));
}
if let Some((expr, alias)) = field.split_once(" AS ") {
let quoted_expr = self.field_expr(expr.trim())?;
let quoted_alias = self.field_expr(alias.trim())?;
return Ok(format!("{quoted_expr} AS {quoted_alias}"));
}
self.field_expr(field)
}
/// Build the CTE prefix and source for non-temporal correlations.
///
/// When per-rule converted queries are available (from `_rule_queries`
/// injected by `convert_collection`), wraps them in a
/// `WITH combined_events AS (q1 UNION ALL q2 ...)` CTE. The aggregate
/// query then reads from `combined_events` instead of the raw table.
///
/// When no per-rule queries are available, falls back to the original
/// behavior: scan the full table with a time-window filter.
///
/// Returns `(cte_prefix, source_table, time_filter)`.
pub(super) fn build_correlation_source(
&self,
rule_refs: &[String],
rule_queries: &HashMap<String, String>,
default_table: &str,
ts: &str,
window_secs: u64,
) -> (String, String, String) {
let matched: Vec<&str> = rule_refs
.iter()
.filter_map(|r| rule_queries.get(r).map(|q| q.as_str()))
.collect();
if matched.is_empty() {
let time_filter = format!(" WHERE {ts} >= NOW() - INTERVAL '{window_secs} seconds'");
(String::new(), default_table.to_string(), time_filter)
} else {
let union = matched.join(" UNION ALL ");
let cte = format!("WITH combined_events AS ({union}) ");
(cte, "combined_events".to_string(), String::new())
}
}
/// Build a sliding window query for `event_count` correlations.
///
/// Generates a two-CTE query inspired by the pySigma Athena backend:
/// ```sql
/// WITH combined_events AS (...),
/// event_counts AS (
/// SELECT *, COUNT(*) OVER (
/// PARTITION BY {group_by}
/// ORDER BY {time_field}
/// RANGE BETWEEN INTERVAL '{N}' SECOND PRECEDING AND CURRENT ROW
/// ) AS correlation_event_count
/// FROM combined_events
/// )
/// SELECT * FROM event_counts WHERE correlation_event_count >= {threshold}
/// ```
///
/// This produces a per-row sliding window that emits every event crossing
/// the threshold within its trailing window.
#[allow(clippy::too_many_arguments)]
pub(super) fn build_sliding_window_query(
&self,
cte_prefix: &str,
source_table: &str,
time_filter: &str,
group_by: &[String],
ts: &str,
window_secs: u64,
condition: &CorrelationCondition,
) -> Result<String> {
let partition_clause = if group_by.is_empty() {
String::new()
} else {
let cols: Vec<String> = group_by
.iter()
.map(|g| self.field_expr(g))
.collect::<Result<_>>()?;
format!("PARTITION BY {} ", cols.join(", "))
};
let where_clause = self.build_threshold_where("correlation_event_count", condition)?;
// When there is a CTE prefix (combined_events), chain the window CTE
// onto it. Otherwise, build a standalone source CTE from the table.
let full_cte = if cte_prefix.is_empty() {
format!(
"WITH source AS (\
SELECT * FROM {source_table}{time_filter}\
), \
event_counts AS (\
SELECT *, COUNT(*) OVER (\
{partition_clause}\
ORDER BY {ts} \
RANGE BETWEEN INTERVAL '{window_secs} seconds' PRECEDING AND CURRENT ROW\
) AS correlation_event_count \
FROM source\
) "
)
} else {
// cte_prefix already has "WITH combined_events AS (...) "
// Strip the trailing space and append the window CTE
let base = cte_prefix.trim_end();
format!(
"{base}, \
event_counts AS (\
SELECT *, COUNT(*) OVER (\
{partition_clause}\
ORDER BY {ts} \
RANGE BETWEEN INTERVAL '{window_secs} seconds' PRECEDING AND CURRENT ROW\
) AS correlation_event_count \
FROM {source_table}\
) "
)
};
Ok(format!(
"{full_cte}SELECT * FROM event_counts WHERE {where_clause}"
))
}
/// Build a WHERE clause from a correlation condition for sliding window queries.
fn build_threshold_where(&self, column: &str, cond: &CorrelationCondition) -> Result<String> {
match cond {
CorrelationCondition::Threshold { predicates, .. } => {
let parts: Vec<String> = predicates
.iter()
.map(|(op, val)| {
let op_str = match op {
ConditionOperator::Lt => "<",
ConditionOperator::Lte => "<=",
ConditionOperator::Gt => ">",
ConditionOperator::Gte => ">=",
ConditionOperator::Eq => "=",
ConditionOperator::Neq => "<>",
};
format!("{column} {op_str} {val}")
})
.collect();
Ok(parts.join(" AND "))
}
CorrelationCondition::Extended(_) => Err(ConvertError::UnsupportedCorrelation(
"extended boolean conditions not yet supported for PostgreSQL".into(),
)),
}
}
/// Build HAVING clause from correlation condition predicates.
/// Uses `{agg}` as placeholder for the aggregate expression.
pub(super) fn build_having_clause(&self, cond: &CorrelationCondition) -> Result<String> {
match cond {
CorrelationCondition::Threshold { predicates, .. } => {
let parts: Vec<String> = predicates
.iter()
.map(|(op, val)| {
let op_str = match op {
ConditionOperator::Lt => "<",
ConditionOperator::Lte => "<=",
ConditionOperator::Gt => ">",
ConditionOperator::Gte => ">=",
ConditionOperator::Eq => "=",
ConditionOperator::Neq => "<>",
};
format!("{{agg}} {op_str} {val}")
})
.collect();
Ok(parts.join(" AND "))
}
CorrelationCondition::Extended(_) => Err(ConvertError::UnsupportedCorrelation(
"extended boolean conditions not yet supported for PostgreSQL".into(),
)),
}
}
/// Build a temporal or temporal_ordered correlation query.
///
/// When all referenced rules target the same table, produces a single-table
/// CTE filtering on `rule_name IN (...)`. When rules target different tables
/// (from `_rule_tables` pipeline state), produces a `UNION ALL` CTE with one
/// leg per rule.
///
/// **Schema compatibility requirement:** The multi-table path uses
/// `SELECT * ... UNION ALL SELECT * ...`. PostgreSQL requires all legs of a
/// `UNION ALL` to produce the same number of columns with compatible types.
/// This works when all referenced tables share an identical schema (e.g. a
/// normalized event schema). If the tables have different column layouts the
/// query will fail at execution time. Callers should ensure that pipeline
/// field-mappings normalize the schemas, or use a single-table approach with
/// a discriminator column instead.
#[allow(clippy::too_many_arguments)]
pub(super) fn build_temporal_query(
&self,
rule: &CorrelationRule,
default_table: &str,
ts: &str,
window_secs: u64,
group_by_select: &str,
group_by_clause: &str,
having_clause: &str,
rule_tables: &HashMap<String, String>,
pipeline_state: &PipelineState,
) -> Result<String> {
let agg = "COUNT(DISTINCT rule_name)";
let having = having_clause.replace("{agg}", agg);
let rule_schemas: HashMap<String, String> = pipeline_state
.state
.get("_rule_schemas")
.and_then(|v| serde_json::from_value(v.clone()).ok())
.unwrap_or_default();
// Collect per-rule tables, qualifying each with its own schema
let mut table_to_rules: BTreeMap<String, Vec<String>> = BTreeMap::new();
for rule_ref in &rule.rules {
let raw_table = rule_tables.get(rule_ref).map(|s| s.as_str());
let per_rule_schema = rule_schemas.get(rule_ref).map(|s| s.as_str());
let qualified = match raw_table {
Some(t) => self.qualify_table_name(t, &pipeline_state.state, per_rule_schema)?,
None => default_table.to_string(),
};
table_to_rules
.entry(qualified)
.or_default()
.push(rule_ref.clone());
}
if table_to_rules.len() <= 1 {
// Single table: filter by rule_name column
let rule_names = rule.rules.join("', '");
Ok(format!(
"WITH matched AS (\
SELECT *, rule_name FROM {default_table} \
WHERE rule_name IN ('{rule_names}') \
AND {ts} >= NOW() - INTERVAL '{window_secs} seconds'\
) \
SELECT {group_by_select}\
{agg} AS distinct_rules, \
MIN({ts}) AS first_seen, MAX({ts}) AS last_seen \
FROM matched\
{group_by_clause} \
HAVING {having}"
))
} else {
// Multi-table: UNION ALL CTE with one leg per rule
let union_parts: Vec<String> = table_to_rules
.iter()
.flat_map(|(tbl, rules)| {
rules.iter().map(move |rule_ref| {
format!(
"SELECT *, '{rule_ref}' AS rule_name FROM {tbl} \
WHERE {ts} >= NOW() - INTERVAL '{window_secs} seconds'"
)
})
})
.collect();
let union_cte = union_parts.join(" UNION ALL ");
Ok(format!(
"WITH matched AS (\
{union_cte}\
) \
SELECT {group_by_select}\
{agg} AS distinct_rules, \
MIN({ts}) AS first_seen, MAX({ts}) AS last_seen \
FROM matched\
{group_by_clause} \
HAVING {having}"
))
}
}
}