diff --git a/lib/src/sparql/eval.rs b/lib/src/sparql/eval.rs index 9c85fca3..b4acac0b 100644 --- a/lib/src/sparql/eval.rs +++ b/lib/src/sparql/eval.rs @@ -2567,7 +2567,21 @@ fn cmp_terms(dataset: &DatasetView, a: Option<&EncodedTerm>, b: Option<&EncodedT a => match b { _ if b.is_named_node() || b.is_blank_node() => Ordering::Greater, _ if b.is_triple() => Ordering::Less, - b => partial_cmp_literals(dataset, a, b).unwrap_or(Ordering::Equal), + b => { + if let Some(ord) = partial_cmp_literals(dataset, a, b) { + ord + } else if let (Ok(Term::Literal(a)), Ok(Term::Literal(b))) = + (dataset.decode_term(a), dataset.decode_term(b)) + { + (a.value(), a.datatype(), a.language()).cmp(&( + b.value(), + b.datatype(), + b.language(), + )) + } else { + Ordering::Equal // Should never happen + } + } }, }, (Some(_), None) => Ordering::Greater, diff --git a/testsuite/oxigraph-tests/sparql/manifest.ttl b/testsuite/oxigraph-tests/sparql/manifest.ttl index 9bfb1476..9da0d1ba 100644 --- a/testsuite/oxigraph-tests/sparql/manifest.ttl +++ b/testsuite/oxigraph-tests/sparql/manifest.ttl @@ -25,6 +25,7 @@ :halloween_problem :nested_path :nested_expression + :order_terms ) . :small_unicode_escape_with_multibytes_char rdf:type mf:NegativeSyntaxTest ; @@ -108,3 +109,8 @@ :nested_expression rdf:type mf:PositiveSyntaxTest11 ; mf:name "A very nested expression" ; mf:action . + +:order_terms rdf:type mf:QueryEvaluationTest ; + mf:name "literals have an order for ORDER BY" ; + mf:action [ qt:query ] ; + mf:result . diff --git a/testsuite/oxigraph-tests/sparql/order_terms.rq b/testsuite/oxigraph-tests/sparql/order_terms.rq new file mode 100644 index 00000000..ada68021 --- /dev/null +++ b/testsuite/oxigraph-tests/sparql/order_terms.rq @@ -0,0 +1,3 @@ +SELECT ?o WHERE { + VALUES ?o { 4.0 "v" "u"@en "a" "d"@de "c"@en "b" "foo"^^ 1 "2021-30-30"^^ "2020-30-29T00:00:00"^^ d } +} ORDER BY ?o \ No newline at end of file diff --git a/testsuite/oxigraph-tests/sparql/order_terms.ttl b/testsuite/oxigraph-tests/sparql/order_terms.ttl new file mode 100644 index 00000000..cc3f81c4 --- /dev/null +++ b/testsuite/oxigraph-tests/sparql/order_terms.ttl @@ -0,0 +1,66 @@ +@prefix rdf: . +@prefix rs: . + +[] rdf:type rs:ResultSet ; + rs:resultVariable + "o" ; + rs:solution [ rs:binding [ rs:value -1e3 ; + rs:variable "o" + ] ; + rs:index 1 + ] ; + rs:solution [ rs:binding [ rs:value 1 ; + rs:variable "o" + ] ; + rs:index 2 + ] ; + rs:solution [ rs:binding [ rs:value "2020-30-29T00:00:00"^^ ; + rs:variable "o" + ] ; + rs:index 3 + ] ; + rs:solution [ rs:binding [ rs:value "2021-30-30"^^ ; + rs:variable "o" + ] ; + rs:index 4 + ] ; + rs:solution [ rs:binding [ rs:value 4.0 ; + rs:variable "o" + ] ; + rs:index 5 + ] ; + rs:solution [ rs:binding [ rs:value "a" ; + rs:variable "o" + ] ; + rs:index 6 + ] ; + rs:solution [ rs:binding [ rs:value "b" ; + rs:variable "o" + ] ; + rs:index 7 + ] ; + rs:solution [ rs:binding [ rs:value "c"@en ; + rs:variable "o" + ] ; + rs:index 8 + ] ; + rs:solution [ rs:binding [ rs:value "d"@de ; + rs:variable "o" + ] ; + rs:index 9 + ] ; + rs:solution [ rs:binding [ rs:value "foo"^^ ; + rs:variable "o" + ] ; + rs:index 10 + ] ; + rs:solution [ rs:binding [ rs:value "u"@en ; + rs:variable "o" + ] ; + rs:index 11 + ] ; + rs:solution [ rs:binding [ rs:value "v" ; + rs:variable "o" + ] ; + rs:index 12 + ] .