<docbook><section><title>VirtTipsAndTricksGuideCustomSPARQLExtensionFunction</title><title> Are custom SPARQL extension functions supported?  How can I use them?</title> Are custom SPARQL extension functions supported?  How can I use them?
<para>Virtuoso supports two ways to write one&#39;s own custom <ulink url="http://www.w3.org/TR/rdf-sparql-query/#extensionFunctions">SPARQL extension functions</ulink>:</para>
<bridgehead class="http://www.w3.org/1999/xhtml:h2"> Use a Stored Procedure</bridgehead>
<para>You can create a SQL stored procedure in Virtuoso PL and, call it using the <emphasis>sql:</emphasis> prefix: </para>
<programlisting>SQL&gt; CREATE PROCEDURE testfunc()
  {
    -- your code here
  }
;

Done;

SQL&gt; SPARQL 
SELECT * 
  WHERE
    { 
      ?s  ?p  `sql:testfunc(?o)` 
    };
</programlisting><para> For more details see <ulink url="http://docs.openlinksw.com/virtuoso/rdfsparql.html#rdfsqlfromsparql">Calling SQL from SPARQL</ulink> Virtuoso Documentation.</para>
<bridgehead class="http://www.w3.org/1999/xhtml:h2">  Create a built-in function</bridgehead>
<para>You can also make a built-in function, which is basically a C function that can be called from both SQL and SPARQL, as in the example from below, using the <emphasis>bif:</emphasis> prefix.</para>
<para>In this SPARQL example, we use the CONTAINS function to do a freetext search on all ?o that contain words starting with &quot;Timo&quot;.</para>
<programlisting>SELECT * 
  FROM &lt;http://www.w3.org/people#&gt; 
  WHERE 
    { 
      ?s  ?p            ?o         . 
      ?o  bif:contains  &#39;&quot;Timo*&quot;&#39;
    }
</programlisting><para>For more details see <ulink url="http://docs.openlinksw.com/virtuoso/sparqlextensions.html#rdfsparqlrulefulltext">Using Full Text Search in SPARQL</ulink> Virtuoso Documentation.</para>
<para>This feature also allows you to use relevant functions from the <ulink url="http://docs.openlinksw.com/virtuoso/functions.html">Virtuoso SQL Functions Guide</ulink> inside a SPARQL query.</para>
<para> </para>
<bridgehead class="http://www.w3.org/1999/xhtml:h2"> Related</bridgehead>
<itemizedlist mark="bullet" spacing="compact"><listitem><ulink url="VirtTipsAndTricksGuide">Virtuoso Tips and Tricks Collection</ulink> </listitem>
<listitem><ulink url="http://docs.openlinksw.com/virtuoso/sparqlextensions.html">Virtuoso Documentation</ulink></listitem>
</itemizedlist></section></docbook>