use std::collections::BTreeMap;
use evalhub_core::validate::is_valid_id;
use evalhub_schema::RecordKind;
use evalhub_schema::error::{ErrorCode, ErrorEntry};
use evalhub_schema::query::{self as envelope, QueryRequest};
use serde_json::Value;
use crate::grammar::{self, Filter, MatchTerm, ScalarOp};
use crate::ir::{self, CardRef};
const CARD_FACETS: [&str; 7] = [
"model",
"task",
"harness",
"generation",
"trial",
"grading",
"env",
];
pub const GENERATED_COLUMNS: &[(&[&str], &str)] = &[
(&["title"], "title"),
(&["producer", "name"], "producer_name"),
(&["model", "id"], "model_id"),
(&["model", "provider"], "model_provider"),
(&["model", "revision"], "model_revision"),
(&["task", "id"], "task_id"),
(&["task", "version"], "task_version"),
(&["task", "split"], "task_split"),
(&["harness", "name"], "harness_name"),
(&["harness", "version"], "harness_version"),
(&["generation", "temperature"], "gen_temperature"),
(&["generation", "top_p"], "gen_top_p"),
(&["generation", "max_tokens"], "gen_max_tokens"),
(&["trial", "k"], "trial_k"),
(&["env", "git", "commit"], "env_git_commit"),
(&["env", "git", "dirty"], "env_git_dirty"),
];
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Indexed {
Full,
EqExistsOnly,
EqExistsUntyped,
}
#[derive(Debug, Clone, PartialEq)]
pub enum PathInfo {
Scalar {
column: ir::Column,
ty: ir::ValueType,
indexed: Indexed,
},
Array {
table: ir::ArrayTable,
},
}
#[derive(Debug, Clone, PartialEq)]
pub struct ExtSchema {
pub ns: String,
pub paths: Vec<(Vec<String>, ir::ValueType)>,
pub applied: bool,
}
#[derive(Debug, Default, Clone)]
pub struct PathTable {
paths: BTreeMap<String, PathInfo>,
target: Target,
}
#[derive(Debug, Default, Clone, PartialEq, Eq)]
enum Target {
#[default]
Record,
Runs {
cards: Vec<CardRef>,
},
}
impl PathTable {
pub fn from_schema(kind: RecordKind) -> Self {
let mut paths = BTreeMap::new();
let schema = kind.schema().to_value();
let root_props = schema
.get("properties")
.and_then(Value::as_object)
.cloned()
.unwrap_or_default();
let defs = schema.get("$defs").cloned().unwrap_or(Value::Null);
for (name, prop) in &root_props {
match name.as_str() {
"ext" => {}
"results" => {
paths.insert(
name.clone(),
PathInfo::Array {
table: ir::ArrayTable::Results,
},
);
}
"relations" => {
paths.insert(
name.clone(),
PathInfo::Array {
table: ir::ArrayTable::Relations,
},
);
}
"attachments" => {
paths.insert(
name.clone(),
PathInfo::Array {
table: ir::ArrayTable::Attachments,
},
);
}
_ => {
let mut leaves = Vec::new();
collect_leaves(&mut leaves, &defs, prop, std::slice::from_ref(name));
for (segments, ty) in leaves {
let column = GENERATED_COLUMNS
.iter()
.find(|(json_path, _)| json_path == &segments.as_slice())
.map(|(_, name)| ir::Column::Generated((*name).to_string()));
let (column, indexed) = match column {
Some(column) => (column, Indexed::Full),
None => (ir::Column::Body(segments.clone()), Indexed::EqExistsOnly),
};
paths.insert(
segments.join("."),
PathInfo::Scalar {
column,
ty,
indexed,
},
);
}
}
}
}
for facet in facets(kind) {
paths.insert(
format!("fingerprint.{facet}"),
PathInfo::Scalar {
column: ir::Column::Fingerprint((*facet).to_string()),
ty: ir::ValueType::String,
indexed: Indexed::Full,
},
);
}
Self {
paths,
target: Target::Record,
}
}
pub fn for_runs(cards: &[CardRef]) -> Self {
use ir::{RunColumn as R, ValueType as V};
let mut paths = BTreeMap::new();
let mut scalar = |path: &str, column: R, ty: V| {
paths.insert(
path.to_string(),
PathInfo::Scalar {
column: ir::Column::Run(column),
ty,
indexed: Indexed::Full,
},
);
};
scalar("run_id", R::RunId, V::String);
scalar("status", R::Status, V::String);
scalar("error.kind", R::ErrorKind, V::String);
scalar("started_at", R::StartedAt, V::String);
scalar("ended_at", R::EndedAt, V::String);
let facets = facets(RecordKind::Eval);
let schema = RecordKind::Eval.schema().to_value();
let defs = schema.get("$defs").cloned().unwrap_or(Value::Null);
let root_props = schema
.get("properties")
.and_then(Value::as_object)
.cloned()
.unwrap_or_default();
for facet in facets {
if let Some(prop) = root_props.get(*facet) {
let mut leaves = Vec::new();
collect_leaves(&mut leaves, &defs, prop, &[(*facet).to_string()]);
for (segments, ty) in leaves {
let path = segments.join(".");
scalar(&path, R::Facet(segments), ty);
}
}
scalar(
&format!("fingerprint.{facet}"),
R::Fingerprint((*facet).to_string()),
V::String,
);
}
let mut named: Vec<CardRef> = Vec::with_capacity(cards.len());
for card in cards {
if !named.contains(card) {
named.push(card.clone());
}
}
Self {
paths,
target: Target::Runs { cards: named },
}
}
pub fn cards(&self) -> &[CardRef] {
match &self.target {
Target::Record => &[],
Target::Runs { cards } => cards,
}
}
pub fn is_runs(&self) -> bool {
matches!(self.target, Target::Runs { .. })
}
pub fn with_ext(&self, entries: &[ExtSchema]) -> Self {
let mut paths = self.paths.clone();
for entry in entries.iter().filter(|e| e.applied) {
for (segments, ty) in &entry.paths {
let path = format!("ext.{}.{}", entry.ns, segments.join("."));
let mut json_path = vec!["ext".to_string(), entry.ns.clone()];
json_path.extend(segments.iter().cloned());
paths.insert(
path,
PathInfo::Scalar {
column: ir::Column::Ext {
path: json_path,
ty: *ty,
},
ty: *ty,
indexed: Indexed::Full,
},
);
}
}
Self {
paths,
target: self.target.clone(),
}
}
pub fn lookup(&self, path: &str) -> Option<PathInfo> {
self.resolve(path).ok()
}
fn resolve(&self, path: &str) -> Result<PathInfo, String> {
if let Some(info) = self.paths.get(path) {
return Ok(info.clone());
}
if let Target::Runs { cards } = &self.target
&& let Some(result) = resolve_run_param(path, cards)
{
return result;
}
if let Some(rest) = path.strip_prefix("ext.") {
if let Some((ns, key)) = rest.split_once('.')
&& !ns.is_empty()
&& !key.is_empty()
{
let mut json_path = vec!["ext".to_string(), ns.to_string()];
json_path.extend(key.split('.').map(str::to_string));
return Ok(PathInfo::Scalar {
column: ir::Column::Body(json_path),
ty: ir::ValueType::String,
indexed: Indexed::EqExistsUntyped,
});
}
}
Err(match self.target {
Target::Record => format!("`{path}` is not a queryable path of this record"),
Target::Runs { .. } => format!("`{path}` is not a queryable path of a run"),
})
}
pub fn paths(&self) -> impl Iterator<Item = (&str, &PathInfo)> {
self.paths.iter().map(|(k, v)| (k.as_str(), v))
}
}
fn resolve_run_param(path: &str, cards: &[CardRef]) -> Option<Result<PathInfo, String>> {
use ir::{ResultField, RunColumn as R, ValueType as V};
let full = |column: R, ty: V| PathInfo::Scalar {
column: ir::Column::Run(column),
ty,
indexed: Indexed::Full,
};
if let Some(rest) = path.strip_prefix("metrics[") {
let Some(metric) = rest.strip_suffix(']') else {
return Some(Err(format!(
"a run's metric is `metrics[{{ns}}/{{name}}]`, a number with no fields; \
`{path}` does not end at the closing bracket"
)));
};
if !is_valid_id(metric) {
return Some(Err(format!(
"`{metric}` is not a metric id of the form `{{ns}}/{{name}}`"
)));
}
return Some(Ok(full(R::Metric(metric.to_string()), V::Number)));
}
if path == "metrics" || path.starts_with("metrics.") {
return Some(Err(format!(
"a run's metrics are addressed with brackets, `metrics[{{ns}}/{{name}}]`, \
because a metric id may contain `.`; `{path}` is not a path"
)));
}
if let Some(rest) = path.strip_prefix("results[") {
const FORM: &str = "`results[{card}][{metric}].value` or `.label`";
let Some((card, rest)) = rest.split_once("][") else {
return Some(Err(format!(
"a judgement is addressed as {FORM}; `{path}` is not of that form"
)));
};
let Some((metric, field)) = rest.split_once("].") else {
return Some(Err(format!(
"a judgement is addressed as {FORM}; `{path}` is not of that form"
)));
};
let Some(card) = CardRef::parse(card) else {
return Some(Err(format!(
"`{card}` is not a Card reference of the form `{{ns}}/{{name}}`"
)));
};
if !cards.contains(&card) {
return Some(Err(format!(
"the Card `{card}` is not named in this request; name it in `cards` to join \
its judgements"
)));
}
if !is_valid_id(metric) {
return Some(Err(format!(
"`{metric}` is not a metric id of the form `{{ns}}/{{name}}`"
)));
}
let (field, ty) = match field {
"value" => (ResultField::Value, V::Number),
"label" => (ResultField::Label, V::String),
_ => {
return Some(Err(format!(
"a judgement has `value` and `label`; `{field}` is neither"
)));
}
};
return Some(Ok(full(
R::Result {
card,
metric: metric.to_string(),
field,
},
ty,
)));
}
if path == "results" || path.starts_with("results.") {
return Some(Err(format!(
"a Card's judgements are addressed with brackets, \
`results[{{card}}][{{metric}}].value` or `.label`, because a Card name and a \
metric id may contain `.`; `{path}` is not a path"
)));
}
None
}
fn facets(kind: RecordKind) -> &'static [&'static str] {
match kind {
RecordKind::Card => &CARD_FACETS,
RecordKind::Eval => &["model", "task", "harness", "generation", "trial", "env"],
}
}
fn collect_leaves(
out: &mut Vec<(Vec<String>, ir::ValueType)>,
defs: &Value,
prop: &Value,
segments: &[String],
) {
let resolved = resolve(defs, prop);
let Some(resolved) = resolved else { return };
if let Some(properties) = resolved.get("properties").and_then(Value::as_object) {
for (name, child) in properties {
if name == "ext" {
continue;
}
let mut next = segments.to_vec();
next.push(name.clone());
collect_leaves(out, defs, child, &next);
}
return;
}
let Some(ty) = scalar_type(&resolved) else {
return;
};
out.push((segments.to_vec(), ty));
}
fn resolve(defs: &Value, prop: &Value) -> Option<Value> {
if let Some(reference) = prop.get("$ref").and_then(Value::as_str) {
let name = reference.rsplit('/').next()?;
return defs.get(name).cloned();
}
if let Some(any_of) = prop.get("anyOf").and_then(Value::as_array) {
for branch in any_of {
if branch.get("type").and_then(Value::as_str) == Some("null") {
continue;
}
if let Some(resolved) = resolve(defs, branch) {
return Some(resolved);
}
}
return None;
}
Some(prop.clone())
}
fn scalar_type(schema: &Value) -> Option<ir::ValueType> {
let ty = schema.get("type")?;
let names: Vec<&str> = match ty {
Value::String(s) => vec![s.as_str()],
Value::Array(items) => items.iter().filter_map(Value::as_str).collect(),
_ => return None,
};
for name in names {
match name {
"string" => return Some(ir::ValueType::String),
"number" | "integer" => return Some(ir::ValueType::Number),
"boolean" => return Some(ir::ValueType::Boolean),
_ => continue,
}
}
None
}
fn array_column(table: ir::ArrayTable, key: &str) -> Option<(ir::ArrayColumn, ir::ValueType)> {
use ir::{ArrayColumn as C, ArrayTable as T, ValueType as V};
match (table, key) {
(T::Results, "metric") => Some((C::ResultMetric, V::String)),
(T::Results, "aggregation") => Some((C::ResultAggregation, V::String)),
(T::Results, "value") => Some((C::ResultValue, V::Number)),
(T::Results, "n") => Some((C::ResultN, V::Number)),
(T::Relations, "type") => Some((C::RelationType, V::String)),
(T::Relations, "to") => Some((C::RelationTo, V::String)),
(T::Attachments, "path") => Some((C::AttachmentPath, V::String)),
(T::Attachments, "sha256") => Some((C::AttachmentSha256, V::String)),
_ => None,
}
}
fn array_keys(table: ir::ArrayTable) -> &'static [&'static str] {
match table {
ir::ArrayTable::Results => &["metric", "aggregation", "value", "n"],
ir::ArrayTable::Relations => &["type", "to"],
ir::ArrayTable::Attachments => &["path", "sha256"],
}
}
fn entry(path: &str, code: ErrorCode, hint: impl Into<String>) -> ErrorEntry {
ErrorEntry {
path: path.to_string(),
code,
hint: Some(hint.into()),
}
}
fn op_suits(op: ScalarOp, ty: ir::ValueType) -> bool {
use ir::ValueType as V;
match op {
ScalarOp::Eq | ScalarOp::Ne | ScalarOp::In => true,
ScalarOp::Gt | ScalarOp::Gte | ScalarOp::Lt | ScalarOp::Lte => {
matches!(ty, V::Number | V::String)
}
ScalarOp::Prefix | ScalarOp::Contains => matches!(ty, V::String),
}
}
fn index_serves(indexed: Indexed, op: ScalarOp) -> bool {
match indexed {
Indexed::Full => true,
Indexed::EqExistsOnly | Indexed::EqExistsUntyped => matches!(op, ScalarOp::Eq),
}
}
fn literal_matches(value: &Value, ty: ir::ValueType) -> bool {
match ty {
ir::ValueType::Number => value.is_number(),
ir::ValueType::String => value.is_string(),
ir::ValueType::Boolean => value.is_boolean(),
}
}
fn type_name(ty: ir::ValueType) -> &'static str {
match ty {
ir::ValueType::Number => "a number",
ir::ValueType::String => "a string",
ir::ValueType::Boolean => "a boolean",
}
}
fn check_cmp(
column: ir::Column,
ty: ir::ValueType,
indexed: Indexed,
op: ScalarOp,
value: &Value,
pointer: &str,
errors: &mut Vec<ErrorEntry>,
) -> Option<ir::Cmp> {
let mut ok = true;
let typed = indexed != Indexed::EqExistsUntyped;
if typed && !op_suits(op, ty) {
errors.push(entry(
&format!("{pointer}/op"),
ErrorCode::TypeMismatch,
format!("`{op}` does not apply to {}", type_name(ty)),
));
ok = false;
} else if !index_serves(indexed, op) {
errors.push(entry(
&format!("{pointer}/op"),
ErrorCode::NotIndexed,
format!(
"`{op}` needs an index this path does not have; it answers `eq` and `exists` \
from the index over the whole record. Register an `ext_schema` for it in the \
registry (`PUT /registry/ext_schemas/{{ns}}/{{id}}@{{version}}`) to make it \
sortable and rangeable."
),
));
ok = false;
}
if op == ScalarOp::In {
match value.as_array() {
None => {
errors.push(entry(
&format!("{pointer}/value"),
ErrorCode::TypeMismatch,
"`in` takes an array",
));
ok = false;
}
Some(items) => {
if let Some(bad) = items.iter().position(|v| typed && !literal_matches(v, ty)) {
errors.push(entry(
&format!("{pointer}/value/{bad}"),
ErrorCode::TypeMismatch,
format!("this path holds {}", type_name(ty)),
));
ok = false;
}
}
}
} else if typed && !literal_matches(value, ty) {
errors.push(entry(
&format!("{pointer}/value"),
ErrorCode::TypeMismatch,
format!("this path holds {}", type_name(ty)),
));
ok = false;
}
ok.then(|| ir::Cmp {
column,
op: to_ir_op(op),
value: value.clone(),
})
}
fn to_ir_op(op: ScalarOp) -> ir::Op {
match op {
ScalarOp::Eq => ir::Op::Eq,
ScalarOp::Ne => ir::Op::Ne,
ScalarOp::Gt => ir::Op::Gt,
ScalarOp::Gte => ir::Op::Gte,
ScalarOp::Lt => ir::Op::Lt,
ScalarOp::Lte => ir::Op::Lte,
ScalarOp::In => ir::Op::In,
ScalarOp::Prefix => ir::Op::Prefix,
ScalarOp::Contains => ir::Op::Contains,
}
}
pub fn check(ast: &Filter, table: &PathTable) -> Result<ir::Filter, Vec<ErrorEntry>> {
let mut errors = Vec::new();
let filter = check_at(ast, table, "/where", &mut errors);
match (filter, errors.is_empty()) {
(Some(filter), true) => Ok(filter),
_ => Err(errors),
}
}
fn check_at(
ast: &Filter,
table: &PathTable,
pointer: &str,
errors: &mut Vec<ErrorEntry>,
) -> Option<ir::Filter> {
match ast {
Filter::And { children, .. } | Filter::Or { children, .. } => {
let key = grammar::wire_key(ast);
let checked: Vec<ir::Filter> = children
.iter()
.enumerate()
.filter_map(|(i, child)| {
check_at(child, table, &format!("{pointer}/{key}/{i}"), errors)
})
.collect();
if checked.len() != children.len() {
return None;
}
Some(match ast {
Filter::And { .. } => ir::Filter::And(checked),
_ => ir::Filter::Or(checked),
})
}
Filter::Not { child, .. } => {
let inner = check_at(child, table, &format!("{pointer}/not"), errors)?;
Some(ir::Filter::Not(Box::new(inner)))
}
Filter::Compare {
path, op, value, ..
} => {
let info = lookup_or_report(table, path, pointer, errors)?;
match info {
PathInfo::Scalar {
column,
ty,
indexed,
} => {
check_cmp(column, ty, indexed, *op, value, pointer, errors).map(ir::Filter::Cmp)
}
PathInfo::Array { .. } => {
errors.push(entry(
&format!("{pointer}/op"),
ErrorCode::TypeMismatch,
format!("`{path}` is an array; search it with `any` and a `match`"),
));
None
}
}
}
Filter::Exists { path, .. } => {
let info = lookup_or_report(table, path, pointer, errors)?;
match info {
PathInfo::Scalar { column, .. } => Some(ir::Filter::Exists { column }),
PathInfo::Array { .. } => {
errors.push(entry(
&format!("{pointer}/path"),
ErrorCode::TypeMismatch,
format!("`{path}` is an array; search it with `any` and a `match`"),
));
None
}
}
}
Filter::Any { path, terms, .. } => {
let info = lookup_or_report(table, path, pointer, errors)?;
let PathInfo::Array { table: side } = info else {
errors.push(entry(
&format!("{pointer}/op"),
ErrorCode::TypeMismatch,
format!("`{path}` is not an array; `any` ranges over one"),
));
return None;
};
check_terms(side, terms, pointer, errors).map(|conditions| ir::Filter::Any {
table: side,
conditions,
})
}
}
}
fn check_terms(
side: ir::ArrayTable,
terms: &BTreeMap<String, MatchTerm>,
pointer: &str,
errors: &mut Vec<ErrorEntry>,
) -> Option<Vec<ir::Cmp>> {
if terms.is_empty() {
errors.push(entry(
&format!("{pointer}/match"),
ErrorCode::Schema,
format!(
"`match` needs at least one of {}",
array_keys(side)
.iter()
.map(|k| format!("`{k}`"))
.collect::<Vec<_>>()
.join(", ")
),
));
return None;
}
let mut conditions = Vec::new();
let mut ok = true;
for (key, term) in terms {
let term_pointer = format!("{pointer}/match/{key}");
let Some((column, ty)) = array_column(side, key) else {
errors.push(entry(
&term_pointer,
ErrorCode::UnknownPath,
format!(
"unknown key `{key}`; this array has {}",
array_keys(side)
.iter()
.map(|k| format!("`{k}`"))
.collect::<Vec<_>>()
.join(", ")
),
));
ok = false;
continue;
};
match check_cmp(
ir::Column::Array(column),
ty,
Indexed::Full,
term.op,
&term.value,
&term_pointer,
errors,
) {
Some(cmp) => conditions.push(cmp),
None => ok = false,
}
}
ok.then_some(conditions)
}
fn lookup_or_report(
table: &PathTable,
path: &str,
pointer: &str,
errors: &mut Vec<ErrorEntry>,
) -> Option<PathInfo> {
match table.resolve(path) {
Ok(info) => Some(info),
Err(hint) => {
errors.push(entry(
&format!("{pointer}/path"),
ErrorCode::UnknownPath,
hint,
));
None
}
}
}
fn compile_filter(
request: &QueryRequest,
table: &PathTable,
errors: &mut Vec<ErrorEntry>,
) -> Option<ir::Filter> {
match &request.where_ {
None => None,
Some(value) => match grammar::parse(value) {
Ok(ast) => match check(&ast, table) {
Ok(filter) => Some(filter),
Err(mut e) => {
errors.append(&mut e);
None
}
},
Err(mut e) => {
errors.append(&mut e);
None
}
},
}
}
fn to_ir_dir(dir: envelope::Dir) -> ir::Dir {
match dir {
envelope::Dir::Asc => ir::Dir::Asc,
envelope::Dir::Desc => ir::Dir::Desc,
}
}
pub fn compile(
request: &QueryRequest,
kind: RecordKind,
table: &PathTable,
limit: u32,
cursor: Option<ir::Cursor>,
) -> Result<ir::Query, Vec<ErrorEntry>> {
debug_assert!(!table.is_runs(), "a record query needs a record table");
let mut errors = Vec::new();
let filter = compile_filter(request, table, &mut errors);
let mut sort = Vec::new();
for (i, key) in request.sort.iter().enumerate() {
let pointer = format!("/sort/{i}");
if let Some(checked) = check_sort(&key.path, table, &pointer, &mut errors) {
sort.push(ir::Sort {
key: checked,
dir: to_ir_dir(key.dir),
});
}
}
if let Some(cursor) = &cursor
&& cursor.keys.len() != sort.len()
{
errors.push(entry(
"/cursor",
ErrorCode::Schema,
"the cursor does not match this sort; start the page sequence again",
));
}
if !errors.is_empty() {
return Err(errors);
}
Ok(ir::Query {
record_type: match kind {
RecordKind::Card => ir::RecordType::Card,
RecordKind::Eval => ir::RecordType::Eval,
},
filter,
sort,
limit,
cursor,
expand: request
.expand
.iter()
.map(|e| match e {
envelope::Expand::Relations => ir::Expand::Relations,
envelope::Expand::Badges => ir::Expand::Badges,
envelope::Expand::Fingerprints => ir::Expand::Fingerprints,
envelope::Expand::Changed => ir::Expand::Changed,
})
.collect(),
version: match request.version {
None | Some(envelope::VersionSelector::Latest) => ir::VersionSelector::Latest,
Some(envelope::VersionSelector::All) => ir::VersionSelector::All,
},
})
}
fn check_sort(
path: &str,
table: &PathTable,
pointer: &str,
errors: &mut Vec<ErrorEntry>,
) -> Option<ir::SortKey> {
if path == "created_at" {
return Some(ir::SortKey::CreatedAt);
}
if let Some(rest) = path.strip_prefix("results[")
&& let Some((metric, tail)) = rest.split_once(']')
{
if tail != ".value" {
errors.push(entry(
&format!("{pointer}/path"),
ErrorCode::UnknownPath,
"a metric sorts by its `value`: `results[{metric}].value`",
));
return None;
}
if !metric.contains('/') {
errors.push(entry(
&format!("{pointer}/path"),
ErrorCode::UnknownPath,
format!("`{metric}` is not a metric id of the form `{{ns}}/{{name}}`"),
));
return None;
}
return Some(ir::SortKey::Metric(metric.to_string()));
}
match table.lookup(path) {
Some(PathInfo::Scalar {
column,
indexed: Indexed::Full,
..
}) => Some(ir::SortKey::Column(column)),
Some(PathInfo::Scalar { .. }) => {
errors.push(entry(
&format!("{pointer}/path"),
ErrorCode::NotIndexed,
format!(
"`{path}` has no ordered index, so it cannot be sorted on. Register an \
`ext_schema` for it in the registry to make it sortable."
),
));
None
}
Some(PathInfo::Array { .. }) => {
errors.push(entry(
&format!("{pointer}/path"),
ErrorCode::TypeMismatch,
format!("`{path}` is an array; sort by `results[{{metric}}].value` instead"),
));
None
}
None => {
errors.push(entry(
&format!("{pointer}/path"),
ErrorCode::UnknownPath,
format!("`{path}` is not a queryable path of this record"),
));
None
}
}
}
pub fn compile_runs(
request: &QueryRequest,
table: &PathTable,
limit: u32,
cursor: Option<ir::RunCursor>,
) -> Result<ir::RunQuery, Vec<ErrorEntry>> {
debug_assert!(table.is_runs(), "a run query needs PathTable::for_runs");
let mut errors = Vec::new();
let filter = compile_filter(request, table, &mut errors);
let mut sort = Vec::new();
for (i, key) in request.sort.iter().enumerate() {
let pointer = format!("/sort/{i}");
if let Some(checked) = check_run_sort(&key.path, table, &pointer, &mut errors) {
sort.push(ir::Sort {
key: checked,
dir: to_ir_dir(key.dir),
});
}
}
if let Some(cursor) = &cursor
&& cursor.keys.len() != sort.len()
{
errors.push(entry(
"/cursor",
ErrorCode::Schema,
"the cursor does not match this sort; start the page sequence again",
));
}
if !errors.is_empty() {
return Err(errors);
}
Ok(ir::RunQuery {
cards: table.cards().to_vec(),
filter,
sort,
limit,
cursor,
})
}
fn check_run_sort(
path: &str,
table: &PathTable,
pointer: &str,
errors: &mut Vec<ErrorEntry>,
) -> Option<ir::SortKey> {
match table.resolve(path) {
Ok(PathInfo::Scalar {
column: ir::Column::Run(column),
..
}) => Some(ir::SortKey::Run(column)),
Ok(PathInfo::Scalar {
column,
indexed: Indexed::Full,
..
}) => Some(ir::SortKey::Column(column)),
Ok(PathInfo::Scalar { .. }) => {
errors.push(entry(
&format!("{pointer}/path"),
ErrorCode::NotIndexed,
format!(
"`{path}` has no declared type, so it has no order. Register an \
`ext_schema` for it in the registry to make it sortable."
),
));
None
}
Ok(PathInfo::Array { .. }) => {
errors.push(entry(
&format!("{pointer}/path"),
ErrorCode::TypeMismatch,
format!("`{path}` is an array and cannot be sorted on"),
));
None
}
Err(hint) => {
errors.push(entry(
&format!("{pointer}/path"),
ErrorCode::UnknownPath,
hint,
));
None
}
}
}