diff --git a/src/validation/queries/common/preflight_missing_token_conformance.rq b/src/validation/queries/common/preflight_missing_token_conformance.rq index 695248d..52f9286 100644 --- a/src/validation/queries/common/preflight_missing_token_conformance.rq +++ b/src/validation/queries/common/preflight_missing_token_conformance.rq @@ -25,12 +25,22 @@ PREFIX vcfc: # two predicates above excluded, the HG005 cell still returned ten rows -- one # per Number=. INFO declaration in its header. Only the Number attribute is # excluded; a bare "." on any other attribute is still reported. + +# The dot filters sit in their own group so they run before the OPTIONAL. A +# group's FILTERs apply after its OPTIONAL, so with all of them in one group the +# left join ran over every triple in the graph. QLever absorbs that, but the +# COTTAS engine looks up each triple's subject: on a 10,000-record expanded +# graph (959k triples) the check took 1,766 s or hit the 1,800 s timeout, against +# about 37 s before the Number exclusion was added. Filtered first, the OPTIONAL +# sees only the handful of "." rows. SELECT ?s ?p ?o (DATATYPE(?o) AS ?datatype) WHERE { - ?s ?p ?o . - FILTER(ISLITERAL(?o) && STR(?o) = ".") - FILTER(DATATYPE(?o) != vcfc:Null) - FILTER(?p NOT IN (vcfc:fieldNumber, vcfc:genotypeString)) + { + ?s ?p ?o . + FILTER(ISLITERAL(?o) && STR(?o) = ".") + FILTER(DATATYPE(?o) != vcfc:Null) + FILTER(?p NOT IN (vcfc:fieldNumber, vcfc:genotypeString)) + } OPTIONAL { ?s vcfc:attributeKey ?attributeKey } FILTER(?p != vcfc:attributeValue || !BOUND(?attributeKey) || STR(?attributeKey) != "Number") } diff --git a/src/validation/queries/common/preflight_missing_token_conformance_count.rq b/src/validation/queries/common/preflight_missing_token_conformance_count.rq index 10d747d..656ac82 100644 --- a/src/validation/queries/common/preflight_missing_token_conformance_count.rq +++ b/src/validation/queries/common/preflight_missing_token_conformance_count.rq @@ -4,12 +4,22 @@ PREFIX vcfc: # The exclusions -- the two predicates and the header Number attribute -- must # match preflight_missing_token_conformance.rq exactly, or the count and its # sample describe different populations. + +# The dot filters sit in their own group so they run before the OPTIONAL. A +# group's FILTERs apply after its OPTIONAL, so with all of them in one group the +# left join ran over every triple in the graph. QLever absorbs that, but the +# COTTAS engine looks up each triple's subject: on a 10,000-record expanded +# graph (959k triples) the check took 1,766 s or hit the 1,800 s timeout, against +# about 37 s before the Number exclusion was added. Filtered first, the OPTIONAL +# sees only the handful of "." rows. SELECT (COUNT(*) AS ?anomalyCount) WHERE { - ?s ?p ?o . - FILTER(ISLITERAL(?o) && STR(?o) = ".") - FILTER(DATATYPE(?o) != vcfc:Null) - FILTER(?p NOT IN (vcfc:fieldNumber, vcfc:genotypeString)) + { + ?s ?p ?o . + FILTER(ISLITERAL(?o) && STR(?o) = ".") + FILTER(DATATYPE(?o) != vcfc:Null) + FILTER(?p NOT IN (vcfc:fieldNumber, vcfc:genotypeString)) + } OPTIONAL { ?s vcfc:attributeKey ?attributeKey } FILTER(?p != vcfc:attributeValue || !BOUND(?attributeKey) || STR(?attributeKey) != "Number") } diff --git a/test/test_missing_token_scope_unit.py b/test/test_missing_token_scope_unit.py index 5344d77..2141b2c 100644 --- a/test/test_missing_token_scope_unit.py +++ b/test/test_missing_token_scope_unit.py @@ -115,6 +115,41 @@ def test_a_bare_dot_elsewhere_is_still_reported(self): self.assertEqual(anomaly_count(graph), 2) +def left_joins(node) -> list: + """Every LeftJoin (OPTIONAL) in an rdflib SPARQL algebra tree.""" + found = [] + if getattr(node, "name", None) == "LeftJoin": + found.append(node) + if isinstance(node, dict): + for child in node.values(): + found += left_joins(child) + elif isinstance(node, (list, tuple)): + for child in node: + found += left_joins(child) + return found + + +@unittest.skipIf(rdflib is None, "rdflib is required to compile the queries") +class FilterBeforeOptionalTests(VerboseTestCase): + """The dot filters must narrow the rows before the OPTIONAL joins them. + + A group's FILTERs apply after its OPTIONAL. With the filters and the OPTIONAL in one + group, the left join ran over every triple in the graph: harmless on QLever, but the + COTTAS engine looks up each triple's subject. On a 10,000-record expanded graph (959k + triples) the check then took 1,766 s or hit the 1,800 s query timeout in v3.3.3, + against about 37 s in v3.1.0, before the Number exclusion was added. With the filters + in an inner group it takes about 31 s. + """ + + def test_the_optional_joins_only_filtered_rows(self): + from rdflib.plugins.sparql import prepareQuery + + for path in (SAMPLE_QUERY, COUNT_QUERY): + joins = left_joins(prepareQuery(path.read_text(encoding="utf-8")).algebra) + self.assertEqual(len(joins), 1, path.name) + self.assertEqual(joins[0].p1.name, "Filter", path.name) + + class ExclusionTextTests(VerboseTestCase): """Static guarantees that hold without rdflib."""