Ternary conditional operator (ternary-if)
The Deephaven Query Language (DQL) implements if-then-else logic using a one-line ternary conditional operator, also known as ternary-if.
The syntax for the ternary conditional operator is:
condition ? expressionIfTrue : expressionIfFalse
The question mark (?
) separates the condition from the expressions, and the colon (:
) separates the expression evaluated when the condition is true from the expression evaluated when the condition is false.
The expression x ? y : z
evaluates as follows:
- If
x
is true, the expression evaluates toy
. - If
x
is false, the expression evaluates toz
.
The expression x ? (y ? 1 : 2) : 3
evaluates as follows:
- If both
x
andy
are true, the expression evaluates to 1. - If
x
is true, andy
is false, the expression evaluates to 2. - If
x
is false, the expression evaluates to 3.
Syntax
condition ? expressionTrue : expressionFalse
Parameters
Parameter | Type | Description |
---|---|---|
condition | boolean | The condition to be evaluated. |
expressionTrue | expression | If |
expressionFalse | expression | If |
Returns
expressionTrue
if condition
is true, and expressionFalse
if condition
is false.
Examples
In the following example, a new column, Budget
, is created. The column contains yes
, if the value in the Price
column is less than or equal to 3.50, and contains no
otherwise.
source = newTable(
stringCol("Type", "Pine", "Fir", "Cedar", "Oak", "Ash", "Walnut", "Beech", "Cherry"),
stringCol("Hardness", "soft", "soft", "soft", "hard", "hard", "hard", "hard", "hard"),
doubleCol("Price", 1.95, 3.70, 3.25, 3.45, 4.25, 7.95 , 4.10, 5.25)
)
result = source.update("Budget = (Price <= 3.50) ? `yes` : `no` ")
- source
- result
In the following example, a ternary-if is used to categorize woods into three categories:
hard-cheap
: hardwoods costing less than or equal to $3.50,hard-expensive
: hardwoods costing more than $3.50, andsoft
: softwoods.
source = newTable(
stringCol("Type", "Pine", "Fir", "Cedar", "Oak", "Ash", "Walnut", "Beech", "Cherry"),
stringCol("Hardness", "soft", "soft", "soft", "hard", "hard", "hard", "hard", "hard"),
doubleCol("Price", 1.95, 3.70, 3.25, 3.45, 4.25, 7.95 , 4.10, 5.25)
)
result = source.update(
"Category = (Hardness == `hard`) ? ( (Price <= 3.50) ? `hard-cheap` : `hard-expensive` ) : `soft` ")
- source
- result