UNPKG

@jahed/sparql-engine

Version:

SPARQL query engine for servers and web browsers.

25 lines (23 loc) 771 B
// SPDX-License-Identifier: MIT import { cloneDeep, partition } from "lodash-es"; import type { Pattern, UnionPattern } from "sparqljs"; import PlanVisitor from "../plan-visitor.ts"; /** * Implements the UNION Merge rule: all SPARQL UNION clauses in the same group pattern * should be merged as one single UNION clause. */ export default class UnionMerge extends PlanVisitor { visitUnion(node: UnionPattern): UnionPattern { const newNode = cloneDeep(node); const parts = partition( newNode.patterns, (group) => group.type === "union" ); const singleUnion = parts[0].reduce<Pattern[]>( (acc, c) => acc.concat(c.patterns), [] ); newNode.patterns = (parts[1] as Pattern[]).concat(singleUnion); return newNode; } }