Skip to content

perf(executor): IF and COALESCE on term ids (#11) - #172

Merged
MathiasVDA merged 4 commits into
mainfrom
claude/project-thread-n3oul3-2
Nov 22, 2026
Merged

MathiasVDA merged 4 commits into
mainfrom
claude/project-thread-n3oul3-2

Conversation

@MathiasVDA

@MathiasVDA MathiasVDA commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

What and why

#11: under --executor ids, a BIND with IF or COALESCE sent the whole query to the evaluator. These are common in rule sets (COALESCE(?x, 0) after an OPTIONAL, IF(?q > 50, "big", "small")). They now run on term ids, with the evaluator's answers.

  • Conditions: the new idvalue::condition covers:
    • comparisons of numbers and dates (!= is !(a = b));
    • !, and && / || with SPARQL's errors (an error and false is false, an error or true is true);
    • BOUND.
  • IF computes only the branch it takes, as the evaluator does, so a branch that would give up only does so on the rows that take it. An error in the condition leaves the value unbound.
  • COALESCE gives the first argument that is not an error.
  • Where each one runs:
    • With branches that are numbers, they are value kernels. That means no bare variable, and constants only in the form the kernels write. They reach inline ids directly and also work inside arithmetic and filters.
    • Otherwise they are term kernels in idstring. There the branches are any term those kernels make, including variables, IRIs and strings.
  • A branch is the term it gives, now that perf(executor): rounding of doubles and numeric casts on term ids (#11) #162 is in main:
    • An xsd:decimal cast in a branch keeps its text (7 of an integer, 1500.0 of a double). idvalue::compile_output now recurses through IF and COALESCE.
    • A constant branch is only a value when its text is the form the kernels write. So "2.50"^^xsd:decimal and "007"^^xsd:integer go to the term kernels, which copy the constant.
    • A cast nested in arithmetic (ABS(xsd:decimal(?x))) is a computed decimal (7.0), as in the evaluator.
  • direct::computable, idstring::computable and idtree::value_expression accept them, so a query that can't run on ids is found before the patterns run.

How it was tested

  • tests/id_rows.rs BINDS has 16 new cases, each compared with the evaluator:

    • number and string branches;
    • &&, ||, !, BOUND, and conditions on unbound values from OPTIONAL;
    • COALESCE of an unbound variable, of a product, of ?x / 0, and of a string or STR;
    • IF inside a FILTER;
    • casts and constants in branches, and a cast nested in ABS, * 1 and STR(IF(…));
    • cases left to the evaluator (an IRI compared in the condition, a quotient of 24 digits, SUM(IF(…))).
  • Oracle with ids as the default, after merging main: all suites pass, apart from the two known plan-shape assertions in bind_keyed_lookup.

  • 133k orders, result cache off, same answers:

    query spareval ids
    BIND(IF(?q > 50, "big", "small") AS ?k) 67 ms 35 ms
    BIND(IF(?q > 50, 1, 0) AS ?k) 76 ms 41 ms
    OPTIONAL { ?o ex:qty ?q } BIND(COALESCE(?q * 2, 0) AS ?t) 173 ms 46 ms
    OPTIONAL { ?o ex:discount ?q } BIND(COALESCE(?q, 0) AS ?t) 94 ms 32 ms
  • cargo fmt --check and cargo clippy -p tripletrail --all-targets -D warnings pass.

Two gaps showed up that this PR does not change; both are for a follow-up.

  • GROUP BY over a computed key is slower on ids. BIND(IF(?q > 50, 1, 0) AS ?k) … GROUP BY ?k takes 20 ms against 12, and the same holds for ?q * 2 or ABS(?q) today. The evaluator's plan counts per ?q first and computes the BIND only on the distinct values.
  • An OPTIONAL with a FILTER inside it still goes to the evaluator.

Documentation

  • docs/store.md: the id executor's kernels.
  • CHANGELOG: a line under Unreleased / Changed, next to the division entry.

Docs checked:

  • In the repo: docs/store.md and CHANGELOG are updated. The guide's --executor ids section in 13-production.md lists no functions and stays true. The README doesn't describe the kernels.
  • On the website: tripletrail-website has no page on the executor or these functions, so it needs no change.

Main, with #162, is merged in.

A product of decimals with more than 18 digits after the point gave
no value (oxsdatatypes refuses it). It is now the exact product, as
Jena's BigDecimal product: a decimal when it fits 18 fraction digits,
else the literal with all its digits, as a long quotient is.
…-n3oul3-2

# Conflicts:
#	CHANGELOG.md
#	crates/tripletrail/tests/id_rows.rs
#	docs/store.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant