use crate::input::proto::substrait;
use crate::output::diagnostic;
use crate::parse::context;
use crate::util;
pub fn parse_fetch_rel(
x: &substrait::FetchRel,
y: &mut context::Context,
) -> diagnostic::Result<()> {
let in_type = handle_rel_input!(x, y);
y.set_schema(in_type);
proto_primitive_field!(x, y, offset, |x, y| {
if *x < 0 {
diagnostic!(y, Error, IllegalValue, "offsets cannot be negative");
}
Ok(())
});
proto_primitive_field!(x, y, count, |x, y| {
if *x < 0 {
diagnostic!(y, Error, IllegalValue, "count cannot be negative");
}
Ok(())
});
if x.count == 1 {
describe!(
y,
Relation,
"Propagate only the {} row",
(x.offset + 1)
.try_into()
.map(util::string::describe_nth)
.unwrap_or_else(|_| String::from("?"))
);
} else if x.count > 1 {
if x.offset > 1 {
describe!(
y,
Relation,
"Propagate only {} rows, starting from the {}",
x.count,
(x.offset + 1)
.try_into()
.map(util::string::describe_nth)
.unwrap_or_else(|_| String::from("?"))
);
} else {
describe!(y, Relation, "Propagate only the first {} rows", x.count);
}
} else if x.offset == 0 {
describe!(y, Relation, "Fetch all rows");
} else if x.offset == 1 {
describe!(y, Relation, "Discard the first row");
} else if x.offset > 1 {
describe!(y, Relation, "Discard the first {} rows", x.offset);
} else {
describe!(y, Relation, "Invalid fetch relation");
}
handle_rel_common!(x, y);
handle_advanced_extension!(x, y);
Ok(())
}