Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Put BIND requests back in query when required #120

Merged
merged 1 commit into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions lib/sparql.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { isomorphic } from 'rdf-isomorphic';
import * as RDF from '@rdfjs/types'
import { termToString } from 'rdf-string';
import { stringToTerm, termToString } from 'rdf-string';
import {
AggregateExpression,
BgpPattern,
Expand Down Expand Up @@ -483,7 +483,7 @@ function translateProject(op: Algebra.Project | Algebra.Ask | Algebra.Describe,
aggregators[translateTerm(agg.variable)] = translateExpression(agg);

// do these in reverse order since variables in one extend might apply to an expression in an other extend
let extensions: any = {};
let extensions: Record<string, any> = {};
for (let i = context.extend.length-1; i >= 0; --i)
{
let e = context.extend[i];
Expand Down Expand Up @@ -514,17 +514,35 @@ function translateProject(op: Algebra.Project | Algebra.Ask | Algebra.Describe,
{
select.variables = variables.map((term): Variable => {
let v = translateTerm(term);
if (extensions[v])
if (extensions[v]) {
let result = extensions[v];
delete extensions[v]; // remove used extensions so only unused ones remain
return {
variable : term,
expression: extensions[v]
variable: term,
expression: result,
};
}
return term;
});
// if the * didn't match any variables this would be empty
if (select.variables.length === 0)
select.variables = [new Wildcard()];
}

// It is possible that at this point some extensions have not yet been resolved.
// These would be bind operations that are not used in a GROUP BY or SELECT body.
// We still need to add them though, as they could be relevant to the other extensions.
const extensionEntries = Object.entries(extensions);
if (extensionEntries.length > 0) {
select.where = select.where || [];
for (const [ key, value ] of extensionEntries) {
select.where.push({
type: 'bind',
variable: stringToTerm(key) as RDF.Variable,
expression: value
});
}
}


// convert filter to 'having' if it contains an aggregator variable
Expand Down
99 changes: 99 additions & 0 deletions test/algebra/sparql11-query/unused-extend.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
{
"type": "project",
"input": {
"type": "extend",
"input": {
"type": "extend",
"input": {
"type": "bgp",
"patterns": [
{
"type": "pattern",
"termType": "Quad",
"subject": {
"termType": "Variable",
"value": "a"
},
"predicate": {
"termType": "NamedNode",
"value": "http://mydom#startTime"
},
"object": {
"termType": "Variable",
"value": "st"
},
"graph": {
"termType": "DefaultGraph",
"value": ""
}
},
{
"type": "pattern",
"termType": "Quad",
"subject": {
"termType": "Variable",
"value": "a"
},
"predicate": {
"termType": "NamedNode",
"value": "http://mydom#endTime"
},
"object": {
"termType": "Variable",
"value": "et"
},
"graph": {
"termType": "DefaultGraph",
"value": ""
}
}
]
},
"variable": {
"termType": "Variable",
"value": "duration"
},
"expression": {
"type": "expression",
"expressionType": "operator",
"operator": "-",
"args": [
{
"type": "expression",
"expressionType": "term",
"term": {
"termType": "Variable",
"value": "et"
}
},
{
"type": "expression",
"expressionType": "term",
"term": {
"termType": "Variable",
"value": "st"
}
}
]
}
},
"variable": {
"termType": "Variable",
"value": "d"
},
"expression": {
"type": "expression",
"expressionType": "term",
"term": {
"termType": "Variable",
"value": "duration"
}
}
},
"variables": [
{
"termType": "Variable",
"value": "d"
}
]
}
7 changes: 7 additions & 0 deletions test/sparql/sparql11-query/unused-extend.sparql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
PREFIX : <http://mydom#>
SELECT (?duration as ?d) WHERE {
?a :startTime ?st;
:endTime ?et.

BIND( (?et - ?st) as ?duration)
}
Loading