Site icon Spark By {Examples}

Spark AND | OR | NOT Operators

Spark AND OR operator

Both PySpark & Spark AND, OR and NOT operators are part of logical operations that supports determining the conditional-based logic relation among the operands.

In Spark/PySpark SQL expression, you need to use the following operators for AND & OR.

1. Logical Operations

Both PySpark & Spark supports standard logical operators such as ANDOR and NOT. These operators take Boolean expressions as arguments and return a Boolean value.

Below we can take a look at the behavior of the Spark AND & OR operator based on the Boolean expression.

LEFT OPERANDRIGHT OPERANDAND OR
TRUETRUETRUETRUE
TRUEFALSEFALSETRUE
FALSETRUEFALSETRUE
FALSEFALSEFALSEFALSE
Spark AND & OR operator

2. Null handling in Logical AND & OR Operations

The below table illustrates the behavior of Spark logical AND & OR operators when a NULL value is encountered.

LEFT OperandRIGHT OperandORAND
TRUENULLTRUENULL
NULLTRUETRUENULL
FALSENULLNULLFALSE
NULLFALSENULLFALSE
NULLNULLNULLNULL
Spark AND & OR Operator

In the case of OR operations when a null value is one of the boolean expressions then

In the case of AND operations when a null value is one of the boolean expressions then

When both the expressions of an AND, and OR are NULL the final result is obviously NULL.

3. Spark Using And & OR Operator

Usually, AND (&&) operator is useful when you wanted to filter the Spark DataFrame by multiple conditions.

Similarly, let’s use the OR (||) operator to filter the Spark DataFrame.


// Spark AND Operator 
df.filter(df("state") === "OH" && df("gender") === "M")
    .show(false)

// Spark OR Operator
df.filter(df("state") === "OH" || df("gender") === "M")
    .show(false)

4. PySpark Using OR Operator

PySpark Logical operations use the bitwise operators:


// PySpark AND Operator 
df.filter(df("state") === "OH" & df("gender") === "M")
    .show(false)

// PySpark OR Operator
df.filter(df("state") === "OH" | df("gender") === "M")
    .show(false)

3. Conclusion

Spark & PySpark support standard logical operators such as ANDOR and NOT. These operators take Boolean expressions as arguments and return a Boolean value.

Exit mobile version