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

Avoid false positives when linting for pyspark patterns #2381

Merged
merged 2 commits into from
Aug 12, 2024
Merged
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
11 changes: 9 additions & 2 deletions src/databricks/labs/ucx/source_code/linters/pyspark.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,9 @@ def _find_dest(index: MigrationIndex, value: str, schema: str):
class ReturnValueMatcher(Matcher):

def matches(self, node: NodeNG):
return isinstance(node, Call) and isinstance(node.func, Attribute)
return (
isinstance(node, Call) and isinstance(node.func, Attribute) and Tree(node.func.expr).is_from_module("spark")
)

def lint(
self, from_table: FromTable, index: MigrationIndex, session_state: CurrentSessionState, node: Call
Expand Down Expand Up @@ -190,7 +192,12 @@ class DirectFilesystemAccessMatcher(Matcher):
}

def matches(self, node: NodeNG):
return isinstance(node, Call) and isinstance(node.func, Attribute) and self._get_table_arg(node) is not None
return (
isinstance(node, Call)
and self._get_table_arg(node) is not None
and isinstance(node.func, Attribute)
and (Tree(node.func.expr).is_from_module("spark") or Tree(node.func.expr).is_from_module("dbutils"))
)

def lint(
self, from_table: FromTable, index: MigrationIndex, session_state: CurrentSessionState, node: NodeNG
Expand Down
7 changes: 4 additions & 3 deletions tests/unit/source_code/linters/test_pyspark.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,8 @@ def test_spark_sql_fix(migration_index):
),
# Test for option function
(
"""(df.write
"""df=spark.sql('SELECT * FROm dual')
(df.write
.format("parquet")
.option("path", "s3a://your_bucket_name/your_directory/")
.option("spark.hadoop.fs.s3a.access.key", "your_access_key")
Expand All @@ -277,9 +278,9 @@ def test_spark_sql_fix(migration_index):
code='direct-filesystem-access',
message='The use of direct filesystem references is deprecated: '
"s3a://your_bucket_name/your_directory/",
start_line=0,
start_line=1,
start_col=1,
end_line=2,
end_line=3,
end_col=59,
)
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@
for table in spark.catalog.listTables(f"boop{stuff}"):
do_stuff_with_table(table)

## Some trivial references to the method or table in unrelated contexts that should not trigger warnigns.
# FIXME: This is a false positive; any method named 'listTables' is triggering the warning.
# ucx[changed-result-format-in-uc:+1:0:+1:39] Call to 'listTables' will return a list of <catalog>.<database>.<table> instead of <database>.<table>.
## Some trivial references to the method or table in unrelated contexts that should not trigger warnings.
something_else.listTables("old.things")
a_function("old.things")