Skip to content

GH-5314: add validation check for construct query template #5321

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

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from

Conversation

linnaung
Copy link
Contributor

GitHub issue resolved: #5314

Briefly describe the changes proposed in this PR:

add validation check for construct query template


PR Author Checklist (see the contributor guidelines for more details):

  • my pull request is self-contained
  • I've added tests for the changes I made
  • I've applied code formatting (you can use mvn process-resources to format from the command line)
  • I've squashed my commits where necessary
  • every commit message starts with the issue number (GH-xxxx) followed by a meaningful description of the change

@linnaung linnaung force-pushed the GH-5314-add-property-paths-validation-for-construct-query branch 2 times, most recently from c38d1ab to 2ae8560 Compare April 25, 2025 16:09
@linnaung linnaung force-pushed the GH-5314-add-property-paths-validation-for-construct-query branch from 2ae8560 to e2bf139 Compare April 25, 2025 16:15
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2015 Eclipse RDF4J contributors, Aduna, and others.
* Copyright (c) 2025 Eclipse RDF4J contributors.
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you revert the copyright headers?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done.


// recursively check child nodes
for (int i = 0; i < node.jjtGetNumChildren(); i++) {
if (isInvalidConstructQueryTemplate(node.jjtGetChild(i), isInConstructTemplate)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you have an example query that recurses several levels deep?

Copy link
Contributor Author

@linnaung linnaung May 7, 2025

Choose a reason for hiding this comment

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

yes, it's provided as an invalid test case.

@Test
public void testInvalidConstructQueryWithZeroOrMorePathInPredicate() {
String invalidSparqlQuery = "PREFIX foaf: <http://xmlns.com/foaf/0.1/> " +
		"CONSTRUCT {\n" +
		"  ?s  foaf:knows* ?o . " +
		"}\n" +
		"WHERE {\n" +
		"  ?s  foaf:knows* ?o  .\n" +
		"} ";
assertThrows(MalformedQueryException.class, () -> {
	parser.parseQuery(invalidSparqlQuery, null);
});
}

@Test
public void testInvalidConstructQueryWithZeroOrMorePathInPredicates() {
String invalidSparqlQuery = "PREFIX foaf: <http://xmlns.com/foaf/0.1/> " +
		"CONSTRUCT {\n" +
		"  ?s  foaf:knows ?o . " +
		"  ?s  foaf:name+ ?o . " +
		"}\n" +
		"WHERE {\n" +
		"  ?s  foaf:knows* ?o  .\n" +
		"  ?s foaf:name ?o .\n" +
		"} ";
assertThrows(MalformedQueryException.class, () -> {
	parser.parseQuery(invalidSparqlQuery, null);
});
}

@aschwarte10
Copy link
Contributor

@hmottestad is this a piece of code where you are familiar with to do a review?

A bit of background on why we propose the change: as many others we are currently integrating capabilities of LLMs. And the LLM sometimes produces property path expressions in the CONSTRUCT block. We now ideally want to detect such syntactical errors using static validation and with RDF4J library support

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.

3 participants