|
1 | 1 | import logging |
2 | 2 | from sqlglot import parse as parse_sql |
3 | | -from sqlglot.expressions import Table, Expression, Use, Create |
| 3 | +from sqlglot.expressions import Table, Expression, Use, Create, Drop |
4 | 4 | from databricks.labs.ucx.hive_metastore.table_migration_status import TableMigrationIndex |
5 | | -from databricks.labs.ucx.source_code.base import Deprecation, CurrentSessionState, SqlLinter, Fixer |
| 5 | +from databricks.labs.ucx.source_code.base import Deprecation, CurrentSessionState, SqlLinter, Fixer, Failure |
6 | 6 |
|
7 | 7 | logger = logging.getLogger(__name__) |
8 | 8 |
|
@@ -43,37 +43,55 @@ def schema(self): |
43 | 43 |
|
44 | 44 | def lint_expression(self, expression: Expression): |
45 | 45 | for table in expression.find_all(Table): |
46 | | - if isinstance(expression, Use): |
47 | | - # Sqlglot captures the database name in the Use statement as a Table, with |
48 | | - # the schema as the table name. |
49 | | - self._session_state.schema = table.name |
50 | | - continue |
51 | | - if isinstance(expression, Create) and getattr(expression, "kind", None) == "SCHEMA": |
52 | | - # Sqlglot captures the schema name in the Create statement as a Table, with |
53 | | - # the schema as the db name. |
54 | | - self._session_state.schema = table.db |
55 | | - continue |
| 46 | + try: |
| 47 | + yield from self._unsafe_lint_expression(expression, table) |
| 48 | + except Exception as _: # pylint: disable=broad-exception-caught |
| 49 | + yield Failure( |
| 50 | + code='sql-parse-error', |
| 51 | + message=f"Could not parse SQL expression: {expression} ", |
| 52 | + # SQLGlot does not propagate tokens yet. See https://github.com/tobymao/sqlglot/issues/3159 |
| 53 | + start_line=0, |
| 54 | + start_col=0, |
| 55 | + end_line=0, |
| 56 | + end_col=1024, |
| 57 | + ) |
56 | 58 |
|
57 | | - # we only migrate tables in the hive_metastore catalog |
58 | | - if self._catalog(table) != 'hive_metastore': |
59 | | - continue |
60 | | - # Sqlglot uses db instead of schema, watch out for that |
61 | | - src_schema = table.db if table.db else self._session_state.schema |
62 | | - if not src_schema: |
63 | | - logger.error(f"Could not determine schema for table {table.name}") |
64 | | - continue |
65 | | - dst = self._index.get(src_schema, table.name) |
66 | | - if not dst: |
67 | | - continue |
68 | | - yield Deprecation( |
69 | | - code='table-migrated-to-uc', |
70 | | - message=f"Table {src_schema}.{table.name} is migrated to {dst.destination()} in Unity Catalog", |
71 | | - # SQLGlot does not propagate tokens yet. See https://github.com/tobymao/sqlglot/issues/3159 |
72 | | - start_line=0, |
73 | | - start_col=0, |
74 | | - end_line=0, |
75 | | - end_col=1024, |
76 | | - ) |
| 59 | + def _unsafe_lint_expression(self, expression: Expression, table: Table): |
| 60 | + if isinstance(expression, Use): |
| 61 | + # Sqlglot captures the database name in the Use statement as a Table, with |
| 62 | + # the schema as the table name. |
| 63 | + self._session_state.schema = table.name |
| 64 | + return |
| 65 | + if isinstance(expression, Drop) and getattr(expression, "kind", None) == "SCHEMA": |
| 66 | + # Sqlglot captures the schema name in the Drop statement as a Table, with |
| 67 | + # the schema as the db name. |
| 68 | + return |
| 69 | + if isinstance(expression, Create) and getattr(expression, "kind", None) == "SCHEMA": |
| 70 | + # Sqlglot captures the schema name in the Create statement as a Table, with |
| 71 | + # the schema as the db name. |
| 72 | + self._session_state.schema = table.db |
| 73 | + return |
| 74 | + |
| 75 | + # we only migrate tables in the hive_metastore catalog |
| 76 | + if self._catalog(table) != 'hive_metastore': |
| 77 | + return |
| 78 | + # Sqlglot uses db instead of schema, watch out for that |
| 79 | + src_schema = table.db if table.db else self._session_state.schema |
| 80 | + if not src_schema: |
| 81 | + logger.error(f"Could not determine schema for table {table.name}") |
| 82 | + return |
| 83 | + dst = self._index.get(src_schema, table.name) |
| 84 | + if not dst: |
| 85 | + return |
| 86 | + yield Deprecation( |
| 87 | + code='table-migrated-to-uc', |
| 88 | + message=f"Table {src_schema}.{table.name} is migrated to {dst.destination()} in Unity Catalog", |
| 89 | + # SQLGlot does not propagate tokens yet. See https://github.com/tobymao/sqlglot/issues/3159 |
| 90 | + start_line=0, |
| 91 | + start_col=0, |
| 92 | + end_line=0, |
| 93 | + end_col=1024, |
| 94 | + ) |
77 | 95 |
|
78 | 96 | @staticmethod |
79 | 97 | def _catalog(table): |
|
0 commit comments