mod apply;
mod filter;
mod flow;
mod summary;
mod value;
pub use filter::Filter;
pub use flow::{RowFlow, Step};
#[derive(Debug, Clone, Default)]
pub struct Pipeline {
pub select: Vec<String>,
pub filters: Vec<Filter>,
pub sort: Option<String>,
pub dedupe_by: Option<String>,
pub cursor: Option<usize>,
pub limit: Option<usize>,
pub count_only: bool,
pub summary: bool,
pub redact: bool,
pub truncate_cell: Option<usize>,
pub max_output_bytes: Option<usize>,
pub most_frequent_shown: Option<usize>,
}
impl Pipeline {
#[must_use]
pub fn is_identity(&self) -> bool {
self.select.is_empty()
&& self.filters.is_empty()
&& self.sort.is_none()
&& self.dedupe_by.is_none()
&& self.cursor.is_none()
&& self.limit.is_none()
&& !self.count_only
&& !self.summary
&& !self.redact
&& self.truncate_cell.is_none()
&& self.max_output_bytes.is_none()
}
#[must_use]
pub const fn accumulates(&self) -> bool {
!self.accumulated_for().is_empty()
}
#[must_use]
pub const fn accumulated_for(&self) -> &'static [&'static str] {
match (self.sort.is_some(), self.count_only, self.summary) {
(true, true, _) => &["sort", "count-only"],
(true, false, true) => &["sort", "summary"],
(true, false, false) => &["sort"],
(false, true, _) => &["count-only"],
(false, false, true) => &["summary"],
(false, false, false) => &[],
}
}
#[must_use]
pub fn columns_after_projection(&self, declared: &[String]) -> Vec<String> {
if self.select.is_empty() {
return declared.to_vec();
}
self.select
.iter()
.filter(|path| {
let path = path.as_str();
let head = path.split('.').next().unwrap_or(path);
declared.iter().any(|name| name == head)
})
.cloned()
.collect()
}
#[must_use]
pub fn flow(&self) -> RowFlow<'_> {
self.flow_charging(0)
}
pub fn flow_within(&self, frame: usize) -> crate::error::Result<RowFlow<'_>> {
if self.max_output_bytes.is_some_and(|ceiling| frame > ceiling) {
let ceiling = self.max_output_bytes.unwrap_or_default();
return Err(crate::error::Failure::limit(
"limit.output_bytes",
format!("the answer needs {frame} bytes and the volume ceiling is {ceiling}"),
)
.with_detail("detail", format!("{frame} > {ceiling}"))
.with_hint("raise --max-output-bytes, or ask for fewer columns"));
}
Ok(self.flow_charging(frame))
}
pub fn flow_framed(
&self,
formatter: &mut dyn crate::output::format::Formatter,
tail: &crate::output::Envelope,
columns: &[String],
rows: usize,
gaps: &[crate::output::envelope::Gap],
) -> crate::error::Result<RowFlow<'_>> {
if self.max_output_bytes.is_none() {
return Ok(self.flow());
}
let mut measured = Vec::new();
formatter.begin_stream(&mut measured, tail, columns)?;
let opening = measured.len();
let cut = self.widest_report(rows);
let mut whole = cut.clone();
whole.output_truncated = false;
let emitted = rows as u64;
formatter.end_stream(&mut measured, tail, emitted, gaps, Some(&cut))?;
let after_cut = measured.len();
formatter.end_stream(&mut measured, tail, emitted, gaps, Some(&whole))?;
let closing = (after_cut - opening).max(measured.len() - after_cut);
self.flow_within(opening + closing)
}
fn flow_charging(&self, charged: usize) -> RowFlow<'_> {
RowFlow {
pipeline: self,
seen: std::collections::BTreeSet::new(),
passed_over: 0,
offered: 0,
emitted: 0,
bytes: charged,
whole: true,
shortened: false,
redacted: false,
over_the_ceiling: false,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_projection_names_what_it_kept_and_nothing_it_invented() {
let declared = vec!["a".to_owned(), "b".to_owned(), "c".to_owned()];
let idle = Pipeline::default();
assert_eq!(idle.columns_after_projection(&declared), declared);
let projecting = Pipeline {
select: vec!["c".to_owned(), "a".to_owned(), "nowhere".to_owned()],
..Pipeline::default()
};
assert_eq!(
projecting.columns_after_projection(&declared),
["c", "a"],
"the order asked for is kept, and the path nothing declared is dropped"
);
}
#[test]
fn a_path_reaching_into_a_declared_column_survives() {
let declared = vec!["document".to_owned()];
let pipeline = Pipeline {
select: vec!["document.total".to_owned()],
..Pipeline::default()
};
assert_eq!(
pipeline.columns_after_projection(&declared),
["document.total"]
);
}
#[test]
fn only_an_order_or_a_count_needs_the_whole_set() {
assert!(Pipeline {
sort: Some("id".to_owned()),
..Pipeline::default()
}
.accumulates());
assert!(Pipeline {
count_only: true,
..Pipeline::default()
}
.accumulates());
for streaming in [
Pipeline {
limit: Some(10),
..Pipeline::default()
},
Pipeline {
filters: vec![Filter::parse("a=b").expect("parses")],
..Pipeline::default()
},
Pipeline {
select: vec!["a".to_owned()],
..Pipeline::default()
},
Pipeline {
dedupe_by: Some("a".to_owned()),
..Pipeline::default()
},
Pipeline {
truncate_cell: Some(10),
..Pipeline::default()
},
Pipeline {
max_output_bytes: Some(10),
..Pipeline::default()
},
] {
assert!(
!streaming.accumulates(),
"{streaming:?} decides one row at a time"
);
}
}
}