Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/query_processing/src/expressions/functions/str_after.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ pub fn str_after(c: Column, s: String) -> Result<Column, PolarsError> {
if let Some(range_to) = range_to {
Some(&x[range_to + s.len()..])
} else {
Some(x)
// SPARQL 1.1 17.4.3.9/17.4.3.10: no occurrence gives the empty string
Some("")
}
} else {
None
Expand Down
3 changes: 2 additions & 1 deletion lib/query_processing/src/expressions/functions/str_before.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ pub fn str_before(c: Column, s: String) -> Result<Column, PolarsError> {
if let Some(range_to) = range_to {
Some(&x[0..range_to])
} else {
Some(x)
// SPARQL 1.1 17.4.3.9/17.4.3.10: no occurrence gives the empty string
Some("")
}
} else {
None
Expand Down
51 changes: 51 additions & 0 deletions py_maplib/tests/test_str_before_after.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import pytest

from maplib import Model

# SPARQL 1.1 17.4.3.9 (STRBEFORE) and 17.4.3.10 (STRAFTER):
# "If there is no such occurrence, an empty simple literal is returned."


@pytest.mark.parametrize(
"expression,expected",
[
('STRAFTER("abc", "b")', "c"),
('STRAFTER("abc", "xyz")', ""),
('STRAFTER("abc", "")', "abc"),
('STRBEFORE("abc", "b")', "a"),
('STRBEFORE("abc", "xyz")', ""),
('STRBEFORE("abc", "")', ""),
],
)
def test_str_before_after_simple_literal(expression, expected):
m = Model()
df = m.query(f"SELECT (STR({expression}) AS ?s) WHERE {{}}")
assert df["s"].to_list() == [expected]


@pytest.mark.parametrize(
"expression,expected",
[
('STRAFTER("abc"@en, "b")', "c"),
('STRAFTER("abc"@en, "z")', ""),
('STRBEFORE("abc"@en, "b")', "a"),
('STRBEFORE("abc"@en, "z")', ""),
],
)
def test_str_before_after_lang_string(expression, expected):
m = Model()
df = m.query(f"SELECT (STR({expression}) AS ?s) WHERE {{}}")
assert df["s"].to_list() == [expected]


def test_str_after_no_match_on_data():
m = Model()
m.insert(
"""PREFIX ex: <http://example.org/>
CONSTRUCT { ex:a ex:v "prefix_a" . ex:b ex:v "other_b" . } WHERE {}"""
)
df = m.query(
"""PREFIX ex: <http://example.org/>
SELECT ?x (STRAFTER(?v, "prefix_") AS ?s) WHERE { ?x ex:v ?v } ORDER BY ?x"""
)
assert df["s"].to_list() == ["a", ""]