Product SiteDocumentation Site

5.2.5. Comparison

ComparisonExpr
Figure 5.20. ComparisonExpr

ValueComp
Figure 5.21. ValueComp

Atomics can be compared with the usual six comparison operators (equality, non-equality, lower-than, greater-than, lower-or-equal, greater-or-equal), and with the same two-letter symbols as in MongoDB.
Example 5.44. Equality comparison
1 + 1 eq 2, 1 lt 2
Result (run with Zorba):
true true

Comparison is only possible between two compatible types, otherwise, an error is raised.
Example 5.45. Comparisons with a type mismatch
"foo" eq 1
Result (run with Zorba):
An error was raised: "xs:string": invalid type: can not compare for equality to type "xs:integer"

null can be compared for equality or inequality to anything - it is only equal to itself so that false is returned when comparing if for equality with any non-null atomic. True is returned when comparing it with non-equality with any non-null atomic.
Example 5.46. Equality and non-equality comparison with null
1 eq null, "foo" ne null, null eq null
Result (run with Zorba):
false true true

For ordering operators (lt, le, gt, ge), null is considered the smallest possible value (like in JavaScript).
Example 5.47. Ordering comparison with null
1 lt null
Result (run with Zorba):
false

Like for arithmetic operations, if an operand is the empty sequence, the empty sequence is returned as well.
Example 5.48. Comparison with the empty sequence
() eq 1
Result (run with Zorba):

Comparisons and logic operators are fundamental for a query language and for the implementation of a query processor as they impact query optimization greatly. The current comparison semantics for them is carefully chosen to have the right characteristics as to enable optimization.