ursa-cli 0.9.0

The ursa command-line tool: SQL over Parquet, CSV, JSON and Arrow files, local or in object storage.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
//! The SQL docs and the agent skill, checked against the `ursa` binary.
//!
//! Three checks:
//!
//! - **`sql_docs_run`**: the SQL on every page under `docs/sql/` runs and
//!   prints what the page shows.
//! - **`skill_examples_run`**: the same for `skills/ursa-sql/SKILL.md`.
//! - **`function_reference_is_current`**: `docs/sql/reference/functions.md` is
//!   what `SELECT * FROM ursa_functions()` generates. To regenerate it, run
//!
//!   ```text
//!   URSA_BLESS=1 cargo test -p ursa-cli --test docs function_reference
//!   ```
//!
//! # The convention for SQL in the docs
//!
//! - A ```` ```sql ```` block runs. A page's blocks run in order, in one
//!   database, `docs.ursa`, in a fresh directory that holds the bundled
//!   datasets (`python/ursa/datasets/data/`). Each block is one
//!   `ursa docs.ursa -c "<block>"` process, so a block sees every view and
//!   graph the blocks before it defined, as a reader typing them into one
//!   shell would.
//! - A ```` ```text ```` block that directly follows a ```` ```sql ```` block
//!   (only blank lines between) is its output, and is compared exactly with
//!   what the block prints (trailing spaces and blank lines at either end are
//!   ignored). If the expected output starts with `Error:`, the block must
//!   fail, and the text is compared with its error message instead.
//! - A block with no ```` ```text ```` after it must run without error; its
//!   output is not checked.
//! - When the shell's output changes on purpose, rewrite the ```` ```text ````
//!   blocks that differ with what the blocks now print, and read the diff:
//!
//!   ```text
//!   URSA_BLESS=1 cargo test -p ursa-cli --test docs _run
//!   ```
//! - `<!-- skip -->` on the line before a ```` ```sql ```` block skips it. Use
//!   it for SQL that cannot run here: object storage, placeholder paths.
//! - Blocks indented inside an admonition or a content tab count too.
//! - Shell dot-commands and `ursa …` command lines go in ```` ```text ```` or
//!   ```` ```bash ```` blocks, which do not run. The command lines of the
//!   Getting Started page are checked by `tests/getting_started.rs`.

use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};

fn workspace() -> PathBuf {
    Path::new(env!("CARGO_MANIFEST_DIR"))
        .parent()
        .unwrap()
        .to_path_buf()
}

/// One ```` ```sql ```` block: where it is, its SQL, and its expected output.
#[derive(Debug)]
struct Example {
    line: usize,
    sql: String,
    expected: Option<String>,
    /// Where the expected output's lines are in the page: the first, the
    /// one after the last, and their indent.
    expected_at: Option<(usize, usize, usize)>,
    skip: bool,
}

/// A fenced block: its language, its first line number and its text,
/// dedented to its fence.
struct Fence {
    lang: String,
    line: usize,
    body: String,
    /// The body's lines in the page: the first, the one after the last, and
    /// the fence's indent.
    span: (usize, usize, usize),
    /// Whether `<!-- skip -->` is on the line before it.
    skip: bool,
    /// Whether only blank lines separate it from the previous fence.
    adjacent: bool,
}

fn fences(text: &str) -> Vec<Fence> {
    let lines: Vec<&str> = text.lines().collect();
    let mut out = Vec::new();
    let mut i = 0;
    // The index of the line after the previous fence's closing line.
    let mut after_previous: Option<usize> = None;
    while i < lines.len() {
        let line = lines[i];
        let trimmed = line.trim_start();
        let Some(info) = trimmed.strip_prefix("```") else {
            i += 1;
            continue;
        };
        let indent = line.len() - trimmed.len();
        let lang = info.split_whitespace().next().unwrap_or("").to_string();
        let skip = i > 0 && lines[i - 1].trim() == "<!-- skip -->";
        let adjacent =
            after_previous.is_some_and(|start| lines[start..i].iter().all(|l| l.trim().is_empty()));
        let start = i + 1;
        let mut end = start;
        while end < lines.len() && lines[end].trim() != "```" {
            end += 1;
        }
        let body = lines[start..end.min(lines.len())]
            .iter()
            .map(|l| l.get(indent..).unwrap_or(l.trim_start()))
            .collect::<Vec<_>>()
            .join("\n");
        out.push(Fence {
            lang,
            line: i + 1,
            body,
            span: (start, end.min(lines.len()), indent),
            skip,
            adjacent,
        });
        i = end + 1;
        after_previous = Some(i.min(lines.len()));
    }
    out
}

/// The ```` ```sql ```` blocks of a page, each with the ```` ```text ````
/// block that follows it, if any.
fn examples(text: &str) -> Vec<Example> {
    let fences = fences(text);
    let mut out = Vec::new();
    for (i, fence) in fences.iter().enumerate() {
        if fence.lang != "sql" {
            continue;
        }
        let next = fences
            .get(i + 1)
            .filter(|next| next.lang == "text" && next.adjacent);
        out.push(Example {
            line: fence.line,
            sql: fence.body.clone(),
            expected: next.map(|next| next.body.clone()),
            expected_at: next.map(|next| next.span),
            skip: fence.skip,
        });
    }
    out
}

/// Trailing spaces and blank lines at either end don't count.
fn normalize(text: &str) -> String {
    text.lines()
        .map(str::trim_end)
        .collect::<Vec<_>>()
        .join("\n")
        .trim_matches('\n')
        .to_string()
}

/// A fresh directory holding the bundled datasets.
fn data_dir() -> tempfile::TempDir {
    let dir = tempfile::tempdir().unwrap();
    let data = workspace().join("python/ursa/datasets/data");
    for entry in std::fs::read_dir(&data).unwrap() {
        let path = entry.unwrap().path();
        if path.is_file() {
            std::fs::copy(&path, dir.path().join(path.file_name().unwrap())).unwrap();
        }
    }
    dir
}

/// Run every example of the page at `path`, in order, in one database.
/// Returns the failures, each naming the page and line.
fn run_page(path: &Path) -> Vec<String> {
    let text = std::fs::read_to_string(path).unwrap();
    let shown = path
        .strip_prefix(workspace())
        .unwrap_or(path)
        .display()
        .to_string();
    let dir = data_dir();
    let mut failures = Vec::new();
    let bless = std::env::var_os("URSA_BLESS").is_some();
    // With URSA_BLESS: the output blocks to rewrite, and their new lines.
    let mut blessed: Vec<((usize, usize, usize), String)> = Vec::new();
    for example in examples(&text) {
        if example.skip {
            continue;
        }
        let output = Command::new(env!("CARGO_BIN_EXE_ursa"))
            .args(["docs.ursa", "-c", &example.sql])
            .current_dir(dir.path())
            .env_remove("URSA_INDEX_STATS")
            .stdin(Stdio::null())
            .output()
            .expect("ursa runs");
        let stdout = String::from_utf8_lossy(&output.stdout);
        let stderr = String::from_utf8_lossy(&output.stderr);
        let at = format!("{shown}:{}", example.line);
        match (&example.expected, output.status.success()) {
            (Some(expected), _) if expected.trim_start().starts_with("Error:") => {
                if output.status.success() {
                    failures.push(format!(
                        "{at}: expected an error, but the block ran\n{}",
                        example.sql
                    ));
                } else if normalize(&stderr) != normalize(expected) {
                    failures.push(format!(
                        "{at}: the error differs\n--- expected\n{}\n--- got\n{}",
                        normalize(expected),
                        normalize(&stderr)
                    ));
                }
            }
            (_, false) => {
                failures.push(format!("{at}: the block failed\n{}\n{stderr}", example.sql))
            }
            (Some(expected), true) => {
                if normalize(&stdout) != normalize(expected) && bless {
                    blessed.push((example.expected_at.unwrap(), normalize(&stdout)));
                } else if normalize(&stdout) != normalize(expected) {
                    failures.push(format!(
                        "{at}: the output differs\n--- expected\n{}\n--- got\n{}",
                        normalize(expected),
                        normalize(&stdout)
                    ));
                }
            }
            (None, true) => {}
        }
    }
    if !blessed.is_empty() {
        let mut lines: Vec<String> = text.lines().map(str::to_string).collect();
        for ((start, end, indent), output) in blessed.into_iter().rev() {
            let pad = " ".repeat(indent);
            let new: Vec<String> = output
                .lines()
                .map(|l| format!("{pad}{l}").trim_end().to_string())
                .collect();
            lines.splice(start..end, new);
        }
        let mut page = lines.join("\n");
        if text.ends_with('\n') {
            page.push('\n');
        }
        std::fs::write(path, page).unwrap();
    }
    failures
}

fn markdown_files(dir: &Path, out: &mut Vec<PathBuf>) {
    let mut entries: Vec<PathBuf> = std::fs::read_dir(dir)
        .unwrap()
        .map(|e| e.unwrap().path())
        .collect();
    entries.sort();
    for path in entries {
        if path.is_dir() {
            markdown_files(&path, out);
        } else if path.extension().is_some_and(|e| e == "md") {
            out.push(path);
        }
    }
}

fn check(pages: &[PathBuf]) {
    let mut failures = Vec::new();
    let mut ran = 0;
    for page in pages {
        ran += examples(&std::fs::read_to_string(page).unwrap())
            .iter()
            .filter(|e| !e.skip)
            .count();
        failures.extend(run_page(page));
    }
    assert!(ran > 0, "no SQL examples found in {pages:?}");
    assert!(
        failures.is_empty(),
        "{} of {ran} SQL examples failed:\n\n{}",
        failures.len(),
        failures.join("\n\n")
    );
}

#[test]
fn sql_docs_run() {
    let mut pages = Vec::new();
    markdown_files(&workspace().join("docs/sql"), &mut pages);
    check(&pages);
}

#[test]
fn skill_examples_run() {
    check(&[workspace().join("skills/ursa-sql/SKILL.md")]);
}

#[test]
fn the_convention_is_parsed() {
    let page = "Text.\n\n```sql\nSELECT 1;\n```\n\n```text\nout\n```\n\n\
                <!-- skip -->\n```sql\nSELECT 2;\n```\n\nProse.\n\n```text\nnot output\n```\n\n\
                !!! note\n\n    ```sql\n    SELECT 3;\n    ```\n";
    let examples = examples(page);
    assert_eq!(examples.len(), 3);
    assert_eq!(examples[0].sql, "SELECT 1;");
    assert_eq!(examples[0].expected.as_deref(), Some("out"));
    assert!(!examples[0].skip);
    assert!(examples[1].skip && examples[1].expected.is_none());
    assert_eq!(examples[2].sql, "SELECT 3;");
}

// The function reference.

/// Where the generated reference lives.
fn reference_path() -> PathBuf {
    workspace().join("docs/sql/reference/functions.md")
}

const REGENERATE: &str = "URSA_BLESS=1 cargo test -p ursa-cli --test docs function_reference";

#[test]
fn function_reference_is_current() {
    let output = Command::new(env!("CARGO_BIN_EXE_ursa"))
        .args(["--format", "json", "-c", "SELECT * FROM ursa_functions()"])
        .stdin(Stdio::null())
        .output()
        .expect("ursa runs");
    assert!(
        output.status.success(),
        "{}",
        String::from_utf8_lossy(&output.stderr)
    );
    let rows: Vec<serde_json::Value> = serde_json::from_slice(&output.stdout).unwrap();
    let generated = reference(&rows);
    let path = reference_path();
    if std::env::var_os("URSA_BLESS").is_some() {
        std::fs::create_dir_all(path.parent().unwrap()).unwrap();
        std::fs::write(&path, &generated).unwrap();
        return;
    }
    let committed = std::fs::read_to_string(&path).unwrap_or_default();
    assert!(
        committed == generated,
        "docs/sql/reference/functions.md is out of date with ursa_functions(). \
         Regenerate it with\n\n    {REGENERATE}\n\nand commit the result."
    );
}

fn text<'a>(value: &'a serde_json::Value, key: &str) -> &'a str {
    value.get(key).and_then(|v| v.as_str()).unwrap_or("")
}

fn list<'a>(value: &'a serde_json::Value, key: &str) -> &'a [serde_json::Value] {
    value
        .get(key)
        .and_then(|v| v.as_array())
        .map_or(&[], Vec::as_slice)
}

/// A table cell: pipes escaped, empty shown as a dash.
fn cell(text: &str) -> String {
    if text.is_empty() {
        "–".to_string()
    } else {
        text.replace('|', "\\|")
    }
}

fn code(text: &str) -> String {
    format!("`{text}`")
}

const FAMILIES: [(&str, &str); 5] = [
    ("algorithm", "Algorithms"),
    ("statistic", "Statistics"),
    ("aggregation", "Neighbour aggregation"),
    ("traversal", "Traversals"),
    ("combinator", "Combining functions"),
];

/// The reference page, from the rows of `ursa_functions()`.
fn reference(rows: &[serde_json::Value]) -> String {
    let mut out = String::new();
    out.push_str(
        "<!-- Generated from SELECT * FROM ursa_functions(). Do not edit by hand: run\n     ",
    );
    out.push_str(REGENERATE);
    out.push_str("\n-->\n\n");
    out.push_str("# Function reference\n\n");
    out.push_str(
        "Every graph function Ursa's SQL engine offers. The page is generated from \
         `SELECT * FROM ursa_functions()`, so it matches the engine you run.\n\n\
         An algorithm or a statistic takes the graph as its first argument, then named \
         parameters with `=>`. It returns one row per node: the node's `id` and its output \
         columns. `neighbors_agg` does too, from a value per node it reads. A traversal \
         returns rows of its own: the nodes reached, a path's edges, or walks. \
         `algorithms` runs several per-node functions in one call.\n\n\
         A parameter's default is the SQL literal used when the parameter is left out. \
         `required` means the call must give it. A `string` parameter with choices takes one \
         of them, quoted: `direction => 'both'`. An `expression` parameter takes an \
         expression over the edge table's columns: `weight => log10(1 + weight)`, or for \
         `neighbors_agg`'s `attribute` over the node table's. A `query` parameter takes a \
         subquery in parentheses.\n\n\
         A `node ids` or `node id` parameter takes integers or strings: a list, a query of \
         one column, or one value. On a graph with integer ids, a string must be an integer, \
         such as `'7'`; floats (even `2.0`), booleans and dates are refused. On a graph with \
         string ids, an integer is taken as its string. A NULL, or an id not in the graph, \
         matches no node.\n\n",
    );
    for (family, heading) in FAMILIES {
        let members: Vec<&serde_json::Value> = rows
            .iter()
            .filter(|row| text(row, "family") == family)
            .collect();
        if members.is_empty() {
            continue;
        }
        out.push_str(&format!("## {heading}\n\n"));
        let links: Vec<String> = members
            .iter()
            .map(|row| text(row, "name"))
            .map(|name| format!("[`{name}`](#{name})"))
            .collect();
        out.push_str(&links.join(" · "));
        out.push_str("\n\n");
        for row in members {
            out.push_str(&function_section(row));
        }
    }
    // Any family this page does not know yet still appears.
    let others: Vec<&serde_json::Value> = rows
        .iter()
        .filter(|row| !FAMILIES.iter().any(|(f, _)| text(row, "family") == *f))
        .collect();
    if !others.is_empty() {
        out.push_str("## Other functions\n\n");
        for row in others {
            out.push_str(&function_section(row));
        }
    }
    out.truncate(out.trim_end().len());
    out.push('\n');
    out
}

fn function_section(row: &serde_json::Value) -> String {
    let name = text(row, "name");
    let mut out = format!("### {name}\n\n{}\n\n", text(row, "summary"));
    let params = list(row, "parameters");

    // The call, with every parameter at its default.
    let mut call = vec!["graph".to_string()];
    for p in params {
        let default = text(p, "default");
        call.push(format!(
            "{} => {}",
            text(p, "name"),
            if default == "required" {
                "…"
            } else {
                default
            }
        ));
    }
    if name != "algorithms" {
        out.push_str(&format!("```text\n{name}({})\n```\n\n", call.join(", ")));
    }

    if !params.is_empty() {
        out.push_str("| Parameter | Type | Default | Choices | Description |\n");
        out.push_str("|---|---|---|---|---|\n");
        for p in params {
            let choices: Vec<String> = list(p, "choices")
                .iter()
                .filter_map(|c| c.as_str())
                .map(|c| code(&format!("'{c}'")))
                .collect();
            out.push_str(&format!(
                "| {} | {} | {} | {} | {} |\n",
                code(text(p, "name")),
                cell(text(p, "type")),
                code(text(p, "default")),
                cell(&choices.join(", ")),
                cell(text(p, "doc")),
            ));
        }
        out.push('\n');
    }

    let directions: Vec<String> = list(row, "directions")
        .iter()
        .filter_map(|d| d.as_str())
        .map(|d| code(&format!("'{d}'")))
        .collect();
    if !directions.is_empty() {
        out.push_str(&format!("**Directions:** {}\n\n", directions.join(", ")));
    }

    let outputs = list(row, "outputs");
    if !outputs.is_empty() {
        let mut columns: Vec<String> = Vec::new();
        let shape = match text(row, "family") {
            "algorithm" => {
                columns.push(format!("{} (the node)", code("id")));
                "One row per node: "
            }
            "statistic" => "One row: ",
            _ => "",
        };
        for o in outputs {
            let nullable = if o.get("nullable").and_then(|n| n.as_bool()) == Some(true) {
                ", may be NULL"
            } else {
                ""
            };
            columns.push(format!(
                "{} ({}{nullable})",
                code(text(o, "name")),
                text(o, "type")
            ));
        }
        out.push_str(&format!("**Returns:** {shape}{}\n\n", columns.join(", ")));
    }

    let networkx = text(row, "networkx");
    if !networkx.is_empty() {
        out.push_str(&format!("**NetworkX:** `{networkx}`\n\n"));
    }
    out
}