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

feat: add support for sqlite connectors #680

Merged

Conversation

Tanmaypatil123
Copy link
Contributor

@Tanmaypatil123 Tanmaypatil123 commented Oct 24, 2023

Added support for sqlite file database. Implemented SqliteConnector and SqliteConnectorConfig where
file path can be parsed as database key to config parameter.

Summary by CodeRabbit

  • New Feature: Added support for SQLite databases, expanding the range of databases that can be connected to. Users can now retrieve data from SQLite databases using the new SqliteConnector.
  • Documentation: Updated the documentation to include examples and usage of the new SqliteConnector.
  • Test: Introduced unit tests for the SqliteConnector to ensure its functionality and reliability.

This update enhances the software's versatility by supporting more types of databases, making it more useful for users who work with SQLite databases. It also provides clear instructions and examples for using the new feature.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 24, 2023

Walkthrough

The changes introduce support for SQLite databases in the pandasai library. A new SqliteConnector class is added, along with its configuration class SqliteConnectorConfig. The SmartDatalake class is updated to include SQLite connectors. Documentation and examples are updated accordingly, and unit tests are added to ensure the functionality of the new connector.

Changes

File(s) Summary
docs/connectors.md Added a new section for the SQLite connector, demonstrating its usage with SmartDataframe.
examples/from_sql.py Introduced support for SQLite databases by adding a SqliteConnector and its configuration.
pandasai/connectors/__init__.py Included SqliteConnector in the list of exported modules.
pandasai/connectors/base.py Added a new SqliteConnectorConfig class for SQLite database configuration.
pandasai/connectors/sql.py Introduced a new SqliteConnector class with methods for SQLite database connection and data retrieval.
tests/connectors/test_sqlite.py Added unit tests for the SqliteConnector class, covering its constructor, methods, and properties.

🐇💻

In the land of code, where the rabbits play,

A new connector was born today.

SQLite now joins the fray,

Making data access a child's play.

With tests and docs along the way,

We celebrate this code ballet. 🎉🎊


Tips

Chat with CodeRabbit Bot (@coderabbitai)

  • If you reply to a review comment from CodeRabbit, the bot will automatically respond.
  • To engage with CodeRabbit bot directly around the specific lines of code in the PR, mention @coderabbitai in your review comment
  • Note: Review comments are made on code diffs or files, not on the PR overview.
  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai help to get help.

Note: For conversation with the bot, please use the review comments on code diffs or files.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.json

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 0d56e93 and b5429a2.
Files selected for processing (5)
  • docs/connectors.md (1 hunks)
  • examples/from_sql.py (2 hunks)
  • pandasai/connectors/init.py (2 hunks)
  • pandasai/connectors/base.py (1 hunks)
  • pandasai/connectors/sql.py (2 hunks)
Files skipped from review due to trivial changes (3)
  • docs/connectors.md
  • pandasai/connectors/init.py
  • pandasai/connectors/base.py
Additional comments: 5
examples/from_sql.py (3)
  • 5-5: The import of SqliteConnector is added correctly. Ensure that the SqliteConnector class is correctly implemented and tested.

  • 41-51: The SqliteConnector is instantiated correctly. However, the "database" attribute in the config dictionary should be an absolute path to the SQLite database file. Please verify that "local_path_to_db" is a placeholder and will be replaced with the actual path in the production code.

  • 53-53: The invoice_connector is correctly added to the SmartDatalake constructor. Ensure that the SmartDatalake class can handle multiple connectors of different types.

pandasai/connectors/sql.py (2)
  • 8-8: The SqliteConnectorConfig import is added to support the new SqliteConnector class. Ensure that the SqliteConnectorConfig class is defined in the base module.

  • 367-450: The SqliteConnector class is introduced to handle connections to SQLite databases. It extends the SQLConnector class and overrides the __init__, _load_connector_config, _init_connection, __del__, head, and __repr__ methods. The __init__ method now includes SQLite-specific environment variables. The _init_connection method creates an SQLite engine and connection. The head method builds a query to fetch the first 5 rows from the SQLite database. The __repr__ method returns a string representation of the SQLite connector.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between b5429a2 and 10e3604.
Files selected for processing (1)
  • examples/from_sql.py (2 hunks)
Additional comments: 3
examples/from_sql.py (3)
  • 5-5: The import of SqliteConnector is added correctly. Ensure that the SqliteConnector class is implemented and exported correctly in the pandasai.connectors module.

  • 41-51: The SqliteConnector is being used to connect to a SQLite database. The database parameter should be a path to the SQLite database file. Please ensure that "local_path_to_db" is replaced with the actual path to the SQLite database file.

  • 53-56: The invoice_connector is added to the list of connectors in the SmartDatalake constructor. Ensure that the SmartDatalake class can handle multiple connectors including the new SqliteConnector.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 1

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 10e3604 and 80e5439.
Files selected for processing (3)
  • pandasai/connectors/base.py (1 hunks)
  • pandasai/connectors/sql.py (2 hunks)
  • tests/connectors/test_sqlite.py (1 hunks)
Files skipped from review due to trivial changes (1)
  • tests/connectors/test_sqlite.py
Additional comments: 5
pandasai/connectors/base.py (1)
  • 42-49: The SqliteConnectorConfig class is introduced to handle SQLite-specific configuration parameters. It inherits from SQLBaseConnectorConfig and adds two new attributes: table and database. Ensure that these attributes are correctly used in the SqliteConnector class and that they are properly documented for users. Also, consider adding default values or validation checks for these attributes if necessary.
pandasai/connectors/sql.py (4)
  • 8-8: The import statement is updated to include SqliteConnectorConfig which is a new class introduced for handling SQLite-specific configuration parameters. Ensure that this class is defined and implemented correctly in the base module.

  • 367-450: A new SqliteConnector class is introduced, extending the SQLConnector class. This class includes methods to initialize the SQLite database connection and load SQLite-specific configurations. The __init__ method is updated to set the dialect to sqlite and populate the configuration from environment variables if a dictionary is passed. The _load_connector_config method is overridden to return an instance of SqliteConnectorConfig. The _init_connection method is overridden to create a SQLite database connection. The head method is overridden to run a SQL query to get all the column names and 5 random rows from the SQLite database. The __repr__ method is overridden to return the string representation of the SQLite connector. Ensure that these changes are compatible with the rest of the codebase and that the SQLite database connection is correctly initialized and used.

  • 406-407: The SQLite database connection is initialized using the create_engine function from sqlalchemy with the sqlite dialect and the database path from the configuration. Ensure that the database path is correctly formed and that the SQLite database exists at that path. Also, ensure that the create_engine function is correctly used with the sqlite dialect and that the connection is correctly initialized.

  • 432-435: A SQL query is built to get all the column names and 5 random rows from the SQLite database, and the pd.read_sql function is used to execute the query and return the result. Ensure that the _build_query method correctly builds the SQL query and that the pd.read_sql function correctly executes the query and returns the result. Also, ensure that the SQLite database supports the RANDOM() function for random row selection.

Comment on lines +379 to +382
config["dialect"] = "sqlite"
if isinstance(config, dict):
sqlite_env_vars = {"database": "SQLITE_DB_PATH", "table": "TABLENAME"}
config = self._populate_config_from_env(config, sqlite_env_vars)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The dialect is hardcoded to sqlite and the configuration is populated from environment variables if a dictionary is passed. This could lead to unexpected behavior if the user passes a configuration dictionary with a different dialect or without the necessary environment variables. Consider adding error handling or validation to ensure that the configuration is correctly formed and that the necessary environment variables are set.

@gventuri gventuri changed the title Added support for sqlite connectors feat: add support for sqlite connectors Oct 24, 2023
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 80e5439 and 0d27947.
Files selected for processing (1)
  • examples/from_sql.py (2 hunks)
Additional comments: 3
examples/from_sql.py (3)
  • 5-5: The import of SqliteConnector is added correctly. Ensure that the SqliteConnector class is implemented and available in the pandasai.connectors module.

  • 41-49: The SqliteConnector is instantiated with a configuration dictionary. The database key should contain the path to the SQLite database file. Please verify that the value "local_path_to_db" is a placeholder and will be replaced with the actual path to the SQLite database file in the production code.

  • 51-53: The invoice_connector is added to the list of connectors passed to the SmartDatalake constructor. This will allow the SmartDatalake to retrieve data from the SQLite database. Ensure that the SmartDatalake class can handle multiple connectors of different types.

@gventuri gventuri changed the base branch from main to release/v1.4 October 24, 2023 22:30
@gventuri gventuri merged commit 5569df0 into Sinaptik-AI:release/v1.4 Oct 24, 2023
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants