sparql.anything icon indicating copy to clipboard operation
sparql.anything copied to clipboard

Injecting code dynamically within the SPARQL query for manipulating data

Open luigi-asprino opened this issue 3 years ago • 7 comments

We can use for example a python interpreter for running the code

luigi-asprino avatar Nov 29 '21 11:11 luigi-asprino

I think Apache Jena's value functions are pretty convenient. I used that feature here.

Do you have in mind something like the following?

select * where {
fx:properties fx:location "https://sparql-anything.cc/example1.json" .
?s ?p ?o .
fx:properties fx:custom-code "filter(lambda x: x not in 'aeiou', o)" 
}

Assuming variables bound in SPARQL get passed to the python environment.

justin2004 avatar Dec 07 '21 02:12 justin2004

Might not be too much to implement with something like: https://www.jython.org/

justin2004 avatar Dec 09 '21 02:12 justin2004

A custom magic property approach could be expanded to also pass parameters to Python functions, returning data can then be expressed as Facade-X. In other words, one could use the external component as an intermediate processor of the data source.

E.g. JSON File > Python function (returns any Python object) > Facade-X representation to be queried in the service clause.

This may require the development of a small vocabulary:

  • Support inline lambdas/functions
  • Refer to an external source code/function
  • Pass parameters to the function

enridaga avatar Jan 17 '22 10:01 enridaga

Apparently, APache Pig uses Jython for the same reason (custom functions): https://pig.apache.org/docs/r0.17.0/udf.html#jython-udfs

enridaga avatar Jan 17 '22 10:01 enridaga

Note that Jython currently only supports Python 2.7 (Python 3.8 is under development but without a clear timeline: https://github.com/jython/jython/issues/24#issuecomment-670022103)

Another project to look into is GraalVM for Python https://github.com/oracle/graalpython/blob/master/docs/user/Jython.md

enridaga avatar Jan 17 '22 10:01 enridaga

Maybe there is value in considering supporting script invocation (e.g. via a SH terminal) with the assumption that STDOUT shall return any of the supported SPARQL Anything formats.

enridaga avatar Jan 17 '22 10:01 enridaga

I am not sure where we want to go with this one.

  • Jena has a number of convenient ways to extend the library of functions (e.g. using Javascript)
  • Is there a use case that cannot be easily developed with just sending the data to an external script with the fx:command option?

enridaga avatar Apr 07 '22 16:04 enridaga