gpt4 book ai didi

xquery - "empty greatest"和 "empty least"在Order By中的作用是什么

转载 作者:行者123 更新时间:2023-12-04 02:25:34 29 4
gpt4 key购买 nike

在阅读 MarkLogic 时 Query Performance and Tuning Guide ,我了解了 empty greatestempty least 以及如何将其与 order by 一起使用。但是,除此之外,没有太多细节或示例可供理解:

You can specify either empty greatest or empty least, but empties always need to be at the end for the order by optimizations to work. For example, empty greatest is optimized with ascending; empty least is optimized with descending. If neither is specified, MarkLogic chooses the order that is optimized. The following example goes against the default. It orders the list by $doc/document/modified_date, ascending order, with empty least:

xquery version "1.0-ml";
for $doc in fn:doc()
order by $doc/document/modified_date ascending empty least
return $doc

谁能帮我理解empty greatestempty least的实际用例?

最佳答案

在 XQuery 规范中 https://www.w3.org/TR/xquery-31/#id-order-by-clause它被解释为

For the purpose of determining their relative position in the orderingsequence, the greater-than relationship between two orderspec values Wand V is defined as follows:

When the orderspec specifies empty least, the following rules areapplied in order:

If V is an empty sequence and W is not an empty sequence, then Wgreater-than V is true.

If V is NaN and W is neither NaN nor an empty sequence, then Wgreater-than V is true.

If a specific collation C is specified, and V and W are both of typexs:string or are convertible to xs:string by subtype substitutionand/or type promotion, then:

If fn:compare(V, W, C) is less than zero, then W greater-than V istrue; otherwise W greater-than V is false.

If none of the above rules apply, then:

If W gt V is true, then W greater-than V is true; otherwise Wgreater-than V is false.

When the orderspec specifies empty greatest, the following rules areapplied in order:

If W is an empty sequence and V is not an empty sequence, then Wgreater-than V is true.

If W is NaN and V is neither NaN nor an empty sequence, then Wgreater-than V is true.

If a specific collation C is specified, and V and W are both of typexs:string or are convertible to xs:string by subtype substitutionand/or type promotion, then:

If fn:compare(V, W, C) is less than zero, then W greater-than V istrue; otherwise W greater-than V is false.

If none of the above rules apply, then:

If W gt V is true, then W greater-than V is true; otherwise Wgreater-than V is false.

基本上,如果在 order by 子句中使用的表达式为要排序的项目提供空序列,则 empty least 声明将其排序在具有非空排序键的项目之前,同时empty greatest 在这些项目之后对其进行排序。

一个简单的例子:

XQuery

declare namespace output = "http://www.w3.org/2010/xslt-xquery-serialization";

declare option output:method 'xml';
declare option output:indent 'yes';

for $item in root/item
order by $item/name, $item/cat empty greatest
return $item

排序

<root>
<item>
<name>a</name>
<cat>z</cat>
</item>
<item>
<name>a</name>
</item>
<item>
<name>a</name>
<cat>d</cat>
</item>
</root>

进入

<item>
<name>a</name>
<cat>d</cat>
</item>
<item>
<name>a</name>
<cat>z</cat>
</item>
<item>
<name>a</name>
</item>

https://xqueryfiddle.liberty-development.net/eiZQFp9/1

关于xquery - "empty greatest"和 "empty least"在Order By中的作用是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67946099/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com