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

Fix SparqlHandler to refrain from returning DELETE DATA with variables #353

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

andrewzhurov
Copy link

Closes #352

@andrewzhurov
Copy link
Author

@jeswr Let me know if there are any adjustments you'd like to have.

Copy link
Member

@jeswr jeswr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your work @andrewzhurov ! Just have a couple of comments.

mutationType,
// Add DATA clause if there are no variables in mutation expression,
// that is so when subject is specified (not coming from a WHERE clause) and all predicateObjects have predicates and objects
where.length === 0 && arePredicateObjectsSpecified(predicateObjects) ? ' DATA ' : ' ',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC there is nothing stopping us from instantiating the subject as a variable - can you check this and if so also include the DATA when the subject is a variable.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I gather, according to the spec INSERT DATA must not allow variables in quad patterns, so when there is a subject variable it's not INSERT DATA but INSERT.

When there are no where and predicate objects are specified - there are no vars and we're safe to use INSERT DATA, it seems.

@@ -82,13 +82,19 @@ export default class SparqlHandler {
mutations.push(...this.triplePatterns(subject, predicate, objectStrings, reverse));
}
const mutationClauses = `{\n ${mutations.join('\n ')}\n}`;
function arePredicateObjectsSpecified(predicateObjects2) {
return predicateObjects2.filter(({ predicate, objects }) => objects === null || predicate === null).length === 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return predicateObjects2.filter(({ predicate, objects }) => objects === null || predicate === null).length === 0;
return predicateObjects2.some(({ predicate, objects }) => objects === null || predicate === null);

Copy link
Member

@jeswr jeswr May 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this a function rather than just assigning const variablePredicateOrObject = predicateObjects.some(({ predicate, objects }) => objects === null || predicate === null); and using that const later on

Furthermore objects is a list so you may need to check if each element in that array could be a variable. I'm not currently in a position to check for myself if it is possible for those objects to be variables - so could you quickly investigate this yourself?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a function and not a const in order to not execute it ahead of time, but only after where.length === 0 predicate passes, to give us a bit of performance. And it's a function and not an inline expression to give a bit of readability.
There is some! Gotta use that. JS is not my first language.:)

I'll give objects a look, thanks for showing the gotcha

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding objects, it seems we either have them undefined altogether, or values present (a value cannot be undefined).

@andrewzhurov
Copy link
Author

An expression like this db.fov.set(90), results in such SPARQL

DELETE {
  <http://example.com/db> <ex:fov> ?fov.
}
;
INSERT DATA {
  <http://example.com/db> <ex:fov> "90"^^<http://www.w3.org/2001/XMLSchema#integer>.
}

which is, interestingly, consider invalid by sparql.js

SparqlParser.js:675 Uncaught (in promise) Error: Parse error on line 3:
...b> <ex:fov> ?fov.};INSERT DATA {  <h
---------------------^
Expecting 'WHERE', 'INSERT', 'USING', got ';'
    at Parser.parseError (SparqlParser.js:675:25)
    at Parser.parse (SparqlParser.js:742:22)
    at Parser.parser.parse (sparql.js:37:37)
    at ActorQueryParseSparql.run (ActorQueryParseSparql.js:24:37)
    at ActorQueryParseSparql.runObservable (Actor.js:58:29)
    at MediatorRace.mediate (Mediator.js:80:22)
    at async QueryEngine.queryOrExplain (QueryEngineBase.js:105:38)
    at async QueryEngine.query (QueryEngineBase.js:43:24)
    at async QueryEngine.queryOfType (QueryEngineBase.js:30:24)

Although it seems to be valid, according to the spec

@jeswr jeswr self-requested a review October 16, 2023 10:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

.set("val").sparql may result in DELETE DATA query with variables
2 participants