Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions DESIGN_SYSTEM.md
Original file line number Diff line number Diff line change
Expand Up @@ -357,8 +357,10 @@ pass `rank={null}` for a field `/` should not focus.
Where a field keeps its own keyboard handling or toolbar cluster, put
`search-box no-field-frame` on the input (icon positioned over it), or
`search-shell` on a wrapper row that holds icon, input and buttons. Both draw
the same outline. Filter rows at the top of a menu or popover list
(`data-slot="menu-search"`) stay frameless: the menu is already the box.
the same outline. A filter row at the top of a menu or popover list
(`data-slot="menu-search"`) is a `search-shell` inset with `m-1.5` (`h-8`),
so it reads as the same field inside the menu. The command palette's title
row is the one exception: the palette itself is the field.

**Prominent pick lists** (provider DB lists, account lists):
```
Expand Down
27 changes: 26 additions & 1 deletion src-tauri/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1234,7 +1234,32 @@ pub async fn pg_explain_sql(
let lines = crate::db::oracle::explain_lines(&h, &sql).await?;
Ok(explain_from_text_lines(lines, "oracle"))
}
_ => Err("EXPLAIN isn't supported for this engine yet".into()),
ActiveConnection::LibSql(cfg) => {
// libSQL is SQLite: the same (id, parent, notused, detail) rows.
let res = crate::db::libsql::query(&cfg, &format!("EXPLAIN QUERY PLAN {sql}"), vec![]).await?;
Ok(explain_from_sqlite_plan(&res, "libsql"))
}
ActiveConnection::Doc(crate::db::docdb::DocConn::Mongo(m)) => Ok(ExplainResult {
plan: crate::db::mongo::explain_plan(&m, &sql).await?,
planning_time: 0.0,
execution_time: 0.0,
driver: "mongodb".into(),
}),
ActiveConnection::Doc(crate::db::docdb::DocConn::Elastic(cfg)) => Ok(ExplainResult {
plan: crate::db::elastic::explain_plan(&cfg, &sql).await?,
planning_time: 0.0,
execution_time: 0.0,
driver: "elasticsearch".into(),
}),
ActiveConnection::Doc(crate::db::docdb::DocConn::Couch(cfg)) => Ok(ExplainResult {
plan: crate::db::couchdb::explain_plan(&cfg, &sql).await?,
planning_time: 0.0,
execution_time: 0.0,
driver: "couchdb".into(),
}),
// Redis, DynamoDB, Firestore, Cassandra/ScyllaDB and PostHog's HogQL
// API expose no query plan.
_ => Err("This engine doesn't expose a query plan".into()),
}
}

Expand Down
39 changes: 39 additions & 0 deletions src-tauri/src/db/couchdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,45 @@ pub async fn query(cfg: &CouchConfig, stmt: &str) -> Result<super::query::SqlRes
Ok(docs_result(docs, message, stmt, t0))
}

/// A Mango find's plan from `_explain`: which index CouchDB picked (or that
/// it scans every document), with the selector and the fields it reads.
pub async fn explain_plan(cfg: &CouchConfig, stmt: &str) -> Result<Value, String> {
let Statement::Find { db, selector, opts } = parse(stmt)? else {
return Err("Explain works on find queries: db.find({selector})".into());
};
let mut body = json!({ "selector": selector });
if let Some(o) = opts.as_object() {
for (k, v) in o {
body[k] = v.clone();
}
}
let out = request(cfg, "POST", &format!("{}/_explain", enc(&db)), Some(&body)).await?;
Ok(explain_tree(&out, &db))
}

/// `_explain` output → plan tree. Pure, so it is tested on recorded output.
pub fn explain_tree(out: &Value, db: &str) -> Value {
let ix = &out["index"];
let kind = ix["type"].as_str().unwrap_or("");
let name = ix["name"].as_str().unwrap_or("");
let scan = match kind {
"special" => json!({ "Node Type": "All documents scan", "Relation Name": db }),
_ => json!({
"Node Type": format!("{} index scan", if kind.is_empty() { "Index" } else { kind }),
"Relation Name": db,
"Index Name": if name.is_empty() { ix["ddoc"].as_str().unwrap_or("") } else { name },
"Index Cond": ix["def"]["fields"].to_string(),
}),
};
json!({
"Node Type": "Find",
"Relation Name": db,
"Filter": out["selector"].to_string(),
"Plan Rows": out["limit"].as_i64(),
"Plans": [scan],
})
}

/// A result-grid cell: Extended JSON scalars (`{"$oid": ...}`, `{"$date": ...}`,
/// `{"$numberLong": ...}`, `{"$numberDecimal": ...}`) as their plain value, and
/// the type name for the column header.
Expand Down
40 changes: 35 additions & 5 deletions src-tauri/src/db/duckdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,14 +165,45 @@ fn for_each_row(
params: &[DuckValue],
on_columns: impl FnOnce(Vec<ColumnInfo>),
mut on_row: impl FnMut(Vec<Value>) -> bool,
) -> Result<(), String> {
for_each_row_in(conn, sql, params, false, on_columns, on_row)
}

/// `for_each_row` on DuckDB's streaming result: chunks are produced as they
/// are read, so DuckDB never holds the whole result either. For reads only
/// (streaming execution refuses DML that returns rows).
fn for_each_row_streaming(
conn: &::duckdb::Connection,
sql: &str,
on_columns: impl FnOnce(Vec<ColumnInfo>),
on_row: impl FnMut(Vec<Value>) -> bool,
) -> Result<(), String> {
for_each_row_in(conn, sql, &[], true, on_columns, on_row)
}

fn for_each_row_in(
conn: &::duckdb::Connection,
sql: &str,
params: &[DuckValue],
streaming: bool,
on_columns: impl FnOnce(Vec<ColumnInfo>),
mut on_row: impl FnMut(Vec<Value>) -> bool,
) -> Result<(), String> {
let mut stmt = conn.prepare(sql).map_err(|e| format!("DuckDB error: {e}"))?;

// column_names()/column_count() panic before the statement is executed, so we
// run query() first (it executes eagerly) and read metadata via rows.as_ref().
let mut rows = stmt
.query(::duckdb::params_from_iter(params.iter()))
.map_err(|e| format!("DuckDB error: {e}"))?;
//
// Streaming: stream_arrow executes in DuckDB's streaming mode, and
// raw_query reads that same result through the row API. Its own Arrow
// iterator panics on a mid-stream error (fatal with panic=abort); the
// row API returns the error instead.
let mut rows = if streaming {
drop(stmt.stream_arrow(::duckdb::params_from_iter(params.iter())).map_err(|e| format!("DuckDB error: {e}"))?);
stmt.raw_query()
} else {
stmt.query(::duckdb::params_from_iter(params.iter())).map_err(|e| format!("DuckDB error: {e}"))?
};

let (col_names, col_count): (Vec<String>, usize) = match rows.as_ref() {
Some(s) => (s.column_names(), s.column_count()),
Expand Down Expand Up @@ -365,10 +396,9 @@ pub async fn stream_into(
g.1 = Some(conn.interrupt_handle());
}
let mut batch = Vec::with_capacity(BATCH);
let out = for_each_row(
let out = for_each_row_streaming(
conn,
&sql_owned,
&[],
|c| {
let _ = tx.blocking_send(Msg::Columns(c));
},
Expand Down
156 changes: 156 additions & 0 deletions src-tauri/src/db/elastic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,48 @@ enum Body<'a> {
Ndjson(String),
}

tokio::task_local! {
/// The console run's id, sent as X-Opaque-Id so Stop can find its tasks.
static OPAQUE_ID: String;
}

/// `query`, stoppable on the server. Every request the statement makes
/// carries an X-Opaque-Id; Stop drops the request and cancels the tasks that
/// carry that id. Searches already end when their connection closes, but
/// `_sql`, `_delete_by_query`, `_update_by_query` and the like run on.
pub async fn query_cancellable(cfg: &EsConfig, stmt: &str, stop: &super::stop::Stop) -> Result<super::query::SqlResult, String> {
static SEQ: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
let id = format!("stroke-{:x}-{:x}", std::process::id(), SEQ.fetch_add(1, std::sync::atomic::Ordering::Relaxed));
let run = OPAQUE_ID.scope(id.clone(), query(cfg, stmt));
tokio::pin!(run);
tokio::select! {
r = &mut run => r,
_ = stop.wait() => {
drop(run);
let _ = tokio::time::timeout(Duration::from_secs(10), cancel_tasks(cfg, &id)).await;
Err(super::query::QUERY_CANCELLED.into())
}
}
}

/// Cancel every task whose X-Opaque-Id is `id`. Returns how many.
pub async fn cancel_tasks(cfg: &EsConfig, id: &str) -> Result<usize, String> {
let v = send(cfg, "GET", "_tasks?detailed=true&group_by=none", None).await?;
let ids: Vec<String> = v["tasks"]
.as_array()
.map(|t| {
t.iter()
.filter(|t| t["headers"]["X-Opaque-Id"].as_str() == Some(id) && t["cancellable"].as_bool() != Some(false))
.filter_map(|t| Some(format!("{}:{}", t["node"].as_str()?, t["id"].as_i64()?)))
.collect()
})
.unwrap_or_default();
for t in &ids {
let _ = send(cfg, "POST", &format!("_tasks/{t}/_cancel"), None).await;
}
Ok(ids.len())
}

async fn send(cfg: &EsConfig, method: &str, path: &str, body: Option<Body<'_>>) -> Result<Value, String> {
let url = base(cfg)?.join(path.trim_start_matches('/')).map_err(|e| format!("Invalid path \"{path}\": {e}"))?;
let m = reqwest::Method::from_bytes(method.as_bytes()).map_err(|e| e.to_string())?;
Expand All @@ -244,6 +286,9 @@ async fn send(cfg: &EsConfig, method: &str, path: &str, body: Option<Body<'_>>)
} else if let Some(u) = cfg.user.as_deref().filter(|u| !u.is_empty()) {
req = req.basic_auth(u, cfg.password.as_deref());
}
if let Ok(id) = OPAQUE_ID.try_with(Clone::clone) {
req = req.header("X-Opaque-Id", id);
}
match body {
Some(Body::Json(b)) => req = req.json(b),
Some(Body::Ndjson(s)) => req = req.header("Content-Type", "application/x-ndjson").body(s),
Expand Down Expand Up @@ -276,6 +321,78 @@ async fn send(cfg: &EsConfig, method: &str, path: &str, body: Option<Body<'_>>)
Ok(v)
}

/// A search's plan: the statement run once with `"profile": true`, and the
/// profile drawn as the plan view's tree (search, then each shard, then its
/// Lucene queries and aggregations with their times). Only searches have one.
pub async fn explain_plan(cfg: &EsConfig, stmt: &str) -> Result<Value, String> {
let (index, mut body) = match parse(stmt)? {
Statement::Search { index, body } => (index, body),
Statement::Count { index, body } => (index, json!({ "query": body.get("query").cloned().unwrap_or(json!({"match_all": {}})), "size": 0 })),
Statement::Raw { method, path, body } if (method == "GET" || method == "POST") && path.trim_end_matches('/').ends_with("_search") => {
let index = path.trim_start_matches('/').split('/').next().unwrap_or("").to_string();
(index, body.unwrap_or(json!({})))
}
_ => return Err("Explain works on searches: GET index/_search, index.search({...}) or index.count({...})".into()),
};
if !body.is_object() {
body = json!({});
}
body["profile"] = json!(true);
let path = if index.is_empty() || index == "_search" { "_search".to_string() } else { format!("{}/_search", enc(&index)) };
let v = request(cfg, "POST", &path, Some(&body)).await?;
Ok(profile_tree(&v, &index))
}

/// A profiled search response → plan tree. Pure, so it is tested on recorded output.
pub fn profile_tree(v: &Value, index: &str) -> Value {
fn ms(nanos: &Value) -> Value {
nanos.as_f64().map(|n| json!((n / 1e6 * 1000.0).round() / 1000.0)).unwrap_or(Value::Null)
}
fn part(q: &Value) -> Value {
let mut n = json!({
"Node Type": q["type"].as_str().unwrap_or("Query"),
"Filter": q["description"].as_str().unwrap_or(""),
"Actual Total Time": ms(&q["time_in_nanos"]),
});
if let Some(kids) = q["children"].as_array().filter(|k| !k.is_empty()) {
n["Plans"] = Value::Array(kids.iter().map(part).collect());
}
n
}
let shards: Vec<Value> = v["profile"]["shards"]
.as_array()
.map(|sh| {
sh.iter()
.map(|shard| {
let mut plans: Vec<Value> = shard["searches"]
.as_array()
.map(|ss| ss.iter().flat_map(|s| s["query"].as_array().cloned().unwrap_or_default()).map(|q| part(&q)).collect())
.unwrap_or_default();
plans.extend(shard["aggregations"].as_array().map(|a| a.iter().map(part).collect::<Vec<_>>()).unwrap_or_default());
let total: f64 = plans.iter().filter_map(|p| p["Actual Total Time"].as_f64()).sum();
let id = shard["id"].as_str().unwrap_or("");
// `[node][index][shard]`: the last two say which.
let label = id.split(']').filter(|p| !p.is_empty()).map(|p| p.trim_start_matches('[')).skip(1).collect::<Vec<_>>().join(" shard ");
json!({
"Node Type": "Shard",
"Relation Name": if label.is_empty() { id.to_string() } else { label },
"Actual Total Time": (total * 1000.0).round() / 1000.0,
"Plans": plans,
})
})
.collect()
})
.unwrap_or_default();
let hits = v["hits"]["total"]["value"].as_i64().or_else(|| v["hits"]["total"].as_i64());
json!({
"Node Type": "Search",
"Relation Name": index,
"Actual Rows": hits,
"Actual Total Time": v["took"].as_f64(),
"Plans": shards,
})
}

/// What `GET /` says about the server.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
Expand Down Expand Up @@ -862,6 +979,45 @@ mod tests {
assert_eq!(urlencoding::encode("a/b"), "a%2Fb");
}

/// Stop on a slow search: while it runs the server lists a task with our
/// X-Opaque-Id, and after Stop none is left.
// STROKE_TEST_ES=http://127.0.0.1:19200 cargo test --lib elastic::tests::live_stop
#[tokio::test]
async fn live_stop_cancels_the_task() {
let Ok(url) = std::env::var("STROKE_TEST_ES") else { return };
let cfg = EsConfig { ssh: None, via: None, name: "t".into(), url, user: None, password: None, api_key: None, flavor: None, insecure: true };
let cfg = connect(cfg).await.unwrap();
let _ = send(&cfg, "DELETE", "stroke_slow", None).await;
let mut nd = String::new();
for i in 0..20_000 {
nd.push_str(&format!("{{\"index\":{{\"_index\":\"stroke_slow\"}}}}\n{{\"n\":{i}}}\n"));
}
send(&cfg, "POST", "_bulk?refresh=true", Some(Body::Ndjson(nd))).await.unwrap();
let ours = |v: &Value| -> usize {
v["tasks"].as_array().map(|t| t.iter().filter(|t| t["headers"]["X-Opaque-Id"].as_str().is_some_and(|h| h.starts_with("stroke-"))).count()).unwrap_or(0)
};
let (tx, rx) = tokio::sync::oneshot::channel();
let stop = super::super::stop::Stop::from_rx(rx);
let probe = cfg.clone();
let during = tokio::spawn(async move {
tokio::time::sleep(Duration::from_millis(1500)).await;
let v = send(&probe, "GET", "_tasks?detailed=true&group_by=none", None).await.unwrap();
let n = ours(&v);
let _ = tx.send(());
n
});
let slow = "GET stroke_slow/_search\n{\"query\": {\"script\": {\"script\": \"long x = 0; for (int i = 0; i < 900000; i++) { x += i; } return x > 0;\"}}}";
let t0 = Instant::now();
let e = query_cancellable(&cfg, slow, &stop).await.unwrap_err();
assert_eq!(e, super::super::query::QUERY_CANCELLED);
assert!(during.await.unwrap() > 0, "the search carried our X-Opaque-Id");
assert!(t0.elapsed() < Duration::from_secs(20));
tokio::time::sleep(Duration::from_millis(1500)).await;
let v = send(&cfg, "GET", "_tasks?detailed=true&group_by=none", None).await.unwrap();
assert_eq!(ours(&v), 0, "a task is still running after Stop");
let _ = send(&cfg, "DELETE", "stroke_slow", None).await;
}

/// Against a local server: `cargo test --lib es_live -- --ignored`, with
/// ES_URL (default http://localhost:9200).
#[tokio::test]
Expand Down
Loading
Loading