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

Revert workaround for #2887 #2937

Merged
merged 4 commits into from
Oct 11, 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
21 changes: 6 additions & 15 deletions src/databricks/labs/ucx/source_code/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,27 +262,23 @@ def collect_tables(self, source_code: str) -> Iterable[UsedTable]: ...


@dataclass
class TableInfoNode:
class UsedTableNode:
table: UsedTable
node: NodeNG


class TablePyCollector(TableCollector, ABC):

def collect_tables(self, source_code: str):
def collect_tables(self, source_code: str) -> Iterable[UsedTable]:
try:
tree = Tree.normalize_and_parse(source_code)
for table_node in self.collect_tables_from_tree(tree):
# see https://github.com/databrickslabs/ucx/issues/2887
if isinstance(table_node, UsedTable):
yield table_node
else:
yield table_node.table
yield table_node.table
except AstroidSyntaxError as e:
logger.warning('syntax-error', exc_info=e)

@abstractmethod
def collect_tables_from_tree(self, tree: Tree) -> Iterable[TableInfoNode]: ...
def collect_tables_from_tree(self, tree: Tree) -> Iterable[UsedTableNode]: ...


class TableSqlCollector(TableCollector, ABC): ...
Expand Down Expand Up @@ -458,16 +454,11 @@ def collect_tables(self, source_code: str) -> Iterable[UsedTable]:
try:
tree = self._parse_and_append(source_code)
for table_node in self.collect_tables_from_tree(tree):
# there's a bug in the code that causes this to be necessary
# see https://github.com/databrickslabs/ucx/issues/2887
if isinstance(table_node, UsedTable):
yield table_node
else:
yield table_node.table
yield table_node.table
except AstroidSyntaxError as e:
logger.warning('syntax-error', exc_info=e)

def collect_tables_from_tree(self, tree: Tree) -> Iterable[TableInfoNode]:
def collect_tables_from_tree(self, tree: Tree) -> Iterable[UsedTableNode]:
for collector in self._table_collectors:
yield from collector.collect_tables_from_tree(tree)

Expand Down
10 changes: 5 additions & 5 deletions src/databricks/labs/ucx/source_code/linters/pyspark.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
SqlLinter,
Fixer,
UsedTable,
TableInfoNode,
UsedTableNode,
TablePyCollector,
TableSqlCollector,
DfsaPyCollector,
Expand Down Expand Up @@ -388,14 +388,14 @@ def _find_matcher(self, node: NodeNG):
return None
return matcher if matcher.matches(node) else None

def collect_tables_from_tree(self, tree: Tree) -> Iterable[TableInfoNode]:
def collect_tables_from_tree(self, tree: Tree) -> Iterable[UsedTableNode]:
for node in tree.walk():
matcher = self._find_matcher(node)
if matcher is None:
continue
assert isinstance(node, Call)
for used_table in matcher.collect_tables(self._from_table, self._index, self._session_state, node):
yield TableInfoNode(used_table, node) # B
yield UsedTableNode(used_table, node)


class _SparkSqlAnalyzer:
Expand Down Expand Up @@ -468,11 +468,11 @@ class SparkSqlTablePyCollector(_SparkSqlAnalyzer, TablePyCollector):
def __init__(self, sql_collector: TableSqlCollector):
self._sql_collector = sql_collector

def collect_tables_from_tree(self, tree: Tree) -> Iterable[TableInfoNode]:
def collect_tables_from_tree(self, tree: Tree) -> Iterable[UsedTableNode]:
assert self._sql_collector
for call_node, query in self._visit_call_nodes(tree):
for value in InferredValue.infer_from_node(query):
if not value.is_inferred():
continue # TODO error handling strategy
for table in self._sql_collector.collect_tables(value.as_string()):
yield TableInfoNode(table, call_node) # A
yield UsedTableNode(table, call_node)
11 changes: 11 additions & 0 deletions tests/unit/source_code/samples/functional/table-access.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Databricks notebook source
# ucx[default-format-changed-in-dbr8:+1:0:+1:18] The default format changed in Databricks Runtime 8.0, from Parquet to Delta
spark.table("a.b").count()
spark.sql("SELECT * FROM b.c LEFT JOIN c.d USING (e)")
%sql SELECT * FROM b.c LEFT JOIN c.d USING (e)

# COMMAND ----------

# MAGIC %sql
# MAGIC SELECT * FROM b.c LEFT JOIN c.d USING (e)

12 changes: 12 additions & 0 deletions tests/unit/source_code/samples/functional/table-access.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-- Databricks notebook source

SELECT * FROM b.c LEFT JOIN c.d USING (e)

-- COMMAND ----------

-- MAGIC %python
-- ucx[default-format-changed-in-dbr8:+1:0:+1:18] The default format changed in Databricks Runtime 8.0, from Parquet to Delta
-- MAGIC spark.table("a.b").count()
-- MAGIC spark.sql("SELECT * FROM b.c LEFT JOIN c.d USING (e)")