Showing results for 
Search instead for 
Did you mean: 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
Showing results for 
Search instead for 
Did you mean: 

Transpile a SQL Script into PySpark DataFrame API equivalent code

New Contributor II

Input SQL Script (assume any dialect) :


SELECT b.se10,
  (SELECT se10,
                             ORDER BY se_aggrtr_indctr DESC) AS rn,
   FROM fraud_details_data_whole
   GROUP BY se10,
            se_aggrtr_indctr ,
            key_swipe_ind) b
WHERE b.rn<2

Output PySpark Code using DataFrame API :


from pyspark.sql import SparkSession
from pyspark.sql.window import Window
from pyspark.sql.functions import row_number

# Create a SparkSession
spark = SparkSession.builder \
    .appName("TranspileSQLExample") \

# Sample data (replace with your actual DataFrame)
data = [
    ("se10_value1", "se3_value1", "aggrtr_value1", "swipe_value1"),
    ("se10_value1", "se3_value2", "aggrtr_value2", "swipe_value2"),
    ("se10_value2", "se3_value3", "aggrtr_value3", "swipe_value3"),
    ("se10_value2", "se3_value4", "aggrtr_value4", "swipe_value4"),
    ("se10_value3", "se3_value5", "aggrtr_value5", "swipe_value5"),
    ("se10_value3", "se3_value6", "aggrtr_value6", "swipe_value6")

# Create DataFrame
fraud_details_data_whole = spark.createDataFrame(data, ["se10", "se3", "se_aggrtr_indctr", "key_swipe_ind"])

# Define Window specification for row_number() function
windowSpec = Window.partitionBy("se10").orderBy(fraud_details_data_whole["se_aggrtr_indctr"].desc())

# Add row number column
fraud_details_data_whole = fraud_details_data_whole.withColumn("rn", row_number().over(windowSpec))

# Filter rows where rn < 2
result_df = fraud_details_data_whole.filter("rn < 2")

# Select required columns
result_df ="se10", "se3", "se_aggrtr_indctr", "key_swipe_ind")

# Show the result DataFrame

Is there any way to translate the above Sql Query into its equivalent PySpark DataFrame API code? The result must be equal when we execute Sql Script and its transpiled PySpark code separately.

Kindly suggest


Community Manager
Community Manager

Hi @thecodecache, When executed separately, the PySpark code mentioned above should produce the same result as your original SQL query.

New Contributor II

Hi @Kaniz, Thanks for your response. 
I'm looking for a utility or an automated way of translating any generic SQL into PySpark DataFrame code.

So, the translate function should look like below:

def translate(input_sql):
    # translate/convert it into pyspark dataframe code
    return pyspark_code

Please suggest

Hi @thecodecacheHere’s a simplified example of how you might approach this (note that this is a basic illustration, and you can expand it to handle more complex SQL queries):

from pyspark.sql import SparkSession

def translate(input_sql):
    # Create a SparkSession (you can adjust this based on your environment)
    spark = SparkSession.builder.appName("SQLToPySpark").getOrCreate()

    # Parse the input SQL query (you'll need a more robust parser for production use)
    # Example: SELECT col1, col2 FROM my_table WHERE col3 > 10
    # Extract relevant components (SELECT, FROM, WHERE, etc.)

    # Create a DataFrame (replace with your actual data source)
    df ="path/to/your/csv/file.csv", header=True)

    # Apply transformations based on parsed components
    # Example:"col1", "col2").filter(df["col3"] > 10)

    # Generate the PySpark code
    pyspark_code = "\"col1\", \"col2\").filter(df[\"col3\"] > 10)"

    return pyspark_code

# Example usage
input_sql_query = "SELECT col1, col2 FROM my_table WHERE col3 > 10"
translated_code = translate(input_sql_query)
print("Translated PySpark code:")

If you have any further questions or need additional assistance, feel free to ask. 

Join 100K+ Data Experts: Register Now & Grow with Us!

Excited to expand your horizons with us? Click here to Register and begin your journey to success!

Already a member? Login and join your local regional user group! If there isn’t one near you, fill out this form and we’ll create one for you to join!