ursa-engine 0.12.0

Ursa's SQL engine session: a DataFusion session that reads files by path, locally or in object storage.
Documentation
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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
//! `SIZE BY x` and `COLOR BY y`: where a name's values come from, one value
//! per node, and how they are drawn.
//!
//! x and y name, in turn: a column of `NODES (query)`; a column of the
//! property graph's vertex tables; `degree`; or a per-node algorithm, run on
//! the graph with defaults through `algorithms(G, …)`. A source that is rows
//! (a query, a table, the last result) has no vertex tables and no graph to
//! run an algorithm on: NODES and degree only.

use std::collections::HashMap;
use std::sync::Arc;

use datafusion::arrow::array::{
    new_null_array, Array, ArrayRef, Float32Array, RecordBatch, UInt32Array,
};
use datafusion::arrow::compute::{cast, concat, interleave};
use datafusion::arrow::datatypes::{DataType, SchemaRef};
use datafusion::arrow::util::display::{ArrayFormatter, FormatOptions};
use datafusion::common::TableReference;
use datafusion::error::Result;
use datafusion::prelude::SessionContext;

use super::build::{EdgeList, NodeIndex};
use super::clauses::Name;
use super::{plan_err, scan_columns, thousands, RunSql, MAX_CATEGORIES};
use crate::graph::functions;
use crate::graph::schema::graph_table;
use crate::graph::PropertyGraph;
use ursa_viz::model::NULL;

/// The colour index of a node in no top category, or with no value.
#[cfg(test)]
pub(crate) const OTHER: u32 = MAX_CATEGORIES as u32;

/// `SIZE BY x`: one value per node (NaN for a node with none), which the page
/// maps to radii.
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct SizeBy {
    /// What it is by, as typed.
    pub by: String,
    pub values: Vec<f32>,
    /// Nodes with no value.
    pub missing: usize,
}

/// `COLOR BY y`: each node's category, the categories ranked by how many
/// nodes they have. The first [`MAX_CATEGORIES`] are coloured; the rest are
/// drawn as "other".
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct ColorBy {
    pub by: String,
    /// Every category's value, as the shell prints it: the largest first,
    /// equal ones as first seen.
    pub values: Vec<String>,
    /// How many nodes each category has.
    pub counts: Vec<u32>,
    /// Each node's category, as an index into `values`, or [`NULL`] for a
    /// node with no value.
    pub codes: Vec<u32>,
    /// Nodes with no value (NULL, or none given).
    pub missing: u32,
}

#[cfg(test)]
impl ColorBy {
    /// The categories coloured, by colour index.
    pub(crate) fn labels(&self) -> &[String] {
        &self.values[..self.values.len().min(MAX_CATEGORIES)]
    }

    /// How many nodes each coloured category has.
    pub(crate) fn top_counts(&self) -> &[u32] {
        &self.counts[..self.counts.len().min(MAX_CATEGORIES)]
    }

    /// Nodes in the categories past the coloured ones.
    pub(crate) fn other(&self) -> u32 {
        self.counts.iter().skip(MAX_CATEGORIES).sum()
    }

    /// Each node's colour index: its category's, or [`OTHER`].
    pub(crate) fn index(&self) -> Vec<u32> {
        self.codes.iter().map(|&c| c.min(OTHER)).collect()
    }
}

/// Where a name's values come from.
pub(crate) enum Found {
    /// A column of NODES's query, by index.
    Nodes(usize),
    /// A column of the result drawn, whose rows are nodes (rule 5).
    Own(usize),
    /// A column of the property graph's vertex tables.
    Vertex,
    /// A per-node algorithm, run on the graph.
    Algorithm(&'static str),
    /// Each node's degree in the drawing.
    Degree,
}

/// The per-node algorithms `algorithms(G, …)` runs: every one that reads no
/// node values.
pub(crate) fn per_node_algorithm(name: &str) -> Option<&'static str> {
    functions::lookup(name)
        .filter(|s| functions::is_per_node(s) && !s.reads_attribute())
        .map(|s| s.name)
}

fn per_node_algorithms() -> Vec<&'static str> {
    functions::specs()
        .filter(|s| functions::is_per_node(s) && !s.reads_attribute())
        .map(|s| s.name)
        .collect()
}

/// `names[i]` that is `ident`: exactly, or else in any case.
fn find_name<'a>(names: impl Iterator<Item = &'a str> + Clone, ident: &str) -> Option<usize> {
    names
        .clone()
        .position(|n| n == ident)
        .or_else(|| names.clone().position(|n| n.eq_ignore_ascii_case(ident)))
}

/// Where `name` (after `clause`) takes its values from: NODES's columns,
/// then the graph's vertex columns, then degree, then an algorithm.
pub(crate) async fn resolve(
    ctx: &SessionContext,
    graph: Option<&PropertyGraph>,
    nodes: Option<&NodeValues>,
    own: Option<&NodeValues>,
    clause: &str,
    name: &Name,
) -> Result<Found> {
    if let Some(i) = nodes.and_then(|n| n.find(&name.ident)) {
        return Ok(Found::Nodes(i));
    }
    if let Some(i) = own.and_then(|n| n.find(&name.ident)) {
        return Ok(Found::Own(i));
    }
    let mut columns = Vec::new();
    if let Some(g) = graph {
        for (_, _, names) in vertex_columns(ctx, g).await? {
            if find_name(names.iter().map(String::as_str), &name.ident).is_some() {
                return Ok(Found::Vertex);
            }
            columns.extend(names);
        }
    }
    if name.ident == "degree" {
        return Ok(Found::Degree);
    }
    let algorithm = per_node_algorithm(&name.ident);
    let shown = &name.shown;
    let nodes_columns = nodes
        .map(|n| format!("; NODES (…) has {}", n.value_names().join(", ")))
        .or_else(|| own.map(|n| format!("; the result has {}", n.value_names().join(", "))))
        .unwrap_or_default();
    match (graph, algorithm) {
        (Some(_), Some(algorithm)) => Ok(Found::Algorithm(algorithm)),
        (Some(g), None) => {
            columns.sort();
            columns.dedup();
            plan_err(format!(
                "{clause} {shown}: graph '{}' has no vertex column or algorithm named '{}'. Its \
                 vertex columns are {}; the algorithms are degree, {}{nodes_columns}",
                g.name,
                name.ident,
                if columns.is_empty() {
                    "none (it has no vertex tables)".to_string()
                } else {
                    columns.join(", ")
                },
                per_node_algorithms()
                    .into_iter()
                    .filter(|a| *a != "degree")
                    .collect::<Vec<_>>()
                    .join(", ")
            ))
        }
        (None, Some(algorithm)) => plan_err(format!(
            "{clause} {shown} runs {}() on a property graph, and this draws rows, which have none \
             to run it on: draw the graph by name (.graph NAME {clause} {shown}), or give the \
             values: NODES (SELECT id, … AS {shown} FROM …) {clause} {shown}",
            algorithm
        )),
        (None, None) => plan_err(format!(
            "{clause} {shown}: no column named '{}'. Rows drawn as edges are sized or coloured \
             by degree, or by a column of NODES (SELECT id, … FROM …){nodes_columns}",
            name.ident
        )),
    }
}

/// Each vertex table of `graph`: its name, its key, and its columns.
async fn vertex_columns(
    ctx: &SessionContext,
    graph: &PropertyGraph,
) -> Result<Vec<(TableReference, String, Vec<String>)>> {
    let state = ctx.state();
    let mut out = Vec::new();
    for v in &graph.vertex_tables {
        let provider = graph_table(&state, &graph.name, &v.table).await?;
        let names = provider
            .schema()
            .fields()
            .iter()
            .map(|f| f.name().clone())
            .collect();
        out.push((v.table.clone(), v.key.clone(), names));
    }
    Ok(out)
}

/// The vertex column `ident`, from every vertex table that has it.
pub(crate) async fn vertex_column(
    ctx: &SessionContext,
    graph: &PropertyGraph,
    ident: &str,
) -> Result<Column> {
    let mut column = Column {
        ids: Some(Vec::new()),
        values: Vec::new(),
        label: None,
    };
    for (table, key, names) in vertex_columns(ctx, graph).await? {
        let Some(i) = find_name(names.iter().map(String::as_str), ident) else {
            continue;
        };
        // The key itself (COLOR BY id) is scanned once.
        let columns = if names[i] == key {
            vec![key.as_str()]
        } else {
            vec![key.as_str(), names[i].as_str()]
        };
        for b in scan_columns(ctx, graph, &table, &columns).await? {
            let ids = column.ids.as_mut().expect("ids");
            ids.push(Arc::clone(b.column(0)));
            column.values.push(Arc::clone(b.column(columns.len() - 1)));
        }
    }
    Ok(column)
}

/// Each node's degree as drawn: its edges' ends (a loop's two).
pub(crate) fn degree_column(edges: &EdgeList) -> Column {
    let mut degree = vec![0u32; edges.n as usize];
    for (&s, &d) in edges.src.iter().zip(&edges.dst) {
        degree[s as usize] += 1;
        degree[d as usize] += 1;
    }
    Column {
        ids: None,
        values: vec![Arc::new(UInt32Array::from(degree))],
        label: None,
    }
}

/// `algorithms(graph, …)` with each of `specs` at the view's defaults: read
/// as undirected (`direction => 'both'`) where it can be, as the view draws
/// the graph, and seeded (`seed => 1`) so a drawing is the same each time.
pub(crate) async fn run_algorithms(
    ctx: &SessionContext,
    sql: &dyn RunSql,
    graph: &PropertyGraph,
    algorithms: &[&'static str],
) -> Result<NodeValues> {
    let calls: Vec<String> = algorithms
        .iter()
        .map(|a| format!("{a} => {}", default_call(a)))
        .collect();
    let query = format!(
        "SELECT * FROM algorithms(\"{}\", {})",
        graph.name.replace('"', "\"\""),
        calls.join(", ")
    );
    NodeValues::of_query(ctx, sql, &query).await
}

/// A call to the algorithm `name` with the view's defaults.
fn default_call(name: &str) -> String {
    let spec = functions::lookup(name).expect("a catalog function");
    let mut args = Vec::new();
    if spec.directions().contains(&"both") {
        args.push("direction => 'both'");
    }
    if spec.param("seed").is_some() {
        args.push("seed => 1");
    }
    format!("{}({})", spec.name, args.join(", "))
}

/// Rows of node values: an id column (`id`, or else the first) and others.
pub(crate) struct NodeValues {
    schema: SchemaRef,
    batches: Vec<RecordBatch>,
    id: usize,
}

impl NodeValues {
    pub(crate) async fn of_query(
        ctx: &SessionContext,
        sql: &dyn RunSql,
        query: &str,
    ) -> Result<NodeValues> {
        let (schema, batches) = sql.run(ctx, query).await?;
        if schema.fields().len() < 2 {
            return plan_err(format!(
                "NODES (…) needs two columns or more, each node's id and its values; got {}",
                schema.fields().len()
            ));
        }
        let id = find_name(schema.fields().iter().map(|f| f.name().as_str()), "id").unwrap_or(0);
        Ok(NodeValues {
            schema,
            batches,
            id,
        })
    }

    /// A result's own rows as node values: the nodes' ids are in column `id`.
    pub(crate) fn of_rows(schema: SchemaRef, batches: Vec<RecordBatch>, id: usize) -> NodeValues {
        NodeValues {
            schema,
            batches,
            id,
        }
    }

    /// The column called `ident`, other than the ids.
    fn find(&self, ident: &str) -> Option<usize> {
        let names = self.schema.fields().iter().map(|f| f.name().as_str());
        find_name(names, ident).filter(|&i| i != self.id)
    }

    pub(crate) fn column_named(&self, name: &str) -> Column {
        self.column(self.find(name).expect("the column asked for"))
    }

    fn value_names(&self) -> Vec<String> {
        let fields = self.schema.fields().iter().enumerate();
        fields
            .filter(|(i, _)| *i != self.id)
            .map(|(_, f)| f.name().clone())
            .collect()
    }

    pub(crate) fn column(&self, i: usize) -> Column {
        Column {
            ids: Some(
                self.batches
                    .iter()
                    .map(|b| Arc::clone(b.column(self.id)))
                    .collect(),
            ),
            values: self
                .batches
                .iter()
                .map(|b| Arc::clone(b.column(i)))
                .collect(),
            label: None,
        }
    }
}

/// Values with the node ids they belong to, or (no ids) one per node.
pub(crate) struct Column {
    ids: Option<Vec<ArrayRef>>,
    values: Vec<ArrayRef>,
    /// The vertex table the ids are keys of, if the values are one table's.
    label: Option<u16>,
}

impl Column {
    /// `values`, each the value of the node of vertex table `label` whose
    /// key is at its place in `ids`.
    pub(crate) fn of_keys(ids: ArrayRef, values: ArrayRef, label: Option<u16>) -> Column {
        Column {
            ids: Some(vec![ids]),
            values: vec![values],
            label,
        }
    }

    /// One value per node, in node order: NULL for a node with none. An id
    /// given twice keeps its first value.
    pub(crate) fn per_node(&self, index: &NodeIndex, n: usize) -> Result<ArrayRef> {
        let data_type = self
            .values
            .first()
            .map(|v| v.data_type().clone())
            .unwrap_or(DataType::Null);
        let mut sources: Vec<ArrayRef> = Vec::with_capacity(self.values.len() + 1);
        for v in &self.values {
            sources.push(if v.data_type() == &data_type {
                Arc::clone(v)
            } else {
                cast(v, &data_type)?
            });
        }
        let Some(ids) = &self.ids else {
            return Ok(concat(
                &sources.iter().map(|a| a.as_ref()).collect::<Vec<_>>(),
            )?);
        };
        let missing = sources.len();
        sources.push(new_null_array(&data_type, 1));
        let mut at = vec![(missing, 0usize); n];
        let mut given = vec![false; n];
        for (b, ids) in ids.iter().enumerate() {
            for (row, node) in index.lookup_in(self.label, ids)?.into_iter().enumerate() {
                if let Some(node) = node {
                    let node = node as usize;
                    if !given[node] {
                        given[node] = true;
                        at[node] = (b, row);
                    }
                }
            }
        }
        let refs: Vec<&dyn Array> = sources.iter().map(|a| a.as_ref()).collect();
        Ok(interleave(&refs, &at)?)
    }
}

/// `SIZE BY`: the values as numbers.
pub(crate) fn size_by(by: &str, values: &ArrayRef) -> Result<SizeBy> {
    let t = values.data_type();
    if !t.is_numeric() {
        let t = match t {
            DataType::Utf8 | DataType::LargeUtf8 | DataType::Utf8View => "text".to_string(),
            other => other.to_string(),
        };
        return plan_err(format!(
            "SIZE BY {by} needs numbers, and {by} is {t}; colour nodes by it instead: COLOR BY {by}"
        ));
    }
    let numbers = cast(values, &DataType::Float32)?;
    let numbers = numbers
        .as_any()
        .downcast_ref::<Float32Array>()
        .expect("cast to Float32");
    if numbers.null_count() == numbers.len() {
        return plan_err(format!(
            "SIZE BY {by}: no node has a value (are the ids the graph's?)"
        ));
    }
    Ok(SizeBy {
        by: by.to_string(),
        values: numbers.iter().map(|v| v.unwrap_or(f32::NAN)).collect(),
        missing: numbers.null_count(),
    })
}

/// `COLOR BY`: the values as categories, the largest coloured.
pub(crate) fn color_by(by: &str, values: &ArrayRef) -> Result<ColorBy> {
    let options = FormatOptions::default();
    let shown = ArrayFormatter::try_new(values.as_ref(), &options)?;
    // Each category: its label, its count, and where it was first seen.
    let mut seen: HashMap<String, usize> = HashMap::new();
    let mut categories: Vec<(String, u32)> = Vec::new();
    let mut of_node = Vec::with_capacity(values.len());
    for i in 0..values.len() {
        if values.is_null(i) {
            of_node.push(None);
            continue;
        }
        let label = shown.value(i).to_string();
        let c = *seen.entry(label.clone()).or_insert_with(|| {
            categories.push((label, 0));
            categories.len() - 1
        });
        categories[c].1 += 1;
        of_node.push(Some(c));
    }
    let with_values = values.len() - values.null_count();
    if with_values == 0 {
        return plan_err(format!(
            "COLOR BY {by}: no node has a value (are the ids the graph's?)"
        ));
    }
    let distinct = categories.len();
    let t = values.data_type();
    let fractional = matches!(
        t,
        DataType::Float16
            | DataType::Float32
            | DataType::Float64
            | DataType::Decimal32(..)
            | DataType::Decimal64(..)
            | DataType::Decimal128(..)
            | DataType::Decimal256(..)
    );
    if distinct > MAX_CATEGORIES && (fractional || 2 * distinct > with_values) {
        let advice = if fractional {
            format!(" A measure is better shown by size: SIZE BY {by}")
        } else if t.is_numeric() {
            format!(
                " Colour by a column with fewer values, or size nodes by this one: SIZE BY {by}"
            )
        } else {
            " Colour by a column with fewer values.".to_string()
        };
        return plan_err(format!(
            "COLOR BY {by}: {} distinct values over {} nodes are too many categories to tell \
             apart by colour.{advice}",
            thousands(distinct),
            thousands(with_values),
        ));
    }
    // The largest first; equal ones as first seen.
    let mut order: Vec<usize> = (0..distinct).collect();
    order.sort_by(|&a, &b| categories[b].1.cmp(&categories[a].1).then(a.cmp(&b)));
    let mut rank = vec![0u32; distinct];
    for (r, &c) in order.iter().enumerate() {
        rank[c] = r as u32;
    }
    Ok(ColorBy {
        by: by.to_string(),
        values: order.iter().map(|&c| categories[c].0.clone()).collect(),
        counts: order.iter().map(|&c| categories[c].1).collect(),
        codes: of_node
            .iter()
            .map(|c| c.map_or(NULL, |c| rank[c]))
            .collect(),
        missing: values.null_count() as u32,
    })
}

/// One value per node as a node column: numbers as they are (NaN for none),
/// anything else as text.
pub(crate) fn property_column(name: &str, values: &ArrayRef) -> Result<ursa_viz::model::Column> {
    use ursa_viz::model::{Column as ModelColumn, Role, Values};
    if values.data_type().is_numeric() {
        let numbers = cast(values, &DataType::Float32)?;
        let numbers = numbers
            .as_any()
            .downcast_ref::<Float32Array>()
            .expect("cast to Float32");
        let values = numbers.iter().map(|v| v.unwrap_or(f32::NAN)).collect();
        return Ok(ModelColumn::new(
            name,
            Some(Role::Property),
            Values::F32(values),
        ));
    }
    let options = FormatOptions::default();
    let shown = ArrayFormatter::try_new(values.as_ref(), &options)?;
    let texts: Vec<Option<String>> = (0..values.len())
        .map(|i| values.is_valid(i).then(|| shown.value(i).to_string()))
        .collect();
    Ok(ModelColumn::new(
        name,
        Some(Role::Property),
        Values::dict(texts.iter().map(|t| t.as_deref())),
    ))
}