use std::collections::HashSet;
use datafusion::arrow::array::{Array, AsArray};
use datafusion::arrow::compute::cast;
use datafusion::arrow::datatypes::{DataType, Int64Type};
use datafusion::common::TableReference;
use datafusion::error::{DataFusionError, Result};
use datafusion::prelude::SessionContext;
use ursa_viz::model::{GraphResult, Key};
use super::session::Session;
use super::{plan_err, thousands, RunSql};
use crate::graph::registry_of;
use crate::graph::schema::{ID, NODES};
pub const MAX_SELECTED: usize = 1_000;
pub const MAX_LISTED: usize = 20;
const USAGE: &str = "usage: .find TEXT or .find WHERE condition";
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Query {
Text(String),
Where(String),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Find {
pub query: Query,
pub sql_only: bool,
}
impl Find {
pub fn parse(line: &str) -> Result<Option<Find>> {
let text = line.trim().trim_end_matches(';').trim_end();
let (name, rest) = match text.find(char::is_whitespace) {
Some(at) => (&text[..at], text[at..].trim()),
None => (text, ""),
};
if !name.eq_ignore_ascii_case(".find") {
return Ok(None);
}
let (rest, sql_only) = match rest.strip_suffix("--sql") {
Some(before) if before.is_empty() || before.ends_with(char::is_whitespace) => {
(before.trim_end(), true)
}
_ => (rest, false),
};
let is_where = rest
.get(..5)
.is_some_and(|w| w.eq_ignore_ascii_case("where"))
&& rest[5..].chars().next().is_none_or(char::is_whitespace);
let query = if is_where {
let condition = rest[5..].trim();
if condition.is_empty() {
return plan_err(format!(
"WHERE takes a condition on the vertex table's columns: .find WHERE club = \
'Officer'; {USAGE}"
));
}
Query::Where(condition.to_string())
} else {
let unquoted = ['\'', '"'].iter().find_map(|&q| {
let inner = rest.strip_prefix(q)?.strip_suffix(q)?;
(rest.len() >= 2).then(|| inner.replace(&format!("{q}{q}"), &q.to_string()))
});
let text = unquoted.unwrap_or_else(|| rest.to_string());
if text.is_empty() {
return plan_err(USAGE);
}
Query::Text(text)
};
Ok(Some(Find { query, sql_only }))
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NotShown {
pub key: String,
pub hidden: bool,
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct Found {
pub text: String,
pub selected: Vec<u32>,
pub matches: usize,
pub not_shown: Vec<NotShown>,
pub not_shown_total: usize,
pub sql: Vec<String>,
}
struct Table {
name: String,
label: Option<String>,
key: String,
text: Option<String>,
}
impl Table {
fn out(&self) -> &'static str {
if self.key.eq_ignore_ascii_case("node") {
"node_key"
} else {
"node"
}
}
}
fn ident(name: &str) -> String {
format!("\"{}\"", name.replace('"', "\"\""))
}
fn string(text: &str) -> String {
format!("'{}'", text.replace('\'', "''"))
}
fn is_text(ty: &DataType) -> bool {
match ty {
DataType::Utf8 | DataType::LargeUtf8 | DataType::Utf8View => true,
DataType::Dictionary(_, values) => is_text(values),
_ => false,
}
}
async fn tables(ctx: &SessionContext, r: &GraphResult) -> Result<Option<Vec<Table>>> {
let Some(graph) = r.graph().and_then(|name| registry_of(ctx).get(name)) else {
return Ok(None);
};
let mut out = Vec::new();
for v in &graph.vertex_tables {
let schema = ctx
.table(v.table.clone())
.await?
.schema()
.as_arrow()
.clone();
let text = schema
.fields()
.iter()
.find(|f| f.name() != &v.key && is_text(f.data_type()))
.map(|f| f.name().clone());
out.push(Table {
name: v.table.to_quoted_string(),
label: Some(v.name.clone()),
key: v.key.clone(),
text,
});
}
if out.is_empty() && graph.default_pair().is_some() {
out.push(Table {
name: TableReference::partial(graph.name.as_str(), NODES).to_quoted_string(),
label: None,
key: ID.to_string(),
text: None,
});
}
Ok(Some(out))
}
struct Matches<'a> {
r: &'a GraphResult,
seen: Vec<bool>,
outside: HashSet<String>,
found: Found,
}
impl Matches<'_> {
fn node(&mut self, i: u32) {
if std::mem::replace(&mut self.seen[i as usize], true) {
return;
}
if self.r.node_is_removed(i as usize) {
self.miss(self.r.node_keys().get(i as usize).to_string(), true);
} else {
self.found.matches += 1;
if self.found.selected.len() < MAX_SELECTED {
self.found.selected.push(i);
}
}
}
fn miss(&mut self, key: String, hidden: bool) {
self.found.not_shown_total += 1;
if self.found.not_shown.len() < MAX_LISTED {
self.found.not_shown.push(NotShown { key, hidden });
}
}
fn row(&mut self, table: &Table, key: &str) {
let labels = self.r.node_labels();
let of = |l: usize| {
let key = Key::parse(labels[l].key, key).ok()?;
self.r.node(l as u16, &key)
};
let own = table
.label
.as_ref()
.and_then(|name| labels.iter().position(|l| &l.name == name));
let node = own.and_then(of).or_else(|| (0..labels.len()).find_map(of));
match node {
Some(i) => self.node(i),
None => {
if self.outside.insert(key.to_string()) {
self.miss(key.to_string(), false);
}
}
}
}
}
fn nodes(n: usize) -> String {
format!("{} node{}", thousands(n), if n == 1 { "" } else { "s" })
}
impl Found {
fn say(&mut self, what: &str) {
let mut out = match (self.matches, self.selected.len()) {
(0, _) if self.not_shown_total == 0 => format!("No node matches {what}"),
(0, _) => format!("No node in the view matches {what}"),
(all, some) if some < all => format!(
"Selected the first {} of {} that match {what}",
thousands(some),
nodes(all)
),
(all, _) => format!("Selected {}", nodes(all)),
};
if self.not_shown_total > 0 {
let named: Vec<String> = self
.not_shown
.iter()
.map(|n| match n.hidden {
true => format!("{} (hidden)", n.key),
false => n.key.clone(),
})
.collect();
let more = self.not_shown_total - named.len();
out.push_str(&format!("\nNot shown: {}", named.join(", ")));
if more > 0 {
out.push_str(&format!(" and {} more", thousands(more)));
}
if self.not_shown.iter().any(|n| n.hidden) {
out.push_str("\n.show ID shows a hidden node again");
}
}
self.text = out;
}
}
pub async fn find(
session: &Session,
ctx: &SessionContext,
sql: &dyn RunSql,
line: &str,
) -> Result<Found> {
let Some(Find { query, sql_only }) = Find::parse(line)? else {
return plan_err(format!("'{}' is not .find; {USAGE}", line.trim()));
};
let r = session.result();
let tables = tables(ctx, r).await?;
let mut matches = Matches {
r,
seen: vec![false; r.n_nodes()],
outside: HashSet::new(),
found: Found::default(),
};
let what;
match &query {
Query::Text(text) => {
what = format!("'{text}'");
let needle = text.to_lowercase();
let key_of = |t: &Table| format!("lower(CAST({} AS VARCHAR))", ident(&t.key));
let whole = r.provenance().rule.as_deref() == Some("graph");
let asked: Vec<&Table> = tables
.iter()
.flatten()
.filter(|t| !(whole && t.label.is_none()))
.collect();
let statements: Vec<String> = asked
.iter()
.map(|t| {
let (key, lit) = (key_of(t), string(&needle));
let prefix = format!("starts_with({key}, {lit})");
let (label, rank) = match &t.text {
Some(c) => (
format!(" OR strpos(lower({}), {lit}) > 0", ident(c)),
format!(
"CASE WHEN {key} = {lit} THEN 0 WHEN {prefix} THEN 1 ELSE 2 END"
),
),
None => (
String::new(),
format!("CASE WHEN {key} = {lit} THEN 0 ELSE 1 END"),
),
};
format!(
"SELECT CAST({k} AS VARCHAR) AS {}, {rank} AS rank FROM {} \
WHERE {prefix}{label} ORDER BY rank, {k}",
t.out(),
t.name,
k = ident(&t.key),
)
})
.collect();
if sql_only {
return Ok(said_sql(
statements,
".find TEXT here matches the keys drawn",
));
}
let mut ranked: [Vec<(usize, String)>; 3] = Default::default();
for (at, statement) in statements.iter().enumerate() {
let (_, batches) = sql.run(ctx, statement).await?;
for b in &batches {
let ids = cast(b.column(0), &DataType::Utf8)?;
let ids = ids.as_string::<i32>();
let rank = cast(b.column(1), &DataType::Int64)?;
let rank = rank.as_primitive::<Int64Type>();
for i in (0..b.num_rows()).filter(|&i| ids.is_valid(i)) {
let rank = rank.value(i).clamp(0, 2) as usize;
ranked[rank].push((at, ids.value(i).to_string()));
}
}
}
let mut exact = Vec::new();
let mut starts = Vec::new();
for (i, key) in r.node_keys().iter().enumerate() {
let hit = if key.is_ascii() {
key.len() >= needle.len()
&& key.as_bytes()[..needle.len()].eq_ignore_ascii_case(needle.as_bytes())
} else {
key.to_lowercase().starts_with(&needle)
};
if hit {
let whole_key = key.len() == needle.len() || key.to_lowercase() == needle;
if whole_key { &mut exact } else { &mut starts }.push(i as u32);
}
}
for (rank, own) in [(0, &exact), (1, &starts), (2, &Vec::new())] {
for (at, key) in &ranked[rank] {
matches.row(asked[*at], key);
}
for &i in own {
matches.node(i);
}
}
matches.found.sql = statements;
}
Query::Where(condition) => {
what = condition.clone();
let Some(tables) = &tables else {
return plan_err(
"this drawing is not of a property graph, so there is no vertex table for a \
condition; .find TEXT matches the keys drawn, or draw a graph's nodes \
first: .graph NAME",
);
};
if tables.is_empty() {
return plan_err(
"the graph that is drawn has several edge tables and no vertex table, so \
there are no columns for a condition; .find TEXT matches the keys drawn",
);
}
let statements: Vec<String> = tables
.iter()
.map(|t| {
format!(
"SELECT CAST({k} AS VARCHAR) AS {} FROM {} WHERE ({condition}) ORDER BY {k}",
t.out(),
t.name,
k = ident(&t.key),
)
})
.collect();
if sql_only {
return Ok(said_sql(statements, ""));
}
let mut failed: Option<DataFusionError> = None;
let mut ran = Vec::new();
for (table, statement) in tables.iter().zip(statements) {
match sql.run(ctx, &statement).await {
Ok((_, batches)) => {
for b in &batches {
let ids = cast(b.column(0), &DataType::Utf8)?;
let ids = ids.as_string::<i32>();
for i in (0..b.num_rows()).filter(|&i| ids.is_valid(i)) {
matches.row(table, ids.value(i));
}
}
ran.push(statement);
}
Err(e) => failed = failed.or(Some(e)),
}
}
match failed {
Some(e) if ran.is_empty() => return Err(e),
_ => matches.found.sql = ran,
}
}
}
let mut found = matches.found;
found.say(&what);
Ok(found)
}
fn said_sql(statements: Vec<String>, none: &str) -> Found {
let text = if statements.is_empty() {
format!("-- .find runs no SQL: {none}")
} else {
statements
.iter()
.map(|s| format!("{s};"))
.collect::<Vec<_>>()
.join("\n")
};
Found {
text,
sql: statements,
..Found::default()
}
}
#[cfg(test)]
mod tests {
use super::*;
fn parsed(line: &str) -> Find {
Find::parse(line).unwrap().unwrap()
}
#[test]
fn find_lines_are_read() {
assert_eq!(
parsed(".find 33"),
Find {
query: Query::Text("33".into()),
sql_only: false
}
);
assert_eq!(
parsed(" .FIND Mr Hi ;").query,
Query::Text("Mr Hi".into())
);
assert_eq!(
parsed(".find 'O''Neil'").query,
Query::Text("O'Neil".into())
);
assert_eq!(
parsed(".find WHERE club = 'Officer';").query,
Query::Where("club = 'Officer'".into())
);
assert_eq!(
parsed(".find where id > 3 --sql"),
Find {
query: Query::Where("id > 3".into()),
sql_only: true
}
);
assert_eq!(
parsed(".find wherever").query,
Query::Text("wherever".into())
);
assert_eq!(parsed(".find 3 --sql").query, Query::Text("3".into()));
}
#[test]
fn other_lines_are_not_find() {
for line in [".finder", ".expand 3", "SELECT 1", ".graph karate", ""] {
assert_eq!(Find::parse(line).unwrap(), None, "{line}");
}
for line in [".find", ".find ;", ".find WHERE", ".find --sql"] {
let e = Find::parse(line).unwrap_err().to_string();
assert!(e.contains("usage: .find TEXT"), "{line}: {e}");
}
}
}