@comunica/actor-optimize-query-operation-prune-empty-source-operations 4.4.1 → 4.4.2-alpha.49.0
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.
- package/components/ActorOptimizeQueryOperationPruneEmptySourceOperations.jsonld +0 -0
- package/components/components.jsonld +0 -0
- package/components/context.jsonld +0 -0
- package/lib/ActorOptimizeQueryOperationPruneEmptySourceOperations.d.ts +3 -6
- package/lib/ActorOptimizeQueryOperationPruneEmptySourceOperations.js +71 -83
- package/lib/ActorOptimizeQueryOperationPruneEmptySourceOperations.js.map +1 -1
- package/lib/index.d.ts +0 -0
- package/lib/index.js +0 -0
- package/lib/index.js.map +0 -0
- package/package.json +8 -8
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
@@ -2,7 +2,7 @@ import type { IActionOptimizeQueryOperation, IActorOptimizeQueryOperationOutput,
|
|
|
2
2
|
import { ActorOptimizeQueryOperation } from '@comunica/bus-optimize-query-operation';
|
|
3
3
|
import type { IActorTest, TestResult } from '@comunica/core';
|
|
4
4
|
import type { IActionContext, IQuerySourceWrapper } from '@comunica/types';
|
|
5
|
-
import { Algebra,
|
|
5
|
+
import { Algebra, AlgebraFactory } from '@comunica/utils-algebra';
|
|
6
6
|
/**
|
|
7
7
|
* A comunica Prune Empty Source Operations Optimize Query Operation Actor.
|
|
8
8
|
*/
|
|
@@ -13,10 +13,7 @@ export declare class ActorOptimizeQueryOperationPruneEmptySourceOperations exten
|
|
|
13
13
|
run(action: IActionOptimizeQueryOperation): Promise<IActorOptimizeQueryOperationOutput>;
|
|
14
14
|
protected static hasEmptyOperation(operation: Algebra.Operation): boolean;
|
|
15
15
|
protected collectMultiOperationInputs(inputs: Algebra.Operation[], collectedOperations: (Algebra.Pattern | Algebra.Link)[], inputType: (Algebra.Pattern | Algebra.Link)['type']): void;
|
|
16
|
-
protected mapMultiOperation<O extends Algebra.Union | Algebra.Alt>(
|
|
17
|
-
result: Algebra.Operation;
|
|
18
|
-
recurse: boolean;
|
|
19
|
-
};
|
|
16
|
+
protected mapMultiOperation<O extends Algebra.Union | Algebra.Alt>(operationCopy: O, origOp: O, emptyOperations: Set<Algebra.Operation>, multiOperationFactory: (input: O['input']) => Algebra.Operation): Algebra.Operation;
|
|
20
17
|
/**
|
|
21
18
|
* Check if the given query operation will produce at least one result in the given source.
|
|
22
19
|
* @param algebraFactory The algebra factory.
|
|
@@ -24,7 +21,7 @@ export declare class ActorOptimizeQueryOperationPruneEmptySourceOperations exten
|
|
|
24
21
|
* @param input A query operation.
|
|
25
22
|
* @param context The query context.
|
|
26
23
|
*/
|
|
27
|
-
hasSourceResults(algebraFactory:
|
|
24
|
+
hasSourceResults(algebraFactory: AlgebraFactory, source: IQuerySourceWrapper, input: Algebra.Operation, context: IActionContext): Promise<boolean>;
|
|
28
25
|
}
|
|
29
26
|
export interface IActorOptimizeQueryOperationPruneEmptySourceOperationsArgs extends IActorOptimizeQueryOperationArgs {
|
|
30
27
|
/**
|
|
@@ -4,8 +4,8 @@ exports.ActorOptimizeQueryOperationPruneEmptySourceOperations = void 0;
|
|
|
4
4
|
const bus_optimize_query_operation_1 = require("@comunica/bus-optimize-query-operation");
|
|
5
5
|
const context_entries_1 = require("@comunica/context-entries");
|
|
6
6
|
const core_1 = require("@comunica/core");
|
|
7
|
+
const utils_algebra_1 = require("@comunica/utils-algebra");
|
|
7
8
|
const utils_query_operation_1 = require("@comunica/utils-query-operation");
|
|
8
|
-
const sparqlalgebrajs_1 = require("sparqlalgebrajs");
|
|
9
9
|
/**
|
|
10
10
|
* A comunica Prune Empty Source Operations Optimize Query Operation Actor.
|
|
11
11
|
*/
|
|
@@ -21,31 +21,27 @@ class ActorOptimizeQueryOperationPruneEmptySourceOperations extends bus_optimize
|
|
|
21
21
|
}
|
|
22
22
|
async run(action) {
|
|
23
23
|
const dataFactory = action.context.getSafe(context_entries_1.KeysInitQuery.dataFactory);
|
|
24
|
-
const algebraFactory = new
|
|
24
|
+
const algebraFactory = new utils_algebra_1.AlgebraFactory(dataFactory);
|
|
25
25
|
let operation = action.operation;
|
|
26
26
|
// Collect all operations with source types
|
|
27
27
|
// Only consider unions of patterns or alts of links, since these are created during exhaustive source assignment.
|
|
28
28
|
const collectedOperations = [];
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
},
|
|
40
|
-
[sparqlalgebrajs_1.Algebra.types.SERVICE]() {
|
|
41
|
-
return false;
|
|
42
|
-
},
|
|
29
|
+
utils_algebra_1.algebraUtils.visitOperation(operation, {
|
|
30
|
+
[utils_algebra_1.Algebra.Types.UNION]: { preVisitor: (subOperation) => {
|
|
31
|
+
this.collectMultiOperationInputs(subOperation.input, collectedOperations, utils_algebra_1.Algebra.Types.PATTERN);
|
|
32
|
+
return {};
|
|
33
|
+
} },
|
|
34
|
+
[utils_algebra_1.Algebra.Types.ALT]: { preVisitor: (subOperation) => {
|
|
35
|
+
this.collectMultiOperationInputs(subOperation.input, collectedOperations, utils_algebra_1.Algebra.Types.LINK);
|
|
36
|
+
return { continue: false };
|
|
37
|
+
} },
|
|
38
|
+
[utils_algebra_1.Algebra.Types.SERVICE]: { preVisitor: () => ({ continue: false }) },
|
|
43
39
|
});
|
|
44
40
|
// Determine in an async manner whether or not these sources return non-empty results
|
|
45
41
|
const emptyOperations = new Set();
|
|
46
42
|
await Promise.all(collectedOperations.map(async (collectedOperation) => {
|
|
47
|
-
const checkOperation = collectedOperation.type ===
|
|
48
|
-
algebraFactory.createPattern(dataFactory.variable('s'), collectedOperation.iri, dataFactory.variable('o')) :
|
|
43
|
+
const checkOperation = collectedOperation.type === utils_algebra_1.Algebra.Types.LINK ?
|
|
44
|
+
algebraFactory.createPattern(dataFactory.variable('s'), collectedOperation.iri, dataFactory.variable('?o')) :
|
|
49
45
|
collectedOperation;
|
|
50
46
|
if (!await this.hasSourceResults(algebraFactory, (0, utils_query_operation_1.getOperationSource)(collectedOperation), checkOperation, action.context)) {
|
|
51
47
|
emptyOperations.add(collectedOperation);
|
|
@@ -55,43 +51,27 @@ class ActorOptimizeQueryOperationPruneEmptySourceOperations extends bus_optimize
|
|
|
55
51
|
if (emptyOperations.size > 0) {
|
|
56
52
|
this.logDebug(action.context, `Pruning ${emptyOperations.size} source-specific operations`);
|
|
57
53
|
// Rewrite operations by removing the empty children
|
|
58
|
-
operation =
|
|
59
|
-
[
|
|
60
|
-
|
|
54
|
+
operation = utils_algebra_1.algebraUtils.mapOperation(operation, {
|
|
55
|
+
[utils_algebra_1.Algebra.Types.UNION]: { transform: (subOperation, origOp) => this.mapMultiOperation(subOperation, origOp, emptyOperations, children => algebraFactory.createUnion(children)) },
|
|
56
|
+
[utils_algebra_1.Algebra.Types.ALT]: { transform: (subOperation, origOp) => this.mapMultiOperation(subOperation, origOp, emptyOperations, children => algebraFactory.createAlt(children)) },
|
|
57
|
+
// Remove operations that have become empty now due to missing variables
|
|
58
|
+
[utils_algebra_1.Algebra.Types.PROJECT]: {
|
|
59
|
+
transform: (subOperation) => {
|
|
60
|
+
// Remove projections that have become empty now due to missing variables
|
|
61
|
+
if (ActorOptimizeQueryOperationPruneEmptySourceOperations.hasEmptyOperation(subOperation)) {
|
|
62
|
+
return algebraFactory.createUnion([]);
|
|
63
|
+
}
|
|
64
|
+
return subOperation;
|
|
65
|
+
},
|
|
61
66
|
},
|
|
62
|
-
[
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
if (ActorOptimizeQueryOperationPruneEmptySourceOperations.hasEmptyOperation(subOperation)) {
|
|
71
|
-
return {
|
|
72
|
-
recurse: false,
|
|
73
|
-
result: factory.createUnion([]),
|
|
74
|
-
};
|
|
75
|
-
}
|
|
76
|
-
return {
|
|
77
|
-
recurse: true,
|
|
78
|
-
result: subOperation,
|
|
79
|
-
};
|
|
80
|
-
},
|
|
81
|
-
[sparqlalgebrajs_1.Algebra.types.LEFT_JOIN](subOperation) {
|
|
82
|
-
// Remove left joins with empty right operation
|
|
83
|
-
if (ActorOptimizeQueryOperationPruneEmptySourceOperations.hasEmptyOperation(subOperation.input[1])) {
|
|
84
|
-
return {
|
|
85
|
-
recurse: true,
|
|
86
|
-
result: subOperation.input[0],
|
|
87
|
-
};
|
|
88
|
-
}
|
|
89
|
-
return {
|
|
90
|
-
recurse: true,
|
|
91
|
-
result: subOperation,
|
|
92
|
-
};
|
|
93
|
-
},
|
|
94
|
-
}, algebraFactory);
|
|
67
|
+
[utils_algebra_1.Algebra.Types.LEFT_JOIN]: { transform: (subOperation) => {
|
|
68
|
+
// Remove left joins with empty right operation
|
|
69
|
+
if (ActorOptimizeQueryOperationPruneEmptySourceOperations.hasEmptyOperation(subOperation.input[1])) {
|
|
70
|
+
return subOperation.input[0];
|
|
71
|
+
}
|
|
72
|
+
return subOperation;
|
|
73
|
+
} },
|
|
74
|
+
});
|
|
95
75
|
}
|
|
96
76
|
return { operation, context: action.context };
|
|
97
77
|
}
|
|
@@ -100,51 +80,59 @@ class ActorOptimizeQueryOperationPruneEmptySourceOperations extends bus_optimize
|
|
|
100
80
|
// But if we find a union with multiple children,
|
|
101
81
|
// *all* of the children must be empty before the full operation is considered empty.
|
|
102
82
|
let emptyOperation = false;
|
|
103
|
-
|
|
104
|
-
[
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
83
|
+
utils_algebra_1.algebraUtils.visitOperation(operation, {
|
|
84
|
+
[utils_algebra_1.Algebra.Types.UNION]: { preVisitor: (unionOp) => {
|
|
85
|
+
if (unionOp.input.every(subSubOperation => ActorOptimizeQueryOperationPruneEmptySourceOperations
|
|
86
|
+
.hasEmptyOperation(subSubOperation))) {
|
|
87
|
+
emptyOperation = true;
|
|
88
|
+
return { shortcut: true };
|
|
89
|
+
}
|
|
90
|
+
return { continue: false };
|
|
91
|
+
} },
|
|
92
|
+
[utils_algebra_1.Algebra.Types.LEFT_JOIN]: { preVisitor: (leftJoinOp) => {
|
|
93
|
+
// Only recurse into left part of left-join
|
|
94
|
+
if (ActorOptimizeQueryOperationPruneEmptySourceOperations.hasEmptyOperation(leftJoinOp.input[0])) {
|
|
95
|
+
emptyOperation = true;
|
|
96
|
+
return { shortcut: true };
|
|
97
|
+
}
|
|
98
|
+
return { continue: false };
|
|
99
|
+
} },
|
|
100
|
+
[utils_algebra_1.Algebra.Types.ALT]: { preVisitor: (altOp) => {
|
|
101
|
+
if (altOp.input.length === 0) {
|
|
102
|
+
emptyOperation = true;
|
|
103
|
+
return { shortcut: true };
|
|
104
|
+
}
|
|
105
|
+
return { continue: false };
|
|
106
|
+
} },
|
|
124
107
|
});
|
|
125
108
|
return emptyOperation;
|
|
126
109
|
}
|
|
127
110
|
collectMultiOperationInputs(inputs, collectedOperations, inputType) {
|
|
128
111
|
for (const input of inputs) {
|
|
129
|
-
if ((0, utils_query_operation_1.getOperationSource)(input) && input
|
|
112
|
+
if ((0, utils_query_operation_1.getOperationSource)(input) && (0, utils_algebra_1.isKnownOperation)(input, inputType)) {
|
|
130
113
|
collectedOperations.push(input);
|
|
131
114
|
}
|
|
132
115
|
}
|
|
133
116
|
}
|
|
134
|
-
mapMultiOperation(
|
|
117
|
+
mapMultiOperation(operationCopy, origOp, emptyOperations, multiOperationFactory) {
|
|
135
118
|
// Determine which operations return non-empty results
|
|
136
|
-
const nonEmptyInputs =
|
|
119
|
+
const nonEmptyInputs = [];
|
|
120
|
+
for (const [idx, input] of operationCopy.input.entries()) {
|
|
121
|
+
if (!emptyOperations.has(origOp.input[idx])) {
|
|
122
|
+
nonEmptyInputs.push(input);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
137
125
|
// Remove empty operations
|
|
138
|
-
if (nonEmptyInputs.length ===
|
|
139
|
-
return
|
|
126
|
+
if (nonEmptyInputs.length === operationCopy.input.length) {
|
|
127
|
+
return operationCopy;
|
|
140
128
|
}
|
|
141
129
|
if (nonEmptyInputs.length === 0) {
|
|
142
|
-
return
|
|
130
|
+
return multiOperationFactory([]);
|
|
143
131
|
}
|
|
144
132
|
if (nonEmptyInputs.length === 1) {
|
|
145
|
-
return
|
|
133
|
+
return nonEmptyInputs[0];
|
|
146
134
|
}
|
|
147
|
-
return
|
|
135
|
+
return multiOperationFactory(nonEmptyInputs);
|
|
148
136
|
}
|
|
149
137
|
/**
|
|
150
138
|
* Check if the given query operation will produce at least one result in the given source.
|
|
@@ -1 +1 @@
|
|
|
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,CAAC,IAAI,kDAAkD,CAAC,CAAC;QACxF,CAAC;QACD,OAAO,IAAA,mBAAY,GAAE,CAAC;IACxB,CAAC;IAEM,KAAK,CAAC,GAAG,CAAC,MAAqC;QACpD,MAAM,WAAW,GAAwB,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,+BAAa,CAAC,WAAW,CAAC,CAAC;QAC3F,MAAM,cAAc,GAAG,IAA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O,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"]}
|
|
1
|
+
{"version":3,"file":"ActorOptimizeQueryOperationPruneEmptySourceOperations.js","sourceRoot":"","sources":["ActorOptimizeQueryOperationPruneEmptySourceOperations.ts"],"names":[],"mappings":";;;AAKA,yFAAqF;AACrF,+DAAmF;AAEnF,yCAAwD;AAQxD,2DAAkG;AAClG,2EAA+F;AAE/F;;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,GA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,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;AA3ND,sHA2NC","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 { Algebra, AlgebraFactory, algebraUtils, isKnownOperation } from '@comunica/utils-algebra';\nimport { doesShapeAcceptOperation, getOperationSource } from '@comunica/utils-query-operation';\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 AlgebraFactory(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 algebraUtils.visitOperation(operation, {\n [Algebra.Types.UNION]: { preVisitor: (subOperation) => {\n this.collectMultiOperationInputs(subOperation.input, collectedOperations, Algebra.Types.PATTERN);\n return {};\n } },\n [Algebra.Types.ALT]: { preVisitor: (subOperation) => {\n this.collectMultiOperationInputs(subOperation.input, collectedOperations, Algebra.Types.LINK);\n return { continue: false };\n } },\n [Algebra.Types.SERVICE]: { preVisitor: () => ({ continue: false }) },\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 === Algebra.Types.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 = algebraUtils.mapOperation(operation, {\n [Algebra.Types.UNION]: { transform: (subOperation, origOp) =>\n this.mapMultiOperation(subOperation, origOp, emptyOperations, children =>\n algebraFactory.createUnion(children)) },\n [Algebra.Types.ALT]: { transform: (subOperation, origOp) =>\n this.mapMultiOperation(subOperation, origOp, emptyOperations, children =>\n algebraFactory.createAlt(children)) },\n\n // Remove operations that have become empty now due to missing variables\n [Algebra.Types.PROJECT]: {\n transform: (subOperation) => {\n // Remove projections that have become empty now due to missing variables\n if (ActorOptimizeQueryOperationPruneEmptySourceOperations.hasEmptyOperation(subOperation)) {\n return algebraFactory.createUnion([]);\n }\n return subOperation;\n },\n },\n [Algebra.Types.LEFT_JOIN]: { transform: (subOperation) => {\n // Remove left joins with empty right operation\n if (ActorOptimizeQueryOperationPruneEmptySourceOperations.hasEmptyOperation(subOperation.input[1])) {\n return subOperation.input[0];\n }\n return subOperation;\n } },\n });\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 algebraUtils.visitOperation(operation, {\n [Algebra.Types.UNION]: { preVisitor: (unionOp) => {\n if (unionOp.input.every(subSubOperation => ActorOptimizeQueryOperationPruneEmptySourceOperations\n .hasEmptyOperation(subSubOperation))) {\n emptyOperation = true;\n return { shortcut: true };\n }\n return { continue: false };\n } },\n [Algebra.Types.LEFT_JOIN]: { preVisitor: (leftJoinOp) => {\n // Only recurse into left part of left-join\n if (ActorOptimizeQueryOperationPruneEmptySourceOperations.hasEmptyOperation(leftJoinOp.input[0])) {\n emptyOperation = true;\n return { shortcut: true };\n }\n return { continue: false };\n } },\n [Algebra.Types.ALT]: { preVisitor: (altOp) => {\n if (altOp.input.length === 0) {\n emptyOperation = true;\n return { shortcut: true };\n }\n return { continue: 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) && isKnownOperation(input, inputType)) {\n collectedOperations.push(input);\n }\n }\n }\n\n protected mapMultiOperation<O extends Algebra.Union | Algebra.Alt>(\n operationCopy: O,\n origOp: O,\n emptyOperations: Set<Algebra.Operation>,\n multiOperationFactory: (input: O['input']) => Algebra.Operation,\n ): Algebra.Operation {\n // Determine which operations return non-empty results\n const nonEmptyInputs: Algebra.Operation[] = [];\n for (const [ idx, input ] of operationCopy.input.entries()) {\n if (!emptyOperations.has(origOp.input[idx])) {\n nonEmptyInputs.push(input);\n }\n }\n\n // Remove empty operations\n if (nonEmptyInputs.length === operationCopy.input.length) {\n return operationCopy;\n }\n if (nonEmptyInputs.length === 0) {\n return multiOperationFactory([]);\n }\n if (nonEmptyInputs.length === 1) {\n return nonEmptyInputs[0];\n }\n return multiOperationFactory(nonEmptyInputs);\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: AlgebraFactory,\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/lib/index.d.ts
CHANGED
|
File without changes
|
package/lib/index.js
CHANGED
|
File without changes
|
package/lib/index.js.map
CHANGED
|
File without changes
|
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.4.
|
|
3
|
+
"version": "4.4.2-alpha.49.0",
|
|
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": "
|
|
45
|
-
"@comunica/context-entries": "
|
|
46
|
-
"@comunica/core": "
|
|
47
|
-
"@comunica/types": "
|
|
48
|
-
"@comunica/utils-
|
|
49
|
-
"
|
|
44
|
+
"@comunica/bus-optimize-query-operation": "4.4.2-alpha.49.0",
|
|
45
|
+
"@comunica/context-entries": "4.4.2-alpha.49.0",
|
|
46
|
+
"@comunica/core": "4.4.2-alpha.49.0",
|
|
47
|
+
"@comunica/types": "4.4.2-alpha.49.0",
|
|
48
|
+
"@comunica/utils-algebra": "4.4.2-alpha.49.0",
|
|
49
|
+
"@comunica/utils-query-operation": "4.4.2-alpha.49.0"
|
|
50
50
|
},
|
|
51
|
-
"gitHead": "
|
|
51
|
+
"gitHead": "ef6f96cfd8faf7c37955bb7e0fe9f6fc6a994bdf"
|
|
52
52
|
}
|