aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Baumgartner <dev@igsor.net>2023-03-02 12:23:49 +0100
committerMatthias Baumgartner <dev@igsor.net>2023-03-02 12:23:49 +0100
commitcd27775b406482b11f44575ab196501a30d9b075 (patch)
treee515a7a6c754c9f714e73516bdcb7ed0003c3a03
parent87f437380c1dd8f420437cddc028c0f3174ee1c9 (diff)
downloadbsfs-cd27775b406482b11f44575ab196501a30d9b075.tar.gz
bsfs-cd27775b406482b11f44575ab196501a30d9b075.tar.bz2
bsfs-cd27775b406482b11f44575ab196501a30d9b075.zip
default sort order in sparql backend
-rw-r--r--bsfs/triple_store/sparql/utils.py3
-rw-r--r--test/triple_store/sparql/test_utils.py6
2 files changed, 5 insertions, 4 deletions
diff --git a/bsfs/triple_store/sparql/utils.py b/bsfs/triple_store/sparql/utils.py
index deca4d8..51de893 100644
--- a/bsfs/triple_store/sparql/utils.py
+++ b/bsfs/triple_store/sparql/utils.py
@@ -127,11 +127,12 @@ class Query():
"""Return an executable sparql query."""
select = ' '.join(f'({head} as ?{name})' for head, name in self.select)
return f'''
- SELECT {self.root_head} {select}
+ SELECT DISTINCT {self.root_head} {select}
WHERE {{
{self.root_head} <{ns.rdf.type}>/<{ns.rdfs.subClassOf}>* <{self.root_type}> .
{self.where}
}}
+ ORDER BY str({self.root_head})
'''
def __call__(self, graph: rdflib.Graph) -> rdflib.query.Result:
diff --git a/test/triple_store/sparql/test_utils.py b/test/triple_store/sparql/test_utils.py
index 073b8f8..edcf6d7 100644
--- a/test/triple_store/sparql/test_utils.py
+++ b/test/triple_store/sparql/test_utils.py
@@ -118,13 +118,13 @@ class TestQuery(unittest.TestCase):
return value
# query composes a valid query
q = Query(self.root_type, self.root_head, self.select, self.where)
- self.assertEqual(normalize(q.query), normalize(f'select ?root (?head as ?name) where {{ ?root <{ns.rdf.type}>/<{ns.rdfs.subClassOf}>* <http://bsfs.ai/schema/Entity> . ?root <{ns.bse.tag}> ?head }}'))
+ self.assertEqual(normalize(q.query), normalize(f'select distinct ?root (?head as ?name) where {{ ?root <{ns.rdf.type}>/<{ns.rdfs.subClassOf}>* <http://bsfs.ai/schema/Entity> . ?root <{ns.bse.tag}> ?head }} order by str(?root)'))
# select and where are optional
q = Query(self.root_type, self.root_head)
- self.assertEqual(normalize(q.query), normalize(f'select ?root where {{ ?root <{ns.rdf.type}>/<{ns.rdfs.subClassOf}>* <http://bsfs.ai/schema/Entity> . }}'))
+ self.assertEqual(normalize(q.query), normalize(f'select distinct ?root where {{ ?root <{ns.rdf.type}>/<{ns.rdfs.subClassOf}>* <http://bsfs.ai/schema/Entity> . }} order by str(?root)'))
# select and where need not to correspond
q = Query(self.root_type, self.root_head, (('?head', 'name'), ))
- self.assertEqual(normalize(q.query), normalize(f'select ?root (?head as ?name) where {{ ?root <{ns.rdf.type}>/<{ns.rdfs.subClassOf}>* <http://bsfs.ai/schema/Entity> . }}'))
+ self.assertEqual(normalize(q.query), normalize(f'select distinct ?root (?head as ?name) where {{ ?root <{ns.rdf.type}>/<{ns.rdfs.subClassOf}>* <http://bsfs.ai/schema/Entity> . }} order by str(?root)'))
# query is used for string representation
self.assertEqual(str(q), q.query)