@comunica/actor-optimize-query-operation-group-sources 4.4.1 → 4.5.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.
@@ -166,11 +166,10 @@ class ActorOptimizeQueryOperationGroupSources extends bus_optimize_query_operati
166
166
  if ((0, utils_query_operation_1.doesShapeAcceptOperation)(shape, operation, { wildcardAcceptAllExtensionFunctions })) {
167
167
  const extensionFunctions = context.get(context_entries_1.KeysInitQuery.extensionFunctions);
168
168
  const expression = operation.expression;
169
- if (!extensionFunctions || expression?.expressionType !== sparqlalgebrajs_1.Algebra.expressionTypes.NAMED ||
169
+ return !extensionFunctions ||
170
+ expression?.expressionType !== sparqlalgebrajs_1.Algebra.expressionTypes.NAMED ||
170
171
  !(expression?.name.value in extensionFunctions) ||
171
- (0, utils_query_operation_1.doesShapeAcceptOperation)(shape, expression, { wildcardAcceptAllExtensionFunctions })) {
172
- return true;
173
- }
172
+ (0, utils_query_operation_1.doesShapeAcceptOperation)(shape, expression, { wildcardAcceptAllExtensionFunctions });
174
173
  }
175
174
  return false;
176
175
  }
@@ -1 +1 @@
1
- {"version":3,"file":"ActorOptimizeQueryOperationGroupSources.js","sourceRoot":"","sources":["ActorOptimizeQueryOperationGroupSources.ts"],"names":[],"mappings":";;;AAKA,yFAAqF;AACrF,+DAA0D;AAE1D,yCAAwD;AAExD,2EAKyC;AACzC,qDAAmD;AAEnD;;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,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,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,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;IAED;;;;;;;;;OASG;IACI,uCAAuC,CAC5C,SAAY,EACZ,KAA4B,EAC5B,OAAuB;QAEvB,MAAM,mCAAmC,GAAG,OAAO,CAAC,GAAG,CAAC,+BAAa,CAAC,gCAAgC,CAAC,CAAC;QACxG,IAAI,IAAA,gDAAwB,EAAC,KAAK,EAAE,SAAS,EAAE,EAAE,mCAAmC,EAAE,CAAC,EAAE,CAAC;YACxF,MAAM,kBAAkB,GAAG,OAAO,CAAC,GAAG,CAAC,+BAAa,CAAC,kBAAkB,CAAC,CAAC;YACzE,MAAM,UAAU,GAAmC,SAAS,CAAC,UAAU,CAAC;YACxE,IAAI,CAAC,kBAAkB,IAAI,UAAU,EAAE,cAAc,KAAK,yBAAO,CAAC,eAAe,CAAC,KAAK;gBACrF,CAAC,CAAC,UAAU,EAAE,IAAI,CAAC,KAAK,IAAI,kBAAkB,CAAC;gBAC/C,IAAA,gDAAwB,EAAC,KAAK,EAAE,UAAU,EAAE,EAAE,mCAAmC,EAAE,CAAC,EAAE,CAAC;gBACvF,OAAO,IAAI,CAAC;YACd,CAAC;QACH,CAAC;QACD,OAAO,KAAK,CAAC;IACf,CAAC;CACF;AArMD,0FAqMC","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, FragmentSelectorShape, 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 operation = await this.moveSourceAnnotationUpwardsIfPossible(operation, [ groupedInput ], source, context);\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 && this.isPossibleToMoveSourceAnnotationUpwards(\n operation,\n await source.source.getSelectorShape(context),\n context,\n )) {\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 /**\n * Checks if it's possible to move the source annotation upwards using the following rules:\n * - If the shape doesn't accept the operation, then it's not possible.\n * - If it does and the operation does not contain extension functions or\n * comunica doesn't support them, then it's possible.\n * - If comunica does support them, then it's possible only if the shape accepts the extension function expressions.\n * @param operation A grouped operation consisting of all given input operations.\n * @param shape The common source's shape.\n * @param context The action context.\n */\n public isPossibleToMoveSourceAnnotationUpwards<O extends Algebra.Operation>(\n operation: O,\n shape: FragmentSelectorShape,\n context: IActionContext,\n ): boolean {\n const wildcardAcceptAllExtensionFunctions = context.get(KeysInitQuery.extensionFunctionsAlwaysPushdown);\n if (doesShapeAcceptOperation(shape, operation, { wildcardAcceptAllExtensionFunctions })) {\n const extensionFunctions = context.get(KeysInitQuery.extensionFunctions);\n const expression: Algebra.Expression | undefined = operation.expression;\n if (!extensionFunctions || expression?.expressionType !== Algebra.expressionTypes.NAMED ||\n !(expression?.name.value in extensionFunctions) ||\n doesShapeAcceptOperation(shape, expression, { wildcardAcceptAllExtensionFunctions })) {\n return true;\n }\n }\n return false;\n }\n}\n"]}
1
+ {"version":3,"file":"ActorOptimizeQueryOperationGroupSources.js","sourceRoot":"","sources":["ActorOptimizeQueryOperationGroupSources.ts"],"names":[],"mappings":";;;AAKA,yFAAqF;AACrF,+DAA0D;AAE1D,yCAAwD;AAExD,2EAKyC;AACzC,qDAAmD;AAEnD;;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,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,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;Q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Y,EACZ,KAA4B,EAC5B,OAAuB;QAEvB,MAAM,mCAAmC,GAAG,OAAO,CAAC,GAAG,CAAC,+BAAa,CAAC,gCAAgC,CAAC,CAAC;QACxG,IAAI,IAAA,gDAAwB,EAAC,KAAK,EAAE,SAAS,EAAE,EAAE,mCAAmC,EAAE,CAAC,EAAE,CAAC;YACxF,MAAM,kBAAkB,GAAG,OAAO,CAAC,GAAG,CAAC,+BAAa,CAAC,kBAAkB,CAAC,CAAC;YACzE,MAAM,UAAU,GAAmC,SAAS,CAAC,UAAU,CAAC;YACxE,OAAO,CAAC,kBAAkB;gBACxB,UAAU,EAAE,cAAc,KAAK,yBAAO,CAAC,eAAe,CAAC,KAAK;gBAC5D,CAAC,CAAC,UAAU,EAAE,IAAI,CAAC,KAAK,IAAI,kBAAkB,CAAC;gBAC/C,IAAA,gDAAwB,EAAC,KAAK,EAAE,UAAU,EAAE,EAAE,mCAAmC,EAAE,CAAC,CAAC;QACzF,CAAC;QACD,OAAO,KAAK,CAAC;IACf,CAAC;CACF;AApMD,0FAoMC","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, FragmentSelectorShape, 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 operation = await this.moveSourceAnnotationUpwardsIfPossible(operation, [ groupedInput ], source, context);\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 && this.isPossibleToMoveSourceAnnotationUpwards(\n operation,\n await source.source.getSelectorShape(context),\n context,\n )) {\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 /**\n * Checks if it's possible to move the source annotation upwards using the following rules:\n * - If the shape doesn't accept the operation, then it's not possible.\n * - If it does and the operation does not contain extension functions or\n * comunica doesn't support them, then it's possible.\n * - If comunica does support them, then it's possible only if the shape accepts the extension function expressions.\n * @param operation A grouped operation consisting of all given input operations.\n * @param shape The common source's shape.\n * @param context The action context.\n */\n public isPossibleToMoveSourceAnnotationUpwards<O extends Algebra.Operation>(\n operation: O,\n shape: FragmentSelectorShape,\n context: IActionContext,\n ): boolean {\n const wildcardAcceptAllExtensionFunctions = context.get(KeysInitQuery.extensionFunctionsAlwaysPushdown);\n if (doesShapeAcceptOperation(shape, operation, { wildcardAcceptAllExtensionFunctions })) {\n const extensionFunctions = context.get(KeysInitQuery.extensionFunctions);\n const expression: Algebra.Expression | undefined = operation.expression;\n return !extensionFunctions ||\n expression?.expressionType !== Algebra.expressionTypes.NAMED ||\n !(expression?.name.value in extensionFunctions) ||\n doesShapeAcceptOperation(shape, expression, { wildcardAcceptAllExtensionFunctions });\n }\n return false;\n }\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@comunica/actor-optimize-query-operation-group-sources",
3
- "version": "4.4.1",
3
+ "version": "4.5.0",
4
4
  "description": "A group-sources 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.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
- "sparqlalgebrajs": "^4.3.8"
44
+ "@comunica/bus-optimize-query-operation": "^4.5.0",
45
+ "@comunica/context-entries": "^4.5.0",
46
+ "@comunica/core": "^4.5.0",
47
+ "@comunica/types": "^4.5.0",
48
+ "@comunica/utils-query-operation": "^4.5.0",
49
+ "sparqlalgebrajs": "^5.0.2"
50
50
  },
51
- "gitHead": "c5cc36caf88da31173a0969a7da88cadb7f469ac"
51
+ "gitHead": "2bcd98c387a021fc5c08d375793c205ca3d1bf0d"
52
52
  }