Validate an SQL query This will validate: - the syntax (parsing) - the catalog (table and column names)
(self, request: ValidateSQLCommand)
| 28 | router.register(ValidateSQLCommand, self.validate_sql) |
| 29 | |
| 30 | async def _validate_sql_query(self, request: ValidateSQLCommand) -> None: |
| 31 | """Validate an SQL query |
| 32 | |
| 33 | This will validate: |
| 34 | - the syntax (parsing) |
| 35 | - the catalog (table and column names) |
| 36 | """ |
| 37 | request_id = request.request_id |
| 38 | |
| 39 | if request.only_parse: |
| 40 | if request.dialect is None: |
| 41 | broadcast_notification( |
| 42 | ValidateSQLResultNotification( |
| 43 | request_id=request_id, |
| 44 | error="Dialect is required when only parsing", |
| 45 | ), |
| 46 | ) |
| 47 | return |
| 48 | |
| 49 | # Just parse the query (no DB connection required) |
| 50 | parse_result, error = parse_sql(request.query, request.dialect) |
| 51 | broadcast_notification( |
| 52 | ValidateSQLResultNotification( |
| 53 | request_id=request_id, |
| 54 | parse_result=parse_result, |
| 55 | error=error, |
| 56 | ), |
| 57 | ) |
| 58 | return |
| 59 | |
| 60 | # Validate against the database |
| 61 | # This can be cheap for in-memory engines (duckdb, sqlite) |
| 62 | # But potentially expensive and requires an active connection for remote engines |
| 63 | # For failed connections, we should not raise an error |
| 64 | |
| 65 | if request.engine is None: |
| 66 | broadcast_notification( |
| 67 | ValidateSQLResultNotification( |
| 68 | request_id=request_id, |
| 69 | error="Engine is required for validating catalog", |
| 70 | ), |
| 71 | ) |
| 72 | return |
| 73 | |
| 74 | variable_name = cast(VariableName, request.engine) |
| 75 | engine: SQLConnectionType | None = None |
| 76 | if variable_name == INTERNAL_DUCKDB_ENGINE: |
| 77 | engine = DuckDBEngine(connection=None) |
| 78 | error = None |
| 79 | else: |
| 80 | engine, error = self._kernel.get_sql_connection(variable_name) |
| 81 | |
| 82 | if error is not None or engine is None: |
| 83 | broadcast_notification( |
| 84 | ValidateSQLResultNotification( |
| 85 | request_id=request_id, |
| 86 | error="Failed to get engine " + variable_name, |
| 87 | ), |
no test coverage detected