Changes

Jump to navigation Jump to search
264 bytes removed ,  18:30, 1 December 2023
m
Text replacement - "</syntaxhighlight>" to "</pre>"
for $i in 1 to 10
return 6
</syntaxhighlightpre>
==Variable Inlining==
(: rewritten to :)
'Results: ' || count(//nodes)
</syntaxhighlightpre>
As the example shows, variable declarations might be located in the query prolog and in FLWOR expressions. They may also occur (and be inlined) in {{Code|try}}/{{Code|catch}}, {{Code|switch}} or {{Code|typeswitch}} expressions.
for $n in 1 to 5
return $n + 1
</syntaxhighlightpre>
Subsequent rewritings might result in query plans that differ a lot from the original query. As this might complicate debugging, you can disable function inling during development by setting {{Option|INLINELIMIT}} to {{Code|0}}.
(: further rewritten to :)
2, 4
</syntaxhighlightpre>
Folds are unrolled, too:
let $f := function($a, $b) { $a * $b }
return $f($f($f($f(1, 2), 3), 4), 5)
</syntaxhighlightpre>
The standard unroll limit is <code>5</code>. It can be adjusted with the {{Option|UNROLLLIMIT}} option, e.g. via a pragma:
...
db:get('db10')//*[text() = 'abc'],
</syntaxhighlightpre>
The last example indicates that index rewritings might be triggered by unrolling loops with paths on database nodes.
(: rewritten to :)
doc('addressbook.xml')/descendant::city
</syntaxhighlightpre>
…unless the last step does not contain a positional predicate:
<syntaxhighlight lang="xquery">
doc('addressbook.xml')//city[1]
</syntaxhighlightpre>
As the positional test refers to the city child step, a rewritten query would yield different steps.
(: rewritten to :)
a/b/c[d]
</syntaxhighlightpre>
Names of nodes can be specified via name tests or predicates. If names are e.g. supplied via external variables, the predicates can often be dissolved:
(: rewritten to :)
db:get('addressbook')/descendant::city
</syntaxhighlightpre>
==FLWOR Rewritings==
(: the remaining clauses are merged and rewritten to a simple map :)
(1 to 10)[. > 3] ! (. + 2)
</syntaxhighlightpre>
==Static Typing==
for $i in 1 to 5
return 'number'
</syntaxhighlightpre>
==Pure Logic==
(: rewritten to :)
('a', '')[.]
</syntaxhighlightpre>
Boolean algebra (and set theory) comes with a set of laws that can all be applied to XQuery expressions.
(: rewritten to :)
231
</syntaxhighlightpre>
;Return distinct values
(: rewritten to :)
('Muslim', 'Roman Catholic', 'Albanian Orthodox', ...)
</syntaxhighlightpre>
==Index Rewritings==
(: rewritten to :)
db:text('factbook', 'Shenzhen')/parent::name
</syntaxhighlightpre>
Multiple element names and query strings can be supplied in a path:
(: rewritten to :)
db:text('factbook', ('Jewish', 'Muslim'))/(parent::*:ethnicgroups | parent::*:religions)/parent::*
</syntaxhighlightpre>
If multiple candidates for index access are found, the database statistics (if available) are consulted to choose the cheapest candidate:
(: rewritten to :)
db:text('factbook', 'Greeks')/parent::ethnicgroups/parent::country[religions = 'Muslim']
</syntaxhighlightpre>
If index access is possible within more complex FLWOR expressions, only the paths will be rewritten:
order by $country/name[1]
return element { replace($country/@name, ' ', '') } {}
</syntaxhighlightpre>
The [https://projects.cwi.nl/xmark/ XMark XML Benchmark] comes with sample auction data and a bunch of queries, some of which are suitable for index rewritings:
(: rewritten to :)
db:attribute('xmark', 'person0')/self::attribute(id)/parent::person/name/text()
</syntaxhighlightpre>
;XMark Query 8
)
}</item>
</syntaxhighlightpre>
If the accessed database is not known at compile time, or if you want to give a predicate preference to another one, you can [[Indexes#Enforce Rewritings|enforce index rewritings]].
let $input2 := file:read-text-lines('huge2.txt')
return $input1[not(. = $input2)]
</syntaxhighlightpre>
=Changelog=
Bureaucrats, editor, reviewer, Administrators
13,550

edits

Navigation menu