Skip to main content

reverse

Collection function: Returns a reversed string or an array with elements in reverse order. Supports Spark Connect.

For the corresponding Databricks SQL function, see reverse function.

Syntax

Python
from pyspark.databricks.sql import functions as dbf

dbf.reverse(col=<col>)

Parameters

Parameter

Type

Description

col

pyspark.sql.Column or str

The name of the column or an expression that represents the element to be reversed.

Returns

pyspark.sql.Column: A new column that contains a reversed string or an array with elements in reverse order.

Examples

Example 1: Reverse a string

Python
from pyspark.databricks.sql import functions as dbf
df = spark.createDataFrame([('Spark SQL',)], ['data'])
df.select(dbf.reverse(df.data)).show()
Output
+-------------+
|reverse(data)|
+-------------+
| LQS krapS|
+-------------+

Example 2: Reverse an array

Python
from pyspark.databricks.sql import functions as dbf
df = spark.createDataFrame([([2, 1, 3],) ,([1],) ,([],)], ['data'])
df.select(dbf.reverse(df.data)).show()
Output
+-------------+
|reverse(data)|
+-------------+
| [3, 1, 2]|
| [1]|
| []|
+-------------+