oxirs-arq 0.4.0

Jena-style SPARQL algebra with extension points and query optimization
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
//! # QueryParser - parsing Methods
//!
//! This module contains method implementations for `QueryParser`.
//!
//! 🤖 Generated with [SplitRS](https://github.com/cool-japan/splitrs)

use crate::algebra::{Algebra, Expression, Literal, TriplePattern, UnaryOperator, Variable};
use crate::update::{GraphReference, QuadPattern, UpdateOperation};
use anyhow::{bail, Result};
use oxirs_core::model::NamedNode;
use std::collections::HashMap;

use super::types::{Token, UpdateRequest};

use super::queryparser_type::QueryParser;

/// Validate the argument count of a SPARQL 1.1 built-in call at parse time, so a
/// malformed call (e.g. `REGEX(?x)`, `IF(?a, ?b)`) surfaces as a client parse
/// error (4xx) rather than failing deep in execution. `name` is the canonical
/// lower-case built-in name produced by `builtin_call_name`.
fn validate_builtin_arity(name: &str, argc: usize) -> Result<()> {
    let ok = match name {
        // No-argument built-ins.
        "now" | "rand" | "uuid" | "struuid" => argc == 0,
        // Zero or one argument.
        "bnode" => argc <= 1,
        // Exactly one argument.
        "str" | "lang" | "datatype" | "bound" | "iri" | "uri" | "abs" | "ceil" | "floor"
        | "round" | "strlen" | "ucase" | "lcase" | "encode_for_uri" | "year" | "month" | "day"
        | "hours" | "minutes" | "seconds" | "timezone" | "tz" | "md5" | "sha1" | "sha256"
        | "sha384" | "sha512" | "isiri" | "isuri" | "isblank" | "isliteral" | "isnumeric" => {
            argc == 1
        }
        // Exactly two arguments.
        "langmatches" | "contains" | "strstarts" | "strends" | "strbefore" | "strafter"
        | "strlang" | "strdt" | "sameterm" => argc == 2,
        // Two or three arguments.
        "regex" | "substr" => (2..=3).contains(&argc),
        // Exactly three arguments.
        "if" => argc == 3,
        // Three or four arguments.
        "replace" => (3..=4).contains(&argc),
        // At least one argument.
        "coalesce" => argc >= 1,
        // Variadic (zero or more): CONCAT.
        "concat" => true,
        // Any name not in the table imposes no arity constraint here.
        _ => true,
    };
    if ok {
        Ok(())
    } else {
        bail!("built-in {name} called with wrong number of arguments ({argc})")
    }
}

/// Take the single argument of a validated unary built-in. Arity is checked by
/// [`validate_builtin_arity`] before this is reached, so `args` holds exactly
/// one element; the empty-string fallback keeps the parser total without an
/// `unwrap`.
fn pop_single_arg(args: Vec<Expression>) -> Expression {
    args.into_iter()
        .next()
        .unwrap_or(Expression::Literal(Literal {
            value: String::new(),
            language: None,
            datatype: None,
        }))
}

impl QueryParser {
    pub(super) fn parse_additive_expression(&mut self) -> Result<Expression> {
        let mut expr = self.parse_multiplicative_expression()?;
        while let Some(op) = self.match_additive_operator() {
            let right = self.parse_multiplicative_expression()?;
            expr = Expression::Binary {
                op,
                left: Box::new(expr),
                right: Box::new(right),
            };
        }
        Ok(expr)
    }
    pub(super) fn parse_multiplicative_expression(&mut self) -> Result<Expression> {
        let mut expr = self.parse_unary_expression()?;
        while let Some(op) = self.match_multiplicative_operator() {
            let right = self.parse_unary_expression()?;
            expr = Expression::Binary {
                op,
                left: Box::new(expr),
                right: Box::new(right),
            };
        }
        Ok(expr)
    }
    pub(super) fn parse_unary_expression(&mut self) -> Result<Expression> {
        if let Some(op) = self.match_unary_operator() {
            let expr = self.parse_unary_expression()?;
            Ok(Expression::Unary {
                op,
                operand: Box::new(expr),
            })
        } else {
            self.parse_primary_expression()
        }
    }
    pub(super) fn parse_primary_expression(&mut self) -> Result<Expression> {
        match self.peek() {
            Some(Token::Variable(var)) => {
                let var = var.clone();
                self.advance();
                Ok(Expression::Variable(Variable::new(var)?))
            }
            Some(Token::Iri(iri)) => {
                let iri = iri.clone();
                self.advance();
                Ok(Expression::Iri(NamedNode::new_unchecked(iri)))
            }
            Some(Token::StringLiteral(value)) | Some(Token::NumericLiteral(value)) => {
                let value = value.clone();
                self.advance();
                Ok(Expression::Literal(Literal {
                    value,
                    language: None,
                    datatype: None,
                }))
            }
            Some(Token::RdfLiteral {
                value,
                language,
                datatype,
            }) => {
                // A language-tagged or explicitly-typed literal in an expression,
                // e.g. `FILTER(?l = "hokkaido"@ja)` or `?x = "1"^^xsd:integer`.
                let value = value.clone();
                let language = language.clone();
                let datatype = datatype.clone();
                self.advance();
                let datatype = match datatype {
                    Some(raw) => Some(self.resolve_datatype(&raw)?),
                    None => None,
                };
                Ok(Expression::Literal(Literal {
                    value,
                    language,
                    datatype,
                }))
            }
            Some(Token::BooleanLiteral(value)) => {
                let value = *value;
                self.advance();
                Ok(Expression::Literal(Literal {
                    value: value.to_string(),
                    language: None,
                    datatype: None,
                }))
            }
            Some(Token::LeftParen) => {
                self.advance();
                let expr = self.parse_expression()?;
                self.expect_token(Token::RightParen)?;
                Ok(expr)
            }
            Some(Token::BuiltIn(name)) => {
                let name = name.clone();
                self.advance();
                self.parse_builtin_call(&name)
            }
            Some(Token::PrefixedName(prefix, local)) => {
                let prefix = prefix.clone();
                let local = local.clone();
                let name = format!("{prefix}:{local}");
                self.advance();
                if self.match_token(&Token::LeftParen) {
                    let mut args = Vec::new();
                    // `COUNT(*)` in an expression context (e.g. `HAVING (COUNT(*)
                    // > 1)`): the star is the count-all form, carried as an empty
                    // argument list. It is only accepted when it is the sole
                    // token before `)`, so `SUM(?a * ?b)` (a multiplication) is
                    // untouched.
                    if matches!(self.peek(), Some(Token::Star) | Some(Token::Multiply))
                        && matches!(self.tokens.get(self.position + 1), Some(Token::RightParen))
                    {
                        self.advance(); // `*`
                        self.advance(); // `)`
                        return Ok(Expression::Function { name, args });
                    }
                    while !self.match_token(&Token::RightParen) {
                        args.push(self.parse_expression()?);
                        if !self.match_token(&Token::Comma) {
                            self.expect_token(Token::RightParen)?;
                            break;
                        }
                    }
                    Ok(Expression::Function { name, args })
                } else {
                    let full_iri = if let Some(base) = self.prefixes.get(&prefix) {
                        format!("{base}{local}")
                    } else {
                        name
                    };
                    Ok(Expression::Iri(NamedNode::new_unchecked(full_iri)))
                }
            }
            // `EXISTS { GroupGraphPattern }` as a filter expression. `NOT EXISTS`
            // is `NOT` (a unary operator) applied to this, i.e. `!EXISTS { … }`,
            // and needs no separate arm.
            Some(Token::Exists) => {
                self.advance();
                self.expect_token(Token::LeftBrace)?;
                let pattern = self.parse_group_graph_pattern()?;
                self.expect_token(Token::RightBrace)?;
                Ok(Expression::Exists(Box::new(pattern)))
            }
            _ => bail!("Expected primary expression"),
        }
    }
    /// Parse a SPARQL 1.1 `BuiltInCall` whose name token has already been
    /// consumed. `name` is the canonical lower-case built-in name.
    ///
    /// The argument list is parsed with the ordinary expression grammar, its
    /// arity is validated, and the call is lowered to the AST shape the
    /// evaluator expects: the type-check predicates and `BOUND` become dedicated
    /// [`Expression`] variants (`Unary` / `Bound`), `IF` becomes `Conditional`,
    /// and every other built-in becomes an `Expression::Function` keyed by its
    /// canonical name (matching the evaluator's function table).
    pub(super) fn parse_builtin_call(&mut self, name: &str) -> Result<Expression> {
        self.expect_token(Token::LeftParen)?;
        let mut args = Vec::new();
        // A built-in with no arguments closes immediately, e.g. `NOW()`.
        if !self.match_token(&Token::RightParen) {
            loop {
                args.push(self.parse_expression()?);
                if self.match_token(&Token::Comma) {
                    continue;
                }
                self.expect_token(Token::RightParen)?;
                break;
            }
        }
        validate_builtin_arity(name, args.len())?;

        // Lower to the dedicated AST variant when one exists, so the evaluator
        // reaches its native handler rather than the generic function table.
        match name {
            "isiri" | "isuri" => Ok(Expression::Unary {
                op: UnaryOperator::IsIri,
                operand: Box::new(pop_single_arg(args)),
            }),
            "isblank" => Ok(Expression::Unary {
                op: UnaryOperator::IsBlank,
                operand: Box::new(pop_single_arg(args)),
            }),
            "isliteral" => Ok(Expression::Unary {
                op: UnaryOperator::IsLiteral,
                operand: Box::new(pop_single_arg(args)),
            }),
            "isnumeric" => Ok(Expression::Unary {
                op: UnaryOperator::IsNumeric,
                operand: Box::new(pop_single_arg(args)),
            }),
            "bound" => match pop_single_arg(args) {
                Expression::Variable(var) => Ok(Expression::Bound(var)),
                _ => bail!("BOUND requires a variable argument"),
            },
            "if" => {
                let mut it = args.into_iter();
                let condition = Box::new(it.next().unwrap_or(Expression::Literal(Literal {
                    value: "false".to_string(),
                    language: None,
                    datatype: None,
                })));
                let then_expr = Box::new(it.next().unwrap_or(Expression::Literal(Literal {
                    value: String::new(),
                    language: None,
                    datatype: None,
                })));
                let else_expr = Box::new(it.next().unwrap_or(Expression::Literal(Literal {
                    value: String::new(),
                    language: None,
                    datatype: None,
                })));
                Ok(Expression::Conditional {
                    condition,
                    then_expr,
                    else_expr,
                })
            }
            _ => Ok(Expression::Function {
                name: name.to_string(),
                args,
            }),
        }
    }
    pub(super) fn parse_construct_template(&mut self) -> Result<Vec<TriplePattern>> {
        let mut triples = Vec::new();
        while !self.is_at_end() && !matches!(self.peek(), Some(Token::RightBrace)) {
            self.skip_whitespace_and_newlines();
            if matches!(self.peek(), Some(Token::RightBrace)) {
                break;
            }
            triples.extend(self.parse_triples_same_subject()?);
            if !self.match_token(&Token::Dot) {
                break;
            }
        }
        Ok(triples)
    }
    pub(super) fn expect_variable(&mut self) -> Result<Variable> {
        if let Some(Token::Variable(var)) = self.peek() {
            let var = var.clone();
            self.advance();
            Ok(Variable::new(var)?)
        } else {
            bail!("Expected variable")
        }
    }
    /// Parse UPDATE request with multiple operations
    pub(super) fn parse_update_request(&mut self) -> Result<UpdateRequest> {
        let mut update_request = UpdateRequest {
            operations: Vec::new(),
            prefixes: HashMap::new(),
            base_iri: None,
        };
        self.skip_whitespace();
        while let Some(token) = self.peek() {
            match token {
                Token::Prefix => {
                    self.advance();
                    let prefix = self.expect_prefixed_name()?.0;
                    let iri = self.expect_iri()?;
                    update_request.prefixes.insert(prefix.clone(), iri.clone());
                    self.prefixes.insert(prefix, iri);
                }
                Token::Base => {
                    self.advance();
                    let iri = self.expect_iri()?;
                    update_request.base_iri = Some(iri.clone());
                    self.base_iri = Some(iri);
                }
                _ => break,
            }
        }
        while !self.is_at_end() {
            self.skip_whitespace();
            let operation = match self.peek() {
                Some(Token::Insert) => self.parse_insert_operation()?,
                Some(Token::Delete) => self.parse_delete_operation()?,
                Some(Token::Clear) => self.parse_clear_operation()?,
                Some(Token::Drop) => self.parse_drop_operation()?,
                Some(Token::Create) => self.parse_create_operation()?,
                Some(Token::Load) => self.parse_load_operation()?,
                Some(Token::Copy) => self.parse_copy_operation()?,
                Some(Token::Move) => self.parse_move_operation()?,
                Some(Token::Add) => self.parse_add_operation()?,
                Some(Token::With) => {
                    self.advance();
                    let graph_iri = self.expect_iri()?;
                    let graph_ref = GraphReference::Iri(graph_iri);
                    let mut operation = match self.peek() {
                        Some(Token::Insert) => self.parse_insert_operation()?,
                        Some(Token::Delete) => self.parse_delete_operation()?,
                        _ => bail!("Expected INSERT or DELETE after WITH clause"),
                    };
                    match &mut operation {
                        UpdateOperation::DeleteInsertWhere { using, .. } if using.is_none() => {
                            *using = Some(vec![graph_ref]);
                        }
                        UpdateOperation::InsertWhere { template, .. } => {
                            for quad in template {
                                if quad.graph.is_none() {
                                    quad.graph = Some(graph_ref.clone());
                                }
                            }
                        }
                        UpdateOperation::DeleteWhere { .. } => {}
                        _ => {}
                    }
                    operation
                }
                Some(Token::Eof) => break,
                _ => bail!("Expected UPDATE operation"),
            };
            update_request.operations.push(operation);
            self.match_token(&Token::Semicolon);
            self.skip_whitespace();
        }
        Ok(update_request)
    }
    /// Parse INSERT WHERE operation
    pub(super) fn parse_insert_where(&mut self) -> Result<UpdateOperation> {
        self.expect_token(Token::LeftBrace)?;
        let template = self.parse_quad_pattern_data()?;
        self.expect_token(Token::RightBrace)?;
        self.expect_token(Token::Where)?;
        self.expect_token(Token::LeftBrace)?;
        let where_clause = self.parse_group_graph_pattern()?;
        self.expect_token(Token::RightBrace)?;
        Ok(UpdateOperation::InsertWhere {
            pattern: Box::new(where_clause),
            template,
        })
    }
    /// Parse DELETE WHERE operation
    pub(super) fn parse_delete_where(&mut self) -> Result<UpdateOperation> {
        self.expect_token(Token::Where)?;
        self.expect_token(Token::LeftBrace)?;
        let patterns = self.parse_quad_pattern_data()?;
        self.expect_token(Token::RightBrace)?;
        let triple_patterns: Vec<TriplePattern> = patterns
            .into_iter()
            .map(|qp| TriplePattern::new(qp.subject, qp.predicate, qp.object))
            .collect();
        Ok(UpdateOperation::DeleteWhere {
            pattern: Box::new(Algebra::Bgp(triple_patterns)),
        })
    }
    /// Parse DELETE ... INSERT ... WHERE operation
    pub(super) fn parse_delete_insert_where(&mut self) -> Result<UpdateOperation> {
        self.expect_token(Token::LeftBrace)?;
        let delete_patterns = self.parse_quad_pattern_data()?;
        self.expect_token(Token::RightBrace)?;
        let insert_patterns = if self.match_token(&Token::Insert) {
            self.expect_token(Token::LeftBrace)?;
            let patterns = self.parse_quad_pattern_data()?;
            self.expect_token(Token::RightBrace)?;
            Some(patterns)
        } else {
            None
        };
        self.expect_token(Token::Where)?;
        self.expect_token(Token::LeftBrace)?;
        let where_clause = self.parse_group_graph_pattern()?;
        self.expect_token(Token::RightBrace)?;
        if let Some(insert_patterns) = insert_patterns {
            Ok(UpdateOperation::DeleteInsertWhere {
                delete_template: delete_patterns,
                insert_template: insert_patterns,
                pattern: Box::new(where_clause),
                using: None,
            })
        } else {
            let triple_patterns: Vec<TriplePattern> = delete_patterns
                .into_iter()
                .map(|qp| TriplePattern::new(qp.subject, qp.predicate, qp.object))
                .collect();
            Ok(UpdateOperation::DeleteWhere {
                pattern: Box::new(Algebra::Bgp(triple_patterns)),
            })
        }
    }
    /// Parse quad data for INSERT/DELETE DATA
    pub(super) fn parse_quad_data(&mut self) -> Result<Vec<QuadPattern>> {
        let mut quads = Vec::new();
        while !self.is_at_end() && !matches!(self.peek(), Some(Token::RightBrace)) {
            let quad = self.parse_quad()?;
            quads.push(quad);
            self.match_token(&Token::Dot);
        }
        Ok(quads)
    }
}