-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SONARPY-2496 Create rule S7193 PySpark DataFrame toPandas function should be avoided #4636
Draft
github-actions
wants to merge
2
commits into
master
Choose a base branch
from
rule/add-RSPEC-S7193
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"title": "PySpark DataFrame toPandas function should be avoided", | ||
"type": "CODE_SMELL", | ||
"status": "ready", | ||
"remediation": { | ||
"func": "Constant\/Issue", | ||
"constantCost": "5min" | ||
}, | ||
"tags": [ | ||
"data-science", | ||
"pyspark" | ||
], | ||
"defaultSeverity": "Major", | ||
"ruleSpecification": "RSPEC-7193", | ||
"sqKey": "S7193", | ||
"scope": "All", | ||
"defaultQualityProfiles": ["Sonar way"], | ||
"quickfix": "unknown", | ||
"code": { | ||
"impacts": { | ||
"RELIABILITY": "MEDIUM" | ||
}, | ||
"attribute": "EFFICIENT" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
This rule raises an issue when `toPandas` is used in PySpark in a way that can lead to memory or performance bottlenecks. | ||
|
||
== Why is this an issue? | ||
|
||
The use of `toPandas` in PySpark can lead to performance and memory management issues. | ||
|
||
PySpark is designed to handle large-scale data processing in a distributed manner, leveraging the power of cluster computing to efficiently manage and process big data. Calling `toPandas` collects all data from a Spark `DataFrame` into a Pandas `DataFrame` on a single machine. This can lead to memory issues and performance bottlenecks, especially with large datasets, which is contrary to the distributed nature of Spark. | ||
|
||
For this reason, it is generally advisable to avoid using `toPandas` unless you are certain that the dataset is small enough to be handled comfortably by a single machine. Instead, consider using Spark's built-in functions and capabilities to perform data processing tasks in a distributed manner. | ||
|
||
If the conversion to Pandas is necessary, ensure that the dataset size is manageable and that the conversion is justified by specific requirements, such as integration with libraries that require Pandas DataFrames. | ||
|
||
=== Exceptions | ||
|
||
This rule will not raise issues in the following context: | ||
|
||
* When visualization is performed with other libraries such as `matplotlib`, `seaborn`, etc. | ||
* When the `DataFrame` is of a limited size (e.g. following a call to `limit` or an aggregation through `groupBy`) | ||
* In tests | ||
|
||
== How to fix it | ||
|
||
To fix this issue, consider using PySpark built-in capabilities without relying on Pandas whenever possible. | ||
|
||
=== Code examples | ||
|
||
==== Noncompliant code example | ||
|
||
[source,python,diff-id=1,diff-type=noncompliant] | ||
---- | ||
# Converting a PySpark DataFrame to a Pandas DataFrame | ||
df = spark.read.csv("my_data.csv") | ||
pandas_df = df.toPandas() # Noncompliant: May cause memory issues with large datasets | ||
filtered_df = df[df['id'] > 1] | ||
print(filtered_df) | ||
---- | ||
|
||
==== Compliant solution | ||
|
||
[source,python,diff-id=1,diff-type=compliant] | ||
---- | ||
from pyspark.sql.functions import col | ||
|
||
# Using PySpark DataFrame operations | ||
df = spark.read.csv("my_data.csv") | ||
|
||
# Perform operations using PySpark's distributed capabilities | ||
filtered_df = df.filter(col("id") > 1) | ||
filtered_df.show() | ||
---- | ||
|
||
== Resources | ||
=== Documentation | ||
|
||
* PySpark Documentation - https://spark.apache.org/docs/latest/api/python/reference/pyspark.sql/api/pyspark.sql.DataFrame.toPandas.html[pyspark.sql.DataFrame.toPandas] | ||
|
||
|
||
ifdef::env-github,rspecator-view[] | ||
|
||
''' | ||
== Implementation Specification | ||
(visible only on this page) | ||
|
||
=== Message | ||
|
||
Consider using PySpark's built-in capabilities instead of converting this `DataFrame` to Pandas. | ||
|
||
endif::env-github,rspecator-view[] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be nice to have both example to the same thing. Adding here the filtering on the Pandas DataFrame, I think would make sense.