Case value expression¶
The case value expression returns a value based on the evaluation of one or more search conditions.
The system evaluates the WHEN conditions one by one in the order in which they are written. Once a condition is evaluated as true, the system returns the value from the corresponding THEN clause. If none of the conditions are true, the system returns the value from the ELSE clause. If no ELSE clause is specified, the system returns NULL.
In GQL, the coalesce() function is equivalent to the case value expression. This function returns the first non-null value in a list value expression. If the values in the list are all evaluated to NULL, the function returns NULL.
Syntax¶
- case_value_expression
case_value_expression ::=
'CASE' ( 'WHEN' search_condition 'THEN' value_expression )+ ( 'ELSE' value_expression )? 'END'
| 'coalesce' '(' value_expression ( ',' value_expression )+ ')'
Parameters¶
search_condition¶
A search condition that evaluates to a boolean type value.
- If the value is
TRUE, the correspondingvalue_expressionis returned. - If the value is
FALSEorUNKNOWN, the nextsearch_conditionis evaluated.
value_expression¶
A value expression that evaluates to a value. For more information, see Value expressions.
Examples¶
Compare a list of values¶
'''
FOR a IN LIST[1,2,3,4,5,6,7,8,9,10]
RETURN
CASE
WHEN a<4 THEN false
WHEN a>5 THEN true
END AS case_value
'''
Return:
+------------+
| case_value |
+------------+
| false |
| false |
| false |
| null |
| null |
| true |
| true |
| true |
| true |
| true |
+------------+
Use coalesce() to return the first non-null value¶
RETURN coalesce(NULL, 1, 2, 3, 4, 5) AS a
Return:
+---+
| a |
+---+
| 1 |
+---+