quelch 0.9.2

Ingest data from Jira, Confluence, and more directly into Azure AI Search
Documentation
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
//! MCP `aggregate` tool — count and sum grouped query results.
//!
//! # Array field fan-out
//!
//! When `group_by` names an array field (e.g. `labels` on `jira_issue`), the
//! tool would ideally emit a Cosmos SQL `JOIN v IN c.labels … GROUP BY v` to
//! fan out each array element into its own group.  However, `InMemoryCosmos`
//! does not support JOIN or GROUP BY syntax.
//!
//! **Current behaviour**: the aggregate tool applies grouping in-process after
//! a full-table scan.  This is semantically correct and works against both
//! `InMemoryCosmos` and the real Cosmos SDK (which would need the JOIN form for
//! large datasets — see the TODO below).
//!
//! Array field detection uses [`crate::mcp::schema::SchemaCatalog`].  If the
//! catalog lists `group_by` as an array field, element-level fan-out is
//! performed.
//!
//! TODO(quelch v2 follow-up): for large Cosmos containers, emit the JOIN-based
//! GROUP BY SQL to push the aggregation down to the server.  The in-process
//! approach works correctly but doesn't scale.

use std::collections::HashMap;

use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::cosmos::CosmosBackend;
use crate::mcp::error::McpError;
use crate::mcp::expose::ExposeResolver;
use crate::mcp::filter::{cosmos_sql::SqlBuilder, parse};
use crate::mcp::schema::SchemaCatalog;

/// Request parameters for the `aggregate` tool.
#[derive(Debug, Deserialize, JsonSchema)]
pub struct AggregateRequest {
    /// Logical data-source name.
    pub data_source: String,
    /// Optional filter (JSON filter grammar) applied before aggregation.
    #[serde(rename = "where")]
    pub r#where: Option<Value>,
    /// Field to group by (scalar or array).
    pub group_by: Option<String>,
    /// Include a document count per group (default: `true`).
    #[serde(default = "default_count_true")]
    pub count: bool,
    /// Sum this numeric field per group.
    pub sum_field: Option<String>,
    /// Limit to the top N groups by count.
    pub top_groups: Option<usize>,
    /// When `true`, include soft-deleted documents.
    #[serde(default)]
    pub include_deleted: bool,
}

fn default_count_true() -> bool {
    true
}

/// A single aggregation group.
#[derive(Debug, Serialize)]
pub struct AggregateGroup {
    /// The group key value (e.g. `"Open"`, `"backend"`).  `None` for the
    /// no-`group_by` total row.
    pub key: Option<String>,
    /// Document count for this group.
    pub count: u64,
    /// Sum of `sum_field` for this group.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sum: Option<f64>,
}

/// Overall totals (across all groups).
#[derive(Debug, Serialize)]
pub struct AggregateTotal {
    pub count: u64,
    pub sum: Option<f64>,
}

/// Response from the `aggregate` tool.
#[derive(Debug, Serialize)]
pub struct AggregateResponse {
    pub groups: Vec<AggregateGroup>,
    pub total: AggregateTotal,
}

/// Execute the `aggregate` tool.
pub async fn run(
    cosmos: &dyn CosmosBackend,
    expose: &ExposeResolver,
    schema: &SchemaCatalog,
    req: AggregateRequest,
) -> Result<AggregateResponse, McpError> {
    let resolved = expose.resolve(&req.data_source)?;

    // Parse and translate the `where` filter.
    let where_ast = match req.r#where {
        Some(ref v) => Some(parse(v)?),
        None => None,
    };

    let builder = SqlBuilder::new(req.include_deleted);
    let user_filter = match &where_ast {
        Some(w) => Some(builder.build(w)?),
        None => None,
    };

    // Build the WHERE clause
    let mut where_sql = String::new();
    let mut params: Vec<(String, Value)> = Vec::new();

    if let Some(uf) = user_filter {
        where_sql = format!(" WHERE {}", uf.sql_fragment);
        params.extend(uf.params);
    } else if !req.include_deleted {
        where_sql = " WHERE (NOT IS_DEFINED(c._deleted) OR c._deleted = false)".to_string();
    }

    // TODO(quelch v2 follow-up): multi-container fan-out.
    let container = &resolved.backed_by[0].container;

    // Fetch all matching documents for in-process aggregation.
    // This avoids the need for GROUP BY / JOIN syntax that InMemoryCosmos doesn't support.
    let sql = format!("SELECT * FROM c{where_sql}");
    let mut stream = cosmos.query(container, &sql, params).await?;

    // Collect all docs
    let mut all_docs: Vec<Value> = Vec::new();
    while let Some(page) = stream.next_page().await? {
        all_docs.extend(page);
    }

    // Determine whether group_by targets an array field
    let is_array_group = req
        .group_by
        .as_deref()
        .map(|f| schema.is_array_field(&resolved.kind, f))
        .unwrap_or(false);

    let groups = aggregate_in_process(&all_docs, &req, is_array_group);

    // Apply top_groups limit
    let mut groups = groups;
    if let Some(top) = req.top_groups {
        // Sort by count descending before truncating
        groups.sort_by_key(|b| std::cmp::Reverse(b.count));
        groups.truncate(top);
    }

    let total_count = groups.iter().map(|g| g.count).sum();
    let total_sum = if req.sum_field.is_some() {
        Some(groups.iter().filter_map(|g| g.sum).sum())
    } else {
        None
    };

    Ok(AggregateResponse {
        groups,
        total: AggregateTotal {
            count: total_count,
            sum: total_sum,
        },
    })
}

/// Perform grouping and aggregation in-process over a slice of documents.
fn aggregate_in_process(
    docs: &[Value],
    req: &AggregateRequest,
    is_array_group: bool,
) -> Vec<AggregateGroup> {
    if req.group_by.is_none() {
        // No grouping — return a single total group.
        let count = docs.len() as u64;
        let sum = req.sum_field.as_deref().map(|f| {
            docs.iter()
                .filter_map(|d| d.get(f).and_then(Value::as_f64))
                .sum::<f64>()
        });
        return vec![AggregateGroup {
            key: None,
            count,
            sum,
        }];
    }

    let group_field = req.group_by.as_deref().unwrap();
    let sum_field = req.sum_field.as_deref();

    // key → (count, sum)
    let mut groups: HashMap<String, (u64, f64)> = HashMap::new();

    for doc in docs {
        if is_array_group {
            // Fan out: each element of the array contributes to its own group
            if let Some(arr) = doc.get(group_field).and_then(Value::as_array) {
                for elem in arr {
                    let key = value_to_group_key(elem);
                    let sum_val = sum_field
                        .and_then(|f| doc.get(f))
                        .and_then(Value::as_f64)
                        .unwrap_or(0.0);
                    let entry = groups.entry(key).or_insert((0, 0.0));
                    entry.0 += 1;
                    entry.1 += sum_val;
                }
            }
            // Documents without the array field are excluded from array fan-out groups
        } else {
            // Scalar grouping
            let key = doc
                .get(group_field)
                .map(value_to_group_key)
                .unwrap_or_else(|| "<null>".to_string());
            let sum_val = sum_field
                .and_then(|f| doc.get(f))
                .and_then(Value::as_f64)
                .unwrap_or(0.0);
            let entry = groups.entry(key).or_insert((0, 0.0));
            entry.0 += 1;
            entry.1 += sum_val;
        }
    }

    groups
        .into_iter()
        .map(|(key, (count, sum))| AggregateGroup {
            key: Some(key),
            count,
            sum: req.sum_field.as_ref().map(|_| sum),
        })
        .collect()
}

/// Convert a JSON value to a string group key.
fn value_to_group_key(v: &Value) -> String {
    match v {
        Value::String(s) => s.clone(),
        Value::Number(n) => n.to_string(),
        Value::Bool(b) => b.to_string(),
        Value::Null => "<null>".to_string(),
        other => other.to_string(),
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use crate::cosmos::InMemoryCosmos;
    use crate::mcp::schema::SchemaCatalog;
    use crate::mcp::tools::test_helpers::{
        build_cosmos_with_jira_issues, build_expose_jira_issues,
    };
    use serde_json::json;

    fn schema() -> SchemaCatalog {
        SchemaCatalog::new()
    }

    #[tokio::test]
    async fn aggregate_count_with_filter() {
        let cosmos = build_cosmos_with_jira_issues().await;
        let expose = build_expose_jira_issues();
        let req = AggregateRequest {
            data_source: "jira_issues".into(),
            r#where: Some(json!({"status": "Open"})),
            group_by: None,
            count: true,
            sum_field: None,
            top_groups: None,
            include_deleted: false,
        };
        let resp = run(&cosmos, &expose, &schema(), req).await.unwrap();
        assert_eq!(resp.total.count, 3); // 3 Open, non-deleted
        assert_eq!(resp.groups.len(), 1);
        assert_eq!(resp.groups[0].count, 3);
    }

    #[tokio::test]
    async fn aggregate_group_by_scalar() {
        let cosmos = build_cosmos_with_jira_issues().await;
        let expose = build_expose_jira_issues();
        let req = AggregateRequest {
            data_source: "jira_issues".into(),
            r#where: None,
            group_by: Some("status".into()),
            count: true,
            sum_field: None,
            top_groups: None,
            include_deleted: false,
        };
        let resp = run(&cosmos, &expose, &schema(), req).await.unwrap();
        // 3 Open + 2 Done (i6 is soft-deleted)
        let open_group = resp
            .groups
            .iter()
            .find(|g| g.key.as_deref() == Some("Open"))
            .unwrap();
        let done_group = resp
            .groups
            .iter()
            .find(|g| g.key.as_deref() == Some("Done"))
            .unwrap();
        assert_eq!(open_group.count, 3);
        assert_eq!(done_group.count, 2);
    }

    #[tokio::test]
    async fn aggregate_group_by_array_field_fans_out() {
        // All 5 non-deleted docs (i1-i5) have labels: ["backend"]
        // i6 has labels: ["backend","frontend"] but is soft-deleted
        // After soft-delete filter: 5 docs contribute "backend", frontend excluded
        let cosmos = build_cosmos_with_jira_issues().await;
        let expose = build_expose_jira_issues();
        let req = AggregateRequest {
            data_source: "jira_issues".into(),
            r#where: None,
            group_by: Some("labels".into()),
            count: true,
            sum_field: None,
            top_groups: None,
            include_deleted: false,
        };
        let resp = run(&cosmos, &expose, &schema(), req).await.unwrap();
        // "backend" should count all 5 non-deleted docs
        let backend_group = resp
            .groups
            .iter()
            .find(|g| g.key.as_deref() == Some("backend"))
            .unwrap();
        assert_eq!(
            backend_group.count, 5,
            "backend should count all 5 non-deleted docs"
        );
        // i6 is soft-deleted; frontend should not appear
        assert!(
            resp.groups
                .iter()
                .all(|g| g.key.as_deref() != Some("frontend")),
            "frontend from soft-deleted doc should not appear"
        );
    }

    #[tokio::test]
    async fn aggregate_group_by_array_field_with_include_deleted() {
        // With include_deleted: true, i6's labels ["backend","frontend"] should be counted
        let cosmos = build_cosmos_with_jira_issues().await;
        let expose = build_expose_jira_issues();
        let req = AggregateRequest {
            data_source: "jira_issues".into(),
            r#where: None,
            group_by: Some("labels".into()),
            count: true,
            sum_field: None,
            top_groups: None,
            include_deleted: true,
        };
        let resp = run(&cosmos, &expose, &schema(), req).await.unwrap();
        let frontend_group = resp
            .groups
            .iter()
            .find(|g| g.key.as_deref() == Some("frontend"));
        assert!(
            frontend_group.is_some(),
            "frontend from i6 should appear with include_deleted=true"
        );
        let backend_group = resp
            .groups
            .iter()
            .find(|g| g.key.as_deref() == Some("backend"))
            .unwrap();
        assert_eq!(backend_group.count, 6, "backend from all 6 docs");
    }

    #[tokio::test]
    async fn aggregate_top_groups_limits() {
        let cosmos = build_cosmos_with_jira_issues().await;
        let expose = build_expose_jira_issues();
        let req = AggregateRequest {
            data_source: "jira_issues".into(),
            r#where: None,
            group_by: Some("status".into()),
            count: true,
            sum_field: None,
            top_groups: Some(1),
            include_deleted: false,
        };
        let resp = run(&cosmos, &expose, &schema(), req).await.unwrap();
        assert_eq!(resp.groups.len(), 1);
        // The top group by count should be "Open" (3 > 2)
        assert_eq!(resp.groups[0].key.as_deref(), Some("Open"));
    }

    #[tokio::test]
    async fn aggregate_sum_field() {
        let cosmos = InMemoryCosmos::new();
        cosmos
            .upsert(
                "jira-issues",
                json!({"id": "a", "_partition_key": "DO", "status": "Open", "story_points": 3.0}),
            )
            .await
            .unwrap();
        cosmos
            .upsert(
                "jira-issues",
                json!({"id": "b", "_partition_key": "DO", "status": "Open", "story_points": 5.0}),
            )
            .await
            .unwrap();
        cosmos
            .upsert(
                "jira-issues",
                json!({"id": "c", "_partition_key": "DO", "status": "Done", "story_points": 2.0}),
            )
            .await
            .unwrap();

        let expose = build_expose_jira_issues();
        let req = AggregateRequest {
            data_source: "jira_issues".into(),
            r#where: None,
            group_by: Some("status".into()),
            count: true,
            sum_field: Some("story_points".into()),
            top_groups: None,
            include_deleted: false,
        };
        let resp = run(&cosmos, &expose, &schema(), req).await.unwrap();
        let open = resp
            .groups
            .iter()
            .find(|g| g.key.as_deref() == Some("Open"))
            .unwrap();
        assert_eq!(open.sum, Some(8.0));
        let done = resp
            .groups
            .iter()
            .find(|g| g.key.as_deref() == Some("Done"))
            .unwrap();
        assert_eq!(done.sum, Some(2.0));
    }

    #[tokio::test]
    async fn aggregate_excludes_soft_deleted() {
        let cosmos = build_cosmos_with_jira_issues().await;
        let expose = build_expose_jira_issues();
        let req = AggregateRequest {
            data_source: "jira_issues".into(),
            r#where: None,
            group_by: None,
            count: true,
            sum_field: None,
            top_groups: None,
            include_deleted: false,
        };
        let resp = run(&cosmos, &expose, &schema(), req).await.unwrap();
        // 5 non-deleted docs out of 6
        assert_eq!(resp.total.count, 5);
    }

    #[tokio::test]
    async fn aggregate_forbidden_for_unexposed_data_source() {
        let cosmos = build_cosmos_with_jira_issues().await;
        let expose = build_expose_jira_issues();
        let req = AggregateRequest {
            data_source: "jira_sprints".into(),
            r#where: None,
            group_by: None,
            count: true,
            sum_field: None,
            top_groups: None,
            include_deleted: false,
        };
        let err = run(&cosmos, &expose, &schema(), req).await.unwrap_err();
        assert!(matches!(err, McpError::Forbidden(_)));
    }

    #[tokio::test]
    async fn aggregate_include_deleted_includes_tombstones() {
        let cosmos = build_cosmos_with_jira_issues().await;
        let expose = build_expose_jira_issues();
        let req = AggregateRequest {
            data_source: "jira_issues".into(),
            r#where: None,
            group_by: None,
            count: true,
            sum_field: None,
            top_groups: None,
            include_deleted: true,
        };
        let resp = run(&cosmos, &expose, &schema(), req).await.unwrap();
        assert_eq!(resp.total.count, 6);
    }
}