Built with Tutify.io

    AIStudyLabs

    Spark AND | OR | NOT Operators

    Spark AND | OR | NOT Operators

    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.

    <a id="h-1-logical-operations"></a>

    1. Logical Operations

    Both PySpark & Spark supports standard logical operators such as AND, OR 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 OPERAND | RIGHT OPERAND | AND | OR | | --- | --- | --- | --- | | TRUE | TRUE | TRUE | TRUE | | TRUE | FALSE | FALSE | TRUE | | FALSE | TRUE | FALSE | TRUE | | FALSE | FALSE | FALSE | FALSE |

    <a id="h-2-null-handling-in-logical-and-amp-or-operations"></a>

    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 Operand | RIGHT Operand | OR | AND | | --- | --- | --- | --- | | TRUE | NULL | TRUE | NULL | | NULL | TRUE | TRUE | NULL | | FALSE | NULL | NULL | FALSE | | NULL | FALSE | NULL | FALSE | | NULL | NULL | NULL | NULL |

    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)
    

    <a id="h-3-conclusion"></a>

    3. Conclusion

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

    <a id="h-related-articles"></a>

    Related Articles