@lde/search-pipeline 0.41.0 → 0.41.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -24,7 +24,9 @@ export interface ExtractionOptions {
24
24
  * - **WHERE**: one UNION branch per field (`?root <path> ?value`), the field’s
25
25
  * `path` embedded as a literal SPARQL property path (sequence / alternative /
26
26
  * inverse). UNION per branch, never a conjunction – conjoining every path in
27
- * one BGP cross-multiplies the multi-valued ones (~4× inflation).
27
+ * one BGP cross-multiplies the multi-valued ones (~4× inflation). Fields
28
+ * whose path is a single predicate share one branch instead, reading them
29
+ * through a `VALUES` table ({@link mergePlainReads}).
28
30
  * - **roots**: the subject variable is left free, to be bound by the pipeline’s
29
31
  * `injectValues` (`VALUES ?root { … }`). Root *selection* is a separate,
30
32
  * deployment concern (`selectByClass` / the stage’s `itemSelector`); this
@@ -47,7 +49,7 @@ export interface ExtractionOptions {
47
49
  * referent’s key field, emitted under the **target’s** alias for it – so the
48
50
  * projection can store the referent’s document key rather than its node IRI.
49
51
  * `OPTIONAL`, so a referent with no key candidate keeps its row. The root side
50
- * needs nothing: a key field is a declared field, so its own branch and
52
+ * needs nothing: a key field is a declared field, so its own read and
51
53
  * template triple are already there.
52
54
  * - **references naming several targets**: the referent may be of any of them,
53
55
  * so its branch also reads the referent’s `rdf:type` (`OPTIONAL`, emitted
@@ -1 +1 @@
1
- {"version":3,"file":"extraction.d.ts","sourceRoot":"","sources":["../src/extraction.ts"],"names":[],"mappings":"AAAA,OAAO,EAKL,KAAK,cAAc,EAIpB,MAAM,2BAA2B,CAAC;AAYnC,OAAO,KAAK,EAGV,YAAY,EACZ,UAAU,EACX,MAAM,aAAa,CAAC;AAQrB,2CAA2C;AAC3C,MAAM,WAAW,iBAAiB;IAChC;;;;;;;OAOG;IACH,QAAQ,CAAC,eAAe,CAAC,EAAE,MAAM,CAAC;CACnC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8CG;AACH,wBAAgB,eAAe,CAC7B,UAAU,EAAE,UAAU,EACtB,MAAM,EAAE,YAAY,EACpB,OAAO,GAAE,iBAAsB,GAC9B,cAAc,CAqBhB;AAED,+EAA+E;AAC/E,wBAAgB,qBAAqB,CACnC,UAAU,EAAE,UAAU,EACtB,MAAM,EAAE,YAAY,EACpB,OAAO,CAAC,EAAE,iBAAiB,GAC1B,MAAM,CAER"}
1
+ {"version":3,"file":"extraction.d.ts","sourceRoot":"","sources":["../src/extraction.ts"],"names":[],"mappings":"AAAA,OAAO,EAKL,KAAK,cAAc,EAKpB,MAAM,2BAA2B,CAAC;AAYnC,OAAO,KAAK,EAGV,YAAY,EACZ,UAAU,EACX,MAAM,aAAa,CAAC;AAQrB,2CAA2C;AAC3C,MAAM,WAAW,iBAAiB;IAChC;;;;;;;OAOG;IACH,QAAQ,CAAC,eAAe,CAAC,EAAE,MAAM,CAAC;CACnC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgDG;AACH,wBAAgB,eAAe,CAC7B,UAAU,EAAE,UAAU,EACtB,MAAM,EAAE,YAAY,EACpB,OAAO,GAAE,iBAAsB,GAC9B,cAAc,CA2BhB;AAED,+EAA+E;AAC/E,wBAAgB,qBAAqB,CACnC,UAAU,EAAE,UAAU,EACtB,MAAM,EAAE,YAAY,EACpB,OAAO,CAAC,EAAE,iBAAiB,GAC1B,MAAM,CAER"}
@@ -18,7 +18,9 @@ const RDF_TYPE = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type';
18
18
  * - **WHERE**: one UNION branch per field (`?root <path> ?value`), the field’s
19
19
  * `path` embedded as a literal SPARQL property path (sequence / alternative /
20
20
  * inverse). UNION per branch, never a conjunction – conjoining every path in
21
- * one BGP cross-multiplies the multi-valued ones (~4× inflation).
21
+ * one BGP cross-multiplies the multi-valued ones (~4× inflation). Fields
22
+ * whose path is a single predicate share one branch instead, reading them
23
+ * through a `VALUES` table ({@link mergePlainReads}).
22
24
  * - **roots**: the subject variable is left free, to be bound by the pipeline’s
23
25
  * `injectValues` (`VALUES ?root { … }`). Root *selection* is a separate,
24
26
  * deployment concern (`selectByClass` / the stage’s `itemSelector`); this
@@ -41,7 +43,7 @@ const RDF_TYPE = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type';
41
43
  * referent’s key field, emitted under the **target’s** alias for it – so the
42
44
  * projection can store the referent’s document key rather than its node IRI.
43
45
  * `OPTIONAL`, so a referent with no key candidate keeps its row. The root side
44
- * needs nothing: a key field is a declared field, so its own branch and
46
+ * needs nothing: a key field is a declared field, so its own read and
45
47
  * template triple are already there.
46
48
  * - **references naming several targets**: the referent may be of any of them,
47
49
  * so its branch also reads the referent’s `rdf:type` (`OPTIONAL`, emitted
@@ -56,11 +58,14 @@ const RDF_TYPE = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type';
56
58
  export function extractionQuery(searchType, schema, options = {}) {
57
59
  const subjectVariable = options.subjectVariable ?? 'root';
58
60
  const subject = factory.termVariable(subjectVariable, factory.gen());
59
- const built = buildFor(searchType, subject, schema, { next: 0 }, new Set());
61
+ const counter = { next: 0 };
62
+ const built = buildFor(searchType, subject, schema, counter, new Set());
60
63
  if (built.branches.length === 0) {
61
64
  throw new Error(`Cannot generate an extraction CONSTRUCT for “${searchType.name}”: it declares no path-bearing field, so there is nothing to extract.`);
62
65
  }
63
- const where = factory.patternGroup([factory.patternUnion(built.branches, factory.gen())], factory.gen());
66
+ const where = factory.patternGroup([
67
+ factory.patternUnion(mergePlainReads(built.branches, built.template, counter), factory.gen()),
68
+ ], factory.gen());
64
69
  return factory.queryConstruct(factory.gen(), [], factory.patternBgp(built.template, factory.gen()), where, {}, factory.datasetClauses([], factory.gen()));
65
70
  }
66
71
  /** {@link extractionQuery}, serialised to a SPARQL string a reader can run. */
@@ -123,7 +128,7 @@ function buildFor(searchType, subject, schema, counter, visiting) {
123
128
  // A single-branch union serialises to just its group, so only add the
124
129
  // nested union when the reference type has fields of its own.
125
130
  if (nestedBranches.length > 0) {
126
- patterns.push(factory.patternUnion(nestedBranches, factory.gen()));
131
+ patterns.push(factory.patternUnion(mergePlainReads(nestedBranches, template, counter), factory.gen()));
127
132
  }
128
133
  branches.push(factory.patternGroup(patterns, factory.gen()));
129
134
  }
@@ -150,18 +155,122 @@ function buildFor(searchType, subject, schema, counter, visiting) {
150
155
  // and the branches of the others simply bind nothing for it. A
151
156
  // single-branch union serialises to just its group.
152
157
  if (expansions.length > 0) {
153
- patterns.push(factory.patternOptional([factory.patternUnion(expansions, factory.gen())], factory.gen()));
158
+ patterns.push(factory.patternOptional([
159
+ factory.patternUnion(mergePlainReads(expansions, template, counter), factory.gen()),
160
+ ], factory.gen()));
154
161
  }
155
162
  branches.push(factory.patternGroup(patterns, factory.gen()));
156
163
  }
157
164
  }
158
165
  return { template, branches };
159
166
  }
167
+ /**
168
+ * Fold a union’s **plain reads** – branches that are one `?s <iri> ?o` triple –
169
+ * into one branch per subject: `?s ?p ?o`, joined with a `VALUES` table that
170
+ * pairs each read’s predicate with the alias it is emitted under, and one
171
+ * template triple `?s ?alias ?o` in place of theirs. The output is the same
172
+ * triples: each table row still binds only its own values, so multi-valued
173
+ * fields still add rather than multiply.
174
+ *
175
+ * It exists for QLever, which evaluates every branch of a union as an operation
176
+ * of its own. Under the `OPTIONAL` of a reference’s reads, those branches ran
177
+ * out of its default 4 GB memory budget on a batch of ten roots that used under
178
+ * 200 MB (ad-freiburg/qlever#3898); one scan per subject instead of one per
179
+ * field also reads a predicate that several targets share – `name`, `sameAs` –
180
+ * once rather than once per target. Measured on LOL’s CreativeWork extraction,
181
+ * median over 30 batches of ten on a running server: 320 ms at an 8 GB budget
182
+ * before, 100 ms at the default 4 GB after, and both LDMax QLever endpoints,
183
+ * which refused the old shape, answer in about 250 ms. A branch with a property
184
+ * path or a hop of its own stays as it is, and so does a subject’s only plain
185
+ * read.
186
+ *
187
+ * The triple comes before the `VALUES`, so `injectValues` still finds the root
188
+ * bound by a leading triple pattern and puts its roots into this branch too.
189
+ */
190
+ function mergePlainReads(branches, template, counter) {
191
+ const readsBySubject = new Map();
192
+ for (const branch of branches) {
193
+ const read = plainReadOf(branch, template);
194
+ if (read !== undefined) {
195
+ const reads = readsBySubject.get(read.subject.value) ?? [];
196
+ reads.push(read);
197
+ readsBySubject.set(read.subject.value, reads);
198
+ }
199
+ }
200
+ // A merged branch takes the place of the first read it replaces; the others
201
+ // are dropped.
202
+ const replacements = new Map();
203
+ const dropped = new Set();
204
+ for (const reads of readsBySubject.values()) {
205
+ if (reads.length < 2) {
206
+ continue;
207
+ }
208
+ const [first, ...others] = reads;
209
+ const { subject } = first;
210
+ const predicate = factory.termVariable(`p${counter.next}`, factory.gen());
211
+ const alias = factory.termVariable(`a${counter.next}`, factory.gen());
212
+ const value = factory.termVariable(`o${counter.next++}`, factory.gen());
213
+ // In the place of the first triple it replaces, so the template keeps the
214
+ // order the fields are declared in.
215
+ for (const read of others) {
216
+ template.splice(template.indexOf(read.templateTriple), 1);
217
+ dropped.add(read.branch);
218
+ }
219
+ template.splice(template.indexOf(first.templateTriple), 1, factory.triple(subject, alias, value));
220
+ const branch = factory.patternGroup([
221
+ factory.patternBgp([factory.triple(subject, predicate, value)], factory.gen()),
222
+ factory.patternValues([predicate, alias], reads.map((read) => ({
223
+ [predicate.value]: read.predicate,
224
+ [alias.value]: read.templateTriple.predicate,
225
+ })), factory.gen()),
226
+ ], factory.gen());
227
+ replacements.set(first.branch, branch);
228
+ }
229
+ return branches
230
+ .filter((branch) => !dropped.has(branch))
231
+ .map((branch) => replacements.get(branch) ?? branch);
232
+ }
233
+ /**
234
+ * The plain read a branch is, if it is one: a single `?s <iri> ?o` triple, with
235
+ * the template triple `?s <alias> ?o` that emits its value.
236
+ */
237
+ function plainReadOf(branch, template) {
238
+ const [pattern, ...rest] = branch.patterns;
239
+ if (pattern === undefined ||
240
+ rest.length > 0 ||
241
+ !factory.isPatternBgp(pattern) ||
242
+ pattern.triples.length !== 1) {
243
+ return undefined;
244
+ }
245
+ const [triple] = pattern.triples;
246
+ if (triple === undefined ||
247
+ !factory.isTriple(triple) ||
248
+ !factory.isTermVariable(triple.subject) ||
249
+ !factory.isTermNamed(triple.predicate) ||
250
+ !factory.isTermVariable(triple.object)) {
251
+ return undefined;
252
+ }
253
+ const { subject, predicate, object } = triple;
254
+ // Every value this generator binds is emitted by exactly one template triple,
255
+ // off the same subject and under a minted alias, so that triple is there. A
256
+ // value read on from – a referent with fields – never gets here: its branch
257
+ // holds the hops too.
258
+ const templateTriple = template.find((candidate) => factory.isTermVariable(candidate.object) &&
259
+ candidate.object.value === object.value);
260
+ return {
261
+ branch,
262
+ subject,
263
+ predicate,
264
+ templateTriple,
265
+ };
266
+ }
160
267
  /**
161
268
  * The hops a reference reads off its referent besides its type: each keyed
162
269
  * target’s key and each `local` target’s own fields. Each is its own branch,
163
270
  * so the caller binds them in ONE union where each binds only its own
164
- * variables, and a multi-valued key multiplies against nothing.
271
+ * variables, and a multi-valued key multiplies against nothing – also once
272
+ * {@link mergePlainReads} has folded the plain ones into a `VALUES` table, whose
273
+ * rows each bind one read too.
165
274
  */
166
275
  function buildReferentHops(field, referent, schema, counter, onPath) {
167
276
  const template = [];
@@ -235,7 +344,8 @@ function buildTypeHop(referent, counter) {
235
344
  * conjoined, so a referent with no key candidate keeps its row and still stores
236
345
  * its own IRI, instead of the whole reference dropping out of the CONSTRUCT.
237
346
  *
238
- * It is a branch of its own in that union, so it multiplies against nothing.
347
+ * It is a branch of its own in that union, or a row of its merged plain reads,
348
+ * so it multiplies against nothing.
239
349
  */
240
350
  function buildKeyHop(target, keyField, referent, counter) {
241
351
  const key = factory.termVariable(`k${counter.next++}`, factory.gen());
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lde/search-pipeline",
3
- "version": "0.41.0",
3
+ "version": "0.41.1",
4
4
  "description": "Search indexing as an @lde/pipeline instance: a Writer<Quad> that applies the @lde/search projection to extracted RDF and fans each document out to the transactional engine writer for its type's collection. Glue between the pipeline quad world and the search-engine document world; owns no projection or engine logic itself.",
5
5
  "repository": {
6
6
  "url": "git+https://github.com/ldelements/lde.git",