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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
//! The result → graph rules (design §3, #433): which rule draws a result,
//! and from which of its columns.
//!
//! [`read`] says what each column is, **by provenance first, names second**,
//! and then takes the first rule that matches:
//!
//! 1. a path column (`vertices(p)`, with `edges(p)` or `element_id(p)`), or
//!    the steps of `shortest_path()`: **paths**;
//! 2. `seed`, `id` and `distance` (a `hop` result): **rings**;
//! 3. two endpoint columns: **edges**, a row an edge;
//! 4. three or more node variables of `GRAPH_TABLE`: **pattern matches**;
//! 5. one node key: **nodes**, with the graph's edges among them;
//! 6. none of these: not a graph, and the message says what would be.
//!
//! Provenance is the `ursa.*` field metadata the graph functions and
//! `GRAPH_TABLE` put on their output ([`crate::graph::provenance`]). A column
//! without it is read by its name and type: `src`/`dst`, `source`/`target`
//! and `from`/`to` are endpoints, `id` or `node` a node key, `seed`, `id`
//! and `distance` together a hop result, a list of ids a path, and, last,
//! the first two columns when both are ids of one kind, as before #433.
//!
//! A node key or an endpoint is an integer, a string or a date
//! ([`id_kind`]): a float or a list is never a node id.
//!
//! A property graph named to `.graph` is drawn whole ([`Rule::Graph`]).
//!
//! An `AS …` clause is rule 0 ([`read_as`], #434): it names the shape and
//! the columns, and nothing is inferred. Its columns must still be what the
//! shape needs (ids, lists of ids, whole numbers), and a column that is not
//! is an error that names it and lists the result's columns.

use datafusion::arrow::datatypes::{DataType, Field, Schema};
use datafusion::error::Result;
use ursa_core::catalog::OutputKind;

use super::clauses::{As, Name};
use super::plan_err;
use super::style::per_node_algorithm;
use crate::graph::functions;
use crate::graph::provenance as meta;

/// The rule that drew a result. A result's provenance names it
/// ([`Rule::name`]), for the pane's status line.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Rule {
    /// `.graph NAME`: a property graph's vertices and edges.
    Graph,
    /// Rows that are paths, or the steps of one (rule 1).
    Path,
    /// A `hop` result: seeds, the nodes reached and their distances (rule 2).
    Rings,
    /// Rows drawn as edges, one a row (rule 3).
    Edges,
    /// The matches of a `GRAPH_TABLE` pattern of three node variables or
    /// more (rule 4).
    Matches,
    /// Rows that are nodes, with the graph's edges among them (rule 5).
    Nodes,
}

impl Rule {
    /// The rule's name in a result's provenance.
    pub fn name(self) -> &'static str {
        match self {
            Rule::Graph => "graph",
            Rule::Path => "path",
            Rule::Rings => "rings",
            Rule::Edges => "edges",
            Rule::Matches => "pattern",
            Rule::Nodes => "nodes",
        }
    }
}

/// One edge of a pattern, between two of the node variables in `COLUMNS`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct MatchEdge {
    /// The key columns of its two ends, as indexes into [`Shape::Matches`]'s
    /// `keys`.
    pub from: usize,
    pub to: usize,
    pub directed: bool,
    /// The edge table.
    pub label: String,
}

/// What a result's rows are, by the rule that matched: the columns to read.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum Shape {
    /// Each row is an edge between the ids in columns `src` and `dst`.
    Edges { src: usize, dst: usize },
    /// Each row is a path: its nodes in a list column, or its nodes and
    /// edges alternating in `elements`; `edges` is its edges' rows.
    Path {
        nodes: Option<usize>,
        edges: Option<usize>,
        elements: Option<usize>,
    },
    /// Each row is a step of a path (`shortest_path()`): an edge, and its
    /// position.
    Steps { src: usize, dst: usize, step: usize },
    /// Each row is a node reached from a seed, and how far.
    Hop {
        seed: usize,
        id: usize,
        distance: usize,
    },
    /// Each row is a match of a pattern: the key column of each node
    /// variable, and the pattern's edges between them.
    Matches {
        keys: Vec<usize>,
        edges: Vec<MatchEdge>,
    },
    /// Each row is a node.
    Nodes { id: usize },
}

impl Shape {
    pub(crate) fn rule(&self) -> Rule {
        match self {
            Shape::Edges { .. } => Rule::Edges,
            Shape::Path { .. } | Shape::Steps { .. } => Rule::Path,
            Shape::Hop { .. } => Rule::Rings,
            Shape::Matches { .. } => Rule::Matches,
            Shape::Nodes { .. } => Rule::Nodes,
        }
    }
}

/// A result as the rules read it.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct Reading {
    pub shape: Shape,
    /// The property graph the node columns say they are from (`ursa.graph`).
    pub graph: Option<String>,
    /// Whether the columns were read by their metadata (else by name).
    pub marked: bool,
    /// The vertex table of each key column the shape reads: (column, table).
    pub labels: Vec<(usize, String)>,
    /// The edge table of the edges, if the result says.
    pub edge_label: Option<String>,
    /// Columns that become edge columns: numbers, one per row.
    pub attributes: Vec<usize>,
    /// Columns that describe nodes: (column, the key column of its nodes).
    pub properties: Vec<(usize, usize)>,
    /// Rule 5's defaults: the column that sizes the nodes and the one that
    /// colours them.
    pub size: Option<usize>,
    pub color: Option<usize>,
    /// Each column the drawing reads, and what as: (name, role).
    pub roles: Vec<(String, String)>,
    /// What the status line adds: what was left out, and other readings.
    pub notes: Vec<String>,
}

impl Reading {
    fn of(shape: Shape) -> Reading {
        Reading {
            shape,
            graph: None,
            marked: false,
            labels: Vec::new(),
            edge_label: None,
            attributes: Vec::new(),
            properties: Vec::new(),
            size: None,
            color: None,
            roles: Vec::new(),
            notes: Vec::new(),
        }
    }

    /// The vertex table of key column `column`, if the result says.
    pub(crate) fn label_of(&self, column: usize) -> Option<&str> {
        self.labels
            .iter()
            .find(|(c, _)| *c == column)
            .map(|(_, l)| l.as_str())
    }
}

/// The most columns carried as edge columns or node properties.
const MAX_CARRIED: usize = 8;

/// The first rule that matches a result of `schema`, or why none does.
#[cfg(test)]
pub(crate) fn shape_of(schema: &Schema) -> Result<Shape> {
    Ok(read(schema)?.shape)
}

/// A column's metadata.
struct Col<'a> {
    i: usize,
    field: &'a Field,
    role: Option<&'a str>,
    graph: Option<&'a str>,
    label: Option<&'a str>,
    var: Option<&'a str>,
}

impl Col<'_> {
    fn name(&self) -> &str {
        self.field.name()
    }
    fn is(&self, role: &str) -> bool {
        self.role == Some(role)
    }
    fn is_id(&self) -> bool {
        id_kind(self.field.data_type()).is_some()
    }
}

/// The type of a list column's elements.
fn list_of(t: &DataType) -> Option<&DataType> {
    match t {
        DataType::List(f)
        | DataType::LargeList(f)
        | DataType::FixedSizeList(f, _)
        | DataType::ListView(f)
        | DataType::LargeListView(f) => Some(f.data_type()),
        _ => None,
    }
}

fn is_id_list(t: &DataType) -> bool {
    list_of(t).is_some_and(|e| id_kind(e).is_some())
}

/// What a column named `name` holds per node, if it is named after an
/// algorithm or, for a category, is one of the usual names for a group.
fn kind_by_name(name: &str, t: &DataType) -> Option<OutputKind> {
    let name = name.to_ascii_lowercase();
    if let Some(kind) = functions::lookup(&name)
        .filter(|s| functions::is_per_node(s))
        .and_then(|s| s.output_kind())
    {
        return match kind {
            OutputKind::Metric if !t.is_numeric() => None,
            kind => Some(kind),
        };
    }
    matches!(
        name.as_str(),
        "community" | "component" | "cluster" | "label" | "group"
    )
    .then_some(OutputKind::Category)
}

/// What the rules make of a result of `schema`: the first rule that matches,
/// or why none does (rule 6).
pub(crate) fn read(schema: &Schema) -> Result<Reading> {
    let cols: Vec<Col> = schema
        .fields()
        .iter()
        .enumerate()
        .map(|(i, f)| {
            let get = |key: &str| f.metadata().get(key).map(String::as_str);
            Col {
                i,
                field: f.as_ref(),
                role: get(meta::ROLE),
                graph: get(meta::GRAPH),
                label: get(meta::LABEL),
                var: get(meta::VAR),
            }
        })
        .collect();
    if cols.iter().any(|c| c.role.is_some()) {
        if let Some(reading) = read_marked(schema, &cols)? {
            return Ok(reading);
        }
    }
    read_named(schema, &cols)
}

/// The result's columns and their types, for a message.
fn columns_of(schema: &Schema) -> String {
    let columns: Vec<String> = schema
        .fields()
        .iter()
        .map(|f| format!("{} ({})", f.name(), f.data_type()))
        .collect();
    match columns.len() {
        0 => "The result has no columns".to_string(),
        1 => format!("The result's one column is {}", columns[0]),
        _ => format!("The result's columns are {}", columns.join(", ")),
    }
}

/// Rule 0: a result read as its `AS …` clause says. The clause names the
/// columns; a column that is missing, or is not what the shape needs, is an
/// error that names it, lists the result's columns and says what to write.
///
/// What the columns say of themselves still counts where the clause says
/// nothing: their vertex and edge tables, and their graph (`ursa.graph`)
/// unless `ON graph` names one (`on`).
pub(crate) fn read_as(schema: &Schema, clause: &As, on: bool) -> Result<Reading> {
    let written = clause.shown();
    let columns = columns_of(schema);
    // The column a name names: as written, or else, unquoted, in any case.
    let find = |name: &Name| -> Result<usize> {
        let fields = schema.fields();
        let exact = fields.iter().position(|f| f.name() == &name.ident);
        let quoted = name.shown.starts_with('"');
        let found = exact.or_else(|| {
            let mut any = fields
                .iter()
                .enumerate()
                .filter(|(_, f)| !quoted && f.name().eq_ignore_ascii_case(&name.ident));
            match (any.next(), any.next()) {
                (Some((i, _)), None) => Some(i),
                _ => None,
            }
        });
        match found {
            Some(i) => Ok(i),
            None => plan_err(format!(
                "{written}: the result has no column {}. {columns}; name one of them",
                name.shown
            )),
        }
    };
    let get = |i: usize, key: &str| schema.field(i).metadata().get(key).cloned();
    let wrong = |i: usize, needs: &str| -> Result<usize> {
        let f = schema.field(i);
        plan_err(format!(
            "{written}: column {} is {} ({}), and {needs}. {columns}",
            f.name(),
            what_is(f.data_type()),
            f.data_type()
        ))
    };
    // A column that holds node ids.
    let id = |name: &Name, what: &str| -> Result<usize> {
        let i = find(name)?;
        match id_kind(schema.field(i).data_type()) {
            Some(_) => Ok(i),
            None => wrong(
                i,
                &format!("{what} is an id: an integer, a string or a date"),
            ),
        }
    };
    let role = |i: usize, role: &str| (schema.field(i).name().to_string(), role.to_string());
    let mut r = match clause {
        As::Edges { src, dst, weight } => {
            let (s, d) = (id(src, "an edge's end")?, id(dst, "an edge's end")?);
            if s == d {
                return plan_err(format!(
                    "{written}: both ends are the column {}, so every edge would join a node \
                     to itself. Name the two columns of an edge's ends. {columns}",
                    schema.field(s).name()
                ));
            }
            let mut r = Reading::of(Shape::Edges { src: s, dst: d });
            r.roles = vec![role(s, meta::ROLE_SRC), role(d, meta::ROLE_DST)];
            if let Some(weight) = weight {
                let w = find(weight)?;
                if !schema.field(w).data_type().is_numeric() {
                    wrong(w, "an edge's weight is a number")?;
                }
                r.attributes.push(w);
                r.roles.push(role(w, "weight"));
            }
            r.edge_label = schema
                .fields()
                .iter()
                .find(|f| f.metadata().get(meta::ROLE).map(String::as_str) == Some(meta::ROLE_EDGE))
                .and_then(|f| f.metadata().get(meta::LABEL).cloned());
            r
        }
        As::Nodes { id: key } => {
            let k = id(key, "a node's id")?;
            let mut r = Reading::of(Shape::Nodes { id: k });
            r.roles.push(role(k, meta::ROLE_NODE));
            // The defaults of rule 5: the first metric sizes the nodes and
            // the first category colours them.
            let kind = |f: &Field| match f.metadata().get(meta::ROLE).map(String::as_str) {
                Some(meta::ROLE_METRIC) => Some(OutputKind::Metric),
                Some(meta::ROLE_CATEGORY) => Some(OutputKind::Category),
                Some(_) => None,
                None => kind_by_name(f.name(), f.data_type()),
            };
            let others = || {
                schema
                    .fields()
                    .iter()
                    .enumerate()
                    .filter(|(i, _)| *i != k)
                    .map(|(i, f)| (i, f.as_ref()))
            };
            if let Some((i, _)) = others()
                .find(|(_, f)| kind(f) == Some(OutputKind::Metric) && f.data_type().is_numeric())
            {
                r.size = Some(i);
                r.roles.push(role(i, meta::ROLE_METRIC));
            }
            if let Some((i, _)) = others().find(|(_, f)| kind(f) == Some(OutputKind::Category)) {
                r.color = Some(i);
                r.roles.push(role(i, meta::ROLE_CATEGORY));
            }
            r
        }
        As::Path { nodes, edges } => {
            let n = find(nodes)?;
            if !is_id_list(schema.field(n).data_type()) {
                wrong(
                    n,
                    "a path's nodes are a list of ids (integers, strings or dates), as \
                     vertices(p) gives",
                )?;
            }
            let e = match edges {
                None => None,
                Some(edges) => {
                    let e = find(edges)?;
                    if !list_of(schema.field(e).data_type()).is_some_and(DataType::is_integer) {
                        wrong(
                            e,
                            "a path's edges are a list of the edges' row numbers, as edges(p) \
                             gives; leave it out to draw the nodes alone, AS path(nodes)",
                        )?;
                    }
                    Some(e)
                }
            };
            // element_id(p) is nodes and edges alternating.
            let elements = get(n, meta::ROLE).as_deref() == Some(meta::ROLE_PATH_ELEMENTS);
            let mut r = Reading::of(Shape::Path {
                nodes: (!elements).then_some(n),
                edges: e.filter(|_| !elements),
                elements: elements.then_some(n),
            });
            r.roles.push(role(
                n,
                if elements {
                    meta::ROLE_PATH_ELEMENTS
                } else {
                    meta::ROLE_PATH_NODES
                },
            ));
            if let Some(e) = e.filter(|_| !elements) {
                r.roles.push(role(e, meta::ROLE_PATH_EDGES));
                r.edge_label = get(e, meta::LABEL);
            }
            r
        }
        As::Hops {
            seed,
            id: key,
            distance,
        } => {
            let (s, k) = (id(seed, "a seed")?, id(key, "a node reached")?);
            let d = find(distance)?;
            if !schema.field(d).data_type().is_integer() {
                wrong(
                    d,
                    "a distance is a whole number of steps (CAST it to INTEGER if it is one)",
                )?;
            }
            let mut r = Reading::of(Shape::Hop {
                seed: s,
                id: k,
                distance: d,
            });
            r.roles = vec![
                role(s, meta::ROLE_SEED),
                role(k, meta::ROLE_NODE),
                role(d, meta::ROLE_DISTANCE),
            ];
            r
        }
    };
    // What the key columns say of themselves: their graph and their tables.
    let keys: Vec<usize> = match &r.shape {
        Shape::Edges { src, dst } => vec![*src, *dst],
        Shape::Nodes { id } => vec![*id],
        Shape::Path {
            nodes, elements, ..
        } => nodes.iter().chain(elements).copied().collect(),
        Shape::Hop { seed, id, .. } => vec![*seed, *id],
        Shape::Steps { .. } | Shape::Matches { .. } => Vec::new(),
    };
    for &k in &keys {
        if let Some(label) = get(k, meta::LABEL) {
            r.labels.push((k, label));
            r.marked = true;
        }
        if let Some(graph) = get(k, meta::GRAPH).filter(|_| !on) {
            match &r.graph {
                Some(other) if *other != graph => {
                    return plan_err(format!(
                        "{written}: the columns come from the graphs {other} and {graph}, and \
                         ids of two graphs do not meet. Name columns of one graph, or say \
                         which graph the ids belong to: ON {other}"
                    ))
                }
                _ => r.graph = Some(graph),
            }
        }
    }
    Ok(r)
}

/// The roles a node's key has.
const KEY_ROLES: [&str; 6] = [
    meta::ROLE_NODE,
    meta::ROLE_SRC,
    meta::ROLE_DST,
    meta::ROLE_SEED,
    meta::ROLE_PATH_NODES,
    meta::ROLE_PATH_ELEMENTS,
];

/// The reading of a result some of whose columns say what they are, or
/// `None` if they are not enough for a rule.
fn read_marked(schema: &Schema, cols: &[Col]) -> Result<Option<Reading>> {
    let first = |role: &str| cols.iter().find(|c| c.is(role));
    let key = |role: &str| cols.iter().find(|c| c.is(role) && c.is_id());
    let done = |mut r: Reading, used: &[&Col]| -> Result<Option<Reading>> {
        r.marked = true;
        // The graph of the columns the drawing reads; ids of two graphs do
        // not meet.
        let mut graphs: Vec<&str> = Vec::new();
        for c in used {
            if let Some(g) = c
                .graph
                .filter(|_| c.role.is_some_and(|r| KEY_ROLES.contains(&r)))
            {
                if !graphs.contains(&g) {
                    graphs.push(g);
                }
            }
            r.roles
                .push((c.name().to_string(), c.role.unwrap_or("").to_string()));
            if let (Some(label), true) = (c.label, c.role.is_some_and(|r| KEY_ROLES.contains(&r))) {
                r.labels.push((c.i, label.to_string()));
            }
        }
        if graphs.len() > 1 {
            return plan_err(format!(
                "cannot draw this result as one graph: its node columns come from the graphs \
                 {}, and ids of two graphs do not meet. Draw one graph's columns: .graph SELECT \
                 … FROM …",
                graphs.join(" and ")
            ));
        }
        r.graph = graphs.first().map(|g| g.to_string());
        Ok(Some(r))
    };
    let numbers = |skip: &[usize]| -> Vec<usize> {
        cols.iter()
            .filter(|c| !skip.contains(&c.i) && c.field.data_type().is_numeric())
            .map(|c| c.i)
            .take(MAX_CARRIED)
            .collect()
    };
    let endpoints = (key(meta::ROLE_SRC), key(meta::ROLE_DST));

    // Rule 1: a path column.
    let path_nodes = cols
        .iter()
        .find(|c| c.is(meta::ROLE_PATH_NODES) && is_id_list(c.field.data_type()));
    let path_elements = cols
        .iter()
        .find(|c| c.is(meta::ROLE_PATH_ELEMENTS) && is_id_list(c.field.data_type()));
    if let Some(anchor) = path_nodes.or(path_elements) {
        // The edges of the same path variable.
        let edges = cols.iter().find(|c| {
            c.is(meta::ROLE_PATH_EDGES)
                && c.var == anchor.var
                && list_of(c.field.data_type()).is_some_and(DataType::is_integer)
        });
        let elements = path_elements.filter(|c| c.var == anchor.var && path_nodes.is_none());
        let mut r = Reading::of(Shape::Path {
            nodes: path_nodes.map(|c| c.i),
            edges: edges.filter(|_| path_nodes.is_some()).map(|c| c.i),
            elements: elements.map(|c| c.i),
        });
        r.edge_label = edges.and_then(|c| c.label).map(str::to_string);
        if let (Some(s), Some(d)) = endpoints {
            r.notes.push(format!(
                "also edges ({}, {}): .graph AS edges({}, {})",
                s.name(),
                d.name(),
                s.name(),
                d.name()
            ));
        }
        let mut used = vec![anchor];
        used.extend(edges.filter(|_| path_nodes.is_some()));
        return done(r, &used);
    }
    // Rule 1 too: the steps of shortest_path(), in order.
    if let (Some(s), Some(d), Some(step)) = (
        endpoints.0,
        endpoints.1,
        first(meta::ROLE_STEP).filter(|c| c.field.data_type().is_integer()),
    ) {
        let mut r = Reading::of(Shape::Steps {
            src: s.i,
            dst: d.i,
            step: step.i,
        });
        r.attributes = numbers(&[s.i, d.i, step.i]);
        return done(r, &[s, d, step]);
    }
    // Rule 2: a hop result.
    let seed = key(meta::ROLE_SEED);
    let nodes: Vec<&Col> = cols
        .iter()
        .filter(|c| c.is(meta::ROLE_NODE) && c.is_id())
        .collect();
    if let (Some(seed), [id], Some(distance)) = (
        seed,
        nodes.as_slice(),
        first(meta::ROLE_DISTANCE).filter(|c| c.field.data_type().is_integer()),
    ) {
        let r = Reading::of(Shape::Hop {
            seed: seed.i,
            id: id.i,
            distance: distance.i,
        });
        return done(r, &[seed, id, distance]);
    }
    // Rule 3: endpoints by role.
    if let (Some(s), Some(d)) = endpoints {
        let mut r = Reading::of(Shape::Edges { src: s.i, dst: d.i });
        r.attributes = numbers(&[s.i, d.i]);
        r.edge_label = first(meta::ROLE_EDGE)
            .and_then(|c| c.label)
            .map(str::to_string);
        return done(r, &[s, d]);
    }
    // A hop result without its distance: two keys are two ends.
    if let (Some(seed), [id]) = (seed, nodes.as_slice()) {
        let mut r = Reading::of(Shape::Edges {
            src: seed.i,
            dst: id.i,
        });
        r.notes.push(format!(
            "no distance column, so no rings: each row is an edge {}–{}",
            seed.name(),
            id.name()
        ));
        return done(r, &[seed, id]);
    }
    // The columns that describe a key column's nodes: the properties of its
    // pattern variable.
    let properties = |keys: &[&Col]| -> Vec<(usize, usize)> {
        cols.iter()
            .filter(|c| c.is(meta::ROLE_PROPERTY) && c.var.is_some())
            .filter_map(|c| {
                let key = keys.iter().find(|k| k.var == c.var)?;
                Some((c.i, key.i))
            })
            .take(MAX_CARRIED)
            .collect()
    };
    match nodes.as_slice() {
        [] => Ok(None),
        // Rule 5: one node key.
        [id] => {
            let mut r = Reading::of(Shape::Nodes { id: id.i });
            let kind = |c: &Col| match c.role {
                Some(meta::ROLE_METRIC) => Some(OutputKind::Metric),
                Some(meta::ROLE_CATEGORY) => Some(OutputKind::Category),
                Some(_) => None,
                None => kind_by_name(c.name(), c.field.data_type()),
            };
            let others = || cols.iter().filter(|c| c.i != id.i);
            let size = others()
                .find(|c| kind(c) == Some(OutputKind::Metric) && c.field.data_type().is_numeric());
            let color = others().find(|c| kind(c) == Some(OutputKind::Category));
            r.size = size.map(|c| c.i);
            r.color = color.map(|c| c.i);
            let mut used = vec![*id];
            used.extend(size);
            used.extend(color);
            let mut r = done(r, &used)?.expect("a reading");
            // A column read by its name says so.
            for c in size.into_iter().chain(color) {
                if c.role.is_none() {
                    let role = if Some(c.i) == r.size {
                        meta::ROLE_METRIC
                    } else {
                        meta::ROLE_CATEGORY
                    };
                    if let Some(entry) = r.roles.iter_mut().find(|(n, _)| n == c.name()) {
                        entry.1 = role.to_string();
                    }
                }
            }
            Ok(Some(r))
        }
        // Rule 3: two node keys are two ends (two variables of a pattern, or
        // a self-join).
        [a, b] => {
            let mut r = Reading::of(Shape::Edges { src: a.i, dst: b.i });
            r.properties = properties(&[a, b]);
            let carried: Vec<usize> = r.properties.iter().map(|p| p.0).collect();
            r.attributes = numbers(&[a.i, b.i])
                .into_iter()
                .filter(|i| !carried.contains(i))
                .collect();
            let pattern = pattern_of(schema);
            r.edge_label = pattern
                .iter()
                .find(|e| {
                    let ends = (e.from.as_deref(), e.to.as_deref());
                    a.var.is_some() && (ends == (a.var, b.var) || ends == (b.var, a.var))
                })
                .map(|e| e.label.clone());
            done(r, &[a, b])
        }
        // Rule 4: three node variables or more.
        keys => {
            let pattern = pattern_of(schema);
            let at = |var: &Option<String>| {
                let var = var.as_deref()?;
                keys.iter().position(|k| k.var == Some(var))
            };
            let mut edges: Vec<MatchEdge> = Vec::new();
            for e in &pattern {
                if let (Some(from), Some(to)) = (at(&e.from), at(&e.to)) {
                    edges.push(MatchEdge {
                        from,
                        to,
                        directed: e.directed,
                        label: e.label.clone(),
                    });
                }
            }
            if edges.is_empty() {
                return Ok(None);
            }
            let mut r = Reading::of(Shape::Matches {
                keys: keys.iter().map(|k| k.i).collect(),
                edges,
            });
            r.properties = properties(keys);
            done(r, keys)
        }
    }
}

/// The edges of the `GRAPH_TABLE` pattern a result of `schema` matched
/// (`ursa.pattern`), or none.
fn pattern_of(schema: &Schema) -> Vec<meta::PatternEdge> {
    let Some(text) = schema.metadata().get(meta::PATTERN) else {
        return Vec::new();
    };
    let Ok(serde_json::Value::Array(edges)) = serde_json::from_str(text) else {
        return Vec::new();
    };
    edges
        .iter()
        .filter_map(|e| {
            let text = |key: &str| e.get(key).and_then(|v| v.as_str()).map(str::to_string);
            Some(meta::PatternEdge {
                from: text("from"),
                to: text("to"),
                directed: e.get("directed")?.as_bool()?,
                edge: text("edge"),
                label: text("label")?,
                quantifier: None,
                path: text("path"),
            })
        })
        .collect()
}

/// The reading of a result by its columns' names and types.
fn read_named(schema: &Schema, cols: &[Col]) -> Result<Reading> {
    let find = |name: &str| cols.iter().find(|c| c.name().eq_ignore_ascii_case(name));
    let id = |name: &str| find(name).filter(|c| c.is_id());
    let role = |c: &Col, role: &str| (c.name().to_string(), role.to_string());
    let weight = |r: &mut Reading| {
        r.attributes = find("weight")
            .filter(|c| c.field.data_type().is_numeric())
            .map(|c| c.i)
            .into_iter()
            .collect();
    };
    // Rule 3: endpoints by name. `src` and `dst` that are not ids are an
    // error, not another shape: the names say what was meant.
    if find("src").is_some() && find("dst").is_some() {
        let (s, d) = endpoint_columns(schema)?;
        let mut r = Reading::of(Shape::Edges { src: s, dst: d });
        r.roles = vec![
            role(&cols[s], meta::ROLE_SRC),
            role(&cols[d], meta::ROLE_DST),
        ];
        weight(&mut r);
        return Ok(r);
    }
    for (s, d) in [("source", "target"), ("from", "to")] {
        if let (Some(s), Some(d)) = (id(s), id(d)) {
            let mut r = Reading::of(Shape::Edges { src: s.i, dst: d.i });
            r.roles = vec![role(s, meta::ROLE_SRC), role(d, meta::ROLE_DST)];
            weight(&mut r);
            return Ok(r);
        }
    }
    // Rule 2: seed, id and distance together.
    let key = id("id").or_else(|| id("node"));
    if let (Some(seed), Some(key)) = (id("seed"), key) {
        let distance = find("distance").filter(|c| c.field.data_type().is_integer());
        let mut r = match distance {
            Some(distance) => {
                let mut r = Reading::of(Shape::Hop {
                    seed: seed.i,
                    id: key.i,
                    distance: distance.i,
                });
                r.roles.push(role(distance, meta::ROLE_DISTANCE));
                r
            }
            None => {
                let mut r = Reading::of(Shape::Edges {
                    src: seed.i,
                    dst: key.i,
                });
                r.notes.push(format!(
                    "no distance column, so no rings: each row is an edge {}–{}",
                    seed.name(),
                    key.name()
                ));
                r
            }
        };
        r.roles.insert(0, role(key, meta::ROLE_NODE));
        r.roles.insert(0, role(seed, meta::ROLE_SEED));
        return Ok(r);
    }
    // Rule 5: a node key by name.
    if let Some(key) = key {
        let mut r = Reading::of(Shape::Nodes { id: key.i });
        r.roles.push(role(key, meta::ROLE_NODE));
        let others = || cols.iter().filter(|c| c.i != key.i);
        let kind = |c: &Col| kind_by_name(c.name(), c.field.data_type());
        if let Some(c) = others().find(|c| kind(c) == Some(OutputKind::Metric)) {
            r.size = Some(c.i);
            r.roles.push(role(c, meta::ROLE_METRIC));
        }
        if let Some(c) = others().find(|c| kind(c) == Some(OutputKind::Category)) {
            r.color = Some(c.i);
            r.roles.push(role(c, meta::ROLE_CATEGORY));
        }
        if let Some(path) = others().find(|c| is_id_list(c.field.data_type())) {
            r.notes.push(format!(
                "also a path ({}): .graph AS path({})",
                path.name(),
                path.name()
            ));
        }
        return Ok(r);
    }
    // The first two columns, when both are ids of one kind, as ever.
    let path = cols.iter().find(|c| is_id_list(c.field.data_type()));
    if cols.len() >= 2 {
        match (endpoint_columns(schema), path) {
            (Ok((s, d)), _) => {
                let mut r = Reading::of(Shape::Edges { src: s, dst: d });
                r.roles = vec![
                    role(&cols[s], meta::ROLE_SRC),
                    role(&cols[d], meta::ROLE_DST),
                ];
                weight(&mut r);
                if let Some(path) = path {
                    r.notes.push(format!(
                        "also a path ({}): .graph AS path({})",
                        path.name(),
                        path.name()
                    ));
                }
                return Ok(r);
            }
            (Err(_), _) => {}
        }
    }
    // Rule 1: a list of ids is a path.
    if let Some(path) = path {
        let mut r = Reading::of(Shape::Path {
            nodes: Some(path.i),
            edges: None,
            elements: None,
        });
        r.roles.push(role(path, meta::ROLE_PATH_NODES));
        return Ok(r);
    }
    Err(not_a_graph(schema))
}

/// Rule 6: a result that is not a graph. The message names the columns and
/// their types, and the `AS` clause that would draw them.
fn not_a_graph(schema: &Schema) -> datafusion::error::DataFusionError {
    let columns: Vec<String> = schema
        .fields()
        .iter()
        .map(|f| format!("{}: {}", f.name(), f.data_type()))
        .collect();
    let ids: Vec<&str> = schema
        .fields()
        .iter()
        .filter(|f| id_kind(f.data_type()).is_some())
        .map(|f| f.name().as_str())
        .collect();
    let lists: Vec<&str> = schema
        .fields()
        .iter()
        .filter(|f| is_id_list(f.data_type()))
        .map(|f| f.name().as_str())
        .collect();
    let mut says = Vec::new();
    match ids.as_slice() {
        [] => {}
        [a] => says.push(format!("if {a} is a node's id: .graph AS nodes({a})")),
        [a, b, ..] => {
            says.push(format!(
                "if {a} and {b} are the two ends of an edge: .graph AS edges({a}, {b})"
            ));
            says.push(format!("if {a} is a node's id: .graph AS nodes({a})"));
        }
    }
    if let Some(l) = lists.first() {
        says.push(format!("if {l} is a path's nodes: .graph AS path({l})"));
    }
    let says = if says.is_empty() {
        "None of its columns can be a node's id (an integer, a string or a date). Draw a \
         property graph by name (.graph NAME), or a query whose rows are edges: .graph SELECT … \
         AS src, … AS dst FROM …"
            .to_string()
    } else {
        format!(
            "Say what its columns are: {}. Or name the ends in a query: .graph SELECT … AS src, \
             … AS dst FROM …",
            says.join("; ")
        )
    };
    let n = schema.fields().len();
    let detail = format!(
        "cannot draw this result as a graph: it has {n} {} ({}), which {} not edges (columns \
         src and dst, or two columns of ids), nodes (a column id), a hop result (seed, id, \
         distance) or paths (a list of ids). {says}",
        if n == 1 { "column" } else { "columns" },
        columns.join(", "),
        if n == 1 { "is" } else { "are" },
    );
    datafusion::error::DataFusionError::Plan(detail)
}

/// The kind of id a column of type `t` holds, if it can be a node's id: an
/// integer, a string or a date. A float, a boolean, a list or a struct is a
/// value, not an id (a score, a flag, a path), and drawing it as a node is
/// nonsense.
fn id_kind(t: &DataType) -> Option<&'static str> {
    match t {
        DataType::Dictionary(_, values) => id_kind(values),
        DataType::Decimal128(_, 0) | DataType::Decimal256(_, 0) => Some("integer"),
        DataType::Utf8 | DataType::LargeUtf8 | DataType::Utf8View => Some("string"),
        DataType::Date32 | DataType::Date64 | DataType::Timestamp(_, _) => Some("date"),
        t if t.is_integer() => Some("integer"),
        _ => None,
    }
}

/// What a column of type `t` holds, in a word, for the message.
fn what_is(t: &DataType) -> String {
    if let Some(element) = list_of(t) {
        return match id_kind(element) {
            Some(kind) => format!("a list of {kind}s"),
            None => "a list".to_string(),
        };
    }
    match id_kind(t) {
        Some("integer") => "an integer".to_string(),
        Some(kind) => format!("a {kind}"),
        None => match value_kind(t) {
            "not an id" => "a value that is no id or number".to_string(),
            kind => kind.to_string(),
        },
    }
}

/// What a column that is no id is, in a word, for the message.
fn value_kind(t: &DataType) -> &'static str {
    match t {
        DataType::Boolean => "a boolean",
        DataType::Struct(_) => "a struct",
        DataType::Map(_, _) => "a map",
        DataType::List(_)
        | DataType::LargeList(_)
        | DataType::FixedSizeList(_, _)
        | DataType::ListView(_)
        | DataType::LargeListView(_) => "a list",
        t if t.is_floating()
            || matches!(t, DataType::Decimal128(_, _) | DataType::Decimal256(_, _)) =>
        {
            "a number that is not whole"
        }
        _ => "not an id",
    }
}

/// The columns that hold each edge's endpoints: `src` and `dst` (in any
/// case), or else the first two. Either way both must hold ids: an integer, a
/// string or a date. The first two columns must also be ids of one kind, since
/// nothing but their position says they are a pair.
pub fn endpoint_columns(schema: &Schema) -> Result<(usize, usize)> {
    let n = schema.fields().len();
    if n < 2 {
        return plan_err(format!(
            "the result has {n} column{}; a graph needs two, each edge's source and destination \
             (columns named src and dst, or else the first two)",
            if n == 1 { "" } else { "s" }
        ));
    }
    let find = |name: &str| {
        schema
            .fields()
            .iter()
            .position(|f| f.name().eq_ignore_ascii_case(name))
    };
    let (s, d, named) = match (find("src"), find("dst")) {
        (Some(s), Some(d)) => (s, d, true),
        _ => (0, 1, false),
    };
    let (source, destination) = (schema.field(s), schema.field(d));
    let kinds = (
        id_kind(source.data_type()),
        id_kind(destination.data_type()),
    );
    let what = match kinds {
        (Some(a), Some(b)) if named || a == b => return Ok((s, d)),
        (Some(a), Some(b)) => format!(
            "'{}' holds {a} ids and '{}' holds {b} ids, so they are not the two ends of an edge",
            source.name(),
            destination.name()
        ),
        _ => [(source, kinds.0), (destination, kinds.1)]
            .iter()
            .filter(|(_, kind)| kind.is_none())
            .map(|(f, _)| {
                format!(
                    "'{}' is {} ({})",
                    f.name(),
                    value_kind(f.data_type()),
                    f.data_type()
                )
            })
            .collect::<Vec<_>>()
            .join(" and "),
    };
    // A column named after a per-node algorithm is that algorithm's result.
    let metric = schema
        .fields()
        .iter()
        .find_map(|f| per_node_algorithm(&f.name().to_ascii_lowercase()))
        .unwrap_or("pagerank");
    plan_err(format!(
        "cannot draw {} as edges: {what}. An edge's ends are node ids (integers, strings or \
         dates, both of one kind). To draw an algorithm's result, draw the graph and size or \
         colour it: .graph NAME SIZE BY {metric}. To draw other rows, name the ends: \
         .graph SELECT … AS src, … AS dst FROM …",
        if named {
            "columns src and dst".to_string()
        } else {
            format!(
                "the first two columns ({}: {}, {}: {})",
                source.name(),
                source.data_type(),
                destination.name(),
                destination.data_type()
            )
        }
    ))
}