rete-core 0.3.4

Core format types for the Rete cloud-native RDF graph file.
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
//! Query diagnostics: a side channel for the expression **type errors** that
//! SPARQL silently resolves: a FILTER or HAVING drops the row (an error stays an
//! error through `!`), BIND, projection and ORDER BY see an unbound value.
//!
//! `FILTER(CONTAINS(?iri, "x"))` is not unsupported — it is a type error,
//! because CONTAINS takes string literals and an IRI is not one, and the spec
//! says a FILTER whose expression errors drops the row. That is correct and it
//! stays: results are never changed by this module. What changes is that the error is
//! *counted*, so a query that came back empty can say why.
//!
//! Recording happens only on the error path (and, for the case-sensitivity
//! hint, once per function per query on the no-match path), into a
//! thread-local sink — SPARQL evaluation is single-threaded, and a
//! thread-local keeps two queries running on two server threads from
//! mixing their warnings. The sink is bounded: one entry per
//! `(function, argument position, argument kind)`, holding a count and the
//! first offending value.

use std::cell::RefCell;

use super::{ArithOp, Builtin, FExpr, Op, QueryOutput};

/// Where a type error was raised: a built-in function, or an operator
/// (`+ - * /`, a comparison, `IN`), which SPARQL defines as functions too
/// (`op:numeric-add`, `op:numeric-equal`, …) but which have no call syntax.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Site {
    Fn(Builtin),
    /// The operator's SPARQL spelling: `+`, `<=`, `IN`, …
    Op(&'static str),
}

impl From<Builtin> for Site {
    fn from(f: Builtin) -> Self {
        Site::Fn(f)
    }
}

impl Site {
    pub(crate) fn arith(op: ArithOp) -> Site {
        Site::Op(match op {
            ArithOp::Add => "+",
            ArithOp::Sub => "-",
            ArithOp::Mul => "*",
            ArithOp::Div => "/",
        })
    }

    pub(crate) fn compare(op: Op) -> Site {
        Site::Op(match op {
            Op::Eq => "=",
            Op::Ne => "!=",
            Op::Lt => "<",
            Op::Le => "<=",
            Op::Gt => ">",
            Op::Ge => ">=",
        })
    }

    fn name(self) -> &'static str {
        match self {
            Site::Fn(f) => fn_name(f),
            Site::Op(sym) => sym,
        }
    }

    /// The family a site belongs to, which decides the hint.
    fn family(self) -> Family {
        match self {
            Site::Fn(Builtin::Abs | Builtin::Ceil | Builtin::Floor | Builtin::Round) => {
                Family::Numeric
            }
            Site::Op("+" | "-" | "*" | "/") => Family::Numeric,
            Site::Op(_) => Family::Compare,
            Site::Fn(Builtin::Lang | Builtin::Datatype) => Family::LiteralAccessor,
            Site::Fn(Builtin::StrDt | Builtin::StrLang) => Family::Constructor,
            Site::Fn(_) => Family::Other,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Family {
    Numeric,
    Compare,
    LiteralAccessor,
    Constructor,
    Other,
}

/// How serious a [`QueryWarning`] is.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum WarningSeverity {
    /// A SPARQL type error was raised and silently absorbed (FILTER drops the
    /// row, BIND → unbound). The results are spec-correct; the query probably is not
    /// what its author meant.
    TypeError,
    /// No error happened; a guess at why a query came back empty (for example,
    /// string matching is case-sensitive). Phrased as a suggestion.
    Hint,
}

impl WarningSeverity {
    /// `"type-error"` or `"hint"` — the JSON spelling.
    pub fn as_str(self) -> &'static str {
        match self {
            WarningSeverity::TypeError => "type-error",
            WarningSeverity::Hint => "hint",
        }
    }
}

/// One diagnostic about a query evaluation: a counted class of type errors, or
/// a hint. Returned by [`eval_query_with_warnings`](super::eval_query_with_warnings).
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct QueryWarning {
    pub severity: WarningSeverity,
    /// The SPARQL function name, upper-case (`"CONTAINS"`), or the operator's
    /// spelling for an operator: `+` `-` `*` `/`, `=` `!=` `<` `<=` `>` `>=`,
    /// `IN`.
    pub function: String,
    /// 1-based argument position the problem is about (0 = not argument-specific).
    /// For an operator, 1 is the left operand and 2 the right one.
    pub argument: usize,
    /// What the argument was: `iri`, `blank-node`, `quoted-triple`, `numeric`,
    /// `typed-literal`, `string`, `language-tagged`, `invalid-number`,
    /// `language-mismatch`, `unbound`, `invalid-regex`, `not-a-datetime`,
    /// `division-by-zero` (argument 2 of `/`), or `case-sensitive` for the
    /// hint.
    pub kind: String,
    /// How many evaluations raised this error (an evaluation is one row reaching
    /// the expression; LIMIT / ASK stop early, so this is not a data count).
    pub count: u64,
    /// The first offending value, truncated to 80 characters (plus `…`).
    pub sample: Option<String>,
    /// A short, actionable suggestion.
    pub hint: String,
    /// The whole warning as one plain-language sentence.
    pub message: String,
}

/// Samples are cut to this many characters (plus `…`).
const SAMPLE_CHARS: usize = 80;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum ArgKind {
    Iri,
    Blank,
    QuotedTriple,
    Numeric,
    TypedLiteral,
    LangMismatch,
    Unbound,
    InvalidRegex,
    NotDatetime,
    /// A string literal (plain or `xsd:string`) where a number, a literal
    /// accessor's operand or a simple literal was needed.
    StringLit,
    /// A language-tagged literal where a simple literal was needed.
    LangTagged,
    /// A literal of a numeric datatype whose lexical form is not a number.
    InvalidNumber,
    /// The divisor of `/` was zero.
    DivByZero,
}

impl ArgKind {
    fn as_str(self) -> &'static str {
        match self {
            ArgKind::Iri => "iri",
            ArgKind::Blank => "blank-node",
            ArgKind::QuotedTriple => "quoted-triple",
            ArgKind::Numeric => "numeric",
            ArgKind::TypedLiteral => "typed-literal",
            ArgKind::LangMismatch => "language-mismatch",
            ArgKind::Unbound => "unbound",
            ArgKind::InvalidRegex => "invalid-regex",
            ArgKind::NotDatetime => "not-a-datetime",
            ArgKind::StringLit => "string",
            ArgKind::LangTagged => "language-tagged",
            ArgKind::InvalidNumber => "invalid-number",
            ArgKind::DivByZero => "division-by-zero",
        }
    }
}

struct Entry {
    site: Site,
    pos: usize,
    kind: ArgKind,
    count: u64,
    sample: String,
    /// Kind-specific detail: a datatype, the two language tags, a regex error.
    detail: String,
}

/// Bits of the case-sensitivity probe ([`note_no_match`]).
pub(crate) const MISS_CONTAINS: u8 = 1;
pub(crate) const MISS_STRSTARTS: u8 = 2;
pub(crate) const MISS_STRENDS: u8 = 4;
pub(crate) const MISS_REGEX: u8 = 8;

#[derive(Default)]
struct Sink {
    entries: Vec<Entry>,
    /// Constant needles of string predicates that returned `false` on valid
    /// string arguments at least once: `(function, needle)`.
    misses: Vec<(Builtin, String)>,
}

thread_local! {
    static SINK: RefCell<Sink> = RefCell::new(Sink::default());
}

/// Start a fresh diagnostic scope (drops anything a previous query left).
pub(crate) fn begin() {
    SINK.with(|s| {
        let mut s = s.borrow_mut();
        s.entries.clear();
        s.misses.clear();
    });
}

/// Record one type error. `describe` builds `(sample, detail)` and runs only
/// for the first occurrence of a `(func, pos, kind)` key.
#[cold]
#[inline(never)]
pub(crate) fn record(
    site: impl Into<Site>,
    pos: usize,
    kind: ArgKind,
    describe: impl FnOnce() -> (String, String),
) {
    let site = site.into();
    SINK.with(|s| {
        let mut s = s.borrow_mut();
        if let Some(e) = s
            .entries
            .iter_mut()
            .find(|e| e.site == site && e.pos == pos && e.kind == kind)
        {
            e.count += 1;
            return;
        }
        let (sample, detail) = describe();
        s.entries.push(Entry {
            site,
            pos,
            kind,
            count: 1,
            sample: truncate(&sample),
            detail,
        });
    });
}

/// Record that a string predicate returned no match on valid string
/// arguments. Called once per function per query (the caller keeps a bit in
/// `Ctx`); the needle is kept only when it is a constant in the query.
#[cold]
#[inline(never)]
pub(crate) fn note_no_match(func: Builtin, needle: &FExpr) {
    if let FExpr::Const(c) = needle {
        let lex = crate::terms::literal_lexical(c).unwrap_or_else(|| c.clone());
        SINK.with(|s| s.borrow_mut().misses.push((func, lex)));
    }
}

/// A string function rejected the bound term `token` at 1-based `pos`.
#[cold]
#[inline(never)]
pub(crate) fn bad_arg(func: Builtin, pos: usize, token: &str) {
    record(func, pos, kind_of(token), || {
        (token.to_string(), detail_of(token))
    });
}

/// Both arguments of a binary string function are strings, but argument 2
/// carries a language tag that argument 1 does not share (§17.4.3).
#[cold]
#[inline(never)]
pub(crate) fn lang_mismatch(func: Builtin, text: &str, pat: &str) {
    record(func, 2, ArgKind::LangMismatch, || {
        let tag = |t: &str| match crate::terms::lang_tag(t).filter(|l| !l.is_empty()) {
            Some(l) => format!("@{l}"),
            None => "no tag".to_string(),
        };
        (pat.to_string(), format!("{} vs {}", tag(pat), tag(text)))
    });
}

/// The argument at 1-based `pos` is the variable `var`, unbound in this row.
#[cold]
#[inline(never)]
pub(crate) fn unbound(site: impl Into<Site>, pos: usize, var: &str) {
    record(site, pos, ArgKind::Unbound, || {
        (format!("?{var}"), String::new())
    });
}

/// The REGEX / REPLACE pattern does not compile.
#[cold]
#[inline(never)]
pub(crate) fn invalid_regex(func: Builtin, pattern: &str, flags: &str) {
    record(func, 2, ArgKind::InvalidRegex, || {
        (
            pattern.to_string(),
            crate::row::Resolver::regex_error(pattern, flags),
        )
    });
}

/// A date/time accessor got a value that does not parse as an xsd:dateTime.
#[cold]
#[inline(never)]
pub(crate) fn bad_datetime(func: Builtin, token: &str) {
    record(func, 1, ArgKind::NotDatetime, || {
        let detail = match crate::terms::literal_datatype(token) {
            Some(dt) if token.starts_with('"') => short_dt(&dt),
            _ => kind_of(token).as_str().to_string(),
        };
        (token.to_string(), detail)
    });
}

/// A numeric function or an arithmetic operator got a term that is not a
/// number (by the rule that site applies: arithmetic takes numeric-typed
/// literals only, ABS / CEIL / FLOOR / ROUND any literal whose text parses).
#[cold]
#[inline(never)]
pub(crate) fn not_a_number(site: impl Into<Site>, pos: usize, token: &str) {
    let kind = match term_kind(token) {
        ArgKind::Numeric => ArgKind::InvalidNumber,
        k => k,
    };
    record(site, pos, kind, || {
        (token.to_string(), detail_of_any(token))
    });
}

/// `/` with a zero divisor (`token` is the divisor as written).
#[cold]
#[inline(never)]
pub(crate) fn div_by_zero(token: &str) {
    record(Site::Op("/"), 2, ArgKind::DivByZero, || {
        (token.to_string(), detail_of(token))
    });
}

/// LANG / DATATYPE got a term that is not a literal.
#[cold]
#[inline(never)]
pub(crate) fn not_a_literal(func: Builtin, token: &str) {
    record(func, 1, kind_of(token), || {
        (token.to_string(), String::new())
    });
}

/// STRDT / STRLANG argument 1 is not a simple literal, or STRDT argument 2 is
/// not an IRI.
#[cold]
#[inline(never)]
pub(crate) fn bad_constructor_arg(func: Builtin, pos: usize, token: &str) {
    record(func, pos, term_kind(token), || {
        (token.to_string(), detail_of_any(token))
    });
}

/// Classify any term, telling string and language-tagged literals apart from
/// other typed ones (no allocation for IRIs and blank nodes).
fn term_kind(token: &str) -> ArgKind {
    if !token.starts_with('"') {
        return kind_of(token);
    }
    if crate::terms::lang_tag(token).is_some_and(|l| !l.is_empty()) {
        return ArgKind::LangTagged;
    }
    match crate::terms::literal_datatype(token) {
        Some(dt) if dt == "http://www.w3.org/2001/XMLSchema#string" => ArgKind::StringLit,
        None => ArgKind::StringLit,
        Some(dt) if is_numeric_datatype(&dt) => ArgKind::Numeric,
        Some(_) => ArgKind::TypedLiteral,
    }
}

/// [`detail_of`], but a language-tagged literal reports its tag (`@en`).
fn detail_of_any(token: &str) -> String {
    match crate::terms::lang_tag(token).filter(|l| !l.is_empty()) {
        Some(l) if token.starts_with('"') => format!("@{l}"),
        _ => detail_of(token),
    }
}

/// Classify a bound term that a string function rejected (no allocation for
/// IRIs and blank nodes, the common case).
fn kind_of(token: &str) -> ArgKind {
    if token.starts_with("<<") {
        ArgKind::QuotedTriple
    } else if token.starts_with('<') {
        ArgKind::Iri
    } else if token.starts_with("_:") {
        ArgKind::Blank
    } else if crate::terms::literal_datatype(token).is_some_and(|dt| is_numeric_datatype(&dt)) {
        ArgKind::Numeric
    } else {
        ArgKind::TypedLiteral
    }
}

/// The short datatype of a literal (`xsd:integer`), else `""`.
fn detail_of(token: &str) -> String {
    match crate::terms::literal_datatype(token) {
        Some(dt) if token.starts_with('"') => short_dt(&dt),
        _ => String::new(),
    }
}

fn is_numeric_datatype(dt: &str) -> bool {
    dt.strip_prefix("http://www.w3.org/2001/XMLSchema#")
        .is_some_and(|local| {
            matches!(
                local,
                "integer"
                    | "decimal"
                    | "float"
                    | "double"
                    | "int"
                    | "long"
                    | "short"
                    | "byte"
                    | "nonNegativeInteger"
                    | "nonPositiveInteger"
                    | "negativeInteger"
                    | "positiveInteger"
                    | "unsignedInt"
                    | "unsignedLong"
                    | "unsignedShort"
                    | "unsignedByte"
            )
        })
}

fn short_dt(dt: &str) -> String {
    match dt.strip_prefix("http://www.w3.org/2001/XMLSchema#") {
        Some(local) => format!("xsd:{local}"),
        None => format!("<{dt}>"),
    }
}

fn truncate(s: &str) -> String {
    match s.char_indices().nth(SAMPLE_CHARS) {
        Some((i, _)) => format!("{}…", &s[..i]),
        None => s.to_string(),
    }
}

/// SPARQL spelling of a builtin, for messages.
pub(crate) fn fn_name(f: Builtin) -> &'static str {
    match f {
        Builtin::Str => "STR",
        Builtin::StrLen => "STRLEN",
        Builtin::UCase => "UCASE",
        Builtin::LCase => "LCASE",
        Builtin::Concat => "CONCAT",
        Builtin::SubStr => "SUBSTR",
        Builtin::StrBefore => "STRBEFORE",
        Builtin::StrAfter => "STRAFTER",
        Builtin::Contains => "CONTAINS",
        Builtin::StrStarts => "STRSTARTS",
        Builtin::StrEnds => "STRENDS",
        Builtin::Regex => "REGEX",
        Builtin::Replace => "REPLACE",
        Builtin::EncodeForUri => "ENCODE_FOR_URI",
        Builtin::Md5 => "MD5",
        Builtin::Sha1 => "SHA1",
        Builtin::Sha256 => "SHA256",
        Builtin::Sha384 => "SHA384",
        Builtin::Sha512 => "SHA512",
        Builtin::Year => "YEAR",
        Builtin::Month => "MONTH",
        Builtin::Day => "DAY",
        Builtin::Hours => "HOURS",
        Builtin::Minutes => "MINUTES",
        Builtin::Seconds => "SECONDS",
        Builtin::Timezone => "TIMEZONE",
        Builtin::Tz => "TZ",
        Builtin::Abs => "ABS",
        Builtin::Ceil => "CEIL",
        Builtin::Floor => "FLOOR",
        Builtin::Round => "ROUND",
        Builtin::Lang => "LANG",
        Builtin::Datatype => "DATATYPE",
        Builtin::StrDt => "STRDT",
        Builtin::StrLang => "STRLANG",
        _ => "function",
    }
}

fn ordinal(site: Site, pos: usize) -> String {
    match (site, pos) {
        (Site::Op(_), 1) => "the left operand".to_string(),
        (Site::Op(_), 2) => "the right operand".to_string(),
        _ => format!("argument {pos}"),
    }
}

fn rows(n: u64) -> String {
    if n == 1 {
        "1 row".to_string()
    } else {
        format!("{n} rows")
    }
}

/// The hint for a site family other than the string functions, or `None` to
/// keep the kind's own hint (which was written for the string functions).
fn family_hint(family: Family, pos: usize, kind: ArgKind) -> Option<&'static str> {
    use ArgKind::*;
    Some(match (family, kind) {
        (_, Unbound | InvalidRegex | NotDatetime | LangMismatch | DivByZero) => return None,
        (Family::Numeric, StringLit) => {
            "a string is not a number; cast one that holds a number first, e.g. \
             xsd:decimal(?x), or restrict the variable with isNumeric()"
        }
        (Family::Numeric, InvalidNumber) => {
            "its text is not a valid number for its datatype; restrict the variable with \
             isNumeric(), which checks the lexical form"
        }
        (Family::Numeric, _) => "it is not a number; restrict the variable with isNumeric()",
        (Family::LiteralAccessor, _) => {
            "LANG() and DATATYPE() take a literal; restrict the variable with isLiteral()"
        }
        (Family::Constructor, Blank | QuotedTriple) if pos == 1 => {
            "argument 1 must be a simple literal; restrict the variable with isLiteral()"
        }
        (Family::Constructor, _) if pos == 1 => {
            "argument 1 must be a simple literal (no language tag, no datatype); use STR() on \
             it to take its lexical form"
        }
        (Family::Constructor, _) => {
            "argument 2 of STRDT must be a datatype IRI, e.g. xsd:integer; IRI(?x) turns a \
             string into one"
        }
        (Family::Compare | Family::Other, _) => return None,
    })
}

impl Entry {
    fn into_warning(self) -> QueryWarning {
        let name = self.site.name();
        let arg = ordinal(self.site, self.pos);
        let n = rows(self.count);
        let dt = |what: &str| {
            if self.detail.is_empty() {
                what.to_string()
            } else {
                format!("{what} ({})", self.detail)
            }
        };
        let (what, hint) = match self.kind {
            ArgKind::Iri => (
                "an IRI".to_string(),
                "wrap it in STR() to match the IRI's text".to_string(),
            ),
            ArgKind::Blank => (
                "a blank node".to_string(),
                "blank nodes are not strings; restrict the variable with isLiteral()".to_string(),
            ),
            ArgKind::QuotedTriple => (
                "a quoted triple".to_string(),
                "quoted triples are not strings; restrict the variable with isLiteral()"
                    .to_string(),
            ),
            ArgKind::Numeric => (
                dt("a number"),
                "wrap it in STR() to match its lexical form".to_string(),
            ),
            ArgKind::TypedLiteral => (
                dt("a non-string typed literal"),
                "wrap it in STR() to match its lexical form".to_string(),
            ),
            ArgKind::StringLit => (
                "a string".to_string(),
                "wrap it in STR() to match its lexical form".to_string(),
            ),
            ArgKind::LangTagged => (
                dt("a language-tagged literal"),
                "use STR() on it to drop the language tag".to_string(),
            ),
            ArgKind::InvalidNumber => (
                dt("a literal whose text is not a valid number for its datatype"),
                String::new(),
            ),
            ArgKind::LangMismatch => (
                format!(
                    "a literal whose language tag does not match argument 1 ({})",
                    self.detail
                ),
                "the language tags differ; use STR() on both arguments".to_string(),
            ),
            ArgKind::Unbound => (
                format!("an unbound variable ({})", self.sample),
                "the variable has no value in those rows (an OPTIONAL that did not match, \
                 or a misspelled variable); use BOUND() or COALESCE() if that is intended"
                    .to_string(),
            ),
            ArgKind::InvalidRegex => (
                "an invalid regex pattern".to_string(),
                format!(
                    "invalid regex pattern ({}); rete uses Rust regex syntax, which has no \
                     look-around or back-references",
                    self.detail
                ),
            ),
            ArgKind::NotDatetime => (
                dt("a value that is not an xsd:dateTime"),
                "this function needs an xsd:dateTime; for a year in a plain or xsd:date value \
                 use SUBSTR(STR(?x), 1, 4)"
                    .to_string(),
            ),
            ArgKind::DivByZero => (
                "zero".to_string(),
                "guard the divisor, e.g. IF(?d != 0, ?n / ?d, 0), or FILTER(?d != 0) first"
                    .to_string(),
            ),
        };
        let hint = match family_hint(self.site.family(), self.pos, self.kind) {
            Some(h) => h.to_string(),
            None => hint,
        };
        let mut message = match (self.kind, self.site) {
            (ArgKind::DivByZero, _) => format!("{name} divided by zero in {n}: a SPARQL error"),
            (_, Site::Op(_)) => {
                format!("Operator {name} received {what} as {arg} in {n}: a SPARQL type error")
            }
            _ => format!("{name} received {what} as {arg} in {n}: a SPARQL type error"),
        };
        message.push_str(
            ", which makes a FILTER drop the row, even under ! (BIND leaves the variable unbound)",
        );
        if !matches!(self.kind, ArgKind::Unbound | ArgKind::DivByZero) && !self.sample.is_empty() {
            message.push_str(&format!("; e.g. {}", self.sample));
        }
        message.push_str(&format!(". Hint: {hint}."));
        QueryWarning {
            severity: WarningSeverity::TypeError,
            function: name.to_string(),
            argument: self.pos,
            kind: self.kind.as_str().to_string(),
            count: self.count,
            sample: (!self.sample.is_empty()).then_some(self.sample),
            hint,
            message,
        }
    }
}

fn is_empty_result(out: &QueryOutput) -> bool {
    match out {
        QueryOutput::Select(_, rows) => rows.is_empty(),
        QueryOutput::Ask(b) => !*b,
        QueryOutput::Construct(t) => t.is_empty(),
    }
}

/// Close the scope opened by [`begin`]: turn the sink into warnings for `out`.
pub(crate) fn finish(out: &QueryOutput) -> Vec<QueryWarning> {
    let (entries, misses) = SINK.with(|s| {
        let mut s = s.borrow_mut();
        (
            std::mem::take(&mut s.entries),
            std::mem::take(&mut s.misses),
        )
    });
    let errored = !entries.is_empty();
    let mut out_w: Vec<QueryWarning> = entries.into_iter().map(Entry::into_warning).collect();
    // Case-sensitivity hint: only for an empty result, only when no type
    // error offers a better explanation, only for a constant needle that has
    // letters with case, and only when the predicate actually ran on string
    // values and said no. It is a suggestion, never an error.
    if !errored && is_empty_result(out) {
        let mut seen: Vec<(Builtin, String)> = Vec::new();
        for (func, needle) in misses {
            if needle.to_lowercase() == needle.to_uppercase()
                || seen.iter().any(|(f, n)| *f == func && *n == needle)
            {
                continue;
            }
            seen.push((func, needle.clone()));
            let name = fn_name(func);
            let hint = if func == Builtin::Regex {
                format!("add the \"i\" flag: REGEX(?x, \"{needle}\", \"i\")")
            } else {
                format!(
                    "compare case-folded: {name}(LCASE(?x), \"{}\")",
                    needle.to_lowercase()
                )
            };
            out_w.push(QueryWarning {
                severity: WarningSeverity::Hint,
                function: name.to_string(),
                argument: 2,
                kind: "case-sensitive".to_string(),
                count: 0,
                sample: Some(truncate(&needle)),
                message: format!(
                    "No results. {name} ran on string values but never matched \"{}\"; \
                     string matching is case-sensitive. If you expected matches, {hint}.",
                    truncate(&needle)
                ),
                hint,
            });
        }
    }
    out_w
}

/// The warnings as a JSON array (`[]` when empty), in the field order of
/// [`QueryWarning`] with `argKind` for `kind`.
pub fn warnings_json(warnings: &[QueryWarning]) -> String {
    use crate::results::push_json_string;
    let mut s = String::from("[");
    for (i, w) in warnings.iter().enumerate() {
        if i > 0 {
            s.push(',');
        }
        s.push_str(r#"{"severity":"#);
        push_json_string(&mut s, w.severity.as_str());
        s.push_str(r#","function":"#);
        push_json_string(&mut s, &w.function);
        s.push_str(&format!(r#","argument":{}"#, w.argument));
        s.push_str(r#","argKind":"#);
        push_json_string(&mut s, &w.kind);
        s.push_str(&format!(r#","count":{}"#, w.count));
        s.push_str(r#","sample":"#);
        match &w.sample {
            Some(v) => push_json_string(&mut s, v),
            None => s.push_str("null"),
        }
        s.push_str(r#","hint":"#);
        push_json_string(&mut s, &w.hint);
        s.push_str(r#","message":"#);
        push_json_string(&mut s, &w.message);
        s.push('}');
    }
    s.push(']');
    s
}