-
Notifications
You must be signed in to change notification settings - Fork 20
feat: add SQL query tool for executing queries on table/view assets #91
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
183d619
feat: add SQL query tool for executing queries on table/view assets
aditya-atlan 0e1f0d8
feat: address query tool review comments and improve parameter consis…
aditya-atlan f9f9a2c
fix: address feedback - improve docstring examples , remove select as…
aditya-atlan 3cf8f78
improve doctring
aditya-atlan 637637e
Merge branch 'main' into aditya-atlan/queryTool
aditya-atlan fefb2b8
formatting
aditya-atlan 4cb5deb
address review feedback and Readme changes
aditya-atlan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| """ | ||
| Query tool for executing SQL queries on table/view assets. | ||
|
|
||
| This module provides functionality to execute SQL queries on data sources | ||
| using the Atlan client. | ||
| """ | ||
|
|
||
| import logging | ||
| from typing import Dict, Any, Optional | ||
|
|
||
| from client import get_atlan_client | ||
| from pyatlan.model.query import QueryRequest | ||
|
|
||
| # Configure logging | ||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def query_asset( | ||
| sql: str, | ||
| connection_qualified_name: str, | ||
| default_schema: Optional[str] = None, | ||
| ) -> Dict[str, Any]: | ||
| """ | ||
| Execute a SQL query on a table/view asset. | ||
|
|
||
| Note: | ||
| Use read-only queries to retrieve data. | ||
| Please add reasonable LIMIT clauses to your SQL queries to avoid | ||
| overwhelming the client or causing timeouts. Large result sets can | ||
| cause performance issues or crash the client application. | ||
|
|
||
| Args: | ||
| sql (str): The SQL query to execute (read-only queries) | ||
| connection_qualified_name (str): Connection qualified name to use for the query | ||
| (e.g., "default/snowflake/1705755637") | ||
| default_schema (str, optional): Default schema name to use for unqualified | ||
| objects in the SQL, in the form "DB.SCHEMA" | ||
| (e.g., "RAW.WIDEWORLDIMPORTERS_WAREHOUSE") | ||
|
|
||
| Returns: | ||
| Dict[str, Any]: Dictionary containing: | ||
| - success: Boolean indicating if the query was successful | ||
| - data: Query result data (rows, columns) if successful | ||
| - error: Error message if query failed | ||
| - query_info: Additional query execution information | ||
|
|
||
| Raises: | ||
| Exception: If there's an error executing the query | ||
| """ | ||
| logger.info( | ||
| f"Starting SQL query execution on connection: {connection_qualified_name}" | ||
| ) | ||
| logger.debug(f"SQL query: {sql}") | ||
| logger.debug(f"Parameters - default_schema: {default_schema}") | ||
|
|
||
| try: | ||
| # Validate required parameters | ||
| if not sql or not sql.strip(): | ||
| error_msg = "SQL query cannot be empty" | ||
| logger.error(error_msg) | ||
| return { | ||
| "success": False, | ||
| "data": None, | ||
| "error": error_msg, | ||
| "query_info": {} | ||
| } | ||
|
|
||
| if not connection_qualified_name or not connection_qualified_name.strip(): | ||
| error_msg = "Connection qualified name cannot be empty" | ||
| logger.error(error_msg) | ||
| return { | ||
| "success": False, | ||
| "data": None, | ||
| "error": error_msg, | ||
| "query_info": {} | ||
| } | ||
|
|
||
|
|
||
| # Get Atlan client | ||
| logger.debug("Getting Atlan client") | ||
| client = get_atlan_client() | ||
|
|
||
| # Build query request | ||
| logger.debug("Building QueryRequest object") | ||
| query_request = QueryRequest( | ||
| sql=sql, | ||
| data_source_name=connection_qualified_name, | ||
| default_schema=default_schema | ||
| ) | ||
|
|
||
| # Execute query | ||
| logger.info("Executing SQL query") | ||
| query_response = client.queries.stream(request=query_request) | ||
|
|
||
| logger.info( | ||
| f"Query executed successfully, returning response" | ||
| ) | ||
|
|
||
| return { | ||
| "success": True, | ||
| "data": query_response, | ||
| "error": None, | ||
| "query_info": { | ||
| "data_source": connection_qualified_name, | ||
| "default_schema": default_schema, | ||
| "sql": sql | ||
| } | ||
| } | ||
|
|
||
| except Exception as e: | ||
| error_msg = f"Error executing SQL query: {str(e)}" | ||
| logger.error(error_msg) | ||
| logger.exception("Exception details:") | ||
|
|
||
| return { | ||
| "success": False, | ||
| "data": None, | ||
| "error": error_msg, | ||
| "query_info": { | ||
| "data_source": connection_qualified_name, | ||
| "default_schema": default_schema, | ||
| "sql": sql | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.