Value Comparison Operators

Syntax

comparison_expression ::= concatenate_expression
   [(value_comparison_operator | any_comparison_operator) add_expression]

value_comparison_operator ::= "=" | "!=" | ">" | ">=" | "<" | "<="

For definitions of syntax components referenced in this statement, see:

Semantics

Value comparison operators are primarily used to compare 2 values, one produced by the left operand and another from the right operand (this is in contrast to the sequence comparisons, defined in the following section which compare two sequences of values). If any operand returns more than one item, an error is raised. If both operands return the empty sequence, the operands are considered equal (so true will be returned if the operator is =, <=, or >=). If only one of the operands returns empty, the result of the comparison is false unless the operator is !=.

For the remainder of this section, we assume that each operand returns exactly one item. If an operand returns NULL, the result of the comparison expression is also NULL. Otherwise, the result is a boolean value that is computed as follows.

Among atomic items, if the types of the items are not comparable, false is returned. The following rules defined what atomic types are comparable and how the comparison is done in each case.

The semantics of comparisons among complex items are defined in a recursive fashion. Specifically:

As with atomic items, if two complex items are not comparable according to the above rules, false is returned. Furthermore, comparisons between atomic and complex items return false always.

The reason for returning false for incomparable items, instead of raising an error, is to handle truly schemaless applications, where different table rows may contain very different data or differently shaped data. As a result, even the writer of the query may not know what kind of items an operand may return and an operand may indeed return different kinds of items from different rows. Nevertheless, when the query writer compares “something” with, say, an integer, they expect that the “something” will be an integer and they would like to see results from the table rows that fulfill that expectation, instead of the whole query being rejected because some rows do not fulfill the expectation.

Example 1 - Fetch the passenger details from the airline baggage tracking application who have more than two transits and did not board from the SFO station

SELECT
fullname,
s.bagInfo.routing
FROM BaggageInfo s
WHERE (size(s.bagInfo[0].flightLegs) >= 3)
AND s.bagInfo[0].flightLegs[0].fltRouteSrc !=any "SFO"

Explanation: In the airline baggage tracking application, you can fetch the list of passengers who have more than two transits in their journey. The bagInfo array in the BaggageInfo table contains the information on the checked bags for passengers. The flightLegs array in the bagInfo JSON includes the source and transit details with each record corresponding to a travel leg. The first record of the flightLegs array, that is, flightLegs[0] has the details of the source location. In the above query, you fetch the details of the passengers who have more than two transits, which means there must be at least three records in the flightLegs array including the source location. You compare the size of the flightLegs array using the ‘>=’ operator. You also want to filter out the passengers who did not originate from SFO airport, so you use a != operator here. It is possible that the passengers have more than one checked bags, in which case there will be more than one element in the bagInfo array. You must consider only the first element of the bagInfo array, that is, bagInfo[0] during value comparison to avoid duplication of results.

Since the flightLegs is an array, the left operand of the comparison operator != is a sequence with more than one item. Hence, use the sequence comparison operator any in addition to the value comparison operator.

Output:

{"fullname":"Fallon Clements","routing":"MXP/CDG/SLC/BZN"}
{"fullname":"Elane Lemons","routing":"MXP/CDG/SLC/BZN"}
{"fullname":"Doris Martin","routing":"BZN/SEA/CDG/MXP"}

JSON collection table:

The following example applies a value comparison operator on a JSON collection table. Consider a sample row from the JSON collection table created for a shopping application:

{"contactPhone":"1917113999","address":{"city":"San Jose","number":501,"state":"San Francisco","street":"Maine","zip":95095},"cart":[{"item":"wallet","priceperunit":950,"quantity":2},{"item":"wall art","priceperunit":9500,"quantity":1}],"firstName":"Sharon","gender":"F","lastName":"Willard","notify":"yes","wishlist":[{"item":"Tshirt","priceperunit":500},{"item":"Jenga","priceperunit":850}]}

Example 2 - Fetch from the storeAcct table the details of shoppers who have wishlisted the item Jenga.

SELECT contactPhone, firstName
FROM storeAcct s
WHERE EXISTS s.wishlist[$element.item ="Jenga"]

Explanation: In the storeAcct table, the items wishlisted by the shoppers are stored in the JSON array wishlist. To fetch the details of the shoppers with Jenga as a wishlisted item, you verify whether or not an item by the specified name exists in the wishlist field. Use the operator ‘=’ to match the item.

Output:

{
  "contactPhone" : "1917113999",
  "firstName" : "Sharon"
}