Comparison

JSONiq follows the W3C standard for comparison, and only extends its semantics to null values as follows.

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 53. 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 54. Ordering comparison with null

1 lt null
      

Result (run with Zorba): false


The following explanations, provided as an informal summary for convenience, are non-normative.

Figure 34. ComparisonExpr

ComparisonExpr

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 55. 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 56. 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"


Like for arithmetic operations, if an operand is the empty sequence, the empty sequence is returned as well.

Example 57. 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.