pyspark.sql.functions.bit_and#

pyspark.sql.functions.bit_and(col)[source]#

Aggregate function: returns the bitwise AND of all non-null input values, or null if none.

New in version 3.5.0.

Parameters
colColumn or str

target column to compute on.

Returns
Column

the bitwise AND of all non-null input values, or null if none.

Examples

Example 1: Bitwise AND with all non-null values

>>> from pyspark.sql import functions as sf
>>> df = spark.createDataFrame([[1],[1],[2]], ["c"])
>>> df.select(sf.bit_and("c")).show()
+----------+
|bit_and(c)|
+----------+
|         0|
+----------+

Example 2: Bitwise AND with null values

>>> from pyspark.sql import functions as sf
>>> df = spark.createDataFrame([[1],[None],[2]], ["c"])
>>> df.select(sf.bit_and("c")).show()
+----------+
|bit_and(c)|
+----------+
|         0|
+----------+

Example 3: Bitwise AND with all null values

>>> from pyspark.sql import functions as sf
>>> from pyspark.sql.types import IntegerType, StructType, StructField
>>> schema = StructType([StructField("c", IntegerType(), True)])
>>> df = spark.createDataFrame([[None],[None],[None]], schema=schema)
>>> df.select(sf.bit_and("c")).show()
+----------+
|bit_and(c)|
+----------+
|      NULL|
+----------+

Example 4: Bitwise AND with single input value

>>> from pyspark.sql import functions as sf
>>> df = spark.createDataFrame([[5]], ["c"])
>>> df.select(sf.bit_and("c")).show()
+----------+
|bit_and(c)|
+----------+
|         5|
+----------+