@comunica/actor-optimize-query-operation-prune-empty-source-operations 4.2.0 → 4.4.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.
|
@@ -154,26 +154,40 @@ class ActorOptimizeQueryOperationPruneEmptySourceOperations extends bus_optimize
|
|
|
154
154
|
* @param context The query context.
|
|
155
155
|
*/
|
|
156
156
|
async hasSourceResults(algebraFactory, source, input, context) {
|
|
157
|
-
|
|
158
|
-
|
|
157
|
+
const mergedContext = source.context ? context.merge(source.context) : context;
|
|
158
|
+
const wildcardAcceptAllExtensionFunctions = mergedContext.get(context_entries_1.KeysInitQuery.extensionFunctionsAlwaysPushdown);
|
|
159
|
+
// Traversal contexts should never be considered empty at optimization time.
|
|
160
|
+
if (mergedContext.get(context_entries_1.KeysQuerySourceIdentify.traverse)) {
|
|
159
161
|
return true;
|
|
160
162
|
}
|
|
161
|
-
//
|
|
163
|
+
// Prefer ASK over COUNT when instructed to, and the source allows it
|
|
162
164
|
if (this.useAskIfSupported) {
|
|
163
165
|
const askOperation = algebraFactory.createAsk(input);
|
|
164
|
-
|
|
165
|
-
|
|
166
|
+
const askSupported = (0, utils_query_operation_1.doesShapeAcceptOperation)(await source.source.getSelectorShape(context), askOperation, { wildcardAcceptAllExtensionFunctions });
|
|
167
|
+
if (askSupported) {
|
|
168
|
+
return source.source.queryBoolean(askOperation, mergedContext);
|
|
166
169
|
}
|
|
167
170
|
}
|
|
168
|
-
//
|
|
169
|
-
const bindingsStream = source.source.queryBindings(input,
|
|
170
|
-
|
|
171
|
+
// Fall back to sending the full operation, and extracting the cardinality from metadata
|
|
172
|
+
const bindingsStream = source.source.queryBindings(input, mergedContext);
|
|
173
|
+
const cardinality = await new Promise((resolve, reject) => {
|
|
171
174
|
bindingsStream.on('error', reject);
|
|
172
175
|
bindingsStream.getProperty('metadata', (metadata) => {
|
|
173
176
|
bindingsStream.destroy();
|
|
174
|
-
resolve(metadata.cardinality
|
|
177
|
+
resolve(metadata.cardinality);
|
|
175
178
|
});
|
|
176
179
|
});
|
|
180
|
+
// If the cardinality is an estimate, such as from a VoID description,
|
|
181
|
+
// verify it using ASK if the source supports it.
|
|
182
|
+
// Since the VoID estimators in Comunica cannot produce false negatives, only positive assignments must be verified.
|
|
183
|
+
if (cardinality.type === 'estimate' && cardinality.value > 0) {
|
|
184
|
+
const askOperation = algebraFactory.createAsk(input);
|
|
185
|
+
const askSupported = (0, utils_query_operation_1.doesShapeAcceptOperation)(await source.source.getSelectorShape(context), askOperation, { wildcardAcceptAllExtensionFunctions });
|
|
186
|
+
if (askSupported) {
|
|
187
|
+
return source.source.queryBoolean(askOperation, mergedContext);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
return cardinality.value > 0;
|
|
177
191
|
}
|
|
178
192
|
}
|
|
179
193
|
exports.ActorOptimizeQueryOperationPruneEmptySourceOperations = ActorOptimizeQueryOperationPruneEmptySourceOperations;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ActorOptimizeQueryOperationPruneEmptySourceOperations.js","sourceRoot":"","sources":["ActorOptimizeQueryOperationPruneEmptySourceOperations.ts"],"names":[],"mappings":";;;AAKA,yFAAqF;AACrF,+DAAmF;AAEnF,yCAAwD;AAExD,2EAA+F;AAC/F,qDAAyD;AAEzD;;GAEG;AACH,MAAa,qDAAsD,SAAQ,0DAA2B;IAGpG,YAAmB,IAAgE;QACjF,KAAK,CAAC,IAAI,CAAC,CAAC;IACd,CAAC;IAEM,KAAK,CAAC,IAAI,CAAC,MAAqC;QACrD,IAAI,IAAA,0CAAkB,EAAC,MAAM,CAAC,SAAS,CAAC,EAAE,CAAC;YACzC,OAAO,IAAA,eAAQ,EAAC,SAAS,IAAI,CAAC,IAAI,kDAAkD,CAAC,CAAC;QACxF,CAAC;QACD,OAAO,IAAA,mBAAY,GAAE,CAAC;IACxB,CAAC;IAEM,KAAK,CAAC,GAAG,CAAC,MAAqC;QACpD,MAAM,WAAW,GAAw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QAA0B,EAAE,EAAE;gBACpE,cAAc,CAAC,OAAO,EAAE,CAAC;gBACzB,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAC,KAAK,GAAG,CAAC,CAAC,CAAC;YAC1C,CAAC,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AA5MD,sHA4MC","sourcesContent":["import type {\n IActionOptimizeQueryOperation,\n IActorOptimizeQueryOperationOutput,\n IActorOptimizeQueryOperationArgs,\n} from '@comunica/bus-optimize-query-operation';\nimport { ActorOptimizeQueryOperation } from '@comunica/bus-optimize-query-operation';\nimport { KeysInitQuery, KeysQuerySourceIdentify } from '@comunica/context-entries';\nimport type { IActorTest, TestResult } from '@comunica/core';\nimport { failTest, passTestVoid } from '@comunica/core';\nimport type { ComunicaDataFactory, IActionContext, IQuerySourceWrapper, MetadataBindings } from '@comunica/types';\nimport { doesShapeAcceptOperation, getOperationSource } from '@comunica/utils-query-operation';\nimport { Algebra, Factory, Util } from 'sparqlalgebrajs';\n\n/**\n * A comunica Prune Empty Source Operations Optimize Query Operation Actor.\n */\nexport class ActorOptimizeQueryOperationPruneEmptySourceOperations extends ActorOptimizeQueryOperation {\n private readonly useAskIfSupported: boolean;\n\n public constructor(args: IActorOptimizeQueryOperationPruneEmptySourceOperationsArgs) {\n super(args);\n }\n\n public async test(action: IActionOptimizeQueryOperation): Promise<TestResult<IActorTest>> {\n if (getOperationSource(action.operation)) {\n return failTest(`Actor ${this.name} does not work with top-level operation sources.`);\n }\n return passTestVoid();\n }\n\n public async run(action: IActionOptimizeQueryOperation): Promise<IActorOptimizeQueryOperationOutput> {\n const dataFactory: ComunicaDataFactory = action.context.getSafe(KeysInitQuery.dataFactory);\n const algebraFactory = new Factory(dataFactory);\n\n let operation = action.operation;\n\n // Collect all operations with source types\n // Only consider unions of patterns or alts of links, since these are created during exhaustive source assignment.\n const collectedOperations: (Algebra.Pattern | Algebra.Link)[] = [];\n // eslint-disable-next-line ts/no-this-alias\n const self = this;\n Util.recurseOperation(operation, {\n [Algebra.types.UNION](subOperation) {\n self.collectMultiOperationInputs(subOperation.input, collectedOperations, Algebra.types.PATTERN);\n return true;\n },\n [Algebra.types.ALT](subOperation) {\n self.collectMultiOperationInputs(subOperation.input, collectedOperations, Algebra.types.LINK);\n return false;\n },\n [Algebra.types.SERVICE]() {\n return false;\n },\n });\n\n // Determine in an async manner whether or not these sources return non-empty results\n const emptyOperations: Set<Algebra.Operation> = new Set();\n await Promise.all(collectedOperations.map(async(collectedOperation) => {\n const checkOperation = collectedOperation.type === 'link' ?\n algebraFactory.createPattern(dataFactory.variable('s'), collectedOperation.iri, dataFactory.variable('o')) :\n collectedOperation;\n if (!await this.hasSourceResults(\n algebraFactory,\n getOperationSource(collectedOperation)!,\n checkOperation,\n action.context,\n )) {\n emptyOperations.add(collectedOperation);\n }\n }));\n\n // Only perform next mapping if we have at least one empty operation\n if (emptyOperations.size > 0) {\n this.logDebug(action.context, `Pruning ${emptyOperations.size} source-specific operations`);\n // Rewrite operations by removing the empty children\n operation = Util.mapOperation(operation, {\n [Algebra.types.UNION](subOperation, factory) {\n return self.mapMultiOperation(subOperation, emptyOperations, children => factory.createUnion(children));\n },\n [Algebra.types.ALT](subOperation, factory) {\n return self.mapMultiOperation(subOperation, emptyOperations, children => factory.createAlt(children));\n },\n }, algebraFactory);\n\n // Identify and remove operations that have become empty now due to missing variables\n operation = Util.mapOperation(operation, {\n [Algebra.types.PROJECT](subOperation, factory) {\n // Remove projections that have become empty now due to missing variables\n if (ActorOptimizeQueryOperationPruneEmptySourceOperations.hasEmptyOperation(subOperation)) {\n return {\n recurse: false,\n result: factory.createUnion([]),\n };\n }\n return {\n recurse: true,\n result: subOperation,\n };\n },\n [Algebra.types.LEFT_JOIN](subOperation) {\n // Remove left joins with empty right operation\n if (ActorOptimizeQueryOperationPruneEmptySourceOperations.hasEmptyOperation(subOperation.input[1])) {\n return {\n recurse: true,\n result: subOperation.input[0],\n };\n }\n return {\n recurse: true,\n result: subOperation,\n };\n },\n }, algebraFactory);\n }\n\n return { operation, context: action.context };\n }\n\n protected static hasEmptyOperation(operation: Algebra.Operation): boolean {\n // If union (or alt) is empty, consider it empty (`Array.every` on an empty array always returns true)\n // But if we find a union with multiple children,\n // *all* of the children must be empty before the full operation is considered empty.\n let emptyOperation = false;\n Util.recurseOperation(operation, {\n [Algebra.types.UNION](subOperation) {\n if (subOperation.input.every(subSubOperation => ActorOptimizeQueryOperationPruneEmptySourceOperations\n .hasEmptyOperation(subSubOperation))) {\n emptyOperation = true;\n }\n return false;\n },\n [Algebra.types.ALT](subOperation) {\n if (subOperation.input.length === 0) {\n emptyOperation = true;\n }\n return false;\n },\n [Algebra.types.LEFT_JOIN](subOperation) {\n // Only recurse into left part of left-join\n if (ActorOptimizeQueryOperationPruneEmptySourceOperations.hasEmptyOperation(subOperation.input[0])) {\n emptyOperation = true;\n }\n return false;\n },\n });\n return emptyOperation;\n }\n\n protected collectMultiOperationInputs(\n inputs: Algebra.Operation[],\n collectedOperations: (Algebra.Pattern | Algebra.Link)[],\n inputType: (Algebra.Pattern | Algebra.Link)['type'],\n ): void {\n for (const input of inputs) {\n if (getOperationSource(input) && input.type === inputType) {\n collectedOperations.push(input);\n }\n }\n }\n\n protected mapMultiOperation<O extends Algebra.Union | Algebra.Alt>(\n operation: O,\n emptyOperations: Set<Algebra.Operation>,\n multiOperationFactory: (input: O['input']) => Algebra.Operation,\n ): {\n result: Algebra.Operation;\n recurse: boolean;\n } {\n // Determine which operations return non-empty results\n const nonEmptyInputs = operation.input.filter(input => !emptyOperations.has(input));\n\n // Remove empty operations\n if (nonEmptyInputs.length === operation.input.length) {\n return { result: operation, recurse: true };\n }\n if (nonEmptyInputs.length === 0) {\n return { result: multiOperationFactory([]), recurse: false };\n }\n if (nonEmptyInputs.length === 1) {\n return { result: nonEmptyInputs[0], recurse: true };\n }\n return { result: multiOperationFactory(nonEmptyInputs), recurse: true };\n }\n\n /**\n * Check if the given query operation will produce at least one result in the given source.\n * @param algebraFactory The algebra factory.\n * @param source A query source.\n * @param input A query operation.\n * @param context The query context.\n */\n public async hasSourceResults(\n algebraFactory: Factory,\n source: IQuerySourceWrapper,\n input: Algebra.Operation,\n context: IActionContext,\n ): Promise<boolean> {\n // Traversal sources should never be considered empty at optimization time.\n if (source.context?.get(KeysQuerySourceIdentify.traverse) ?? context.get(KeysQuerySourceIdentify.traverse)) {\n return true;\n }\n\n // Send an ASK query\n if (this.useAskIfSupported) {\n const askOperation = algebraFactory.createAsk(input);\n if (doesShapeAcceptOperation(await source.source.getSelectorShape(context), askOperation)) {\n return source.source.queryBoolean(askOperation, context);\n }\n }\n\n // Send the operation as-is and check the response cardinality\n const bindingsStream = source.source.queryBindings(input, context);\n return new Promise((resolve, reject) => {\n bindingsStream.on('error', reject);\n bindingsStream.getProperty('metadata', (metadata: MetadataBindings) => {\n bindingsStream.destroy();\n resolve(metadata.cardinality.value > 0);\n });\n });\n }\n}\n\nexport interface IActorOptimizeQueryOperationPruneEmptySourceOperationsArgs extends IActorOptimizeQueryOperationArgs {\n /**\n * If true, ASK queries will be sent to the source instead of COUNT queries to check emptiness for patterns.\n * This will only be done for sources that accept ASK queries.\n * @default {false}\n */\n useAskIfSupported: boolean;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"ActorOptimizeQueryOperationPruneEmptySourceOperations.js","sourceRoot":"","sources":["ActorOptimizeQueryOperationPruneEmptySourceOperations.ts"],"names":[],"mappings":";;;AAKA,yFAAqF;AACrF,+DAAmF;AAEnF,yCAAwD;AAQxD,2EAA+F;AAC/F,qDAAyD;AAEzD;;GAEG;AACH,MAAa,qDAAsD,SAAQ,0DAA2B;IAGpG,YAAmB,IAAgE;QACjF,KAAK,CAAC,IAAI,CAAC,CAAC;IACd,CAAC;IAEM,KAAK,CAAC,IAAI,CAAC,MAAqC;QACrD,IAAI,IAAA,0CAAkB,EAAC,MAAM,CAAC,SAAS,CAAC,EAAE,CAAC;YACzC,OAAO,IAAA,eAAQ,EAAC,SAAS,IAAI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,MAAM,CAAC,MAAM,CAAC,YAAY,CAAC,YAAY,EAAE,aAAa,CAAC,CAAC;YACjE,CAAC;QACH,CAAC;QAED,wFAAwF;QACxF,MAAM,cAAc,GAAG,MAAM,CAAC,MAAM,CAAC,aAAa,CAAC,KAAK,EAAE,aAAa,CAAC,CAAC;QACzE,MAAM,WAAW,GAAG,MAAM,IAAI,OAAO,CAAyB,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;YAChF,cAAc,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;YACnC,cAAc,CAAC,WAAW,CAAC,UAAU,EAAE,CAAC,QAA0B,EAAE,EAAE;gBACpE,cAAc,CAAC,OAAO,EAAE,CAAC;gBACzB,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAC;YAChC,CAAC,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;QAEH,sEAAsE;QACtE,iDAAiD;QACjD,oHAAoH;QACpH,IAAI,WAAW,CAAC,IAAI,KAAK,UAAU,IAAI,WAAW,CAAC,KAAK,GAAG,CAAC,EAAE,CAAC;YAC7D,MAAM,YAAY,GAAG,cAAc,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;YACrD,MAAM,YAAY,GAAG,IAAA,gDAAwB,EAC3C,MAAM,MAAM,CAAC,MAAM,CAAC,gBAAgB,CAAC,OAAO,CAAC,EAC7C,YAAY,EACZ,EAAE,mCAAmC,EAAE,CACxC,CAAC;YACF,IAAI,YAAY,EAAE,CAAC;gBACjB,OAAO,MAAM,CAAC,MAAM,CAAC,YAAY,CAAC,YAAY,EAAE,aAAa,CAAC,CAAC;YACjE,CAAC;QACH,CAAC;QAED,OAAO,WAAW,CAAC,KAAK,GAAG,CAAC,CAAC;IAC/B,CAAC;CACF;AArOD,sHAqOC","sourcesContent":["import type {\n IActionOptimizeQueryOperation,\n IActorOptimizeQueryOperationOutput,\n IActorOptimizeQueryOperationArgs,\n} from '@comunica/bus-optimize-query-operation';\nimport { ActorOptimizeQueryOperation } from '@comunica/bus-optimize-query-operation';\nimport { KeysInitQuery, KeysQuerySourceIdentify } from '@comunica/context-entries';\nimport type { IActorTest, TestResult } from '@comunica/core';\nimport { failTest, passTestVoid } from '@comunica/core';\nimport type {\n ComunicaDataFactory,\n IActionContext,\n IQuerySourceWrapper,\n MetadataBindings,\n QueryResultCardinality,\n} from '@comunica/types';\nimport { doesShapeAcceptOperation, getOperationSource } from '@comunica/utils-query-operation';\nimport { Algebra, Factory, Util } from 'sparqlalgebrajs';\n\n/**\n * A comunica Prune Empty Source Operations Optimize Query Operation Actor.\n */\nexport class ActorOptimizeQueryOperationPruneEmptySourceOperations extends ActorOptimizeQueryOperation {\n private readonly useAskIfSupported: boolean;\n\n public constructor(args: IActorOptimizeQueryOperationPruneEmptySourceOperationsArgs) {\n super(args);\n }\n\n public async test(action: IActionOptimizeQueryOperation): Promise<TestResult<IActorTest>> {\n if (getOperationSource(action.operation)) {\n return failTest(`Actor ${this.name} does not work with top-level operation sources.`);\n }\n return passTestVoid();\n }\n\n public async run(action: IActionOptimizeQueryOperation): Promise<IActorOptimizeQueryOperationOutput> {\n const dataFactory: ComunicaDataFactory = action.context.getSafe(KeysInitQuery.dataFactory);\n const algebraFactory = new Factory(dataFactory);\n\n let operation = action.operation;\n\n // Collect all operations with source types\n // Only consider unions of patterns or alts of links, since these are created during exhaustive source assignment.\n const collectedOperations: (Algebra.Pattern | Algebra.Link)[] = [];\n // eslint-disable-next-line ts/no-this-alias\n const self = this;\n Util.recurseOperation(operation, {\n [Algebra.types.UNION](subOperation) {\n self.collectMultiOperationInputs(subOperation.input, collectedOperations, Algebra.types.PATTERN);\n return true;\n },\n [Algebra.types.ALT](subOperation) {\n self.collectMultiOperationInputs(subOperation.input, collectedOperations, Algebra.types.LINK);\n return false;\n },\n [Algebra.types.SERVICE]() {\n return false;\n },\n });\n\n // Determine in an async manner whether or not these sources return non-empty results\n const emptyOperations: Set<Algebra.Operation> = new Set();\n await Promise.all(collectedOperations.map(async(collectedOperation) => {\n const checkOperation = collectedOperation.type === 'link' ?\n algebraFactory.createPattern(dataFactory.variable('s'), collectedOperation.iri, dataFactory.variable('o')) :\n collectedOperation;\n if (!await this.hasSourceResults(\n algebraFactory,\n getOperationSource(collectedOperation)!,\n checkOperation,\n action.context,\n )) {\n emptyOperations.add(collectedOperation);\n }\n }));\n\n // Only perform next mapping if we have at least one empty operation\n if (emptyOperations.size > 0) {\n this.logDebug(action.context, `Pruning ${emptyOperations.size} source-specific operations`);\n // Rewrite operations by removing the empty children\n operation = Util.mapOperation(operation, {\n [Algebra.types.UNION](subOperation, factory) {\n return self.mapMultiOperation(subOperation, emptyOperations, children => factory.createUnion(children));\n },\n [Algebra.types.ALT](subOperation, factory) {\n return self.mapMultiOperation(subOperation, emptyOperations, children => factory.createAlt(children));\n },\n }, algebraFactory);\n\n // Identify and remove operations that have become empty now due to missing variables\n operation = Util.mapOperation(operation, {\n [Algebra.types.PROJECT](subOperation, factory) {\n // Remove projections that have become empty now due to missing variables\n if (ActorOptimizeQueryOperationPruneEmptySourceOperations.hasEmptyOperation(subOperation)) {\n return {\n recurse: false,\n result: factory.createUnion([]),\n };\n }\n return {\n recurse: true,\n result: subOperation,\n };\n },\n [Algebra.types.LEFT_JOIN](subOperation) {\n // Remove left joins with empty right operation\n if (ActorOptimizeQueryOperationPruneEmptySourceOperations.hasEmptyOperation(subOperation.input[1])) {\n return {\n recurse: true,\n result: subOperation.input[0],\n };\n }\n return {\n recurse: true,\n result: subOperation,\n };\n },\n }, algebraFactory);\n }\n\n return { operation, context: action.context };\n }\n\n protected static hasEmptyOperation(operation: Algebra.Operation): boolean {\n // If union (or alt) is empty, consider it empty (`Array.every` on an empty array always returns true)\n // But if we find a union with multiple children,\n // *all* of the children must be empty before the full operation is considered empty.\n let emptyOperation = false;\n Util.recurseOperation(operation, {\n [Algebra.types.UNION](subOperation) {\n if (subOperation.input.every(subSubOperation => ActorOptimizeQueryOperationPruneEmptySourceOperations\n .hasEmptyOperation(subSubOperation))) {\n emptyOperation = true;\n }\n return false;\n },\n [Algebra.types.ALT](subOperation) {\n if (subOperation.input.length === 0) {\n emptyOperation = true;\n }\n return false;\n },\n [Algebra.types.LEFT_JOIN](subOperation) {\n // Only recurse into left part of left-join\n if (ActorOptimizeQueryOperationPruneEmptySourceOperations.hasEmptyOperation(subOperation.input[0])) {\n emptyOperation = true;\n }\n return false;\n },\n });\n return emptyOperation;\n }\n\n protected collectMultiOperationInputs(\n inputs: Algebra.Operation[],\n collectedOperations: (Algebra.Pattern | Algebra.Link)[],\n inputType: (Algebra.Pattern | Algebra.Link)['type'],\n ): void {\n for (const input of inputs) {\n if (getOperationSource(input) && input.type === inputType) {\n collectedOperations.push(input);\n }\n }\n }\n\n protected mapMultiOperation<O extends Algebra.Union | Algebra.Alt>(\n operation: O,\n emptyOperations: Set<Algebra.Operation>,\n multiOperationFactory: (input: O['input']) => Algebra.Operation,\n ): {\n result: Algebra.Operation;\n recurse: boolean;\n } {\n // Determine which operations return non-empty results\n const nonEmptyInputs = operation.input.filter(input => !emptyOperations.has(input));\n\n // Remove empty operations\n if (nonEmptyInputs.length === operation.input.length) {\n return { result: operation, recurse: true };\n }\n if (nonEmptyInputs.length === 0) {\n return { result: multiOperationFactory([]), recurse: false };\n }\n if (nonEmptyInputs.length === 1) {\n return { result: nonEmptyInputs[0], recurse: true };\n }\n return { result: multiOperationFactory(nonEmptyInputs), recurse: true };\n }\n\n /**\n * Check if the given query operation will produce at least one result in the given source.\n * @param algebraFactory The algebra factory.\n * @param source A query source.\n * @param input A query operation.\n * @param context The query context.\n */\n public async hasSourceResults(\n algebraFactory: Factory,\n source: IQuerySourceWrapper,\n input: Algebra.Operation,\n context: IActionContext,\n ): Promise<boolean> {\n const mergedContext = source.context ? context.merge(source.context) : context;\n const wildcardAcceptAllExtensionFunctions = mergedContext.get(KeysInitQuery.extensionFunctionsAlwaysPushdown);\n\n // Traversal contexts should never be considered empty at optimization time.\n if (mergedContext.get(KeysQuerySourceIdentify.traverse)) {\n return true;\n }\n\n // Prefer ASK over COUNT when instructed to, and the source allows it\n if (this.useAskIfSupported) {\n const askOperation = algebraFactory.createAsk(input);\n const askSupported = doesShapeAcceptOperation(\n await source.source.getSelectorShape(context),\n askOperation,\n { wildcardAcceptAllExtensionFunctions },\n );\n if (askSupported) {\n return source.source.queryBoolean(askOperation, mergedContext);\n }\n }\n\n // Fall back to sending the full operation, and extracting the cardinality from metadata\n const bindingsStream = source.source.queryBindings(input, mergedContext);\n const cardinality = await new Promise<QueryResultCardinality>((resolve, reject) => {\n bindingsStream.on('error', reject);\n bindingsStream.getProperty('metadata', (metadata: MetadataBindings) => {\n bindingsStream.destroy();\n resolve(metadata.cardinality);\n });\n });\n\n // If the cardinality is an estimate, such as from a VoID description,\n // verify it using ASK if the source supports it.\n // Since the VoID estimators in Comunica cannot produce false negatives, only positive assignments must be verified.\n if (cardinality.type === 'estimate' && cardinality.value > 0) {\n const askOperation = algebraFactory.createAsk(input);\n const askSupported = doesShapeAcceptOperation(\n await source.source.getSelectorShape(context),\n askOperation,\n { wildcardAcceptAllExtensionFunctions },\n );\n if (askSupported) {\n return source.source.queryBoolean(askOperation, mergedContext);\n }\n }\n\n return cardinality.value > 0;\n }\n}\n\nexport interface IActorOptimizeQueryOperationPruneEmptySourceOperationsArgs extends IActorOptimizeQueryOperationArgs {\n /**\n * If true, ASK queries will be sent to the source instead of COUNT queries to check emptiness for patterns.\n * This will only be done for sources that accept ASK queries.\n * @default {false}\n */\n useAskIfSupported: boolean;\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@comunica/actor-optimize-query-operation-prune-empty-source-operations",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.4.1",
|
|
4
4
|
"description": "A prune-empty-source-operations optimize-query-operation actor",
|
|
5
5
|
"lsd:module": true,
|
|
6
6
|
"license": "MIT",
|
|
@@ -41,12 +41,12 @@
|
|
|
41
41
|
"build:components": "componentsjs-generator"
|
|
42
42
|
},
|
|
43
43
|
"dependencies": {
|
|
44
|
-
"@comunica/bus-optimize-query-operation": "^4.
|
|
45
|
-
"@comunica/context-entries": "^4.
|
|
46
|
-
"@comunica/core": "^4.
|
|
47
|
-
"@comunica/types": "^4.
|
|
48
|
-
"@comunica/utils-query-operation": "^4.
|
|
44
|
+
"@comunica/bus-optimize-query-operation": "^4.4.0",
|
|
45
|
+
"@comunica/context-entries": "^4.4.1",
|
|
46
|
+
"@comunica/core": "^4.4.0",
|
|
47
|
+
"@comunica/types": "^4.4.0",
|
|
48
|
+
"@comunica/utils-query-operation": "^4.4.1",
|
|
49
49
|
"sparqlalgebrajs": "^4.3.8"
|
|
50
50
|
},
|
|
51
|
-
"gitHead": "
|
|
51
|
+
"gitHead": "c5cc36caf88da31173a0969a7da88cadb7f469ac"
|
|
52
52
|
}
|