XQuery Extensions

From BaseX Documentation

(Difference between revisions)
Jump to: navigation, search
(Created page with "This article is part of the XQuery Portal. It lists specific extensions and optimizations of the BaseX XQuery processor. =Optimizations= ==Non-determinism== The [h...")
Line 2: Line 2:
It lists specific extensions and optimizations of the BaseX XQuery processor.
It lists specific extensions and optimizations of the BaseX XQuery processor.
 +
 +
=Annotations=
 +
 +
The following implementation-defined annotations are available:
 +
 +
* {{Code|%basex:inline([limit])}} enforces the inlining of a function. Example:
 +
 +
'''Example:'''
 +
<pre class="brush:xquery">
 +
declare option db:inlinelimit '0';
 +
declare %basex:inline function local:id($x) { $x };
 +
local:id(123)
 +
</pre>
 +
 +
In this query, function inlining has been deactivated by setting [[Options#INLINELIMIT|inlinelimit]] to {{Code|0}}. The annotation enforces inlining for the given function, though, resulting in the optimized query expression {{Code|123}}.
 +
 +
If an integer is specified as annotation argument, it will be interpreted a local inline limit.
 +
 +
* {{Code|%basex:lazy}} enforces the lazy evaluation of a global variable. Example:
 +
 +
'''Example:'''
 +
<pre class="brush:xquery">
 +
declare %basex:lazy variable $january := doc('does-not-exist');
 +
if(month-from-date(current-date()) == 1) then $january else ()
 +
</pre>
 +
 +
The annotation ensures that an error will only be thrown if the condition yields true. Without the annotation, the error will always be thrown, because the referenced document is not found.
=Optimizations=
=Optimizations=

Revision as of 02:08, 4 January 2016

This article is part of the XQuery Portal.

It lists specific extensions and optimizations of the BaseX XQuery processor.

Annotations

The following implementation-defined annotations are available:

Example:

declare option db:inlinelimit '0';
declare %basex:inline function local:id($x) { $x };
local:id(123)

In this query, function inlining has been deactivated by setting inlinelimit to 0. The annotation enforces inlining for the given function, though, resulting in the optimized query expression 123.

If an integer is specified as annotation argument, it will be interpreted a local inline limit.

Example:

declare %basex:lazy variable $january := doc('does-not-exist');
if(month-from-date(current-date()) == 1) then $january else ()

The annotation ensures that an error will only be thrown if the condition yields true. Without the annotation, the error will always be thrown, because the referenced document is not found.

Optimizations

Non-determinism

The XQuery Functions and Operators specification defines functions that are “guaranteed to produce ·identical· results from repeated calls within a single ·execution scope· if the explicit and implicit arguments are identical” as deterministic. In BaseX, many extension functions are non-deterministic or side-effecting. If an expression is internally flagged as non-deterministic, various optimizations that might change their execution order will not be applied.

(: QUERY A... :)
let $n := 456
for $i in 1 to 2
return $n

(: ...will be optimized to :)
for $i in 1 to 2
return 456

(: QUERY B will not be rewritten :)
let $n := random:integer()
for $i in 1 to 2
return $n

In some cases, functions may contain non-deterministic code, but the query compiler may not be able to detect this statically. See the following example:

for $read in (file:read-text#1, file:read-binary#1)
let $ignored := non-deterministic $read('input.file')
return ()

Two non-deterministic functions will be bound to $read, and the result of the function call will be bound to $ignored. As the variable is not referenced in the subsequent code, the let clause would usually be discarded by the compiler. In the given query, however, execution will be enforced because of the BaseX-specific non-deterministic keyword.

Personal tools
Namespaces
Variants
Actions
Navigation
Print/export