Difference between revisions of "XQuery Extensions"

From BaseX Documentation
Jump to navigation Jump to search
Line 1: Line 1:
This article is part of the [[XQuery|XQuery Portal]]. It lists specific extensions and optimizations of the BaseX XQuery processor.
+
This article is part of the [[XQuery|XQuery Portal]]. It lists extensions and optimizations that are specific to the BaseX XQuery processor.
  
 
=Option Declarations=
 
=Option Declarations=

Revision as of 03:38, 4 January 2016

This article is part of the XQuery Portal. It lists extensions and optimizations that are specific to the BaseX XQuery processor.

Option Declarations

Local database options can be set in the prolog of an XQuery expression. In the option declaration, options need to be bound to the Database Module namespace. All values will be reset after the evaluation of a query:

declare option db:chop 'false';
doc('doc.xml')

Pragmas

Local database options can be assigned locally via pragmas:

(# db:chop false #) { doc('doc.xml') }

Annotations

The following implementation-defined annotations are available:

  • %basex:inline([limit]) enforces the inlining of a function. The inlining limit can be specified as argument. Inlining can be disabled by specifying 0.

Example:

declare %basex:inline(0) function local:e() { error() };
local:e()

Result:

Stopped at query.xq, 1/53:
[FOER0000] Halted on error().

Stack Trace:
- query.xq, 2/9

In the next example, function inlining was disabled globally by assigning 0 to the INLINELIMIT option. However, annotation is enforced to a single function:

Example:

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

The query will be optimized to 123.

  • %basex:lazy enforces the lazy evaluation of a global variable. Example:

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.

Serialization

  • Since Version 8.4, basex is used as the default serialization method: nodes are serialized as XML, atomic values are serialized as string, and items of binary type are output in their native byte representation. Function items (including maps and arrays) are output just like with the adaptive method.
  • csv allows you to output XML nodes as CSV data (see the CSV Module for more details).

For more information and some additional BaseX-specific parameters, see the article on Serialization.

Optimizations

Non-determinism

In XQuery, deterministic functions are “guaranteed to produce ·identical· results from repeated calls within a single ·execution scope· if the explicit and implicit arguments are identical”. 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.

Other Extensions

Separate articles exist on Full Text and Update extensions.