-
Notifications
You must be signed in to change notification settings - Fork 66
Add Option to Keep Query Prefixes #187
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
base: main
Are you sure you want to change the base?
Add Option to Keep Query Prefixes #187
Conversation
* Added an option to the generator to keep query prefixes even if they are not used in the query
Hi @RubenVerborgh, metaphacts contributed this fix and we would highly appreciate if it would be included in and distributed through a new sparql.js release. Do you have any plans/schedule for the next release or other dependencies? Just asking for planning purpose. Thanks |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@vitalis-wiens Thanks for this PR! I like the case at #186, but have some different suggestions for implementation.
The current proposal has a weird interaction between allPrefixes
and keepQueryPrefixes
, and does not leave room for an intermediate case where the user wants some prefixes to always be present, and some prefixes to only be present when used.
What do you think of an interface along the lines of { allPrefixes: ['db_opt'] }
as a third option, where:
false
means only prefixes that are actually usedtrue
means all prefixes, regardless of usageString[]
means always include the listed prefixes, others as used
An alternative proposal could be { fixedPrefixes: { db_opt: 'https://db.control.param_10' } }
.
We'll also need an addition to the README.
lib/SparqlGenerator.js
Outdated
@@ -90,10 +90,12 @@ Generator.prototype.toQuery = function (q) { | |||
Generator.prototype.baseAndPrefixes = function (q) { | |||
var base = q.base ? ('BASE <' + q.base + '>' + this._newline) : ''; | |||
var prefixes = ''; | |||
var queryPrefixes=Object.keys(q.prefixes || {}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
var queryPrefixes=Object.keys(q.prefixes || {}); | |
var queryPrefixes = Object.keys(q.prefixes || {}); |
lib/SparqlGenerator.js
Outdated
@@ -451,6 +453,7 @@ function mapJoin(array, sep, func, self) { | |||
/** | |||
* @param options { | |||
* allPrefixes: boolean, | |||
* keepQueryPrefixes: boolean, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you call it fixedPrefixes
and make it a key/value object?
test/SparqlGenerator-test.js
Outdated
var parsedQuery = parser.parse('PREFIX db_opt: <https://db.control.param_10> \n SELECT * WHERE {?s a owl:Class}'); | ||
var generator = new SparqlGenerator({allPrefixes: false, keepQueryPrefixes: true}); | ||
var generatedQuery = generator.stringify(parsedQuery); | ||
var expectedQuery ='PREFIX db_opt: <https://db.control.param_10>\nPREFIX owl: <http://www.w3.org/2002/07/owl#>\nSELECT * WHERE { ?s <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> owl:Class. }'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
var expectedQuery ='PREFIX db_opt: <https://db.control.param_10>\nPREFIX owl: <http://www.w3.org/2002/07/owl#>\nSELECT * WHERE { ?s <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> owl:Class. }'; | |
var expectedQuery = 'PREFIX db_opt: <https://db.control.param_10>\nPREFIX owl: <http://www.w3.org/2002/07/owl#>\nSELECT * WHERE { ?s <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> owl:Class. }'; |
thanks for the quick response and the additional perspective. I can follow your reasoning of allowing the user to provide a list of "fixedPrefixes" that are always added to the query (independent of their usages inside the query). Whether this is added by overloading the Our original use-case / improvement request is however slightly different from "fixedPrefixes": we want to configure the generator to explicitly keep all those prefixes in the query prologue that are defined by the user (i.e., in the original query string). The reason for that in our case is that some database vendors use specialized prefixes as a kind of query hint. In addition (what we are already able to do now): we want to inject a fixed list of prefixes, but from those only keep the ones that are used in the query. Note that this is already technically possible now in sparqljs. Considering this: what do you think of the following abstract implementation sketch on configuration level
@vitalis-wiens would be able to do an iteration on the PR if this proposal makes sense to you |
I don't understand the interaction between the three options? Are they fully independent? In particular, the generation has no notion about |
* Added option to the generator to use a fixed prefix list which is added to generated query string * Adjusted tests for the new option
Hi @RubenVerborgh I have prepared a second commit with the suggested implementation for a new options parameter I will try to summarize the different use-cases, expected behavior and interactions between the options
Possible Combinations of options parameters
In our use case we would like to go for the version where we have used prefixes and the ones defined in the query. Form debugging and experimenting this are maybe relevant pointers:
Note: In the current implementation I assume that the fixed prefixes is a subset of all prefixes I hope this explanation is helpfull to find a path forward On a side note: For the next release could you also consider the following PR #184 ? |
@vitalis-wiens Thanks for the detailed explanation! Regarding the table, unless I'm missing something, we have 8 possible input combinations, but only 4 possible output combinations (and I actually think that's 3, because used + fixed + query prefixes sounds like simply “all” to me). That's why I proposed the more simple configuration above.
The misunderstanding might stem from the fact that all of your tests (and perhaps your use case) start from I think this simple extension of the |
lib/SparqlGenerator.js
Outdated
prefixes += 'PREFIX ' + key + ': <' + q.prefixes[key] + '>' + this._newline; | ||
} | ||
// ensure that we do not add prefixes multiple times | ||
if (addedPrefixes.includes(key)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How could that happen when q.prefixes
does not change?
The option `allPrefixes` can now receive an object for the list of prefixes This prefixes will be added to the query string if they are either provided by the query or are defined in the Parser
Hi @RubenVerborgh, thanks for the explanation and the pointer about use case I have now revised the PR:
I have also revised the tests
|
Hi @RubenVerborgh , did you have a chance to review the latest iteration / refinement from Vitalis? We would ideally want to integrate the new version of sparqljs with an improvement for this issue in our upcoming release, where we are aiming for end of March. Any feedback highly appreciated allowing us to align the plans |
Thanks; will get to this ASAP; I'm a volunteer on this and it's teaching semester. |
This PR extends the options paramaters of the SparqlGenerator.
The newly introduced option parameter
keepQueryPrefixes
allows to combine the used prefixes of the query body and the once mentioned in the Prefix defintions.The use-case is the following:
Some database vendors can provide hints to the database using Prefixes, however the current available options (used Prefixes or all Prefixes) either ignore the optimization prefixes or add all available prefixes which results in a very large prefix definitions and more challenging debuging of queries.
Related Issue: #186