From 103e8a5b554a540199e0872d6f388ccb70a002bc Mon Sep 17 00:00:00 2001 From: ecrum19 Date: Mon, 30 Nov 2026 10:00:39 +0100 Subject: [PATCH] Filter the missing-token preflight before its OPTIONAL preflight_missing_token_conformance and its count query look for plain "." literals that should have been typed vcfc:Null. Since f43ec15 (v3.3.0) they also skip a header's own Number=. value, through OPTIONAL { ?s vcfc:attributeKey ?attributeKey }. That OPTIONAL sat in the same group as the dot filters, and a group's FILTERs apply after its OPTIONAL, so the left join ran over every triple in the graph. QLever absorbs that, but the COTTAS engine looks up each triple's subject. In the v3.3.3 rerun of the core benchmark's validation runs, the check took 1,766 s or hit the 1,800 s query timeout on COTTAS on 10,000-record expanded graphs (959k triples), against about 37 s in v3.1.0. Six runs were reported as MISMATCH although every query answer agreed. The dot filters now sit in an inner group, so the OPTIONAL only sees the few rows they keep. The results are unchanged. On the same COTTAS artifact and endpoint, the count and sample queries take about 31 s (count 0), where the old count query had not finished after 600 s. A new test compiles both queries with rdflib and checks that the OPTIONAL's left side is the filtered pattern, not the bare triple scan. It fails on the old form. Co-Authored-By: Claude Opus 5.5 --- .../preflight_missing_token_conformance.rq | 18 +++++++--- ...eflight_missing_token_conformance_count.rq | 18 +++++++--- test/test_missing_token_scope_unit.py | 35 +++++++++++++++++++ 3 files changed, 63 insertions(+), 8 deletions(-) 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."""