Skip to content

armgit / xerj

Public Imported from GitHub
0

AI search engine and MCP server for bots and agents. One Rust binary indexes code, docs, logs, JSONL/CSV and PDFs for BM25 full-text, vector and hybrid search, code search without grep, agent memory and RAG. Elasticsearch-compatible API; Corpus Hub serves hundreds of reference memory corpora.

fix(sql): reject HAVING over /_sql with a 400 instead of dropping it

xerj-org9690cce33c89
diff --git a/engine/crates/xerj-engine/src/sql.rs b/engine/crates/xerj-engine/src/sql.rs
index fbabc83ec..690536b6c 100644
--- a/engine/crates/xerj-engine/src/sql.rs
+++ b/engine/crates/xerj-engine/src/sql.rs
@@ -341,33 +341,12 @@ pub fn parse_sql(sql: &str) -> Result<SqlQuery, String> {
}
}
- // Optional HAVING (parsed but not yet translated — reserved for future use).
+ // HAVING is not supported over /_sql. There is no GROUP BY execution path
+ // here to filter post-aggregation, so silently skipping the predicate would
+ // return ungrouped rows that ignore the filter — a wrong answer. Fail loud
+ // with a parse error that the /_sql handler surfaces as a 400 invalid_query.
if peek_word(pos).as_deref() == Some("HAVING") {
- pos += 1;
- // Skip tokens until ORDER, LIMIT, or end.
- while let Some(tok) = peek(pos) {
- match peek_word(pos).as_deref() {
- Some("ORDER") | Some("LIMIT") => break,
- _ => {}
- }
- match tok {
- Token::Word(_)
- | Token::Num(_)
- | Token::Str(_)
- | Token::Eq
- | Token::Ne
- | Token::Gt
- | Token::Ge
- | Token::Lt
- | Token::Le
- | Token::LParen
- | Token::RParen
- | Token::Comma
- | Token::Star => {
- pos += 1;
- }
- }
- }
+ return Err("HAVING clause is not supported over /_sql".to_string());
}
// Optional ORDER BY.
@@ -716,4 +695,28 @@ mod tests {
assert_eq!(q.sort[0].field, "created");
assert!(matches!(q.sort[0].order, SortOrder::Desc));
}
+
+ #[test]
+ fn test_having_rejected() {
+ // HAVING has no GROUP BY execution path over /_sql; it must fail loud
+ // (surfaced as 400 invalid_query) rather than silently drop the filter.
+ let res = parse_sql(
+ "SELECT category, COUNT(*) FROM products GROUP BY category HAVING COUNT(*) > 5",
+ );
+ match res {
+ Err(err) => assert!(
+ err.contains("HAVING"),
+ "expected HAVING reject error, got: {err}"
+ ),
+ Ok(_) => panic!("expected HAVING clause to be rejected, but query parsed"),
+ }
+ }
+
+ #[test]
+ fn test_group_by_without_having_still_parses() {
+ // A plain GROUP BY (no HAVING) must continue to parse unchanged.
+ let q = parse_sql("SELECT category, COUNT(*) FROM products GROUP BY category").unwrap();
+ assert_eq!(q.index, "products");
+ assert_eq!(q.group_by, vec!["category"]);
+ }
}