From edfbb300a32cb7c2314e7a58718457cef8231a7e Mon Sep 17 00:00:00 2001 From: Joachim Van Herwegen Date: Mon, 30 Oct 2023 08:39:59 +0100 Subject: [PATCH] Correctly convert FROM/CONSTRUCT algebra --- lib/sparql.ts | 6 +- .../sparql-1.1/construct/construct-from.json | 65 +++++++++++++++++++ .../sparql-1.1/construct/construct-from.json | 65 +++++++++++++++++++ .../construct/construct-from.sparql | 4 ++ 4 files changed, 139 insertions(+), 1 deletion(-) create mode 100644 test/algebra-blank-to-var/sparql-1.1/construct/construct-from.json create mode 100644 test/algebra/sparql-1.1/construct/construct-from.json create mode 100644 test/sparql/sparql-1.1/construct/construct-from.sparql diff --git a/lib/sparql.ts b/lib/sparql.ts index 12a4b7c..ccc1567 100644 --- a/lib/sparql.ts +++ b/lib/sparql.ts @@ -279,8 +279,12 @@ function translateExtend(op: Algebra.Extend): any function translateFrom(op: Algebra.From): GroupPattern { const result = translateOperation(op.input); + // Can't type as CONSTRUCT queries do not have `from` field in their type + let obj = result; // project is nested in group object - const obj: SelectQuery = result.patterns[0]; + if (result.type === 'group') { + obj = result.patterns[0]; + } obj.from = { default: op.default, named: op.named diff --git a/test/algebra-blank-to-var/sparql-1.1/construct/construct-from.json b/test/algebra-blank-to-var/sparql-1.1/construct/construct-from.json new file mode 100644 index 0000000..63bdb8d --- /dev/null +++ b/test/algebra-blank-to-var/sparql-1.1/construct/construct-from.json @@ -0,0 +1,65 @@ +{ + "default": [ + { + "termType": "NamedNode", + "value": "http://example.com/data" + } + ], + "input": { + "input": { + "input": { + "patterns": [ + { + "graph": { + "termType": "DefaultGraph", + "value": "" + }, + "object": { + "termType": "Variable", + "value": "o" + }, + "predicate": { + "termType": "Variable", + "value": "p" + }, + "subject": { + "termType": "Variable", + "value": "s" + }, + "termType": "Quad", + "type": "pattern" + } + ], + "type": "bgp" + }, + "template": [ + { + "graph": { + "termType": "DefaultGraph", + "value": "" + }, + "object": { + "termType": "Variable", + "value": "o" + }, + "predicate": { + "termType": "Variable", + "value": "p" + }, + "subject": { + "termType": "Variable", + "value": "s" + }, + "termType": "Quad", + "type": "pattern" + } + ], + "type": "construct" + }, + "length": 100, + "start": 0, + "type": "slice" + }, + "named": [], + "type": "from" +} diff --git a/test/algebra/sparql-1.1/construct/construct-from.json b/test/algebra/sparql-1.1/construct/construct-from.json new file mode 100644 index 0000000..63bdb8d --- /dev/null +++ b/test/algebra/sparql-1.1/construct/construct-from.json @@ -0,0 +1,65 @@ +{ + "default": [ + { + "termType": "NamedNode", + "value": "http://example.com/data" + } + ], + "input": { + "input": { + "input": { + "patterns": [ + { + "graph": { + "termType": "DefaultGraph", + "value": "" + }, + "object": { + "termType": "Variable", + "value": "o" + }, + "predicate": { + "termType": "Variable", + "value": "p" + }, + "subject": { + "termType": "Variable", + "value": "s" + }, + "termType": "Quad", + "type": "pattern" + } + ], + "type": "bgp" + }, + "template": [ + { + "graph": { + "termType": "DefaultGraph", + "value": "" + }, + "object": { + "termType": "Variable", + "value": "o" + }, + "predicate": { + "termType": "Variable", + "value": "p" + }, + "subject": { + "termType": "Variable", + "value": "s" + }, + "termType": "Quad", + "type": "pattern" + } + ], + "type": "construct" + }, + "length": 100, + "start": 0, + "type": "slice" + }, + "named": [], + "type": "from" +} diff --git a/test/sparql/sparql-1.1/construct/construct-from.sparql b/test/sparql/sparql-1.1/construct/construct-from.sparql new file mode 100644 index 0000000..c3ab93f --- /dev/null +++ b/test/sparql/sparql-1.1/construct/construct-from.sparql @@ -0,0 +1,4 @@ +CONSTRUCT { ?s ?p ?o. } +FROM +WHERE { ?s ?p ?o. } +LIMIT 100