UNPKG

@jahed/sparql-engine

Version:

SPARQL query engine for servers and web browsers.

32 lines 1.34 kB
import { Pipeline } from "../engine/pipeline/pipeline.js"; import ExecutionContext from "../engine/context/execution-context.js"; import { PlanBuilder } from "../engine/plan-builder.js"; import { Bindings } from "../rdf/bindings.js"; /** * Handles an SPARQL OPTIONAL clause * @see {@link https://www.w3.org/TR/sparql11-query/#optionals} * @param source - Input {@link PipelineStage} * @param patterns - OPTIONAL clause, i.e., a SPARQL group pattern * @param builder - Instance of the current {@link PlanBuilder} * @param context - Execution context * @return A {@link PipelineStage} which evaluate the OPTIONAL operation */ export default async function optional(source, patterns, builder, context) { const seenBefore = []; const engine = Pipeline.getInstance(); const start = engine.tap(source, (bindings) => { seenBefore.push(bindings); }); let leftOp = await builder._buildWhere(start, patterns, context); leftOp = engine.tap(leftOp, (bindings) => { // remove values that matches a results from seenBefore const index = seenBefore.findIndex((b) => { return b.isSubset(bindings); }); if (index >= 0) { seenBefore.splice(index, 1); } }); return engine.merge(leftOp, engine.from(seenBefore)); } //# sourceMappingURL=optional.js.map