Skip to main content

A Flake8 plugin to check for PySpark withColumn usage in loops

Project description

Flake8-pyspark-with-column

Getting started

pip install flake8-pyspark-with-column
flake8 --select PSRPK001,PSPRT002,PSPRK003,PSPRK004

Alternatively you can add the following tox.ini file to the root of your project:

[flake8]
select = 
    PSPRK001,
    PSPRK002,
    PSPRK003,
    PSPRK004

About

A flake8 plugin that detects of usage withColumn in a loop or inside reduce. From the PySpark documentation about withColumn method:

This method introduces a projection internally. Therefore, calling it multiple times, for instance, via loops in order to add multiple columns can generate big plans which can cause performance issues and even StackOverflowException. To avoid this, use select() with multiple columns at once.

What happens under the hood?

When you run a PySpark application the following happens:

  1. Spark creates Unresolved Logical Plan that is a result of parsing SQL
  2. Spark do analysis of this plan to create an Analyzed Logical Plan
  3. Spark apply optimization rules to create an Optimized Logical Plan

spark-flow

What is the problem with withColumn? It creates a single node in the unresolved plan. So, calling withColumn 500 times will create an unresolved plan with 500 nodes. During the analysis Spark should visit each node to check that column exists and has a right data type. After that Spark will start applying rules, but rules are applyed once per plan recursively, so concatenation of 500 calls to withColumn will require 500 applies of the corresponding rule. All of that may significantly increase the amount of time from Unresolved Logical Plan to Optimized Logical Plan:

bechmark

Rules

This plugin contains the following rules:

  • PSPRK001: Usage of withColumn in a loop detected
  • PSPRK002: Usage of withColumn inside reduce is detected
  • PSPRK003: Usage of withColumnRenamed in a loop detected
  • PSPRK004: Usage of withColumnRenamed inside reduce is detected

Examples

Let's imagine we want to apply an ML model to our data but our Model expects double values and our table contain decimal values. The goal is to cast all Decimal columns to Double.

Implementation with withColumn (bad example):

def cast_to_double(df: DataFrame) -> DataFrame:
  for field in df.schema.fields:
    if isinstance(field.dataType, DecimalType):
      df = df.withColumn(field.name, col(field.name).cast(DoubleType()))
  return df

Implementation without withColumn (good example):

def cast_to_double(df: DataFrame) -> DataFrame:
  cols_to_select = []
  for field in df.schema.fields:
    if isinstance(field.dataType, DecimalType):
      cols_to_select.append(col(field.name).cast(DoubleType()).alias(field.name))
    else:
      cols_to_select.append(col(field.name))
  return df.select(*cols_to_select)

Usage

flake8 %your-code-here%

screenshot of how it works

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flake8_pyspark_with_column-0.0.3.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

flake8_pyspark_with_column-0.0.3-py2.py3-none-any.whl (8.0 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file flake8_pyspark_with_column-0.0.3.tar.gz.

File metadata

File hashes

Hashes for flake8_pyspark_with_column-0.0.3.tar.gz
Algorithm Hash digest
SHA256 e44c47b9de3866de4f5c243cde6f402604871354082303c67f4b9e8077bc673f
MD5 53ef141b5a0fd22245befff6a40ced26
BLAKE2b-256 220c8d57f404a0337b2e5412588184fe9584aa9cd2c8ce3ae2f1955facfcb871

See more details on using hashes here.

File details

Details for the file flake8_pyspark_with_column-0.0.3-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for flake8_pyspark_with_column-0.0.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 98c546cf039009c8e64512c1cf6dab2a5ff1b57b165ddd3e35d03f759ccd6c84
MD5 388473cdc8269796f2e95c6ffadaa0ca
BLAKE2b-256 23e0ce2e43ca3d3024ab55eef0efd9fc4f8d0fe2f3247de6a3dc022608df692f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page