@comunica/actor-optimize-query-operation-group-sources 3.2.2 → 3.2.4-alpha.47.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/ActorOptimizeQueryOperationGroupSources.jsonld +32 -0
- package/components/context.jsonld +6 -0
- package/lib/ActorOptimizeQueryOperationGroupSources.d.ts +2 -2
- package/lib/ActorOptimizeQueryOperationGroupSources.js +25 -24
- package/lib/ActorOptimizeQueryOperationGroupSources.js.map +1 -1
- package/package.json +12 -7
|
@@ -44,6 +44,10 @@
|
|
|
44
44
|
{
|
|
45
45
|
"@type": "ParameterRangeGenericTypeReference",
|
|
46
46
|
"parameterRangeGenericType": "npmd:@comunica/actor-abstract-mediatyped/^3.0.0/components/ActorAbstractMediaTyped.jsonld#ActorAbstractMediaTyped__generic_O"
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
"@type": "ParameterRangeGenericTypeReference",
|
|
50
|
+
"parameterRangeGenericType": "npmd:@comunica/actor-abstract-mediatyped/^3.0.0/components/ActorAbstractMediaTyped.jsonld#ActorAbstractMediaTyped__generic_TS"
|
|
47
51
|
}
|
|
48
52
|
]
|
|
49
53
|
},
|
|
@@ -58,6 +62,10 @@
|
|
|
58
62
|
{
|
|
59
63
|
"@type": "ParameterRangeGenericTypeReference",
|
|
60
64
|
"parameterRangeGenericType": "npmd:@comunica/actor-abstract-mediatyped/^3.0.0/components/ActorAbstractMediaTyped.jsonld#ActorAbstractMediaTyped__generic_O"
|
|
65
|
+
},
|
|
66
|
+
{
|
|
67
|
+
"@type": "ParameterRangeGenericTypeReference",
|
|
68
|
+
"parameterRangeGenericType": "npmd:@comunica/actor-abstract-mediatyped/^3.0.0/components/ActorAbstractMediaTyped.jsonld#ActorAbstractMediaTyped__generic_TS"
|
|
61
69
|
}
|
|
62
70
|
]
|
|
63
71
|
},
|
|
@@ -67,6 +75,20 @@
|
|
|
67
75
|
},
|
|
68
76
|
"comment": "The bus this actor subscribes to."
|
|
69
77
|
},
|
|
78
|
+
{
|
|
79
|
+
"@id": "caoqogs:components/ActorOptimizeQueryOperationGroupSources.jsonld#ActorOptimizeQueryOperationGroupSources_args_busFailMessage",
|
|
80
|
+
"range": {
|
|
81
|
+
"@type": "ParameterRangeUnion",
|
|
82
|
+
"parameterRangeElements": [
|
|
83
|
+
"xsd:string",
|
|
84
|
+
{
|
|
85
|
+
"@type": "ParameterRangeUndefined"
|
|
86
|
+
}
|
|
87
|
+
]
|
|
88
|
+
},
|
|
89
|
+
"default": "Query optimization failed: none of the configured actors were able to optimize",
|
|
90
|
+
"comment": "The message that will be configured in the bus for reporting failures. This message may be a template string that contains references to the executed `action`. For example, the following templated string is allowed: \"RDF dereferencing failed: no actors could handle ${action.handle.mediaType}\""
|
|
91
|
+
},
|
|
70
92
|
{
|
|
71
93
|
"@id": "caoqogs:components/ActorOptimizeQueryOperationGroupSources.jsonld#ActorOptimizeQueryOperationGroupSources_args_beforeActors",
|
|
72
94
|
"range": {
|
|
@@ -89,6 +111,10 @@
|
|
|
89
111
|
{
|
|
90
112
|
"@type": "ParameterRangeGenericTypeReference",
|
|
91
113
|
"parameterRangeGenericType": "npmd:@comunica/actor-abstract-mediatyped/^3.0.0/components/ActorAbstractMediaTyped.jsonld#ActorAbstractMediaTyped__generic_O"
|
|
114
|
+
},
|
|
115
|
+
{
|
|
116
|
+
"@type": "ParameterRangeGenericTypeReference",
|
|
117
|
+
"parameterRangeGenericType": "npmd:@comunica/actor-abstract-mediatyped/^3.0.0/components/ActorAbstractMediaTyped.jsonld#ActorAbstractMediaTyped__generic_TS"
|
|
92
118
|
}
|
|
93
119
|
]
|
|
94
120
|
}
|
|
@@ -147,6 +173,12 @@
|
|
|
147
173
|
"@id": "caoqogs:components/ActorOptimizeQueryOperationGroupSources.jsonld#ActorOptimizeQueryOperationGroupSources_args_bus"
|
|
148
174
|
}
|
|
149
175
|
},
|
|
176
|
+
{
|
|
177
|
+
"keyRaw": "busFailMessage",
|
|
178
|
+
"value": {
|
|
179
|
+
"@id": "caoqogs:components/ActorOptimizeQueryOperationGroupSources.jsonld#ActorOptimizeQueryOperationGroupSources_args_busFailMessage"
|
|
180
|
+
}
|
|
181
|
+
},
|
|
150
182
|
{
|
|
151
183
|
"keyRaw": "beforeActors",
|
|
152
184
|
"value": {
|
|
@@ -14,6 +14,9 @@
|
|
|
14
14
|
"args_bus": {
|
|
15
15
|
"@id": "caoqogs:components/ActorOptimizeQueryOperationGroupSources.jsonld#ActorOptimizeQueryOperationGroupSources_args_bus"
|
|
16
16
|
},
|
|
17
|
+
"args_busFailMessage": {
|
|
18
|
+
"@id": "caoqogs:components/ActorOptimizeQueryOperationGroupSources.jsonld#ActorOptimizeQueryOperationGroupSources_args_busFailMessage"
|
|
19
|
+
},
|
|
17
20
|
"args_beforeActors": {
|
|
18
21
|
"@id": "caoqogs:components/ActorOptimizeQueryOperationGroupSources.jsonld#ActorOptimizeQueryOperationGroupSources_args_beforeActors",
|
|
19
22
|
"@container": "@list"
|
|
@@ -24,6 +27,9 @@
|
|
|
24
27
|
"bus": {
|
|
25
28
|
"@id": "caoqogs:components/ActorOptimizeQueryOperationGroupSources.jsonld#ActorOptimizeQueryOperationGroupSources_args_bus"
|
|
26
29
|
},
|
|
30
|
+
"busFailMessage": {
|
|
31
|
+
"@id": "caoqogs:components/ActorOptimizeQueryOperationGroupSources.jsonld#ActorOptimizeQueryOperationGroupSources_args_busFailMessage"
|
|
32
|
+
},
|
|
27
33
|
"beforeActors": {
|
|
28
34
|
"@id": "caoqogs:components/ActorOptimizeQueryOperationGroupSources.jsonld#ActorOptimizeQueryOperationGroupSources_args_beforeActors",
|
|
29
35
|
"@container": "@list"
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { IActionOptimizeQueryOperation, IActorOptimizeQueryOperationOutput, IActorOptimizeQueryOperationArgs } from '@comunica/bus-optimize-query-operation';
|
|
2
2
|
import { ActorOptimizeQueryOperation } from '@comunica/bus-optimize-query-operation';
|
|
3
|
-
import type { IActorTest } from '@comunica/core';
|
|
3
|
+
import type { IActorTest, TestResult } from '@comunica/core';
|
|
4
4
|
import type { IActionContext, IQuerySourceWrapper } from '@comunica/types';
|
|
5
5
|
import { Algebra } from 'sparqlalgebrajs';
|
|
6
6
|
/**
|
|
@@ -8,7 +8,7 @@ import { Algebra } from 'sparqlalgebrajs';
|
|
|
8
8
|
*/
|
|
9
9
|
export declare class ActorOptimizeQueryOperationGroupSources extends ActorOptimizeQueryOperation {
|
|
10
10
|
constructor(args: IActorOptimizeQueryOperationArgs);
|
|
11
|
-
test(action: IActionOptimizeQueryOperation): Promise<IActorTest
|
|
11
|
+
test(action: IActionOptimizeQueryOperation): Promise<TestResult<IActorTest>>;
|
|
12
12
|
run(action: IActionOptimizeQueryOperation): Promise<IActorOptimizeQueryOperationOutput>;
|
|
13
13
|
/**
|
|
14
14
|
* Group operations belonging to the same source together, only if that source accepts the grouped operations.
|
|
@@ -2,9 +2,10 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ActorOptimizeQueryOperationGroupSources = void 0;
|
|
4
4
|
const bus_optimize_query_operation_1 = require("@comunica/bus-optimize-query-operation");
|
|
5
|
-
const
|
|
5
|
+
const context_entries_1 = require("@comunica/context-entries");
|
|
6
|
+
const core_1 = require("@comunica/core");
|
|
7
|
+
const utils_query_operation_1 = require("@comunica/utils-query-operation");
|
|
6
8
|
const sparqlalgebrajs_1 = require("sparqlalgebrajs");
|
|
7
|
-
const AF = new sparqlalgebrajs_1.Factory();
|
|
8
9
|
/**
|
|
9
10
|
* A comunica Group Sources Optimize Query Operation Actor.
|
|
10
11
|
*/
|
|
@@ -13,10 +14,10 @@ class ActorOptimizeQueryOperationGroupSources extends bus_optimize_query_operati
|
|
|
13
14
|
super(args);
|
|
14
15
|
}
|
|
15
16
|
async test(action) {
|
|
16
|
-
if (
|
|
17
|
-
|
|
17
|
+
if ((0, utils_query_operation_1.getOperationSource)(action.operation)) {
|
|
18
|
+
return (0, core_1.failTest)(`Actor ${this.name} does not work with top-level operation sources.`);
|
|
18
19
|
}
|
|
19
|
-
return
|
|
20
|
+
return (0, core_1.passTestVoid)();
|
|
20
21
|
}
|
|
21
22
|
async run(action) {
|
|
22
23
|
return { operation: await this.groupOperation(action.operation, action.context), context: action.context };
|
|
@@ -29,20 +30,21 @@ class ActorOptimizeQueryOperationGroupSources extends bus_optimize_query_operati
|
|
|
29
30
|
* @param context The action context.
|
|
30
31
|
*/
|
|
31
32
|
async groupOperation(operation, context) {
|
|
33
|
+
const dataFactory = context.getSafe(context_entries_1.KeysInitQuery.dataFactory);
|
|
34
|
+
const algebraFactory = new sparqlalgebrajs_1.Factory(dataFactory);
|
|
32
35
|
// Return operation as-is if the operation already has a single source, or if the operation has no children.
|
|
33
|
-
if (
|
|
36
|
+
if ((0, utils_query_operation_1.getOperationSource)(operation) ?? !('input' in operation)) {
|
|
34
37
|
return operation;
|
|
35
38
|
}
|
|
36
39
|
// If operation has a single input, move source annotation upwards if the source can handle it.
|
|
37
40
|
if (!Array.isArray(operation.input)) {
|
|
38
41
|
const groupedInput = await this.groupOperation(operation.input, context);
|
|
39
42
|
if (groupedInput.metadata?.scopedSource) {
|
|
40
|
-
const source =
|
|
41
|
-
if (
|
|
42
|
-
.doesShapeAcceptOperation(await source.source.getSelectorShape(context), operation)) {
|
|
43
|
+
const source = (0, utils_query_operation_1.getOperationSource)(groupedInput);
|
|
44
|
+
if ((0, utils_query_operation_1.doesShapeAcceptOperation)(await source.source.getSelectorShape(context), operation)) {
|
|
43
45
|
this.logDebug(context, `Hoist 1 source-specific operation into a single ${operation.type} operation for ${source.source.toString()}`);
|
|
44
|
-
|
|
45
|
-
operation =
|
|
46
|
+
(0, utils_query_operation_1.removeOperationSource)(groupedInput);
|
|
47
|
+
operation = (0, utils_query_operation_1.assignOperationSource)(operation, source);
|
|
46
48
|
}
|
|
47
49
|
}
|
|
48
50
|
return { ...operation, input: groupedInput };
|
|
@@ -54,7 +56,7 @@ class ActorOptimizeQueryOperationGroupSources extends bus_optimize_query_operati
|
|
|
54
56
|
// If we just have a single cluster, move the source annotation upwards
|
|
55
57
|
if (clusters.length === 1) {
|
|
56
58
|
const newInputs = clusters[0];
|
|
57
|
-
const source =
|
|
59
|
+
const source = (0, utils_query_operation_1.getOperationSource)(clusters[0][0]);
|
|
58
60
|
return {
|
|
59
61
|
...await this.moveSourceAnnotationUpwardsIfPossible(operation, newInputs, source, context),
|
|
60
62
|
input: newInputs,
|
|
@@ -68,16 +70,16 @@ class ActorOptimizeQueryOperationGroupSources extends bus_optimize_query_operati
|
|
|
68
70
|
let multiFactoryMethod;
|
|
69
71
|
switch (operation.type) {
|
|
70
72
|
case sparqlalgebrajs_1.Algebra.types.JOIN:
|
|
71
|
-
multiFactoryMethod =
|
|
73
|
+
multiFactoryMethod = algebraFactory.createJoin.bind(algebraFactory);
|
|
72
74
|
break;
|
|
73
75
|
case sparqlalgebrajs_1.Algebra.types.UNION:
|
|
74
|
-
multiFactoryMethod =
|
|
76
|
+
multiFactoryMethod = algebraFactory.createUnion.bind(algebraFactory);
|
|
75
77
|
break;
|
|
76
78
|
case sparqlalgebrajs_1.Algebra.types.ALT:
|
|
77
|
-
multiFactoryMethod =
|
|
79
|
+
multiFactoryMethod = algebraFactory.createAlt.bind(algebraFactory);
|
|
78
80
|
break;
|
|
79
81
|
case sparqlalgebrajs_1.Algebra.types.SEQ:
|
|
80
|
-
multiFactoryMethod =
|
|
82
|
+
multiFactoryMethod = algebraFactory.createSeq.bind(algebraFactory);
|
|
81
83
|
break;
|
|
82
84
|
default:
|
|
83
85
|
// While LeftJoin and Minus are also multi-operations,
|
|
@@ -92,10 +94,10 @@ class ActorOptimizeQueryOperationGroupSources extends bus_optimize_query_operati
|
|
|
92
94
|
async groupOperationMulti(clusters, factoryMethod, context) {
|
|
93
95
|
let flatten = true;
|
|
94
96
|
const nestedMerges = await Promise.all(clusters.map(async (cluster) => {
|
|
95
|
-
const source =
|
|
97
|
+
const source = (0, utils_query_operation_1.getOperationSource)(cluster[0]);
|
|
96
98
|
const merged = await this
|
|
97
99
|
.moveSourceAnnotationUpwardsIfPossible(factoryMethod(cluster, true), cluster, source, context);
|
|
98
|
-
if (
|
|
100
|
+
if ((0, utils_query_operation_1.getOperationSource)(merged)) {
|
|
99
101
|
flatten = false;
|
|
100
102
|
}
|
|
101
103
|
return merged;
|
|
@@ -112,7 +114,7 @@ class ActorOptimizeQueryOperationGroupSources extends bus_optimize_query_operati
|
|
|
112
114
|
const sourcelessOperations = [];
|
|
113
115
|
// Cluster by source
|
|
114
116
|
for (const operation of operationsIn) {
|
|
115
|
-
const source =
|
|
117
|
+
const source = (0, utils_query_operation_1.getOperationSource)(operation);
|
|
116
118
|
if (source) {
|
|
117
119
|
if (!sourceOperations.has(source)) {
|
|
118
120
|
sourceOperations.set(source, []);
|
|
@@ -130,7 +132,7 @@ class ActorOptimizeQueryOperationGroupSources extends bus_optimize_query_operati
|
|
|
130
132
|
}
|
|
131
133
|
for (const [source, operations] of sourceOperations.entries()) {
|
|
132
134
|
clusters.push(operations
|
|
133
|
-
.map(operation =>
|
|
135
|
+
.map(operation => (0, utils_query_operation_1.assignOperationSource)(operation, source)));
|
|
134
136
|
}
|
|
135
137
|
return clusters;
|
|
136
138
|
}
|
|
@@ -144,12 +146,11 @@ class ActorOptimizeQueryOperationGroupSources extends bus_optimize_query_operati
|
|
|
144
146
|
* @param context The action context.
|
|
145
147
|
*/
|
|
146
148
|
async moveSourceAnnotationUpwardsIfPossible(operation, inputs, source, context) {
|
|
147
|
-
if (source &&
|
|
148
|
-
.doesShapeAcceptOperation(await source.source.getSelectorShape(context), operation)) {
|
|
149
|
+
if (source && (0, utils_query_operation_1.doesShapeAcceptOperation)(await source.source.getSelectorShape(context), operation)) {
|
|
149
150
|
this.logDebug(context, `Hoist ${inputs.length} source-specific operations into a single ${operation.type} operation for ${source.source.toString()}`);
|
|
150
|
-
operation =
|
|
151
|
+
operation = (0, utils_query_operation_1.assignOperationSource)(operation, source);
|
|
151
152
|
for (const input of inputs) {
|
|
152
|
-
|
|
153
|
+
(0, utils_query_operation_1.removeOperationSource)(input);
|
|
153
154
|
}
|
|
154
155
|
}
|
|
155
156
|
return operation;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ActorOptimizeQueryOperationGroupSources.js","sourceRoot":"","sources":["ActorOptimizeQueryOperationGroupSources.ts"],"names":[],"mappings":";;;AAKA,yFAAqF;AACrF,uEAAoE;AAGpE,qDAAmD;AAEnD,MAAM,EAAE,GAAG,IAAI,yBAAO,EAAE,CAAC;AAEzB;;GAEG;AACH,MAAa,uCAAwC,SAAQ,0DAA2B;IACtF,YAAmB,IAAsC;QACvD,KAAK,CAAC,IAAI,CAAC,CAAC;IACd,CAAC;IAEM,KAAK,CAAC,IAAI,CAAC,MAAqC;QACrD,IAAI,yCAAmB,CAAC,kBAAkB,CAAC,MAAM,CAAC,SAAS,CAAC,EAAE,CAAC;YAC7D,MAAM,IAAI,KAAK,CAAC,SAAS,IAAI,CAAC,IAAI,kDAAkD,CAAC,CAAC;QACxF,CAAC;QACD,OAAO,IAAI,CAAC;IACd,CAAC;IAEM,KAAK,CAAC,GAAG,CAAC,MAAqC;QACpD,OAAO,EAAE,SAAS,EAAE,MAAM,IAAI,CAAC,cAAc,CAAC,MAAM,CAAC,SAAS,EAAE,MAAM,CAAC,OAAO,CAAC,EAAE,OAAO,EAAE,MAAM,CAAC,OAAO,EAAE,CAAC;IAC7G,CAAC;IAED;;;;;;OAMG;IACI,KAAK,CAAC,cAAc,CAAC,SAA4B,EAAE,OAAuB;QAC/E,4GAA4G;QAC5G,IAAI,yCAAmB,CAAC,kBAAkB,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,OAAO,IAAI,SAAS,CAAC,EAAE,CAAC;YACjF,OAAO,SAAS,CAAC;QACnB,CAAC;QAED,+FAA+F;QAC/F,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,SAAS,CAAC,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n IActorOptimizeQueryOperationOutput,\n IActorOptimizeQueryOperationArgs,\n} from '@comunica/bus-optimize-query-operation';\nimport { ActorOptimizeQueryOperation } from '@comunica/bus-optimize-query-operation';\nimport { ActorQueryOperation } from '@comunica/bus-query-operation';\nimport type { IActorTest } from '@comunica/core';\nimport type { IActionContext, IQuerySourceWrapper } from '@comunica/types';\nimport { Algebra, Factory } from 'sparqlalgebrajs';\n\nconst AF = new Factory();\n\n/**\n * A comunica Group Sources Optimize Query Operation Actor.\n */\nexport class ActorOptimizeQueryOperationGroupSources extends ActorOptimizeQueryOperation {\n public constructor(args: IActorOptimizeQueryOperationArgs) {\n super(args);\n }\n\n public async test(action: IActionOptimizeQueryOperation): Promise<IActorTest> {\n if (ActorQueryOperation.getOperationSource(action.operation)) {\n throw new Error(`Actor ${this.name} does not work with top-level operation sources.`);\n }\n return true;\n }\n\n public async run(action: IActionOptimizeQueryOperation): Promise<IActorOptimizeQueryOperationOutput> {\n return { operation: await this.groupOperation(action.operation, action.context), context: action.context };\n }\n\n /**\n * Group operations belonging to the same source together, only if that source accepts the grouped operations.\n * This grouping will be done recursively for the whole operation tree.\n * Operations annotated with sources are considered leaves in the tree.\n * @param operation An operation to group.\n * @param context The action context.\n */\n public async groupOperation(operation: Algebra.Operation, context: IActionContext): Promise<Algebra.Operation> {\n // Return operation as-is if the operation already has a single source, or if the operation has no children.\n if (ActorQueryOperation.getOperationSource(operation) ?? !('input' in operation)) {\n return operation;\n }\n\n // If operation has a single input, move source annotation upwards if the source can handle it.\n if (!Array.isArray(operation.input)) {\n const groupedInput = await this.groupOperation(operation.input, context);\n if (groupedInput.metadata?.scopedSource) {\n const source: IQuerySourceWrapper = ActorQueryOperation.getOperationSource(groupedInput)!;\n if (ActorQueryOperation\n .doesShapeAcceptOperation(await source.source.getSelectorShape(context), operation)) {\n this.logDebug(context, `Hoist 1 source-specific operation into a single ${operation.type} operation for ${source.source.toString()}`);\n ActorQueryOperation.removeOperationSource(groupedInput);\n operation = ActorQueryOperation.assignOperationSource(operation, source);\n }\n }\n return <Algebra.Operation> { ...operation, input: groupedInput };\n }\n\n // If operation has multiple inputs, cluster source annotations.\n const inputs: Algebra.Operation[] = await Promise.all(operation.input\n .map(subInput => this.groupOperation(subInput, context)));\n const clusters = this.clusterOperationsWithEqualSources(inputs);\n\n // If we just have a single cluster, move the source annotation upwards\n if (clusters.length === 1) {\n const newInputs = clusters[0];\n const source = ActorQueryOperation.getOperationSource(clusters[0][0])!;\n return <Algebra.Operation> {\n ...await this.moveSourceAnnotationUpwardsIfPossible(operation, newInputs, source, context),\n input: newInputs,\n };\n }\n\n // If the number of clusters is equal to the number of original inputs, do nothing.\n if (clusters.length === inputs.length) {\n return <Algebra.Operation> { ...operation, input: inputs };\n }\n\n // If we have multiple clusters, created nested multi-operations\n let multiFactoryMethod: (children: Algebra.Operation[], flatten: boolean) => Algebra.Operation;\n switch (operation.type) {\n case Algebra.types.JOIN:\n multiFactoryMethod = AF.createJoin.bind(AF);\n break;\n case Algebra.types.UNION:\n multiFactoryMethod = AF.createUnion.bind(AF);\n break;\n case Algebra.types.ALT:\n multiFactoryMethod = <any> AF.createAlt.bind(AF);\n break;\n case Algebra.types.SEQ:\n multiFactoryMethod = <any> AF.createSeq.bind(AF);\n break;\n default:\n // While LeftJoin and Minus are also multi-operations,\n // these can never occur because they only have 2 inputs,\n // so these cases will always be captured by one of the 2 if-cases above\n // (clusters.length === 1 or clusters.length === input.length)\n\n // In all other cases, error\n throw new Error(`Unsupported operation '${operation.type}' detected while grouping sources`);\n }\n return await this.groupOperationMulti(clusters, multiFactoryMethod, context);\n }\n\n protected async groupOperationMulti(\n clusters: Algebra.Operation[][],\n factoryMethod: (children: Algebra.Operation[], flatten: boolean) => Algebra.Operation,\n context: IActionContext,\n ): Promise<Algebra.Operation> {\n let flatten = true;\n const nestedMerges = await Promise.all(clusters.map(async(cluster) => {\n const source = ActorQueryOperation.getOperationSource(cluster[0])!;\n const merged = await this\n .moveSourceAnnotationUpwardsIfPossible(factoryMethod(cluster, true), cluster, source, context);\n if (ActorQueryOperation.getOperationSource(merged)) {\n flatten = false;\n }\n return merged;\n }));\n return factoryMethod(nestedMerges, flatten);\n }\n\n /**\n * Cluster the given operations by equal source annotations.\n * @param operationsIn An array of operations to cluster.\n */\n public clusterOperationsWithEqualSources(operationsIn: Algebra.Operation[]): Algebra.Operation[][] {\n // Operations can have a source, or no source at all\n const sourceOperations: Map<IQuerySourceWrapper, Algebra.Operation[]> = new Map();\n const sourcelessOperations: Algebra.Operation[] = [];\n\n // Cluster by source\n for (const operation of operationsIn) {\n const source: IQuerySourceWrapper = ActorQueryOperation.getOperationSource(operation)!;\n if (source) {\n if (!sourceOperations.has(source)) {\n sourceOperations.set(source, []);\n }\n sourceOperations.get(source)!.push(operation);\n } else {\n sourcelessOperations.push(operation);\n }\n }\n\n // Return clusters\n const clusters: Algebra.Operation[][] = [];\n if (sourcelessOperations.length > 0) {\n clusters.push(sourcelessOperations);\n }\n for (const [ source, operations ] of sourceOperations.entries()) {\n clusters.push(operations\n .map(operation => ActorQueryOperation.assignOperationSource(operation, source)));\n }\n return clusters;\n }\n\n /**\n * If the given source accepts the grouped operation, annotate the grouped operation with the source,\n * and remove the source annotation from the seperate input operations.\n * Otherwise, return the grouped operation unchanged.\n * @param operation A grouped operation consisting of all given input operations.\n * @param inputs An array of operations that share the same source annotation.\n * @param source The common source.\n * @param context The action context.\n */\n public async moveSourceAnnotationUpwardsIfPossible<O extends Algebra.Operation>(\n operation: O,\n inputs: Algebra.Operation[],\n source: IQuerySourceWrapper | undefined,\n context: IActionContext,\n ): Promise<O> {\n if (source && ActorQueryOperation\n .doesShapeAcceptOperation(await source.source.getSelectorShape(context), operation)) {\n this.logDebug(context, `Hoist ${inputs.length} source-specific operations into a single ${operation.type} operation for ${source.source.toString()}`);\n operation = ActorQueryOperation.assignOperationSource(operation, source);\n for (const input of inputs) {\n ActorQueryOperation.removeOperationSource(input);\n }\n }\n return operation;\n }\n}\n"]}
|
|
1
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AC3C,IAAI,oBAAoB,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YACpC,QAAQ,CAAC,IAAI,CAAC,oBAAoB,CAAC,CAAC;QACtC,CAAC;QACD,KAAK,MAAM,CAAE,MAAM,EAAE,UAAU,CAAE,IAAI,gBAAgB,CAAC,OAAO,EAAE,EAAE,CAAC;YAChE,QAAQ,CAAC,IAAI,CAAC,UAAU;iBACrB,GAAG,CAAC,SAAS,CAAC,EAAE,CAAC,IAAA,6CAAqB,EAAC,SAAS,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC;QACjE,CAAC;QACD,OAAO,QAAQ,CAAC;IAClB,CAAC;IAED;;;;;;;;OAQG;IACI,KAAK,CAAC,qCAAqC,CAChD,SAAY,EACZ,MAA2B,EAC3B,MAAuC,EACvC,OAAuB;QAEvB,IAAI,MAAM,IAAI,IAAA,gDAAwB,EAAC,MAAM,MAAM,CAAC,MAAM,CAAC,gBAAgB,CAAC,OAAO,CAAC,EAAE,SAAS,CAAC,EAAE,CAAC;YACjG,IAAI,CAAC,QAAQ,CAAC,OAAO,EAAE,SAAS,MAAM,CAAC,MAAM,6CAA6C,SAAS,CAAC,IAAI,kBAAkB,MAAM,CAAC,MAAM,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC;YACtJ,SAAS,GAAG,IAAA,6CAAqB,EAAC,SAAS,EAAE,MAAM,CAAC,CAAC;YACrD,KAAK,MAAM,KAAK,IAAI,MAAM,EAAE,CAAC;gBAC3B,IAAA,6CAAqB,EAAC,KAAK,CAAC,CAAC;YAC/B,CAAC;QACH,CAAC;QACD,OAAO,SAAS,CAAC;IACnB,CAAC;CACF;AAzKD,0FAyKC","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 } from '@comunica/context-entries';\nimport type { IActorTest, TestResult } from '@comunica/core';\nimport { failTest, passTestVoid } from '@comunica/core';\nimport type { ComunicaDataFactory, IActionContext, IQuerySourceWrapper } from '@comunica/types';\nimport {\n assignOperationSource,\n doesShapeAcceptOperation,\n getOperationSource,\n removeOperationSource,\n} from '@comunica/utils-query-operation';\nimport { Algebra, Factory } from 'sparqlalgebrajs';\n\n/**\n * A comunica Group Sources Optimize Query Operation Actor.\n */\nexport class ActorOptimizeQueryOperationGroupSources extends ActorOptimizeQueryOperation {\n public constructor(args: IActorOptimizeQueryOperationArgs) {\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 return { operation: await this.groupOperation(action.operation, action.context), context: action.context };\n }\n\n /**\n * Group operations belonging to the same source together, only if that source accepts the grouped operations.\n * This grouping will be done recursively for the whole operation tree.\n * Operations annotated with sources are considered leaves in the tree.\n * @param operation An operation to group.\n * @param context The action context.\n */\n public async groupOperation(operation: Algebra.Operation, context: IActionContext): Promise<Algebra.Operation> {\n const dataFactory: ComunicaDataFactory = context.getSafe(KeysInitQuery.dataFactory);\n const algebraFactory = new Factory(dataFactory);\n\n // Return operation as-is if the operation already has a single source, or if the operation has no children.\n if (getOperationSource(operation) ?? !('input' in operation)) {\n return operation;\n }\n\n // If operation has a single input, move source annotation upwards if the source can handle it.\n if (!Array.isArray(operation.input)) {\n const groupedInput = await this.groupOperation(operation.input, context);\n if (groupedInput.metadata?.scopedSource) {\n const source: IQuerySourceWrapper = <IQuerySourceWrapper> getOperationSource(groupedInput);\n if (doesShapeAcceptOperation(await source.source.getSelectorShape(context), operation)) {\n this.logDebug(context, `Hoist 1 source-specific operation into a single ${operation.type} operation for ${source.source.toString()}`);\n removeOperationSource(groupedInput);\n operation = assignOperationSource(operation, source);\n }\n }\n return <Algebra.Operation> { ...operation, input: groupedInput };\n }\n\n // If operation has multiple inputs, cluster source annotations.\n const inputs: Algebra.Operation[] = await Promise.all(operation.input\n .map(subInput => this.groupOperation(subInput, context)));\n const clusters = this.clusterOperationsWithEqualSources(inputs);\n\n // If we just have a single cluster, move the source annotation upwards\n if (clusters.length === 1) {\n const newInputs = clusters[0];\n const source = getOperationSource(clusters[0][0])!;\n return <Algebra.Operation> {\n ...await this.moveSourceAnnotationUpwardsIfPossible(operation, newInputs, source, context),\n input: newInputs,\n };\n }\n\n // If the number of clusters is equal to the number of original inputs, do nothing.\n if (clusters.length === inputs.length) {\n return <Algebra.Operation> { ...operation, input: inputs };\n }\n\n // If we have multiple clusters, created nested multi-operations\n let multiFactoryMethod: (children: Algebra.Operation[], flatten: boolean) => Algebra.Operation;\n switch (operation.type) {\n case Algebra.types.JOIN:\n multiFactoryMethod = algebraFactory.createJoin.bind(algebraFactory);\n break;\n case Algebra.types.UNION:\n multiFactoryMethod = algebraFactory.createUnion.bind(algebraFactory);\n break;\n case Algebra.types.ALT:\n multiFactoryMethod = <any> algebraFactory.createAlt.bind(algebraFactory);\n break;\n case Algebra.types.SEQ:\n multiFactoryMethod = <any> algebraFactory.createSeq.bind(algebraFactory);\n break;\n default:\n // While LeftJoin and Minus are also multi-operations,\n // these can never occur because they only have 2 inputs,\n // so these cases will always be captured by one of the 2 if-cases above\n // (clusters.length === 1 or clusters.length === input.length)\n\n // In all other cases, error\n throw new Error(`Unsupported operation '${operation.type}' detected while grouping sources`);\n }\n return await this.groupOperationMulti(clusters, multiFactoryMethod, context);\n }\n\n protected async groupOperationMulti(\n clusters: Algebra.Operation[][],\n factoryMethod: (children: Algebra.Operation[], flatten: boolean) => Algebra.Operation,\n context: IActionContext,\n ): Promise<Algebra.Operation> {\n let flatten = true;\n const nestedMerges = await Promise.all(clusters.map(async(cluster) => {\n const source = getOperationSource(cluster[0])!;\n const merged = await this\n .moveSourceAnnotationUpwardsIfPossible(factoryMethod(cluster, true), cluster, source, context);\n if (getOperationSource(merged)) {\n flatten = false;\n }\n return merged;\n }));\n return factoryMethod(nestedMerges, flatten);\n }\n\n /**\n * Cluster the given operations by equal source annotations.\n * @param operationsIn An array of operations to cluster.\n */\n public clusterOperationsWithEqualSources(operationsIn: Algebra.Operation[]): Algebra.Operation[][] {\n // Operations can have a source, or no source at all\n const sourceOperations: Map<IQuerySourceWrapper, Algebra.Operation[]> = new Map();\n const sourcelessOperations: Algebra.Operation[] = [];\n\n // Cluster by source\n for (const operation of operationsIn) {\n const source = getOperationSource(operation);\n if (source) {\n if (!sourceOperations.has(source)) {\n sourceOperations.set(source, []);\n }\n sourceOperations.get(source)!.push(operation);\n } else {\n sourcelessOperations.push(operation);\n }\n }\n\n // Return clusters\n const clusters: Algebra.Operation[][] = [];\n if (sourcelessOperations.length > 0) {\n clusters.push(sourcelessOperations);\n }\n for (const [ source, operations ] of sourceOperations.entries()) {\n clusters.push(operations\n .map(operation => assignOperationSource(operation, source)));\n }\n return clusters;\n }\n\n /**\n * If the given source accepts the grouped operation, annotate the grouped operation with the source,\n * and remove the source annotation from the seperate input operations.\n * Otherwise, return the grouped operation unchanged.\n * @param operation A grouped operation consisting of all given input operations.\n * @param inputs An array of operations that share the same source annotation.\n * @param source The common source.\n * @param context The action context.\n */\n public async moveSourceAnnotationUpwardsIfPossible<O extends Algebra.Operation>(\n operation: O,\n inputs: Algebra.Operation[],\n source: IQuerySourceWrapper | undefined,\n context: IActionContext,\n ): Promise<O> {\n if (source && doesShapeAcceptOperation(await source.source.getSelectorShape(context), operation)) {\n this.logDebug(context, `Hoist ${inputs.length} source-specific operations into a single ${operation.type} operation for ${source.source.toString()}`);\n operation = assignOperationSource(operation, source);\n for (const input of inputs) {\n removeOperationSource(input);\n }\n }\n return operation;\n }\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@comunica/actor-optimize-query-operation-group-sources",
|
|
3
|
-
"version": "3.2.
|
|
3
|
+
"version": "3.2.4-alpha.47.0",
|
|
4
4
|
"description": "A group-sources optimize-query-operation actor",
|
|
5
5
|
"lsd:module": true,
|
|
6
6
|
"license": "MIT",
|
|
7
|
+
"funding": {
|
|
8
|
+
"type": "opencollective",
|
|
9
|
+
"url": "https://opencollective.com/comunica-association"
|
|
10
|
+
},
|
|
7
11
|
"homepage": "https://comunica.dev/",
|
|
8
12
|
"repository": {
|
|
9
13
|
"type": "git",
|
|
@@ -37,11 +41,12 @@
|
|
|
37
41
|
"build:components": "componentsjs-generator"
|
|
38
42
|
},
|
|
39
43
|
"dependencies": {
|
|
40
|
-
"@comunica/bus-optimize-query-operation": "
|
|
41
|
-
"@comunica/
|
|
42
|
-
"@comunica/core": "
|
|
43
|
-
"@comunica/types": "
|
|
44
|
-
"
|
|
44
|
+
"@comunica/bus-optimize-query-operation": "3.2.4-alpha.47.0",
|
|
45
|
+
"@comunica/context-entries": "3.2.4-alpha.47.0",
|
|
46
|
+
"@comunica/core": "3.2.4-alpha.47.0",
|
|
47
|
+
"@comunica/types": "3.2.4-alpha.47.0",
|
|
48
|
+
"@comunica/utils-query-operation": "3.2.4-alpha.47.0",
|
|
49
|
+
"sparqlalgebrajs": "^4.3.8"
|
|
45
50
|
},
|
|
46
|
-
"gitHead": "
|
|
51
|
+
"gitHead": "85bd4c5cf07dfc293ebbc3a1416b70e2db8bfc48"
|
|
47
52
|
}
|