use crate::{Refusal, SqlError, Translation};
use quarb::reflect::QueryArbor;
use quarb::{AstAdapter, NodeId, Value};
pub fn export(quarb: &str) -> Result<Translation, SqlError> {
refuse_marker(quarb)?;
let arbor =
QueryArbor::parse(quarb).map_err(|e| SqlError::Syntax(format!("parsing Quarb: {e}")))?;
refuse_groups(&arbor)?;
let mut ex = Exporter {
arbor,
notes: Vec::new(),
strict: false,
dialect: None,
from_table: String::new(),
join_on_cols: Vec::new(),
join_table: None,
join_every: false,
column_types: ColumnTypes::default(),
from_sql: String::new(),
join_sql: None,
in_on: false,
order: PlanOrder::default(),
aggregate: false,
keyed_hops: false,
from_marks: Vec::new(),
join_marks: Vec::new(),
join_alias: None,
sums: Vec::new(),
};
let query = ex.query()?;
Ok(Translation {
query,
notes: ex.notes,
})
}
fn not_pushed(word: Refusal, reason: String) -> SqlError {
SqlError::NotPushed {
word,
mismatch: false,
reason,
}
}
fn mismatch(word: Refusal, reason: String) -> SqlError {
SqlError::NotPushed {
word,
mismatch: true,
reason,
}
}
fn refuse_marker(quarb: &str) -> Result<(), SqlError> {
for marker in ["__LEFT__", "__AGG__"] {
if quarb.contains(marker) {
return Err(not_pushed(
Refusal::Name,
format!("query text contains the reserved marker \"{marker}\""),
));
}
}
Ok(())
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FloatSum {
pub text: String,
pub mean: bool,
pub table: String,
pub column: String,
pub column_sql: String,
pub order: Vec<(String, String)>,
}
pub struct Pushdown {
pub sql: String,
pub sums: Vec<FloatSum>,
pub order_table: Option<String>,
pub join_left: Option<(String, Vec<String>)>,
pub order_then: Option<String>,
pub order_then_as: Option<String>,
pub order_first: Option<String>,
pub order_desc: bool,
pub order_groups: bool,
pub limit: Option<u64>,
pub record: bool,
pub properties: bool,
pub keyed_by: Option<(Refusal, String)>,
pub column_types: ColumnTypes,
pub tables: Vec<String>,
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct ColumnTypes {
pub numeric: Vec<String>,
pub text: Vec<String>,
pub json: Vec<String>,
pub json_children: bool,
pub columns_compared: bool,
pub computed: Vec<String>,
pub reduced: Vec<String>,
}
pub type ColumnFact = (String, bool, bool, bool);
impl ColumnTypes {
pub fn is_empty(&self) -> bool {
self.numeric.is_empty() && self.text.is_empty() && self.json.is_empty()
}
pub fn unmet(&self, catalog: &[ColumnFact]) -> Vec<String> {
let met = |column: &str, pick: fn(&ColumnFact) -> bool, want: bool| {
let mut found = catalog
.iter()
.filter(|(name, ..)| name.eq_ignore_ascii_case(column))
.peekable();
found.peek().is_some() && found.all(|entry| pick(entry) == want)
};
let numeric = |e: &ColumnFact| e.1;
let json = |e: &ColumnFact| e.2;
let exact_text = |e: &ColumnFact| !e.1 && e.3;
let mut out: Vec<String> = Vec::new();
for (columns, pick, want) in [
(&self.numeric, numeric as fn(&ColumnFact) -> bool, true),
(&self.text, exact_text, true),
(&self.json, json, true),
] {
for c in columns {
if !met(c, pick, want) && !out.contains(c) {
out.push(c.clone());
}
}
}
out
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Dialect {
Postgres,
MySql,
Sqlite,
Mssql,
Oracle,
}
pub fn pushdown(quarb: &str, dialect: Option<Dialect>) -> Option<Pushdown> {
pushdown_explained(quarb, dialect).ok()
}
pub fn pushdown_explained(quarb: &str, dialect: Option<Dialect>) -> Result<Pushdown, SqlError> {
refuse_marker(quarb)?;
let arbor =
QueryArbor::parse(quarb).map_err(|e| SqlError::Syntax(format!("parsing Quarb: {e}")))?;
refuse_groups(&arbor)?;
let mut ex = Exporter {
arbor,
notes: Vec::new(),
strict: true,
dialect,
from_table: String::new(),
join_on_cols: Vec::new(),
join_table: None,
join_every: false,
column_types: ColumnTypes::default(),
from_sql: String::new(),
join_sql: None,
in_on: false,
order: PlanOrder::default(),
aggregate: false,
keyed_hops: false,
from_marks: Vec::new(),
join_marks: Vec::new(),
join_alias: None,
sums: Vec::new(),
};
let sql = ex.query()?;
let order_table = if ex.aggregate && !ex.order.groups {
None
} else {
Some(ex.from_table.clone())
};
let join_left = if ex.join_every {
None
} else {
ex.join_table.clone().map(|t| (t, ex.join_on_cols.clone()))
};
let order_then = if ex.join_every && !ex.aggregate {
ex.join_table.clone()
} else {
None
};
let mut tables = vec![ex.from_table.clone()];
tables.extend(ex.join_table.clone());
let order_first = ex.order.sort.as_ref().map(|(e, desc)| {
let nulls = if ex.order.reversed { " DESC" } else { "" };
let dir = if *desc != ex.order.reversed {
" DESC"
} else {
""
};
format!("({e} IS NULL){nulls}, {e}{dir}")
});
let sums: Vec<FloatSum> = {
let from = ex.from_table.clone();
let joined: Option<(String, String)> = ex
.join_table
.clone()
.map(|t| (t.clone(), ex.join_alias.clone().unwrap_or(t)));
let mut order = vec![(from.clone(), from.clone())];
if let (true, Some(j)) = (ex.join_every, joined.as_ref()) {
order.push(j.clone());
}
ex.sums
.iter()
.map(|(mean, column, column_sql)| {
let table = match (column_sql.rsplit_once('.'), joined.as_ref()) {
(Some((qual, _)), Some((t, name))) if qual == name => t.clone(),
_ => from.clone(),
};
FloatSum {
text: format!("{}({column_sql})", if *mean { "AVG" } else { "SUM" }),
mean: *mean,
table,
column: column.clone(),
column_sql: column_sql.clone(),
order: order.clone(),
}
})
.collect()
};
Ok(Pushdown {
sql,
sums,
order_table,
join_left,
order_then_as: order_then.as_ref().and(ex.join_alias.clone()),
order_then,
order_first,
order_desc: ex.order.reversed,
order_groups: ex.order.groups,
limit: ex.order.limit,
record: ex.order.record,
properties: ex.order.properties,
keyed_by: ex.order.keyed_by.clone(),
column_types: ex.column_types.clone(),
tables,
})
}
fn json_extract(dialect: Dialect, qual: Option<&str>, col: &str, path: &[String]) -> String {
let qcol = match qual {
Some(q) => format!("{q}.{col}"),
None => col.to_string(),
};
match dialect {
Dialect::Postgres => format!("({qcol}::jsonb #>> '{{{}}}')", path.join(",")),
Dialect::MySql => {
format!("JSON_UNQUOTE(JSON_EXTRACT({qcol}, '$.{}'))", path.join("."))
}
Dialect::Sqlite => format!("json_extract({qcol}, '$.{}')", path.join(".")),
Dialect::Mssql | Dialect::Oracle => format!("JSON_VALUE({qcol}, '$.{}')", path.join(".")),
}
}
const JSON_EACH_COLUMNS: &[&str] = &[
"key", "value", "type", "atom", "id", "parent", "fullkey", "path", "json", "root",
];
fn json_valid_guard(dialect: Dialect, qual: Option<&str>, col: &str, cond: String) -> String {
let qcol = match qual {
Some(q) => format!("{q}.{col}"),
None => col.to_string(),
};
match dialect {
Dialect::Sqlite => format!("(json_valid({qcol}) AND {cond})"),
Dialect::MySql => format!("(JSON_VALID({qcol}) AND {cond})"),
Dialect::Mssql => format!("(ISJSON({qcol}) = 1 AND {cond})"),
Dialect::Postgres | Dialect::Oracle => format!("({cond})"),
}
}
fn json_type_probe(
dialect: Dialect,
qual: Option<&str>,
col: &str,
path: &[String],
) -> Option<String> {
let qcol = match qual {
Some(q) => format!("{q}.{col}"),
None => col.to_string(),
};
match dialect {
Dialect::Sqlite => Some(format!("json_type({qcol}, '$.{}')", path.join("."))),
Dialect::Postgres => Some(format!(
"jsonb_typeof({qcol}::jsonb #> '{{{}}}')",
path.join(",")
)),
Dialect::MySql | Dialect::Mssql | Dialect::Oracle => None,
}
}
pub(crate) fn required_literals(pattern: &str) -> Vec<String> {
use regex_syntax::hir::{Hir, HirKind};
fn walk(h: &Hir, out: &mut Vec<String>) {
match h.kind() {
HirKind::Literal(lit) => {
if let Ok(s) = std::str::from_utf8(&lit.0)
&& !s.is_empty()
&& !out.iter().any(|o| o == s)
{
out.push(s.to_string());
}
}
HirKind::Capture(c) => walk(&c.sub, out),
HirKind::Concat(parts) => parts.iter().for_each(|p| walk(p, out)),
HirKind::Repetition(r) if r.min >= 1 => walk(&r.sub, out),
_ => {}
}
}
let Ok(hir) = regex_syntax::parse(pattern) else {
return Vec::new();
};
let mut out = Vec::new();
walk(&hir, &mut out);
out
}
fn contains_sql(dialect: Option<Dialect>, l: &str, lit: &str) -> Option<String> {
if lit
.chars()
.any(|c| c.is_control() || c == '\\' || c == '\'')
{
return None;
}
match dialect {
Some(Dialect::Sqlite | Dialect::MySql) => Some(format!("instr({l}, '{lit}') > 0")),
Some(Dialect::Postgres) => {
let pat = lit.replace('%', "\\%").replace('_', "\\_");
Some(format!("{l} LIKE '%{pat}%' ESCAPE '\\'"))
}
None => Some(format!("strpos({l}, '{lit}') > 0")),
Some(Dialect::Mssql | Dialect::Oracle) => None,
}
}
fn flip_op(op: &str) -> String {
match op {
"<" => ">".into(),
"<=" => ">=".into(),
">" => "<".into(),
">=" => "<=".into(),
other => other.into(),
}
}
const SQL_KEYWORDS: &[&str] = &[
"all",
"and",
"as",
"asc",
"by",
"case",
"cross",
"desc",
"distinct",
"else",
"end",
"except",
"exists",
"from",
"group",
"having",
"in",
"index",
"inner",
"intersect",
"into",
"is",
"join",
"left",
"like",
"limit",
"not",
"null",
"offset",
"on",
"or",
"order",
"outer",
"right",
"select",
"set",
"table",
"then",
"union",
"unique",
"update",
"using",
"values",
"when",
"where",
];
fn is_plain_ident(name: &str) -> bool {
!name.is_empty()
&& name
.chars()
.next()
.is_some_and(|c| c.is_ascii_alphabetic() || c == '_')
&& name.chars().all(|c| c.is_ascii_alphanumeric() || c == '_')
&& !SQL_KEYWORDS.contains(&name.to_ascii_lowercase().as_str())
}
#[derive(Default)]
struct Select {
select: Vec<String>,
distinct: bool,
from: String,
join: Option<(String, String)>, join_outer: bool,
wheres: Vec<String>,
group_by: Option<String>,
having: Option<String>,
order_by: Option<(String, bool)>, limit: Option<String>,
}
impl Select {
fn render(&self) -> String {
let mut out = String::from("SELECT ");
if self.distinct {
out.push_str("DISTINCT ");
}
if self.select.is_empty() {
out.push('*');
} else {
out.push_str(&self.select.join(", "));
}
out.push_str(&format!(" FROM {}", self.from));
if let Some((t, on)) = &self.join {
let kind = if self.join_outer { "LEFT JOIN" } else { "JOIN" };
out.push_str(&format!(" {kind} {t} ON {on}"));
}
if !self.wheres.is_empty() {
out.push_str(&format!(" WHERE {}", self.wheres.join(" AND ")));
}
if let Some(g) = &self.group_by {
out.push_str(&format!(" GROUP BY {g}"));
}
if let Some(h) = &self.having {
out.push_str(&format!(" HAVING {h}"));
}
if let Some((e, desc)) = &self.order_by {
out.push_str(&format!(" ORDER BY {e}"));
if *desc {
out.push_str(" DESC");
}
}
if let Some(n) = &self.limit {
out.push_str(&format!(" LIMIT {n}"));
}
out
}
}
pub struct Partial {
pub table: String,
pub where_sql: String,
pub column_types: ColumnTypes,
}
pub fn partial_pushdown(quarb: &str, dialect: Option<Dialect>) -> Option<Partial> {
partial_pushdown_explained(quarb, dialect).ok()
}
pub fn partial_pushdown_explained(
quarb: &str,
dialect: Option<Dialect>,
) -> Result<Partial, SqlError> {
partial_pushdown_with(quarb, dialect, false)
}
pub fn partial_pushdown_keyed(quarb: &str, dialect: Option<Dialect>) -> Result<Partial, SqlError> {
partial_pushdown_with(quarb, dialect, true)
}
fn partial_pushdown_with(
quarb: &str,
dialect: Option<Dialect>,
keyed_hops: bool,
) -> Result<Partial, SqlError> {
refuse_marker(quarb)?;
let arbor =
QueryArbor::parse(quarb).map_err(|e| SqlError::Syntax(format!("parsing Quarb: {e}")))?;
refuse_groups(&arbor)?;
let mut ex = Exporter {
arbor,
notes: Vec::new(),
strict: true,
dialect,
from_table: String::new(),
join_on_cols: Vec::new(),
join_table: None,
join_every: false,
column_types: ColumnTypes::default(),
from_sql: String::new(),
join_sql: None,
in_on: false,
order: PlanOrder::default(),
aggregate: false,
keyed_hops,
from_marks: Vec::new(),
join_marks: Vec::new(),
join_alias: None,
sums: Vec::new(),
};
ex.partial()
}
fn refuse_groups(arbor: &QueryArbor) -> Result<(), SqlError> {
let mut stack = vec![arbor.root()];
while let Some(n) = stack.pop() {
if arbor.name(n).as_deref() == Some("group") {
return Err(not_pushed(
Refusal::Walk,
"path patterns (groups and quantifiers)".into(),
));
}
stack.extend(arbor.children(n));
}
Ok(())
}
#[derive(Default)]
struct PlanOrder {
sort: Option<(String, bool)>,
reversed: bool,
limit: Option<u64>,
groups: bool,
keyed_by: Option<(Refusal, String)>,
record: bool,
properties: bool,
}
struct Exporter {
arbor: QueryArbor,
notes: Vec<String>,
strict: bool,
dialect: Option<Dialect>,
from_table: String,
join_on_cols: Vec<String>,
join_table: Option<String>,
join_every: bool,
column_types: ColumnTypes,
from_sql: String,
join_sql: Option<String>,
in_on: bool,
order: PlanOrder,
aggregate: bool,
keyed_hops: bool,
from_marks: Vec<String>,
join_marks: Vec<String>,
join_alias: Option<String>,
sums: Vec<(bool, String, String)>,
}
impl Exporter {
fn kids(&self, n: NodeId, kind: &str) -> Vec<NodeId> {
self.arbor
.children(n)
.into_iter()
.filter(|&c| self.arbor.name(c).as_deref() == Some(kind))
.collect()
}
fn kid(&self, n: NodeId, kind: &str) -> Option<NodeId> {
self.kids(n, kind).into_iter().next()
}
fn prop(&self, n: NodeId, key: &str) -> Option<Value> {
self.arbor.property(n, key)
}
fn prop_s(&self, n: NodeId, key: &str) -> String {
self.prop(n, key).map(|v| v.to_string()).unwrap_or_default()
}
fn kind(&self, n: NodeId) -> String {
self.arbor.name(n).unwrap_or_default()
}
fn is_null_literal(&self, n: NodeId) -> bool {
self.kind(n) == "literal" && self.prop_s(n, "type") == "null"
}
fn partial(&mut self) -> Result<Partial, SqlError> {
let root = self.arbor.root();
let q = self
.kid(root, "query")
.ok_or_else(|| not_pushed(Refusal::Name, "empty query".into()))?;
let branches = self.kids(q, "branch");
if branches.len() != 1 {
return Err(not_pushed(Refusal::Union, "a branch union".into()));
}
let (table, preds) = self.table_branch_lead(branches[0], self.keyed_hops)?;
let all = self.walk_all(root);
let mut table_mentions = 0;
for n in &all {
match self.kind(*n).as_str() {
"step" => {
let axis = self.prop_s(*n, "axis");
if matches!(axis.as_str(), "->" | "<-" | "--" | "-->" | "<--" | "=>") {
let scope = self.prop_s(*n, "scope");
let lists_other_table = scope
.strip_prefix('/')
.and_then(|rest| rest.split(['/', '[', '<']).next())
.is_some_and(|first| {
!first.is_empty()
&& first != table
&& first.chars().all(|c| c.is_alphanumeric() || c == '_')
});
let safe = self.keyed_hops
&& if scope.is_empty() {
axis != "=>"
} else {
lists_other_table
};
if !safe {
return Err(not_pushed(
Refusal::Condition,
"crosslink/resolution axes could reach \
the filtered table"
.into(),
));
}
}
if self.prop_s(*n, "matcher") == table {
table_mentions += 1;
}
}
"projection" if self.prop_s(*n, "kind") != "property" => {
return Err(not_pushed(
Refusal::Condition,
"metadata would observe the filtering \
(:::n-rows, :index)"
.into(),
));
}
_ => {}
}
}
if table_mentions != 1 {
return Err(not_pushed(
Refusal::Condition,
"the table is reached more than once".into(),
));
}
let mut conds = Vec::new();
for p in preds {
if self.prop_s(p, "kind") != "expr" {
break;
}
match self.predicate_cond(p, None) {
Ok(c) => conds.push(c),
Err(_) => {
if let Some(c) = self.prefilter_cond(p) {
conds.push(c);
}
}
}
}
if conds.is_empty() {
return Err(not_pushed(
Refusal::Nothing,
"no leading expression predicates to push".into(),
));
}
Ok(Partial {
table,
where_sql: conds.join(" AND "),
column_types: self.column_types.clone(),
})
}
fn prefilter_cond(&mut self, p: NodeId) -> Option<String> {
let parts: Vec<String> = self
.arbor
.children(p)
.into_iter()
.filter_map(|c| {
self.pred_expr(c, None)
.ok()
.or_else(|| self.prefilter_expr(c))
})
.collect();
if parts.is_empty() {
None
} else {
Some(parts.join(" AND "))
}
}
fn prefilter_expr(&mut self, e: NodeId) -> Option<String> {
let dialect = self.dialect;
match self.kind(e).as_str() {
"path" => {
let dialect = dialect?;
let (col, path) = self
.json_path_loose(e)
.or_else(|| self.json_property_path(e))?;
self.note_json_column(e, &col);
let probe = json_type_probe(dialect, None, &col, &path)?;
Some(json_valid_guard(
dialect,
None,
&col,
format!("{probe} IS NOT NULL"),
))
}
"and" | "or" => {
let kind = self.kind(e);
let sides: Vec<Option<String>> = self
.arbor
.children(e)
.into_iter()
.map(|c| {
self.pred_expr(c, None)
.ok()
.or_else(|| self.prefilter_expr(c))
})
.collect();
if kind == "or" {
let sides: Option<Vec<String>> = sides.into_iter().collect();
return sides
.filter(|s| !s.is_empty())
.map(|s| format!("({})", s.join(" OR ")));
}
let sides: Vec<String> = sides.into_iter().flatten().collect();
(!sides.is_empty()).then(|| format!("({})", sides.join(" AND ")))
}
"compare" => {
let op = self.prop_s(e, "op");
let kids = self.arbor.children(e);
if kids.len() != 2 {
return None;
}
if op == "==" {
return self.regex_prefilter(dialect, &kids);
}
let dialect = dialect?;
for (pi, li) in [(0usize, 1usize), (1, 0)] {
let Some((col, path)) = self.json_path(kids[pi]) else {
continue;
};
if self.kind(kids[li]) != "literal" {
continue;
}
let ty = self.prop_s(kids[li], "type");
if ty == "null" {
return None;
}
let raw = self
.prop(kids[li], "value")
.map(|v| v.to_string())
.unwrap_or_default();
let op = if pi == 0 { op.clone() } else { flip_op(&op) };
self.note_json_column(kids[pi], &col);
return self.json_prefilter(dialect, &col, &path, &op, &ty, &raw);
}
for (pi, li) in [(0usize, 1usize), (1, 0)] {
let Some((col, path)) = self.json_property_path(kids[pi]) else {
continue;
};
if self.kind(kids[li]) != "literal" {
continue;
}
let ty = self.prop_s(kids[li], "type");
if ty == "null" {
return None;
}
let raw = self
.prop(kids[li], "value")
.map(|v| v.to_string())
.unwrap_or_default();
let op = if pi == 0 { op.clone() } else { flip_op(&op) };
self.note_json_column(kids[pi], &col);
return self.json_property_prefilter(dialect, &col, &path, &op, &ty, &raw);
}
if dialect == Dialect::Mssql && op == "=" {
for (ci, li) in [(0usize, 1usize), (1, 0)] {
let plain = self.column_operand(kids[ci]).is_some()
&& self.is_text_literal(kids[li])
&& self.prop(kids[li], "value").is_some_and(|v| {
Value::Str(v.to_string()).numeric_reading().is_none()
});
if plain {
let l = self.operand(kids[ci], None).ok()?;
let r = self.operand(kids[li], None).ok()?;
return Some(format!("{l} = {r}"));
}
}
}
None
}
_ => None,
}
}
fn regex_prefilter(&mut self, dialect: Option<Dialect>, kids: &[NodeId]) -> Option<String> {
if !self.is_text_literal(kids[1]) {
return None;
}
let column = self.column_operand(kids[0])?;
let pattern = self.prop(kids[1], "value")?.to_string();
let literals = required_literals(&pattern);
let l = self.operand(kids[0], None).ok()?;
let conds: Vec<String> = literals
.iter()
.filter_map(|lit| contains_sql(dialect, &l, lit))
.collect();
if conds.is_empty() {
return None;
}
if !self.column_types.text.contains(&column) {
self.column_types.text.push(column);
}
Some(conds.join(" AND "))
}
fn json_property_prefilter(
&self,
d: Dialect,
col: &str,
path: &[String],
op: &str,
lit_ty: &str,
raw: &str,
) -> Option<String> {
if !matches!(op, "=" | "!=" | "<" | "<=" | ">" | ">=") {
return None;
}
if op == "!=" {
let owner = &path[..path.len() - 1];
if owner.is_empty() {
return None;
}
let probe = json_type_probe(d, None, col, owner)?;
return Some(json_valid_guard(
d,
None,
col,
format!("{probe} IS NOT NULL"),
));
}
let ty = json_type_probe(d, None, col, path)?;
let numeric_lit = raw.trim().parse::<f64>().is_ok_and(|f| f.is_finite())
&& (lit_ty != "text" || !raw.trim().is_empty());
if numeric_lit {
let num: f64 = raw.trim().parse().ok()?;
if num.abs() >= 9_007_199_254_740_992.0 {
return None;
}
let lit = raw.trim();
match d {
Dialect::Sqlite => {
let ex = json_extract(d, None, col, path);
Some(json_valid_guard(
d,
None,
col,
format!("({ex} {op} {lit} OR {ty} IN ('text', 'array'))"),
))
}
Dialect::Postgres => {
let text = json_extract(d, None, col, path);
Some(format!(
"(CASE WHEN {ty} = 'number' THEN ({text})::float8 {op} {lit} \
WHEN {ty} IN ('string', 'array') THEN true ELSE false END)"
))
}
Dialect::MySql | Dialect::Mssql | Dialect::Oracle => None,
}
} else if lit_ty == "text" {
if raw.contains('\'') || raw.contains('\\') {
return None;
}
match d {
Dialect::Sqlite => {
let ex = json_extract(d, None, col, path);
Some(json_valid_guard(
d,
None,
col,
format!("({ex} {op} '{raw}' OR {ty} = 'array')"),
))
}
Dialect::Postgres if op == "=" => {
let text = json_extract(d, None, col, path);
Some(format!(
"(CASE WHEN {ty} = 'string' THEN {text} = '{raw}' \
WHEN {ty} = 'array' THEN true ELSE false END)"
))
}
_ => None,
}
} else {
None
}
}
fn json_prefilter(
&self,
d: Dialect,
col: &str,
path: &[String],
op: &str,
lit_ty: &str,
raw: &str,
) -> Option<String> {
if !matches!(op, "=" | "!=" | "<" | "<=" | ">" | ">=") {
return None;
}
if op == "!=" {
let probe = json_type_probe(d, None, col, path)?;
return Some(json_valid_guard(
d,
None,
col,
format!("{probe} IS NOT NULL"),
));
}
let numeric_lit = raw.trim().parse::<f64>().is_ok_and(|f| f.is_finite())
&& (lit_ty != "text" || !raw.trim().is_empty());
if numeric_lit {
let num: f64 = raw.trim().parse().ok()?;
if num.abs() >= 9_007_199_254_740_992.0 {
return None;
}
let lit = raw.trim();
match d {
Dialect::Sqlite => {
let ex = json_extract(d, None, col, path);
let ty = json_type_probe(d, None, col, path)?;
Some(json_valid_guard(
d,
None,
col,
format!("({ex} {op} {lit} OR {ty} = 'text')"),
))
}
Dialect::Postgres => {
let text = json_extract(d, None, col, path);
let ty = json_type_probe(d, None, col, path)?;
Some(format!(
"(CASE WHEN {ty} = 'number' THEN ({text})::float8 {op} {lit} \
WHEN {ty} = 'string' THEN true ELSE false END)"
))
}
Dialect::MySql | Dialect::Mssql | Dialect::Oracle => None,
}
} else if lit_ty == "text" {
if raw.contains('\'') || raw.contains('\\') {
return None;
}
match d {
Dialect::Sqlite => {
let ex = json_extract(d, None, col, path);
Some(json_valid_guard(d, None, col, format!("{ex} {op} '{raw}'")))
}
_ => None,
}
} else {
None
}
}
fn note_json_column(&mut self, o: NodeId, col: &str) {
let declared_on_hop = self.kids(o, "step").first().is_some_and(|s| {
self.kids(*s, "trait").iter().any(|t| {
matches!(self.prop(*t, "alts"), Some(Value::List(alts))
if alts.iter().any(|a| matches!(a, Value::Str(x) if x == "json")))
})
});
if !declared_on_hop && !self.column_types.json.iter().any(|c| c == col) {
self.column_types.json.push(col.to_string());
}
let keyed = self
.kid(o, "projection")
.is_some_and(|p| self.prop(p, "key").is_some());
if !keyed {
self.column_types.json_children = true;
}
}
fn json_path_loose(&self, o: NodeId) -> Option<(String, Vec<String>)> {
if self.json_path(o).is_some() {
return self.json_path(o);
}
if self.kind(o) != "path" || self.kid(o, "projection").is_some() {
return None;
}
let steps = self.kids(o, "step");
if steps.len() < 2 {
return None;
}
let mut names = Vec::with_capacity(steps.len());
for s in &steps {
if self.prop_s(*s, "axis") != "/"
|| self.prop_s(*s, "matcher-kind") != "name"
|| self.kid(*s, "predicate").is_some()
{
return None;
}
let name = self.prop_s(*s, "matcher");
let plain = !name.is_empty()
&& name
.chars()
.next()
.is_some_and(|c| c.is_ascii_alphabetic() || c == '_')
&& name.chars().all(|c| c.is_ascii_alphanumeric() || c == '_');
if !plain {
return None;
}
names.push(name);
}
let col = names.remove(0);
Some((col, names))
}
fn walk_all(&self, n: NodeId) -> Vec<NodeId> {
let mut out = vec![n];
let mut i = 0;
while i < out.len() {
out.extend(self.arbor.children(out[i]));
i += 1;
}
out
}
fn query(&mut self) -> Result<String, SqlError> {
let root = self.arbor.root();
let q = self
.kid(root, "query")
.ok_or_else(|| not_pushed(Refusal::Name, "empty query".into()))?;
let mut sel = Select::default();
let corrs = self.kids(q, "query");
let branches = self.kids(q, "branch");
if branches.len() != 1 {
return Err(not_pushed(
Refusal::Union,
"a branch union (SQL has UNION, but result shapes differ; export one branch)"
.into(),
));
}
if corrs.len() > 1 {
return Err(not_pushed(
Refusal::Join,
"more than one correlation context".into(),
));
}
if let Some(corr) = corrs.first() {
let (ltable, lpreds) = self.table_branch(branches[0])?;
let (rtable, rpreds) = self.table_branch(self.kids(*corr, "branch")[0])?;
self.from_marks = self.row_marks(branches[0])?;
self.join_marks = self.row_marks(self.kids(*corr, "branch")[0])?;
if self.kid(*corr, "pipeline").is_some() {
return Err(not_pushed(
Refusal::Join,
"a joined expression with a pipeline of its own".into(),
));
}
let lsql = self.sql_ident(<able, "table name")?;
let rtable_sql = self.sql_ident(&rtable, "table name")?;
self.join_alias = (ltable == rtable).then(|| {
if is_plain_ident(&rtable) {
format!("{rtable}_2")
} else {
"joined_2".to_string()
}
});
let rsql = self
.join_alias
.clone()
.unwrap_or_else(|| rtable_sql.clone());
self.from_sql = lsql.clone();
self.join_sql = Some(rsql.clone());
let driver_conds: Vec<NodeId> = lpreds;
let mut on = Vec::new();
let mut wheres = Vec::new();
for p in rpreds {
self.split_join_pred(p, &rsql, &mut on, &mut wheres)?;
}
let outer = matches!(self.prop(*corr, "outer"), Some(Value::Bool(true)));
if outer {
on.append(&mut wheres);
}
sel.join_outer = outer;
if on.is_empty() {
return Err(not_pushed(
Refusal::Join,
"a correlation without a '$$' equality (no JOIN condition)".into(),
));
}
for p in driver_conds {
let cond = self.predicate_cond(p, Some(&lsql.clone()))?;
wheres.push(cond);
}
self.join_every = matches!(self.prop(*corr, "every"), Some(Value::Bool(true)));
if !self.join_every {
self.notes.push(
"JOIN: Quarb's binding is existential (one row per FROM row, \
bound to its first witness); SQL multiplies rows when \
several joined rows match"
.to_string(),
);
}
sel.from = lsql.clone();
self.from_table = ltable;
self.join_table = Some(rtable);
let joined = match &self.join_alias {
Some(alias) => format!("{rtable_sql} {alias}"),
None => rtable_sql.clone(),
};
sel.join = Some((joined, on.join(" AND ")));
sel.wheres = wheres;
let mut projected = None;
if let Some(proj) = self.kid(branches[0], "projection") {
let name = self.projection_col(proj)?;
let col = self.sql_ident(&name, "column name")?;
let col = format!("{lsql}.{col}");
sel.select.push(col.clone());
projected = Some((col, name));
}
if self
.kid(self.kids(*corr, "branch")[0], "projection")
.is_some()
{
return Err(not_pushed(
Refusal::Join,
"a projection on the joined expression (project the witness \
in the pipeline: '$$1::col')"
.into(),
));
}
self.pipeline(q, &mut sel, Some((&lsql, &rsql)), projected)?;
} else {
let (table, preds) = self.table_branch(branches[0])?;
self.from_marks = self.row_marks(branches[0])?;
sel.from = self.sql_ident(&table, "table name")?;
self.from_table = table;
for p in preds {
let cond = self.predicate_cond(p, None)?;
sel.wheres.push(cond);
}
let mut projected = None;
if let Some(proj) = self.kid(branches[0], "projection") {
let name = self.projection_col(proj)?;
let col = self.sql_ident(&name, "column name")?;
sel.select.push(col.clone());
projected = Some((col, name));
}
self.pipeline(q, &mut sel, None, projected)?;
}
if self.strict && sel.select.is_empty() {
return Err(not_pushed(
Refusal::Nodes,
"a selection of row nodes is answered by their locators".into(),
));
}
Ok(sel.render())
}
fn table_branch(&mut self, b: NodeId) -> Result<(String, Vec<NodeId>), SqlError> {
self.table_branch_lead(b, false)
}
fn table_branch_lead(
&mut self,
b: NodeId,
more: bool,
) -> Result<(String, Vec<NodeId>), SqlError> {
let steps = self.kids(b, "step");
if steps.len() < 2 || (steps.len() > 2 && !more) {
return Err(not_pushed(
Refusal::Walk,
"navigation beyond /table/* (SQL sees tables and rows)".into(),
));
}
let (t, rows) = (steps[0], steps[1]);
if self.prop_s(t, "axis") != "/"
|| self.prop_s(t, "matcher-kind") != "name"
|| self.prop_s(rows, "axis") != "/"
|| self.prop_s(rows, "matcher-kind") != "any"
{
return Err(not_pushed(
Refusal::Walk,
"navigation beyond /table/* (SQL sees tables and rows)".into(),
));
}
if self.kid(t, "predicate").is_some() {
return Err(not_pushed(
Refusal::Walk,
"a predicate on the table step".into(),
));
}
Ok((self.prop_s(t, "matcher"), self.kids(rows, "predicate")))
}
fn row_marks(&self, b: NodeId) -> Result<Vec<String>, SqlError> {
let kids = self.arbor.children(b);
let rows = self.kids(b, "step").get(1).copied();
let after = rows
.and_then(|r| kids.iter().position(|k| *k == r))
.unwrap_or(kids.len());
let mut names = Vec::new();
for (i, k) in kids.iter().enumerate() {
if self.kind(*k) != "mark" {
continue;
}
if i < after {
return Err(not_pushed(
Refusal::Walk,
"a mark before the rows (SQL sees tables and rows)".into(),
));
}
if let Some(Value::Str(n)) = self.prop(*k, "name") {
names.push(n);
}
}
Ok(names)
}
fn mark_anchor(&self, o: NodeId) -> (Option<String>, bool) {
let named = match self.prop(o, "mark") {
Some(Value::Str(n)) => Some(n),
_ => None,
};
let any = named.is_some()
|| [
"mark-index",
"mark-top",
"mark-back",
"marks-all",
"marks-name",
"edge-end",
"node-id",
]
.iter()
.any(|k| self.prop(o, k).is_some());
(named, any)
}
fn predicate_cond(&mut self, p: NodeId, qual: Option<&str>) -> Result<String, SqlError> {
if self.prop_s(p, "kind") != "expr" {
return Err(not_pushed(
Refusal::Take,
"a positional predicate on rows (SQL rows are unordered; ORDER BY + LIMIT)".into(),
));
}
let parts: Vec<String> = self
.arbor
.children(p)
.into_iter()
.map(|c| self.pred_expr(c, qual))
.collect::<Result<_, _>>()?;
Ok(parts.join(" AND "))
}
fn join_cond3(&mut self, e: NodeId, qual: Option<&str>) -> Result<String, SqlError> {
let kids = |ex: &mut Self, e: NodeId| -> Result<Vec<String>, SqlError> {
ex.arbor
.children(e)
.into_iter()
.map(|c| ex.join_cond3(c, qual))
.collect()
};
match self.kind(e).as_str() {
kind @ ("and" | "or") => {
let op = kind.to_uppercase();
Ok(format!("({})", kids(self, e)?.join(&format!(" {op} "))))
}
"not" => Ok(format!("NOT ({})", kids(self, e)?.join(" AND "))),
"parens" => Ok(format!("({})", kids(self, e)?.join(" AND "))),
"compare" if self.subtree_has_driver(e) => self.pred_expr(e, qual),
_ => Ok(format!(
"(CASE WHEN {} THEN 1 ELSE 0 END = 1)",
self.pred_expr(e, qual)?
)),
}
}
fn pred_expr(&mut self, e: NodeId, qual: Option<&str>) -> Result<String, SqlError> {
match self.kind(e).as_str() {
"and" | "or" => {
let op = self.kind(e).to_uppercase();
let kids: Vec<String> = self
.arbor
.children(e)
.into_iter()
.map(|c| self.pred_expr(c, qual))
.collect::<Result<_, _>>()?;
Ok(format!("({})", kids.join(&format!(" {op} "))))
}
"not" => {
if self.strict && self.in_on && self.subtree_has_driver(e) {
let inner: Vec<String> = self
.arbor
.children(e)
.into_iter()
.map(|c| self.join_cond3(c, qual))
.collect::<Result<_, _>>()?;
return Ok(format!("NOT ({})", inner.join(" AND ")));
}
let inner: Vec<String> = self
.arbor
.children(e)
.into_iter()
.map(|c| self.pred_expr(c, qual))
.collect::<Result<_, _>>()?;
if self.strict {
return Ok(format!(
"(CASE WHEN {} THEN 0 ELSE 1 END = 1)",
inner.join(" AND ")
));
}
Ok(format!("NOT ({})", inner.join(" AND ")))
}
"parens" => {
let inner: Vec<String> = self
.arbor
.children(e)
.into_iter()
.map(|c| self.pred_expr(c, qual))
.collect::<Result<_, _>>()?;
Ok(format!("({})", inner.join(" AND ")))
}
"compare" => {
let op = self.prop_s(e, "op");
let kids = self.arbor.children(e);
if matches!(op.as_str(), "=" | "!=")
&& (self.is_null_literal(kids[0]) || self.is_null_literal(kids[1]))
{
let other = if self.is_null_literal(kids[0]) {
kids[1]
} else {
kids[0]
};
let col = self.operand(other, qual)?;
return Ok(if op == "=" {
format!("{col} IS NULL")
} else {
format!("{col} IS NOT NULL")
});
}
if op == "="
&& let Some(dialect) = self.dialect
{
for (pi, li) in [(0usize, 1usize), (1, 0)] {
if self.is_text_literal(kids[li])
&& let Some((col, path)) = self.json_path(kids[pi])
{
self.note_json_column(kids[pi], &col);
let raw = self
.prop(kids[li], "value")
.map(|v| v.to_string())
.unwrap_or_default();
if raw.trim().parse::<f64>().is_ok() {
return Err(mismatch(
Refusal::Type,
"a numeric-looking text literal against a \
JSON path compares numerically in Quarb \
(value coercion); no SQL extractor \
matches that"
.into(),
));
}
if raw.starts_with('[') || raw.starts_with('{') {
return Err(mismatch(
Refusal::Type,
"a text literal that opens like a JSON container \
could equal an array's or an object's own text \
in the extractor, which has no value in Quarb"
.into(),
));
}
let lit = self.operand(kids[li], qual)?;
let extract = json_extract(dialect, qual, &col, &path);
return Ok(json_valid_guard(
dialect,
qual,
&col,
format!("{extract} = {lit}"),
));
}
}
for (pi, li) in [(0usize, 1usize), (1, 0)] {
if self.is_text_literal(kids[li])
&& let Some((col, path)) = self.json_property_path(kids[pi])
{
self.note_json_column(kids[pi], &col);
let raw = self
.prop(kids[li], "value")
.map(|v| v.to_string())
.unwrap_or_default();
if raw.trim().parse::<f64>().is_ok() {
return Err(mismatch(
Refusal::Type,
"a numeric-looking text literal against a \
JSON path compares numerically in Quarb \
(value coercion); no SQL extractor \
matches that"
.into(),
));
}
let shadowed = qual.is_none()
&& JSON_EACH_COLUMNS.contains(&col.to_ascii_lowercase().as_str());
if dialect != Dialect::Sqlite || shadowed {
return Err(mismatch(
Refusal::Type,
"a property of a JSON value may be a list (an \
array of scalars equals the literal when any \
item does); only SQLite's condition carries \
that branch whole; this shape rides the \
partial prefilter"
.into(),
));
}
let lit = self.operand(kids[li], qual)?;
let qcol = match qual {
Some(q) => format!("{q}.{col}"),
None => col.clone(),
};
let at = format!("'$.{}'", path.join("."));
let ty = format!("json_type({qcol}, {at})");
let ex = json_extract(dialect, qual, &col, &path);
return Ok(json_valid_guard(
dialect,
qual,
&col,
format!(
"(({ty} = 'text' AND {ex} = {lit}) OR ({ty} = 'array' \
AND EXISTS (SELECT 1 FROM json_each({qcol}, {at}) \
WHERE json_each.type = 'text' AND json_each.value = {lit}) \
AND NOT EXISTS (SELECT 1 FROM json_each({qcol}, {at}) \
WHERE json_each.type IN ('array', 'object'))))"
),
));
}
}
}
if self.dialect.is_some() {
for side in [kids[0], kids[1]] {
if self.json_path(side).is_some()
|| self.json_path_loose(side).is_some()
|| self.json_property_path(side).is_some()
{
return Err(mismatch(
Refusal::Type,
"a JSON-path comparison pushes whole only as \
fixed-path string equality (Quarb coerces \
numeric-looking values; no extractor matches \
that); this shape rides the partial prefilter"
.into(),
));
}
}
}
if self.strict && !matches!(op.as_str(), "==" | "!==") {
let value =
|k: NodeId| matches!(self.kind(k).as_str(), "path" | "context" | "arith");
if value(kids[0]) && value(kids[1]) {
for k in [kids[0], kids[1]] {
let column = self.column_operand(k);
self.assume_numeric(column);
}
self.column_types.columns_compared = true;
}
for (ci, li) in [(0usize, 1usize), (1, 0)] {
if self.is_text_literal(kids[li]) && self.kind(kids[ci]) == "arith" {
return Err(mismatch(
Refusal::Type,
"a text literal compared with a computed value (SQL orders a \
number below every text; to the engine the two never meet)"
.into(),
));
}
}
for (ci, li) in [(0usize, 1usize), (1, 0)] {
let Some(column) = self.column_operand(kids[ci]) else {
continue;
};
if self.kind(kids[li]) != "literal" {
continue;
}
let numeric = self
.prop(kids[li], "value")
.is_some_and(|v| Value::Str(v.to_string()).numeric_reading().is_some());
let list = match (self.prop_s(kids[li], "type").as_str(), numeric) {
("null", _) => continue,
("text", true) => {
return Err(mismatch(
Refusal::Type,
"a numeric-looking text literal compares \
numerically in Quarb (value coercion); SQL \
compares it as text"
.into(),
));
}
("text", false) => &mut self.column_types.text,
(_, true) => &mut self.column_types.numeric,
(_, false) => continue,
};
if !list.contains(&column) {
list.push(column);
}
}
}
let l = self.operand(kids[0], qual)?;
if self.kind(kids[1]) == "pattern" {
if op.as_str() != "=" {
return Err(not_pushed(
Refusal::Pattern,
format!("a pattern literal on '{op}' (only '=' pushes)"),
));
}
if self.strict && self.dialect != Some(Dialect::Postgres) {
return Err(mismatch(
Refusal::Pattern,
"LIKE case folding differs per engine \
(provably case-sensitive only on PostgreSQL)"
.into(),
));
}
if !self.strict {
self.notes.push(
"pattern → LIKE: Quarb's pattern test is \
case-sensitive; LIKE folds case on \
SQLite/MySQL but not PostgreSQL"
.to_string(),
);
}
let column = self.column_operand(kids[0]);
self.assume_text(column);
let mut like = String::new();
for kid in self.arbor.children(kids[1]) {
match self.kind(kid).as_str() {
"star" => like.push('%'),
_ => {
let raw = self
.prop(kid, "value")
.map(|v| v.to_string())
.unwrap_or_default();
like.push_str(
&raw.replace('\\', "\\\\")
.replace('%', "\\%")
.replace('_', "\\_")
.replace('\'', "''"),
);
}
}
}
return Ok(format!("{l} LIKE '{like}' ESCAPE '\\'"));
}
let r = self.operand(kids[1], qual)?;
let nullable: Vec<&str> = [(kids[0], l.as_str()), (kids[1], r.as_str())]
.into_iter()
.filter(|(k, _)| self.kind(*k) != "literal")
.map(|(_, sql)| sql)
.collect();
let keyed = self.in_on
&& (self.subtree_has_driver(kids[0]) || self.subtree_has_driver(kids[1]));
let text_pair = self.strict
&& ((self.is_text_literal(kids[1]) && self.column_operand(kids[0]).is_some())
|| (self.is_text_literal(kids[0])
&& self.column_operand(kids[1]).is_some()));
if text_pair && matches!(op.as_str(), "=" | "!=" | "<" | "<=" | ">" | ">=") {
match self.dialect {
Some(Dialect::Mssql) => {
return Err(mismatch(
Refusal::Type,
"a text comparison follows the database's collation on SQL \
Server (case, accents and trailing spaces), where the engine \
compares the characters"
.into(),
));
}
Some(Dialect::MySql) => {
let bytes = |s: &str| format!("BINARY CONVERT({s} USING utf8mb4)");
let (bl, br) = (bytes(&l), bytes(&r));
return Ok(match op.as_str() {
"=" => format!("({l} = {r} AND {bl} = {br})"),
"!=" if keyed => format!("{bl} <> {br}"),
"!=" => {
let column = nullable.first().copied().unwrap_or(l.as_str());
format!("({bl} <> {br} OR {column} IS NULL)")
}
order => format!("{bl} {order} {br}"),
});
}
Some(Dialect::Postgres) if !matches!(op.as_str(), "=" | "!=") => {
return Ok(format!("{l} {op} {r} COLLATE \"C\""));
}
_ => {}
}
}
Ok(match op.as_str() {
"=" if self.strict && !keyed && nullable.len() == 2 => {
format!("({l} = {r} OR ({l} IS NULL AND {r} IS NULL))")
}
"=" => format!("{l} = {r}"),
"!=" if self.strict && !keyed => match nullable.as_slice() {
[] => format!("{l} <> {r}"),
[one] => format!("({l} <> {r} OR {one} IS NULL)"),
_ => format!(
"({l} <> {r} OR ({l} IS NULL AND {r} IS NOT NULL) OR ({l} IS NOT \
NULL AND {r} IS NULL))"
),
},
"!=" if self.strict => format!("{l} <> {r}"),
"!=" => {
self.notes.push(
"'!=' → '<>': Quarb keeps rows whose column is NULL; \
SQL's '<>' drops them (use '!= null' for IS NOT NULL)"
.to_string(),
);
format!("{l} <> {r}")
}
"<" | "<=" | ">" | ">=" => format!("{l} {op} {r}"),
"*=" if self.strict
&& self.dialect == Some(Dialect::Sqlite)
&& self.is_text_literal(kids[1])
&& self.column_operand(kids[0]).is_some() =>
{
format!("instr({l}, {r}) > 0")
}
"*=" => {
if self.strict && self.dialect != Some(Dialect::Postgres) {
return Err(mismatch(
Refusal::Pattern,
"LIKE case folding differs per engine \
(a substring test is pushed on SQLite as instr and on \
PostgreSQL as LIKE)"
.into(),
));
}
if !self.is_text_literal(kids[1]) {
return Err(not_pushed(
Refusal::Pattern,
"'*=' with a non-literal pattern".into(),
));
}
self.notes.push(
"*= → LIKE: Quarb's substring test is case-sensitive; LIKE \
folds case on SQLite/MySQL but not PostgreSQL"
.to_string(),
);
let raw = self
.prop(kids[1], "value")
.map(|v| v.to_string())
.unwrap_or_default();
let pat = raw
.replace('\\', "\\\\")
.replace('%', "\\%")
.replace('_', "\\_")
.replace('\'', "''");
format!("{l} LIKE '%{pat}%' ESCAPE '\\'")
}
"==" | "!==" | "=~" | "!~" => {
return Err(mismatch(
Refusal::Pattern,
"regex matching means a different REGEXP engine per \
backend; regexes run engine-side"
.into(),
));
}
other => {
return Err(not_pushed(
Refusal::Operator,
format!("the '{other}' comparison"),
));
}
})
}
_ => {
if self.strict {
return Err(mismatch(
Refusal::Type,
"truthiness diverges (0 and '' are falsy in Quarb)".into(),
));
}
self.notes.push(
"truthiness: '[::c]' exports as IS NOT NULL, but Quarb also treats \
0 and '' as falsy"
.to_string(),
);
Ok(format!("{} IS NOT NULL", self.operand(e, qual)?))
}
}
}
fn operand(&mut self, o: NodeId, qual: Option<&str>) -> Result<String, SqlError> {
match self.kind(o).as_str() {
"literal" => {
let v = self.prop(o, "value").unwrap_or(Value::Null);
match self.prop_s(o, "type").as_str() {
"text" => {
let s = v.to_string();
if self.strict && (s.contains('\'') || s.contains('\\')) {
return Err(mismatch(
Refusal::Name,
"a text literal with a quote or backslash \
has no escaping portable across SQL dialects"
.into(),
));
}
Ok(format!("'{}'", s.replace('\'', "''")))
}
"null" => Ok("NULL".to_string()),
_ => Ok(v.to_string()),
}
}
"path" => {
if self.kid(o, "step").is_some() {
let s = self.kids(o, "step")[0];
if self.prop_s(s, "axis") == "-->" {
return Err(not_pushed(
Refusal::Resolution,
"a '-->' resolution chain: the foreign-key targets live in \
the schema, not the query — spell the join with '<=>' to \
export it"
.into(),
));
}
return Err(not_pushed(
Refusal::Resolution,
"navigation inside a predicate (SQL rows are flat)".into(),
));
}
let p = self
.kid(o, "projection")
.ok_or_else(|| not_pushed(Refusal::Operator, "an empty path operand".into()))?;
let col = self.projection_col(p)?;
let col = self.sql_ident(&col, "column name")?;
let (named, any) = self.mark_anchor(o);
if any {
let Some(name) = named else {
return Err(not_pushed(
Refusal::State,
"a mark read by position, or of several marks".into(),
));
};
if self.join_marks.contains(&name) {
if self.in_on {
return Err(not_pushed(
Refusal::Join,
"the joined expression's own mark inside its ON".into(),
));
}
let Some(join) = self.join_sql.clone() else {
return Err(not_pushed(
Refusal::Join,
"a join mark outside a correlation join".into(),
));
};
return Ok(format!("{join}.{col}"));
}
if self.from_marks.contains(&name) {
return Ok(if self.join_sql.is_some() {
format!("{}.{col}", self.from_sql.clone())
} else {
match qual {
Some(q) => format!("{q}.{col}"),
None => col,
}
});
}
return Err(not_pushed(
Refusal::State,
format!("a read of the mark '.{name}' (not a row of the query's tables)"),
));
}
Ok(match qual {
Some(q) => format!("{q}.{col}"),
None => col,
})
}
"context" => {
let p = self
.kid(o, "projection")
.ok_or_else(|| not_pushed(Refusal::Join, "a bare '$$N' reference".into()))?;
let col = self.projection_col(p)?;
let col = self.sql_ident(&col, "column name")?;
if matches!(self.prop(o, "index"), Some(Value::Int(0))) {
if !self.in_on {
return Err(not_pushed(
Refusal::Join,
"a '$$0' reference outside the join's ON".into(),
));
}
if self.kid(o, "step").is_some() {
return Err(not_pushed(
Refusal::Join,
"a '$$0' reference beyond a plain column ($$0::col)".into(),
));
}
return Ok(format!("{}.{col}", self.from_sql.clone()));
}
if self.in_on {
return Err(not_pushed(
Refusal::Join,
"a '$$N' reference inside the ON (the driver is '$$0')".into(),
));
}
let Some(join) = self.join_sql.clone() else {
return Err(not_pushed(
Refusal::Join,
"a '$$N' reference outside a correlation join".into(),
));
};
match self.prop(o, "index") {
Some(Value::Int(1)) => Ok(format!("{join}.{col}")),
None => Err(not_pushed(Refusal::Join, "a bare '$$N' reference".into())),
Some(v) => Err(not_pushed(
Refusal::Join,
format!("$${v} beyond a two-branch correlation"),
)),
}
}
"invoking" => {
Err(not_pushed(
Refusal::Join,
"a '$$__' reference (the driver inside the ON is '$$0')".into(),
))
}
"arith" => {
let op = self.prop_s(o, "op");
let kids = self.arbor.children(o);
if self.strict {
for &k in &kids {
if self.is_text_literal(k) {
return Err(mismatch(
Refusal::Type,
"arithmetic with a text literal (the engine reads the number \
it spells; SQL's reading differs per engine)"
.into(),
));
}
let column = self.column_operand(k);
if let Some(c) = &column
&& self.strict
&& !self.column_types.computed.contains(c)
{
self.column_types.computed.push(c.clone());
}
self.assume_numeric(column);
}
}
if self.strict && op == "div" {
let nonzero = self.kind(kids[1]) == "literal"
&& self
.prop(kids[1], "value")
.and_then(|v| Value::Str(v.to_string()).numeric_reading())
.is_some_and(|n| {
!matches!(n, Value::Int(0))
&& !matches!(n, Value::Float(f) if f == 0.0)
});
if !nonzero {
return Err(mismatch(
Refusal::Operator,
"'div' by anything but a number other than zero (for a zero \
divisor the engine answers inf or nan, SQL no value or an error)"
.into(),
));
}
let l = self.operand(kids[0], qual)?;
let r = self.operand(kids[1], qual)?;
let float = match self.dialect {
Some(Dialect::Sqlite) => "REAL",
Some(Dialect::Postgres) => "DOUBLE PRECISION",
Some(Dialect::Mssql) => "FLOAT",
Some(Dialect::Oracle) => "BINARY_DOUBLE",
Some(Dialect::MySql) | None => "DOUBLE",
};
return Ok(format!("(CAST({l} AS {float}) / {r})"));
}
if self.strict && op == "mod" {
return Err(mismatch(
Refusal::Operator,
"'mod' (SQL's % reads its operands as integers, and answers no value \
for a zero divisor)"
.into(),
));
}
let l = self.operand(kids[0], qual)?;
let r = self.operand(kids[1], qual)?;
Ok(match op.as_str() {
"+" | "-" | "*" => format!("({l} {op} {r})"),
"div" => format!("({l} / {r})"),
"mod" => format!("({l} % {r})"),
other => {
return Err(not_pushed(
Refusal::Operator,
format!("'{other}' arithmetic"),
));
}
})
}
other => Err(not_pushed(
Refusal::State,
format!(
"the '{other}' operand (registers, topics, and captures are Quarb-side state)"
),
)),
}
}
fn projection_col(&mut self, p: NodeId) -> Result<String, SqlError> {
match self.prop_s(p, "kind").as_str() {
"property" => match self.prop(p, "key") {
Some(k) => Ok(k.to_string()),
None => Err(not_pushed(
Refusal::Operator,
"the bare '::' projection (name the column)".into(),
)),
},
other => Err(not_pushed(
Refusal::Operator,
format!("the {other} metadata projection"),
)),
}
}
fn fold_column(
&mut self,
reducer: &str,
name: &mut Option<String>,
col: &mut Option<String>,
) -> Option<String> {
let column = name.clone();
if let Some(c) = &column
&& self.strict
&& !self.column_types.reduced.contains(c)
{
self.column_types.reduced.push(c.clone());
}
self.assume_numeric(name.take());
let sql = col.take();
if let (true, Some(column), Some(sql)) =
(matches!(reducer, "sum" | "mean"), column, sql.as_ref())
{
self.sums.push((reducer == "mean", column, sql.clone()));
}
sql
}
fn assume_numeric(&mut self, column: Option<String>) {
if !self.strict {
return;
}
if let Some(c) = column
&& !self.column_types.numeric.contains(&c)
{
self.column_types.numeric.push(c);
}
}
fn assume_text(&mut self, column: Option<String>) {
if !self.strict {
return;
}
if let Some(c) = column
&& !self.column_types.text.contains(&c)
{
self.column_types.text.push(c);
}
}
fn column_operand(&self, o: NodeId) -> Option<String> {
match self.kind(o).as_str() {
"path" if self.kid(o, "step").is_none() => {}
"context" => {}
_ => return None,
}
let p = self.kid(o, "projection")?;
if self.prop_s(p, "kind") != "property" {
return None;
}
self.prop(p, "key").map(|k| k.to_string())
}
fn is_text_literal(&self, o: NodeId) -> bool {
self.kind(o) == "literal" && self.prop_s(o, "type") == "text"
}
fn json_path(&self, o: NodeId) -> Option<(String, Vec<String>)> {
if self.kind(o) != "path" {
return None;
}
let steps = self.kids(o, "step");
if steps.len() < 2 {
return None;
}
let proj = self.kid(o, "projection")?;
if self.prop_s(proj, "kind") != "property" || self.prop(proj, "key").is_some() {
return None;
}
let mut names = Vec::with_capacity(steps.len());
for s in &steps {
if self.prop_s(*s, "axis") != "/"
|| self.prop_s(*s, "matcher-kind") != "name"
|| self.kid(*s, "predicate").is_some()
{
return None;
}
let name = self.prop_s(*s, "matcher");
let plain = !name.is_empty()
&& name
.chars()
.next()
.is_some_and(|c| c.is_ascii_alphabetic() || c == '_')
&& name.chars().all(|c| c.is_ascii_alphanumeric() || c == '_');
if !plain {
return None;
}
names.push(name);
}
let col = names.remove(0);
Some((col, names))
}
fn json_property_path(&self, o: NodeId) -> Option<(String, Vec<String>)> {
if self.kind(o) != "path" {
return None;
}
let steps = self.kids(o, "step");
if steps.is_empty() {
return None;
}
let proj = self.kid(o, "projection")?;
if self.prop_s(proj, "kind") != "property" {
return None;
}
let key = self.prop(proj, "key")?.to_string();
let plain = |name: &str| {
!name.is_empty()
&& name
.chars()
.next()
.is_some_and(|c| c.is_ascii_alphabetic() || c == '_')
&& name.chars().all(|c| c.is_ascii_alphanumeric() || c == '_')
};
if !plain(&key) {
return None;
}
let mut names = Vec::with_capacity(steps.len() + 1);
for s in &steps {
if self.prop_s(*s, "axis") != "/"
|| self.prop_s(*s, "matcher-kind") != "name"
|| self.kid(*s, "predicate").is_some()
{
return None;
}
let name = self.prop_s(*s, "matcher");
if !plain(&name) {
return None;
}
names.push(name);
}
names.push(key);
let col = names.remove(0);
Some((col, names))
}
fn alias(&mut self, name: &str) -> Result<String, SqlError> {
if is_plain_ident(name) {
return Ok(name.to_string());
}
if self.strict {
return Err(not_pushed(
Refusal::Name,
format!(
"field name {name:?} needs SQL quoting \
(dialects disagree); rename the field"
),
));
}
self.notes
.push(format!("field name {name:?} double-quoted (ANSI)"));
Ok(format!("\"{}\"", name.replace('"', "\"\"")))
}
fn sql_ident(&mut self, name: &str, what: &str) -> Result<String, SqlError> {
if is_plain_ident(name) {
return Ok(name.to_string());
}
if self.strict {
return Err(not_pushed(
Refusal::Name,
format!("{what} {name:?} is not a plain SQL identifier"),
));
}
self.notes
.push(format!("{what} {name:?} double-quoted (ANSI)"));
Ok(format!("\"{}\"", name.replace('"', "\"\"")))
}
fn split_join_pred(
&mut self,
p: NodeId,
rtable: &str,
on: &mut Vec<String>,
wheres: &mut Vec<String>,
) -> Result<(), SqlError> {
if self.prop_s(p, "kind") != "expr" {
return Err(not_pushed(
Refusal::Take,
"a positional predicate on the joined side".into(),
));
}
fn conjuncts(ex: &Exporter, e: NodeId, out: &mut Vec<NodeId>) {
if ex.kind(e) == "and" {
for c in ex.arbor.children(e) {
conjuncts(ex, c, out);
}
} else {
out.push(e);
}
}
let mut parts = Vec::new();
for c in self.arbor.children(p) {
conjuncts(self, c, &mut parts);
}
for e in parts {
let uses_driver = self.subtree_has_driver(e);
self.in_on = true;
let cond = self.pred_expr(e, Some(rtable));
self.in_on = false;
let cond = cond?;
if uses_driver && self.kind(e) == "compare" && self.prop_s(e, "op") == "=" {
self.collect_join_cols(e)?;
on.push(cond);
} else {
wheres.push(cond);
}
}
Ok(())
}
fn collect_join_cols(&mut self, e: NodeId) -> Result<(), SqlError> {
if self.is_driver(e) {
return Ok(());
}
if self.kind(e) == "path"
&& self.kid(e, "step").is_none()
&& let Some(p) = self.kid(e, "projection")
{
let col = self.projection_col(p)?;
self.join_on_cols.push(col);
}
for c in self.arbor.children(e) {
self.collect_join_cols(c)?;
}
Ok(())
}
fn is_driver(&self, n: NodeId) -> bool {
(self.kind(n) == "context" && matches!(self.prop(n, "index"), Some(Value::Int(0))))
|| (self.kind(n) == "path"
&& matches!(self.mark_anchor(n).0, Some(name) if self.from_marks.contains(&name)))
}
fn subtree_has_driver(&self, n: NodeId) -> bool {
self.is_driver(n)
|| self
.arbor
.children(n)
.into_iter()
.any(|c| self.subtree_has_driver(c))
}
fn pipeline(
&mut self,
q: NodeId,
sel: &mut Select,
join: Option<(&str, &str)>,
projected: Option<(String, String)>,
) -> Result<(), SqlError> {
let Some(pipe) = self.kid(q, "pipeline") else {
return Ok(());
};
let commands: Vec<NodeId> = self.arbor.children(pipe);
let mut i = 0;
let (mut pending_col, mut pending_name) = match projected {
Some((col, name)) => (Some(col), Some(name)),
None => (None, None),
};
let mut grouped: Option<(String, String, String)> = None;
let mut pushed_key: Option<(String, String)> = None;
let mut pushed_key_column: Option<String> = None;
let mut grouped_reg: Option<String> = None;
while i < commands.len() {
let s = commands[i];
match self.kind(s).as_str() {
"expr" => {
let kids = self.arbor.children(s);
pending_col = Some(self.operand(kids[0], join.map(|(l, _)| l))?);
pending_name = self.column_operand(kids[0]);
}
"cmd" => {
let name = self.prop_s(s, "name");
match name.as_str() {
"rec" | "record" => {
sel.select = self.record_fields(s, join)?;
self.order.record = true;
}
"%" if self.strict
&& join.is_none()
&& sel.select.is_empty()
&& pending_col.is_none()
&& grouped.is_none()
&& pushed_key.is_none()
&& !self.aggregate
&& self.arbor.children(s).is_empty()
&& i + 1 == commands.len() =>
{
sel.select = vec!["*".to_string()];
self.order.record = true;
self.order.properties = true;
}
"count" | "sum" | "mean" | "min" | "max" => {
let (key, _, _) = grouped.as_ref().ok_or_else(|| {
not_pushed(
Refusal::Reducer,
format!("'| {name}' outside a group (use '@| {name}')"),
)
})?;
let values =
self.reducer_counts_values(s, &name, pending_col.is_some())?;
let col = if name == "count" {
pending_col.take().filter(|_| values)
} else {
self.fold_column(&name, &mut pending_name, &mut pending_col)
};
let agg = sql_agg(&name, col)?;
grouped = Some((key.clone(), agg.clone(), agg));
}
other => {
if matches!(other, "sort" | "sort_by" | "unique" | "top" | "bottom") {
return Err(mismatch(
if other == "unique" {
Refusal::Unique
} else {
Refusal::Sort
},
format!(
"'{other}' means the backend's collation and \
duplicate semantics; one engine-side ordering \
keeps it identical everywhere"
),
));
}
return Err(not_pushed(
Refusal::Operator,
format!("the '{other}' pipeline command"),
));
}
}
}
"push" => {
if let Some((k, agg, _)) = grouped.take() {
let alias = self.prop_s(s, "name");
grouped = Some((k, agg, alias));
} else {
return Err(not_pushed(
Refusal::State,
"a register push (Quarb-side state)".into(),
));
}
}
"expr-push" if grouped.is_none() && pushed_key.is_none() => {
let kids = self.arbor.children(s);
let key = self.operand(kids[0], join.map(|(l, _)| l))?;
pushed_key = Some((self.prop_s(s, "name"), key));
pushed_key_column = self.column_operand(kids[0]);
}
"subcontext" if grouped.is_none() && pushed_key.is_none() => {
let key = self.pushed_column(s, join.map(|(l, _)| l))?;
pushed_key_column = join.is_none().then(|| key.clone());
pushed_key = Some((self.prop_s(s, "name"), key));
}
"filter" => {
if grouped.is_some() {
let cond = self.having_cond(s)?;
sel.having = Some(cond);
} else {
return Err(not_pushed(
Refusal::State,
"a mid-pipeline filter (put it in the row predicate)".into(),
));
}
}
"recall" => {
if self.prop_s(s, "ref") != "%." {
return Err(not_pushed(
Refusal::State,
"a register recall (Quarb-side state)".into(),
));
}
let (key, agg, alias) = grouped.clone().ok_or_else(|| {
not_pushed(Refusal::Fold, "'%.', with nothing grouped".into())
})?;
let alias = self.alias(&alias)?;
let key_field = match &grouped_reg {
Some(reg) if *reg != key => format!("{key} AS {}", self.alias(reg)?),
_ => key.clone(),
};
sel.select = vec![key_field, format!("{agg} AS {alias}")];
self.order.record = true;
if let Some(h) = sel.having.take() {
sel.having = Some(h.replace("__AGG__", &agg));
}
}
"agg" if self.prop(s, "group").is_some() => {
if self.strict && self.order.limit.is_some() {
return Err(not_pushed(
Refusal::Take,
format!(
"the '{}' command after a positional selection (a plan \
takes its first rows last)",
self.prop_s(s, "name")
),
));
}
let name = self.prop_s(s, "name");
if !matches!(name.as_str(), "count" | "sum" | "mean" | "min" | "max") {
return Err(not_pushed(
Refusal::Fold,
format!("the '{name}' command per group (GROUP BY reduces)"),
));
}
let selector = self.prop_s(s, "group");
if selector.contains(';') {
return Err(not_pushed(
Refusal::Fold,
"a fold by several regulae (one GROUP BY key translates)".into(),
));
}
let (reg, key) = pushed_key.take().ok_or_else(|| {
not_pushed(
Refusal::Fold,
"a fold by a regula no push of a key made".into(),
)
})?;
grouped_reg = Some(reg.clone());
if selector != "." && selector != format!(".{reg}") {
return Err(not_pushed(
Refusal::Fold,
format!("a fold by '{selector}' (the pushed key is '.{reg}')"),
));
}
if self.strict {
let plain = join.is_none()
&& self.order.sort.is_none()
&& self.order.limit.is_none()
&& !self.order.reversed;
match pushed_key_column.take() {
Some(column) if plain => {
self.assume_numeric(Some(column.clone()));
self.order.groups = true;
self.order.keyed_by = Some((Refusal::Fold, column));
}
_ => {
return Err(mismatch(
Refusal::Fold,
"a fold over a join, or after a sort or a selection, or by \
anything but one column"
.into(),
));
}
}
}
sel.group_by = Some(key.clone());
let values = self.reducer_counts_values(s, &name, pending_col.is_some())?;
let col = if name == "count" {
pending_col.take().filter(|_| values)
} else {
self.fold_column(&name, &mut pending_name, &mut pending_col)
};
let agg = sql_agg(&name, col)?;
grouped = Some((key, agg.clone(), agg));
}
"agg" => {
if self.strict && self.order.limit.is_some() {
return Err(not_pushed(
Refusal::Take,
format!(
"the '{}' command after a positional selection (a plan \
takes its first rows last)",
self.prop_s(s, "name")
),
));
}
let name = self.prop_s(s, "name");
match name.as_str() {
"count" | "sum" | "mean" | "min" | "max" => {
let values =
self.reducer_counts_values(s, &name, pending_col.is_some())?;
let col = if name == "count" {
pending_col.take().filter(|_| values)
} else {
self.fold_column(&name, &mut pending_name, &mut pending_col)
};
sel.select = vec![sql_agg(&name, col)?];
self.aggregate = true;
}
"sort_by" if self.strict => {
let kids = self.arbor.children(s);
let column = kids.first().and_then(|&k| self.column_operand(k));
let plain = join.is_none()
&& self.order.sort.is_none()
&& self.order.limit.is_none()
&& !self.order.reversed
&& !self.order.groups
&& !self.aggregate
&& grouped.is_none();
let column = match (column, plain) {
(Some(column), true) => column,
(Some(_), false) => {
return Err(not_pushed(
Refusal::Sort,
"a sort over a join, or after a selection, a fold or \
another sort (a plan has one order)"
.into(),
));
}
(None, _) => {
return Err(mismatch(
Refusal::Sort,
"ORDER BY collations differ per engine".into(),
));
}
};
let mut desc = false;
for &k in &kids[1..] {
let named =
(self.kind(k) == "option").then(|| self.prop_s(k, "name"));
match named.as_deref() {
Some("--desc" | "-d") => desc = true,
_ => {
return Err(mismatch(
Refusal::Sort,
"ORDER BY collations differ per engine".into(),
));
}
}
}
let e = self.operand(kids[0], None)?;
self.assume_numeric(Some(column.clone()));
self.order.sort = Some((e, desc));
self.order.keyed_by = Some((Refusal::Sort, column));
}
"sort_by" => {
if sel.limit.is_some() {
return Err(not_pushed(
Refusal::Sort,
"a sort after a positional selection (SQL orders \
before LIMIT)"
.into(),
));
}
if sel.order_by.is_some() {
return Err(not_pushed(
Refusal::Sort,
"a second sort (SQL has a single ORDER BY)".into(),
));
}
let kids = self.arbor.children(s);
let mut desc = false;
for &k in &kids[1..] {
if self.kind(k) != "option" {
return Err(not_pushed(
Refusal::Sort,
"a sort by several keys".into(),
));
}
match self.prop_s(k, "name").as_str() {
"--desc" | "-d" => desc = true,
"--locale" => {
return Err(mismatch(
Refusal::Sort,
"sort_by(--locale=…) names the engine's collation; \
a backend's differs"
.into(),
));
}
"--upper-first" | "-u" | "--lower-first" | "-l"
| "--ignore-case" | "-i" => {
return Err(mismatch(
Refusal::Sort,
"sort_by's case order is the engine's collation's; \
a backend's differs"
.into(),
));
}
other => {
return Err(not_pushed(
Refusal::Sort,
format!("the '{other}' option of sort_by"),
));
}
}
}
let e = self.operand(kids[0], join.map(|(l, _)| l))?;
sel.order_by = Some((e, desc));
}
"reverse" if self.strict => {
if self.order.reversed
|| self.order.limit.is_some()
|| self.order.groups
|| self.aggregate
|| grouped.is_some()
{
return Err(not_pushed(
Refusal::Sort,
"reverse after a positional selection, a fold or another \
reverse"
.into(),
));
}
self.order.reversed = true;
}
"reverse" => match &mut sel.order_by {
Some((_, desc)) => *desc = true,
None => {
return Err(not_pushed(
Refusal::Sort,
"reverse without an ORDER BY (rows are unordered)".into(),
));
}
},
"top" | "bottom" if self.strict => {
let kids = self.arbor.children(s);
let count = kids.first().and_then(|&k| {
(self.kind(k) == "literal")
.then(|| self.prop(k, "value"))
.flatten()
});
let column = kids.get(1).and_then(|&k| self.column_operand(k));
let plain = kids.len() == 2
&& join.is_none()
&& self.order.sort.is_none()
&& self.order.limit.is_none()
&& !self.order.reversed
&& !self.order.groups
&& !self.aggregate
&& grouped.is_none();
let (Some(Value::Int(k)), Some(column), true) = (count, column, plain)
else {
return Err(not_pushed(
Refusal::Sort,
"'top' and 'bottom' by anything but one column of the \
table, under an option, over a join, or after a sort, a \
selection or a fold (a plan has one order)"
.into(),
));
};
if k < 1 {
return Err(not_pushed(
Refusal::Sort,
"'top' and 'bottom' of no rows".into(),
));
}
let e = self.operand(kids[1], None)?;
sel.wheres.push(format!("{e} IS NOT NULL"));
self.assume_numeric(Some(column.clone()));
self.order.sort = Some((e, name == "top"));
self.order.limit = Some(k as u64);
self.order.keyed_by = Some((Refusal::Sort, column));
}
"top" => {
if sel.limit.is_some() {
return Err(not_pushed(
Refusal::Sort,
"'top' after a positional selection (SQL orders \
before LIMIT)"
.into(),
));
}
if sel.order_by.is_some() {
return Err(not_pushed(
Refusal::Sort,
"a second sort (SQL has a single ORDER BY)".into(),
));
}
let kids = self.arbor.children(s);
if let Some(&k) = kids
.get(2..)
.and_then(|rest| rest.iter().find(|&&k| self.kind(k) == "option"))
{
return Err(mismatch(
Refusal::Sort,
format!(
"top({}) names the engine's collation; a backend's differs",
self.prop_s(k, "name")
),
));
}
let n = self.prop_s(kids[0], "value");
let e = self.operand(kids[1], join.map(|(l, _)| l))?;
sel.order_by = Some((e, true));
sel.limit = Some(n);
}
"unique" => {
if self.strict {
let counted = self.arbor.children(s).is_empty()
&& i + 2 == commands.len()
&& commands.get(i + 1).is_some_and(|&c| {
self.kind(c) == "agg"
&& self.prop_s(c, "name") == "count"
&& self.prop(c, "group").is_none()
&& self.arbor.children(c).is_empty()
});
match (counted, pending_col.take(), pending_name.take()) {
(true, Some(col), Some(name)) => {
self.assume_numeric(Some(name.clone()));
self.order.keyed_by = Some((Refusal::Unique, name));
sel.select = vec![format!(
"COUNT(DISTINCT {col}) + COALESCE(MAX(CASE WHEN \
{col} IS NULL THEN 1 ELSE 0 END), 0)"
)];
self.aggregate = true;
i += 2;
continue;
}
_ => {
return Err(not_pushed(
Refusal::Unique,
"DISTINCT returns rows in no stated order (a count \
of the distinct values of a numeric column is \
pushed: '| ::col @| unique @| count')"
.into(),
));
}
}
}
if sel.limit.is_some() {
return Err(not_pushed(
Refusal::Unique,
"'unique' after a positional selection (SQL applies \
DISTINCT before LIMIT)"
.into(),
));
}
if let Some(c) = pending_col.take() {
sel.select = vec![c];
}
sel.distinct = true;
}
other => {
return Err(not_pushed(
Refusal::Reducer,
format!("the '{other}' aggregate"),
));
}
}
}
"select" if self.strict => {
if self.order.limit.is_some()
|| self.order.groups
|| self.aggregate
|| grouped.is_some()
|| pushed_key.is_some()
{
return Err(not_pushed(
Refusal::Take,
"a positional selection after a reducer, a fold or another \
selection"
.into(),
));
}
let p = self.arbor.children(s)[0];
match (self.prop_s(p, "kind").as_str(), self.prop(p, "to")) {
("range", Some(Value::Int(n)))
if self.prop(p, "from").is_none() && n > 0 =>
{
self.order.limit = Some(n as u64);
}
_ => {
return Err(not_pushed(
Refusal::Take,
"positional selection beyond '@| [..n]'".into(),
));
}
}
}
"select" => {
if sel.limit.is_some() {
return Err(not_pushed(
Refusal::Take,
"a second positional selection".into(),
));
}
let p = self.arbor.children(s)[0];
match (self.prop_s(p, "kind").as_str(), self.prop(p, "to")) {
("range", Some(Value::Int(n)))
if self.prop(p, "from").is_none() && n > 0 =>
{
sel.limit = Some(n.to_string());
}
_ => {
return Err(not_pushed(
Refusal::Take,
"positional selection beyond '@| [..n]'".into(),
));
}
}
}
other => {
return Err(not_pushed(
Refusal::Operator,
format!(
"the '{other}' command (windows, subcontexts, and registers are \
Quarb-side state)"
),
));
}
}
i += 1;
}
if let Some(c) = pending_col
&& sel.select.is_empty()
{
sel.select = vec![c];
}
if self.strict
&& sel.select.is_empty()
&& (self.order.sort.is_some() || self.order.reversed || self.order.limit.is_some())
{
let word = if self.order.limit.is_some() {
Refusal::Take
} else {
Refusal::Sort
};
return Err(not_pushed(
word,
"a sorted, reversed or cut selection of row nodes (project a column or build \
a record after it: '| ::col', '| %(…)')"
.into(),
));
}
if let Some((_, agg, _)) = grouped
&& sel.select.is_empty()
{
sel.select = vec![agg];
}
Ok(())
}
fn pushed_column(&mut self, s: NodeId, qual: Option<&str>) -> Result<String, SqlError> {
let body = || {
let q = self.kid(s, "query")?;
let b = self.kid(q, "branch")?;
let p = self.kid(b, "projection")?;
let bare = self.kids(q, "branch").len() == 1
&& self.kids(q, "pipeline").is_empty()
&& self.arbor.children(b).len() == 1
&& self.prop_s(p, "kind") == "property";
bare.then_some(p)
};
let p = body()
.ok_or_else(|| not_pushed(Refusal::State, "a pushed body (Quarb-side state)".into()))?;
let col = self.projection_col(p)?;
let col = self.sql_ident(&col, "column name")?;
Ok(match qual {
Some(q) => format!("{q}.{col}"),
None => col,
})
}
fn having_cond(&mut self, s: NodeId) -> Result<String, SqlError> {
let kids = self.arbor.children(s);
if kids.len() != 1 || self.kind(kids[0]) != "compare" {
return Err(not_pushed(
Refusal::Fold,
"HAVING translates for a single comparison".into(),
));
}
let e = kids[0];
let op = self.prop_s(e, "op");
let cmp_kids = self.arbor.children(e);
let l = match self.kind(cmp_kids[0]).as_str() {
"topic" | "recall" => "__AGG__".to_string(),
_ => {
return Err(not_pushed(
Refusal::Fold,
"HAVING compares the aggregate ($_ or its register)".into(),
));
}
};
let r = self.operand(cmp_kids[1], None)?;
Ok(format!("{l} {op} {r}"))
}
fn record_fields(
&mut self,
s: NodeId,
join: Option<(&str, &str)>,
) -> Result<Vec<String>, SqlError> {
let kids = self.arbor.children(s);
let mut fields = Vec::new();
let mut i = 0;
while i < kids.len() {
let k = kids[i];
if self.kind(k) == "literal" {
let name = self.prop_s(k, "value");
let value = self.operand(kids[i + 1], join.map(|(l, _)| l))?;
let name = self.alias(&name)?;
fields.push(format!("{value} AS {name}"));
i += 2;
} else {
let value = self.operand(k, join.map(|(l, _)| l))?;
fields.push(value);
i += 1;
}
}
Ok(fields)
}
}
impl Exporter {
fn reducer_counts_values(
&self,
s: NodeId,
name: &str,
projected: bool,
) -> Result<bool, SqlError> {
let mut values = false;
for k in self.arbor.children(s) {
if self.kind(k) != "option" {
return Err(not_pushed(
Refusal::Reducer,
format!("an operand of '{name}'"),
));
}
match (name, self.prop_s(k, "name").as_str()) {
("count", "--values" | "-v") => values = true,
(_, option) => {
return Err(not_pushed(
Refusal::Reducer,
format!("the '{option}' option of '{name}'"),
));
}
}
}
if values && !projected {
return Err(not_pushed(
Refusal::Reducer,
"'count(--values)' over row nodes (project a column: '/table/*::col @| \
count(--values)')"
.into(),
));
}
Ok(values)
}
}
fn sql_agg(name: &str, col: Option<String>) -> Result<String, SqlError> {
let f = match name {
"count" => "COUNT",
"sum" => "SUM",
"mean" => "AVG",
"min" => "MIN",
"max" => "MAX",
_ => unreachable!("checked by caller"),
};
match col {
Some(c) => Ok(format!("{f}({c})")),
None if f == "COUNT" => Ok("COUNT(*)".to_string()),
None => Err(not_pushed(
Refusal::Reducer,
format!("'{name}' over row nodes (project a column: '/table/*::col @| {name}')"),
)),
}
}
#[cfg(test)]
mod null_and_literal_tests {
use super::{Dialect, export, partial_pushdown, pushdown};
const GROUPED: &str = " | ::x | .g(::x) @|{.g} count | .n | %.";
#[test]
fn pattern_literal_pushes_as_like() {
let q = "/users/*[::name == 'app-'*]::id";
let p = pushdown(q, Some(Dialect::Postgres)).unwrap();
assert_eq!(
p.sql,
"SELECT id FROM users WHERE name LIKE 'app-%' ESCAPE '\\'"
);
assert!(pushdown(q, Some(Dialect::Sqlite)).is_none());
assert!(pushdown(q, Some(Dialect::MySql)).is_none());
let p = pushdown("/users/*[::name == *'web'*]::id", Some(Dialect::Postgres)).unwrap();
assert!(p.sql.contains("LIKE '%web%'"), "{}", p.sql);
let p = pushdown(
"/users/*[::name == *'a'*'.gz']::id",
Some(Dialect::Postgres),
)
.unwrap();
assert!(p.sql.contains("LIKE '%a%.gz'"), "{}", p.sql);
let p = pushdown("/users/*[::name == *'50%_x'*]::id", Some(Dialect::Postgres)).unwrap();
assert!(p.sql.contains("LIKE '%50\\%\\_x%'"), "{}", p.sql);
assert!(pushdown("/users/*[::name !== *'web'*]::id", Some(Dialect::Postgres)).is_none());
let p = pushdown("/users/*[::name *= 'web']::id", Some(Dialect::Postgres)).unwrap();
assert!(p.sql.contains("LIKE '%web%'"), "{}", p.sql);
assert_eq!(
pushdown("/users/*[::name *= 'web']::id", Some(Dialect::Sqlite))
.unwrap()
.sql,
"SELECT id FROM users WHERE instr(name, 'web') > 0"
);
assert!(pushdown("/users/*[::name *= 'web']::id", Some(Dialect::MySql)).is_none());
assert!(pushdown("/users/*[::name *= 'web']::id", None).is_none());
}
#[test]
fn json_column_path_pushdown_per_dialect() {
let q = "/orders/*[/data/meta/tier:: = 'gold']::id";
let sql = |d| pushdown(q, Some(d)).unwrap().sql;
assert_eq!(
sql(Dialect::Postgres),
"SELECT id FROM orders WHERE ((data::jsonb #>> '{meta,tier}') = 'gold')"
);
assert_eq!(
sql(Dialect::MySql),
"SELECT id FROM orders WHERE (JSON_VALID(data) AND \
JSON_UNQUOTE(JSON_EXTRACT(data, '$.meta.tier')) = 'gold')"
);
assert_eq!(
sql(Dialect::Sqlite),
"SELECT id FROM orders WHERE (json_valid(data) AND \
json_extract(data, '$.meta.tier') = 'gold')"
);
assert_eq!(
sql(Dialect::Mssql),
"SELECT id FROM orders WHERE (ISJSON(data) = 1 AND \
JSON_VALUE(data, '$.meta.tier') = 'gold')"
);
assert_eq!(
sql(Dialect::Oracle),
"SELECT id FROM orders WHERE (JSON_VALUE(data, '$.meta.tier') = 'gold')"
);
assert!(pushdown(q, None).is_none());
}
#[test]
fn json_pushdown_only_string_equality() {
let d = Some(Dialect::Sqlite);
assert!(pushdown("/o/*[/data/n:: > 2]::id", d).is_none());
assert!(pushdown("/o/*[/data/tier:: != 'gold']::id", d).is_none());
assert!(pushdown("/o/*[/data/items/*/sku:: = 'A1']::id", d).is_none());
}
#[test]
fn json_pushdown_refuses_numeric_looking_literal() {
let q = "/orders/*[/data/total:: = '150']::id";
assert!(pushdown(q, Some(Dialect::Sqlite)).is_none());
assert!(pushdown(q, Some(Dialect::Postgres)).is_none());
}
#[test]
fn a_regex_names_the_literals_it_cannot_match_without() {
let lits = |p: &str| super::required_literals(p);
assert_eq!(lits("^Ma.*s$"), ["Ma", "s"]);
assert_eq!(lits("(us)+$"), ["us"]);
assert_eq!(lits("a?b"), ["b"]);
assert_eq!(lits("(?:foo|bar)baz"), ["baz"]);
assert_eq!(lits("fo{2,}"), ["f", "o"]);
assert_eq!(lits(r"\d+-\d+"), ["-"]);
assert_eq!(lits(r"\bfoo\b"), ["foo"]);
assert_eq!(lits(r"No\.1"), ["No.1"]);
assert_eq!(lits("na\u{ef}ve"), ["na\u{ef}ve"]);
assert_eq!(lits("(?x) a b"), ["ab"]);
assert_eq!(lits("(a|b)c*d"), ["d"]);
assert_eq!(lits("x{0}y"), ["y"]);
assert_eq!(lits("ab(ab)"), ["ab"]);
for p in ["(?i)ma", "Ven|Jup", "a*", "[ab]", "(ab)?", ".*", "", "["] {
assert!(lits(p).is_empty(), "{p}");
}
}
#[test]
fn a_regex_is_narrowed_by_its_literals() {
let part = |q: &str, d: Dialect| {
partial_pushdown(q, Some(d)).map(|p| (p.where_sql, p.column_types.text))
};
assert_eq!(
part("/t/*[::a == (/^Ma.*s$/)]::b", Dialect::Sqlite),
Some((
"instr(a, 'Ma') > 0 AND instr(a, 's') > 0".to_string(),
vec!["a".to_string()]
))
);
assert_eq!(
part("/t/*[::a == (/50%_off/)]::b", Dialect::Postgres).map(|p| p.0),
Some(r"a LIKE '%50\%\_off%' ESCAPE '\'".to_string())
);
assert_eq!(
part("/t/*[::n > 2 && ::a == (/on/)]::b", Dialect::Sqlite).map(|p| p.0),
Some("(n > 2 AND instr(a, 'on') > 0)".to_string())
);
assert_eq!(
part(
"/t/*[::a == (/(?i)x/) && ::a == (/on/)]::b",
Dialect::Sqlite
)
.map(|p| p.0),
Some("(instr(a, 'on') > 0)".to_string())
);
assert_eq!(
part("/t/*[::a == (/Ven/) || ::a == (/Jup/)]::b", Dialect::Sqlite).map(|p| p.0),
Some("(instr(a, 'Ven') > 0 OR instr(a, 'Jup') > 0)".to_string())
);
for q in [
"/t/*[::a == (/Ven/) || ::a == (/(?i)jup/)]::b",
"/t/*[!(::a == (/Ven/))]::b",
"/t/*[::a !== (/Ven/)]::b",
"/t/*[::a == (/it's/)]::b",
"/t/*[::a == (/(?i)ven/)]::b",
"/t/*[::a == (/Ven|Jup/)]::b",
] {
assert_eq!(part(q, Dialect::Sqlite), None, "{q}");
}
assert_eq!(
part("/t/*[::a == (/Ven/)]::b", Dialect::MySql).map(|p| p.0),
Some("instr(a, 'Ven') > 0".to_string())
);
assert_eq!(
partial_pushdown("/t/*[::a == (/^Ma.*s$/)]::b", None).map(|p| p.where_sql),
Some("strpos(a, 'Ma') > 0 AND strpos(a, 's') > 0".to_string())
);
assert_eq!(
partial_pushdown("/t/*[::n > 2 && ::a == (/on/)]::b", None).map(|p| p.where_sql),
Some("(n > 2 AND strpos(a, 'on') > 0)".to_string())
);
assert_eq!(part("/t/*[::a == (/Ven/)]::b", Dialect::Mssql), None);
assert_eq!(part("/t/*[::a == (/Ven/)]::b", Dialect::Oracle), None);
}
#[test]
fn partial_json_prefilters_sqlite() {
let p = partial_pushdown(
"/orders/*[/payload/customer/geo/city:: = 'Lyon']::id",
Some(Dialect::Sqlite),
)
.unwrap();
assert_eq!(p.table, "orders");
assert_eq!(
p.where_sql,
"(json_valid(payload) AND \
json_extract(payload, '$.customer.geo.city') = 'Lyon')"
);
let p = partial_pushdown(
"/orders/*[/payload/total:: > 150]::id",
Some(Dialect::Sqlite),
)
.unwrap();
assert_eq!(
p.where_sql,
"(json_valid(payload) AND (json_extract(payload, '$.total') > 150 \
OR json_type(payload, '$.total') = 'text'))"
);
let p = partial_pushdown("/orders/*[/payload/gift]::id", Some(Dialect::Sqlite)).unwrap();
assert_eq!(
p.where_sql,
"(json_valid(payload) AND json_type(payload, '$.gift') IS NOT NULL)"
);
let p = partial_pushdown(
"/orders/*[/payload/status:: != 'x']::id",
Some(Dialect::Sqlite),
)
.unwrap();
assert_eq!(
p.where_sql,
"(json_valid(payload) AND json_type(payload, '$.status') IS NOT NULL)"
);
let p = partial_pushdown(
"/orders/*[150 < /payload/total::]::id",
Some(Dialect::Sqlite),
)
.unwrap();
assert!(p.where_sql.contains("> 150"), "{}", p.where_sql);
assert!(partial_pushdown("/orders/*[/payload/gift]::id", None).is_none());
}
#[test]
fn partial_json_prefilters_postgres() {
let p = partial_pushdown(
"/orders/*[/payload/total:: > 150]::id",
Some(Dialect::Postgres),
)
.unwrap();
assert_eq!(
p.where_sql,
"(CASE WHEN jsonb_typeof(payload::jsonb #> '{total}') = 'number' \
THEN ((payload::jsonb #>> '{total}'))::float8 > 150 \
WHEN jsonb_typeof(payload::jsonb #> '{total}') = 'string' \
THEN true ELSE false END)"
);
let p = partial_pushdown("/orders/*[/payload/gift]::id", Some(Dialect::Postgres)).unwrap();
assert_eq!(
p.where_sql,
"(jsonb_typeof(payload::jsonb #> '{gift}') IS NOT NULL)"
);
}
#[test]
fn json_property_path_pushdown() {
let q = "/orders/*[/payload/customer/geo::city = 'Lyon']::id";
assert_eq!(
pushdown(q, Some(Dialect::Sqlite)).unwrap().sql,
"SELECT id FROM orders WHERE (json_valid(payload) AND \
((json_type(payload, '$.customer.geo.city') = 'text' AND \
json_extract(payload, '$.customer.geo.city') = 'Lyon') OR \
(json_type(payload, '$.customer.geo.city') = 'array' AND \
EXISTS (SELECT 1 FROM json_each(payload, '$.customer.geo.city') \
WHERE json_each.type = 'text' AND json_each.value = 'Lyon') AND \
NOT EXISTS (SELECT 1 FROM json_each(payload, '$.customer.geo.city') \
WHERE json_each.type IN ('array', 'object')))))"
);
assert!(
pushdown(
"/orders/*[/payload::city = 'Lyon']::id",
Some(Dialect::Sqlite)
)
.unwrap()
.sql
.contains("json_extract(payload, '$.city') = 'Lyon'")
);
for d in [
Dialect::Postgres,
Dialect::MySql,
Dialect::Mssql,
Dialect::Oracle,
] {
assert!(pushdown(q, Some(d)).is_none(), "{d:?}");
}
assert!(pushdown(q, None).is_none());
assert!(
pushdown(
"/orders/*[/payload::total = '150']::id",
Some(Dialect::Sqlite)
)
.is_none()
);
assert!(
pushdown(
"/orders/*[/value::city = 'Lyon']::id",
Some(Dialect::Sqlite)
)
.is_none()
);
}
#[test]
fn partial_json_property_prefilters() {
let sqlite = |q: &str| partial_pushdown(q, Some(Dialect::Sqlite)).map(|p| p.where_sql);
assert_eq!(
sqlite("/orders/*[/payload::total > 150]::id").unwrap(),
"(json_valid(payload) AND (json_extract(payload, '$.total') > 150 \
OR json_type(payload, '$.total') IN ('text', 'array')))"
);
assert_eq!(
sqlite("/orders/*[/payload/customer::tier < 'm']::id").unwrap(),
"(json_valid(payload) AND (json_extract(payload, '$.customer.tier') < 'm' \
OR json_type(payload, '$.customer.tier') = 'array'))"
);
assert_eq!(
sqlite("/orders/*[/payload::gift]::id").unwrap(),
"(json_valid(payload) AND json_type(payload, '$.gift') IS NOT NULL)"
);
assert_eq!(
sqlite("/orders/*[/payload/customer::tier != 'x']::id").unwrap(),
"(json_valid(payload) AND json_type(payload, '$.customer') IS NOT NULL)"
);
assert!(sqlite("/orders/*[/payload::status != 'x']::id").is_none());
assert_eq!(
partial_pushdown(
"/orders/*[/payload/customer/geo::city = 'Lyon']::id",
Some(Dialect::Postgres)
)
.unwrap()
.where_sql,
"(CASE WHEN jsonb_typeof(payload::jsonb #> '{customer,geo,city}') = 'string' \
THEN (payload::jsonb #>> '{customer,geo,city}') = 'Lyon' \
WHEN jsonb_typeof(payload::jsonb #> '{customer,geo,city}') = 'array' \
THEN true ELSE false END)"
);
assert_eq!(
partial_pushdown(
"/orders/*[/payload::total > 150]::id",
Some(Dialect::Postgres)
)
.unwrap()
.where_sql,
"(CASE WHEN jsonb_typeof(payload::jsonb #> '{total}') = 'number' \
THEN ((payload::jsonb #>> '{total}'))::float8 > 150 \
WHEN jsonb_typeof(payload::jsonb #> '{total}') IN ('string', 'array') \
THEN true ELSE false END)"
);
assert!(
partial_pushdown("/orders/*[/payload::total > 150]::id", Some(Dialect::MySql))
.is_none()
);
}
#[test]
fn json_pushdown_refuses_a_container_looking_literal() {
for q in [
"/orders/*[/data/a:: = '[1,2]']::id",
"/orders/*[/data/a:: = '{\"k\":1}']::id",
] {
assert!(pushdown(q, Some(Dialect::Sqlite)).is_none(), "{q}");
assert!(pushdown(q, Some(Dialect::Postgres)).is_none(), "{q}");
}
assert_eq!(
partial_pushdown("/orders/*[/data/a:: = '[1,2]']::id", Some(Dialect::Sqlite))
.unwrap()
.where_sql,
"(json_valid(data) AND json_extract(data, '$.a') = '[1,2]')"
);
}
#[test]
fn partial_skips_untranslatable_keeps_prefix() {
let p = partial_pushdown(
"/orders/*[::status = 'shipped'][/payload/line_items/*/sku:: = 'X']::id",
Some(Dialect::Sqlite),
)
.unwrap();
assert_eq!(p.where_sql, "status = 'shipped'");
}
#[test]
fn null_literal_compares_use_is_null() {
assert_eq!(
export("/t/*[::x = null] | ::x").unwrap().query,
"SELECT x FROM t WHERE x IS NULL"
);
assert_eq!(
export("/t/*[::x != null] | ::x").unwrap().query,
"SELECT x FROM t WHERE x IS NOT NULL"
);
assert_eq!(
pushdown("/t/*[::x = null] | ::x", None).unwrap().sql,
"SELECT x FROM t WHERE x IS NULL"
);
assert_eq!(
pushdown("/t/*[::x != null] | ::x", None).unwrap().sql,
"SELECT x FROM t WHERE x IS NOT NULL"
);
}
#[test]
fn ne_and_not_push_in_the_form_that_keeps_the_null_rows() {
assert_eq!(
pushdown("/t/*[::x != 5] | ::x", None).unwrap().sql,
"SELECT x FROM t WHERE (x <> 5 OR x IS NULL)"
);
assert_eq!(
pushdown("/t/*[::x != ::y] | ::x", None).unwrap().sql,
"SELECT x FROM t WHERE (x <> y OR (x IS NULL AND y IS NOT NULL) OR (x IS NOT NULL \
AND y IS NULL))"
);
assert_eq!(
pushdown("/t/*[!::x = 5] | ::x", None).unwrap().sql,
"SELECT x FROM t WHERE (CASE WHEN x = 5 THEN 0 ELSE 1 END = 1)"
);
assert_eq!(
pushdown("/t/*[::x = ::y] | ::x", None).unwrap().sql,
"SELECT x FROM t WHERE (x = y OR (x IS NULL AND y IS NULL))"
);
assert_eq!(
partial_pushdown(&format!("/t/*[::x != 5]{GROUPED}"), None)
.unwrap()
.where_sql,
"(x <> 5 OR x IS NULL)"
);
let t = export("/t/*[::x != 5] | ::x").unwrap();
assert_eq!(t.query, "SELECT x FROM t WHERE x <> 5");
assert!(t.notes.iter().any(|n| n.contains("NULL")));
}
#[test]
fn unescapable_text_literal_refuses_pushdown() {
assert!(pushdown("/files/*[::path = \"C:\\temp\"] | ::path", None).is_none());
assert!(pushdown("/t/*[::name = \"it's\"] | ::name", None).is_none());
assert!(partial_pushdown(&format!("/t/*[::name = \"it's\"]{GROUPED}"), None).is_none());
assert_eq!(
pushdown("/t/*[::name = \"rare\"] | ::name", None)
.unwrap()
.sql,
"SELECT name FROM t WHERE name = 'rare'"
);
}
}