- 
                Notifications
    You must be signed in to change notification settings 
- Fork 749
add datasource tool #6422
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
                    add datasource tool #6422
Changes from all commits
      Commits
    
    
            Show all changes
          
          
            6 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      b4a0e2b
              
                add tool base
              
              
                Light2Dark 8bde463
              
                rename file, raise error if no conns
              
              
                Light2Dark 257a606
              
                typecheck
              
              
                Light2Dark 4b129bf
              
                fix test
              
              
                Light2Dark 09de4d5
              
                add duplicates test
              
              
                Light2Dark 924eb18
              
                add next steps description
              
              
                Light2Dark 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
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| # Copyright 2025 Marimo. All rights reserved. | ||
|  | ||
| from __future__ import annotations | ||
|  | ||
| from dataclasses import dataclass, field | ||
| from typing import Optional | ||
|  | ||
| from marimo import _loggers | ||
| from marimo._ai._tools.base import ToolBase | ||
| from marimo._ai._tools.types import SuccessResult | ||
| from marimo._ai._tools.utils.exceptions import ToolExecutionError | ||
| from marimo._data.models import DataTable | ||
| from marimo._server.sessions import Session | ||
| from marimo._types.ids import SessionId | ||
| from marimo._utils.fuzzy_match import compile_regex, is_fuzzy_match | ||
|  | ||
| LOGGER = _loggers.marimo_logger() | ||
|  | ||
|  | ||
| @dataclass | ||
| class GetDatabaseTablesArgs: | ||
| session_id: SessionId | ||
| query: Optional[str] = None | ||
|  | ||
|  | ||
| @dataclass | ||
| class TableDetails: | ||
| connection: str | ||
| database: str | ||
| schema: str | ||
| table: DataTable | ||
|  | ||
|  | ||
| @dataclass | ||
| class GetDatabaseTablesOutput(SuccessResult): | ||
| tables: list[TableDetails] = field(default_factory=list) | ||
|  | ||
|  | ||
| class GetDatabaseTables( | ||
| ToolBase[GetDatabaseTablesArgs, GetDatabaseTablesOutput] | ||
| ): | ||
| """ | ||
| Get information about tables in a database. | ||
|  | ||
| Args: | ||
| session_id: The session id. | ||
| query (optional): The query to match the database, schemas, and tables. Regex is supported. | ||
|  | ||
| If a query is provided, it will fuzzy match the query to the database, schemas, and tables available. If no query is provided, all tables are returned. Don't provide a query if you need to see the entire schema view. | ||
|  | ||
| The tables returned contain information about the database, schema and connection name to use in forming SQL queries. | ||
| """ | ||
|  | ||
| def handle(self, args: GetDatabaseTablesArgs) -> GetDatabaseTablesOutput: | ||
| session_id = args.session_id | ||
| session = self.context.get_session(session_id) | ||
|  | ||
| return self._get_tables(session, args.query) | ||
|  | ||
| def _get_tables( | ||
| self, session: Session, query: Optional[str] | ||
| ) -> GetDatabaseTablesOutput: | ||
| session_view = session.session_view | ||
| data_connectors = session_view.data_connectors | ||
|  | ||
| if len(data_connectors.connections) == 0: | ||
| raise ToolExecutionError( | ||
| message="No databases found. Please create a connection first.", | ||
| code="NO_DATABASES_FOUND", | ||
| is_retryable=False, | ||
| ) | ||
|  | ||
| tables: list[TableDetails] = [] | ||
|  | ||
| # Pre-compile regex if query exists | ||
| compiled_pattern = None | ||
| is_regex = False | ||
| if query: | ||
| compiled_pattern, is_regex = compile_regex(query) | ||
|  | ||
| for connection in data_connectors.connections: | ||
| for database in connection.databases: | ||
| for schema in database.schemas: | ||
| # If query is None, match all schemas | ||
| # If matching, add all tables to the list | ||
| if query is None or is_fuzzy_match( | ||
| query, schema.name, compiled_pattern, is_regex | ||
| ): | ||
| for table in schema.tables: | ||
| tables.append( | ||
| TableDetails( | ||
| connection=connection.name, | ||
| database=database.name, | ||
| schema=schema.name, | ||
| table=table, | ||
| ) | ||
| ) | ||
| continue | ||
| for table in schema.tables: | ||
| if is_fuzzy_match( | ||
| query, table.name, compiled_pattern, is_regex | ||
| ): | ||
| tables.append( | ||
| TableDetails( | ||
| connection=connection.name, | ||
| database=database.name, | ||
| schema=schema.name, | ||
| table=table, | ||
| ) | ||
| ) | ||
|  | ||
| return GetDatabaseTablesOutput( | ||
| tables=tables, | ||
| next_steps=[ | ||
| 'Example of an SQL query: _df = mo.sql(f"""SELECT * FROM database.schema.name LIMIT 100""")', | ||
| ], | ||
| ) | ||
  
    
      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,36 @@ | ||
| # Copyright 2025 Marimo. All rights reserved. | ||
|  | ||
| from __future__ import annotations | ||
|  | ||
| import re | ||
|  | ||
|  | ||
| def compile_regex(query: str) -> tuple[re.Pattern[str] | None, bool]: | ||
| """ | ||
| Returns compiled regex pattern and whether the query is a valid regex. | ||
| """ | ||
| try: | ||
| return re.compile(query, re.IGNORECASE), True | ||
| except re.error: | ||
| return None, False | ||
|  | ||
|  | ||
| def is_fuzzy_match( | ||
| query: str, | ||
| name: str, | ||
| compiled_pattern: re.Pattern[str] | None, | ||
| is_regex: bool, | ||
| ) -> bool: | ||
| """ | ||
| Fuzzy match using pre-compiled regex. If is not regex, fallback to substring match. | ||
|  | ||
| Args: | ||
| query: The query to match. | ||
| name: The name to match against. | ||
| compiled_pattern: Pre-compiled regex pattern (None if not regex). | ||
| is_regex: Whether the query is a valid regex. | ||
| """ | ||
| if is_regex and compiled_pattern: | ||
| return bool(compiled_pattern.search(name)) | ||
| else: | ||
| return query.lower() in name.lower() | 
      
      Oops, something went wrong.
        
    
  
      
      Oops, something went wrong.
        
    
  
  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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i wonder if it would help to add an example sql: e.g.
_df = mo.sql("SELECT * FROM database.schema.table LIMIT 100"in the response