Skip to content
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

Create rule S7187: PySpark Pandas DataFrame columns should not use a reserved name #4622

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions rules/S7187/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
25 changes: 25 additions & 0 deletions rules/S7187/python/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"title": "PySpark Pandas DataFrame columns should not use a reserved name",
"type": "CODE_SMELL",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "5min"
},
"tags": [
"data-science",
"pyspark"
],
"defaultSeverity": "Major",
"ruleSpecification": "RSPEC-7187",
"sqKey": "S7187",
"scope": "All",
"defaultQualityProfiles": ["Sonar way"],
"quickfix": "infeasible",
"code": {
"impacts": {
"RELIABILITY": "MEDIUM"
},
"attribute": "CONVENTIONAL"
}
}
42 changes: 42 additions & 0 deletions rules/S7187/python/rule.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
This rule raises an issue when a PySpark Pandas DataFrame column name is set to a reserved name.

== Why is this an issue?

PySpark offers powerful APIs to work with Pandas DataFrames in a distributed environment.
While the integration between PySpark and Pandas is seamless, there are some caveats that should be taken into account.

Spark Pandas API uses some special column names for internal purposes.
These column names contain leading `++__++` and trailing `++__++`.
Therefore, when using PySpark with Pandas and naming or renaming columns,
it is discouraged to use such reserved column names as they are not guaranteed to yield the expected results.

== How to fix it

To fix this issue provide a column name without leading and trailing `++__++`.

=== Code examples

==== Noncompliant code example

[source,python,diff-id=1,diff-type=noncompliant]
----
import pyspark.pandas as ps

df = ps.DataFrame({'__value__': [1, 2, 3]}) # Noncompliant: __value__ is a reserved column name
----

==== Compliant solution

[source,python,diff-id=1,diff-type=compliant]
----
import pyspark.pandas as ps

df = ps.DataFrame({'value': [1, 2, 3]}) # Compliant
----


== Resources
=== Documentation

* PySpark Documentation - https://spark.apache.org/docs/latest/api/python/user_guide/pandas_on_spark/best_practices.html#avoid-reserved-column-names[Best Practices]