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

标签 xquery marklogic

在阅读 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 ordering sequence, the greater-than relationship between two orderspec values W and V is defined as follows:

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

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

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

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

If fn:compare(V, W, C) is less than zero, then W greater-than V is true; 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 W greater-than V is false.

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

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

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

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

If fn:compare(V, W, C) is less than zero, then W greater-than V is true; 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 W greater-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/

相关文章:

xquery - 马克逻辑cts :search node hierarchy

java - 从 MarkLogic Xquery 模块调用 Java

xquery - 如何在 marklogic 中的一处声明命名空间,然后在各种 xquery 文件中导入/调用它们?

ldap - 通过 LDAP 进行外部身份验证

xml - Xquery:获取前一周的天数

sql-server - 更新 XML 列中的子字符串 SQL Server 2008

xml - 使用 XQuery 更新删除节点

linux - Redhat 7 上的 Marklogic 安装

uri - 使用 MLCP 时转换默认 URI

XQuery 基于 xml 结构创建 where 子句作为一种动态 where 子句