Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions crates/ourios-miner/tests/hazards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -895,13 +895,13 @@ fn h5_2_type_expansion_increments_version_and_emits_template_type_expanded() {
);
}

/// Scenario H5.3 — Drift query returns templates that gained a version in window.
/// See `docs/rfcs/0001-template-miner.md` §5.
#[test]
#[ignore = "RFC 0001 Red gate — implementation pending"]
fn h5_3_drift_query_returns_templates_that_gained_a_version() {
todo!("RFC 0001 §6.7");
}
// Scenario H5.3 — Drift query returns templates that gained a version in
// window (RFC 0001 §6.7) — RELOCATED. The drift query path lives in
// `ourios-querier`, which the miner crate cannot run, so the real test is
// `ourios-querier/tests/drift.rs::h5_3_drift_query_returns_templates_that_gained_a_version`
// (the RFC 0010 §6.1 `drift` surface that RFC 0010 added to discharge this
// criterion). Cf. RFC0001.5/.6, also relocated to
// `ourios-querier/tests/rfc0001_query_semantics.rs`.

/// Scenario H7.1 — Reconstruction property holds across the corpus.
/// See `docs/rfcs/0001-template-miner.md` §5.
Expand Down
5 changes: 4 additions & 1 deletion crates/ourios-querier/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,10 @@ fn resolve_window(
}
}

fn resolve_time(time: &Time, now: u64) -> Result<u64, QueryError> {
/// Resolve a §7 [`Time`] bound to absolute nanoseconds against `now`. Shared
/// with the RFC 0010 drift path (`crate::drift`), which reuses the same `time`
/// grammar for its window (RFC 0010 §6.5).
pub(crate) fn resolve_time(time: &Time, now: u64) -> Result<u64, QueryError> {
match time {
Time::Now => Ok(now),
Time::Duration { neg, literal } => {
Expand Down
473 changes: 473 additions & 0 deletions crates/ourios-querier/src/drift.rs

Large diffs are not rendered by default.

23 changes: 21 additions & 2 deletions crates/ourios-querier/src/dsl/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,29 @@
use std::fmt::Write as _;

use super::ir::{
AggFn, Call, CmpOp, Field, OrdOp, Predicate, Query, SeverityName, SeverityValue, Stage, Time,
Value,
AggFn, Call, CmpOp, DriftQuery, Field, OrdOp, Predicate, Query, SeverityName, SeverityValue,
Stage, Statement, Time, Value,
};

/// Serialise a [`Statement`] (a log query or a RFC 0010 `drift` query) to its
/// canonical single-line β form. Round-trips through [`super::parse_statement`].
#[must_use]
pub fn serialize_statement(statement: &Statement) -> String {
match statement {
Statement::Logs(query) => serialize(query),
Statement::Drift(drift) => serialize_drift(drift),
}
}

/// Serialise a [`DriftQuery`] to `drift from <t1> to <t2>` (RFC 0010 §6.1).
fn serialize_drift(drift: &DriftQuery) -> String {
let mut out = String::from("drift from ");
write_time(&mut out, &drift.from);
out.push_str(" to ");
write_time(&mut out, &drift.to);
out
}

/// Serialise a [`Query`] to its canonical single-line β form.
#[must_use]
pub fn serialize(query: &Query) -> String {
Expand Down
28 changes: 27 additions & 1 deletion crates/ourios-querier/src/dsl/ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,39 @@
//! the §7 grammar exactly; no `datafusion`/`arrow`/SQL type appears here
//! (hazard `CLAUDE.md` §4.6).

/// A whole query: a predicate followed by ordered pipe stages (§7 `query`).
/// A top-level statement — either a log-record query (the RFC 0002
/// `predicate { | stage }` pipeline) or a RFC 0010 `drift` query over the
/// audit stream. The two are distinct shapes, not a flag on one struct: a
/// drift query has no predicate and no stages (RFC 0010 §6.1), so mixing the
/// two is unrepresentable.
#[derive(Debug, Clone, PartialEq)]
pub enum Statement {
/// A log-record query against the `data/` series (RFC 0002).
Logs(Query),
/// A `drift from <t1> to <t2>` query against the `audit/` series
/// (RFC 0010 §6.1).
Drift(DriftQuery),
}

/// A whole log query: a predicate followed by ordered pipe stages (§7 `query`).
#[derive(Debug, Clone, PartialEq)]
pub struct Query {
pub predicate: Predicate,
pub stages: Vec<Stage>,
}

/// A RFC 0010 `drift` query: the closed-form audit-stream question "which
/// templates gained a version in the window `[from, to)`" (RFC 0010 §6.1).
/// It carries only the resolved window — the projection, grouping, and
/// ordering are fixed by §6.3, so there is nothing else to express and no
/// `|` stage to compose. `from` / `to` reuse the §7 [`Time`] production
/// verbatim (RFC 0010 §6.5); the window is half-open `[from, to)`.
#[derive(Debug, Clone, PartialEq)]
pub struct DriftQuery {
pub from: Time,
pub to: Time,
}

/// A boolean expression over the `OTel` log data model (§7 `predicate`).
#[derive(Debug, Clone, PartialEq)]
pub enum Predicate {
Expand Down
8 changes: 4 additions & 4 deletions crates/ourios-querier/src/dsl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ mod display;
mod parse;
mod structured;

pub use display::serialize;
pub use ir::Query;
pub use parse::parse;
pub use structured::parse_structured;
pub use display::{serialize, serialize_statement};
pub use ir::{DriftQuery, Query, Statement};
pub use parse::{parse, parse_statement};
pub use structured::{parse_structured, parse_structured_statement};

/// The published JSON Schema (draft 2020-12) for the structured query surface
/// (RFC 0002 §6.4). Versioned alongside the parser and snapshot-tested so any
Expand Down
139 changes: 131 additions & 8 deletions crates/ourios-querier/src/dsl/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,56 @@

use super::DslError;
use super::ir::{
AggFn, Call, CmpOp, Field, OrdOp, Predicate, Query, SeverityName, SeverityValue, Stage, Time,
Value,
AggFn, Call, CmpOp, DriftQuery, Field, OrdOp, Predicate, Query, SeverityName, SeverityValue,
Stage, Statement, Time, Value,
};

/// Parse a β-surface query string into the shared [`Query`] IR.
/// Parse a β-surface query string into a [`Statement`] — a log query or a
/// RFC 0010 `drift` query, dispatched on the leading token (`drift` is the
/// only verb head; anything else parses as the RFC 0002 `predicate { | stage }`
/// pipeline).
///
/// # Errors
///
/// Returns [`DslError`] for any lexical or grammatical violation of §7,
/// citing the offending token/clause.
pub fn parse(input: &str) -> Result<Query, DslError> {
/// Returns [`DslError`] for any lexical or grammatical violation of the §7
/// grammar or the RFC 0010 §6.1 `drift` head, citing the offending
/// token/clause.
pub fn parse_statement(input: &str) -> Result<Statement, DslError> {
let tokens = tokenize(input)?;
let mut parser = Parser {
tokens: &tokens,
pos: 0,
};
let query = parser.parse_query()?;
// `drift` is a reserved verb head (RFC 0010 §6.1): a query that opens with
// it is a drift query, not a `drift == …` log predicate.
let statement = if matches!(parser.peek(), Some(Tok::Ident(s)) if s == "drift") {
Statement::Drift(parser.parse_drift_query()?)
} else {
Statement::Logs(parser.parse_query()?)
};
parser.expect_eof()?;
Ok(query)
Ok(statement)
}

/// Parse a β-surface query string into the shared [`Query`] IR (the RFC 0002
/// log-query pipeline).
///
/// # Errors
///
/// Returns [`DslError`] for any lexical or grammatical violation of §7,
/// citing the offending token/clause. A RFC 0010 `drift` head is rejected
/// here — use [`parse_statement`] to accept it.
pub fn parse(input: &str) -> Result<Query, DslError> {
match parse_statement(input)? {
Statement::Logs(query) => Ok(query),
Statement::Drift(_) => Err(DslError::new(
concat!(
"`drift` is an audit-stream query, not a log query; ",
"it has no place in a log-record pipeline"
)
.to_string(),
)),
Comment thread
jensholdgaard marked this conversation as resolved.
Resolved
}
}

// ---- tokenizer ----------------------------------------------------------
Expand Down Expand Up @@ -488,6 +519,29 @@ impl<'a> Parser<'a> {
Ok(Query { predicate, stages })
}

/// `drift_query = "drift" , "from" , time , "to" , time` (RFC 0010 §6.1).
/// A closed verb head: it admits no `|` stages (the projection, grouping,
/// and ordering are fixed by §6.3), so a trailing pipe is rejected by the
/// caller's `expect_eof`.
fn parse_drift_query(&mut self) -> Result<DriftQuery, DslError> {
self.pos += 1; // consume `drift`
if !self.eat_keyword("from") {
return Err(DslError::new(format!(
"expected `from` after `drift`, found {}",
describe(self.peek())
)));
}
let from = self.parse_time()?;
if !self.eat_keyword("to") {
return Err(DslError::new(format!(
"expected `to` after the drift window's `from` bound, found {}",
describe(self.peek())
)));
}
let to = self.parse_time()?;
Ok(DriftQuery { from, to })
}

/// `predicate = or_expr`.
fn parse_predicate(&mut self) -> Result<Predicate, DslError> {
self.parse_or()
Expand Down Expand Up @@ -1390,4 +1444,73 @@ mod tests {
assert!(validate_sort_key("").is_err());
assert!(validate_sort_key("3rd").is_err());
}

#[test]
fn parses_drift_head_with_all_time_forms() {
// Arrange / Act — `drift from <t1> to <t2>` over each §7 time form.
use crate::dsl::ir::Statement;
let rel = parse_statement("drift from -7d to now").unwrap();
let abs =
parse_statement("drift from 2026-06-01T00:00:00Z to 2026-06-02T00:00:00Z").unwrap();
// Assert
match rel {
Statement::Drift(d) => {
assert_eq!(
d.from,
Time::Duration {
neg: true,
literal: "7d".into()
}
);
assert_eq!(d.to, Time::Now);
}
Statement::Logs(_) => panic!("expected Drift, got Logs"),
}
assert!(matches!(
abs,
Statement::Drift(d) if matches!(d.from, Time::Timestamp(_)) && matches!(d.to, Time::Timestamp(_))
));
}

#[test]
fn drift_head_forecloses_trailing_stages() {
// A drift query is a closed verb head (RFC 0010 §6.1): a trailing `|`
// stage is rejected, not silently composed.
assert!(parse_statement("drift from -7d to now | limit 10").is_err());
assert!(parse_statement("drift from -7d to now | count by template_id").is_err());
}

#[test]
fn drift_head_requires_from_and_to() {
assert!(parse_statement("drift").is_err());
assert!(parse_statement("drift from -7d").is_err());
assert!(parse_statement("drift -7d to now").is_err());
assert!(parse_statement("drift from -7d now").is_err());
}

#[test]
fn parse_rejects_drift_as_a_log_query() {
// The RFC 0002 `parse` entry stays log-only: a drift head is an error
// there (callers wanting it use `parse_statement`).
let err = parse("drift from -7d to now").unwrap_err();
assert!(err.message().contains("audit-stream"), "{}", err.message());
}

#[test]
fn drift_is_not_a_reserved_field_in_a_log_query() {
// `drift` is only special as the *leading* verb head (RFC 0010 §6.1):
// once a query has started, a `drift` token is an ordinary identifier
// (here an attribute key), so a log query never swallows it as a verb.
// The query must contain a `drift` token after the head, else the test
// wouldn't exercise the thing it names.
use crate::dsl::ir::{Field, Predicate, Statement};
let s = parse_statement("attr.drift == \"x\" | limit 5").unwrap();
let Statement::Logs(query) = s else {
panic!("a mid-query `drift` token must stay a Logs query, got {s:?}");
};
assert!(matches!(
query.predicate,
Predicate::Comparison { field: Field::Attr(ref k), .. } if k == "drift"
));
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}
Loading
Loading