Sql alchemy commands used in table info (#1135)

This approach has several advantages:

* it improves the readability of the code
* removes incompatibilities between SQL dialects
* fixes a bug with `datetime` values in rows and `ast.literal_eval`

Huge thanks and credits to @jzluo for finding the weaknesses in the
current approach and for the thoughtful discussion on the best way to
implement this.

---------

Co-authored-by: Francisco Ingham <>
Co-authored-by: Jon Luo <20971593+jzluo@users.noreply.github.com>
This commit is contained in:
Francisco Ingham
2023-02-18 15:58:29 -03:00
committed by GitHub
parent 483821ea3b
commit 3f29742adc
3 changed files with 63 additions and 60 deletions

View File

@@ -3,7 +3,7 @@
from sqlalchemy import Column, Integer, MetaData, String, Table, create_engine, insert
from langchain.sql_database import _TEMPLATE_PREFIX, SQLDatabase
from langchain.sql_database import SQLDatabase
metadata_obj = MetaData()
@@ -29,13 +29,13 @@ def test_table_info() -> None:
db = SQLDatabase(engine)
output = db.table_info
expected_output = """
CREATE TABLE user (
CREATE TABLE user (
user_id INTEGER NOT NULL,
user_name VARCHAR(16) NOT NULL,
PRIMARY KEY (user_id)
)
SELECT * FROM 'user' LIMIT 3
SELECT * FROM 'user' LIMIT 3;
user_id user_name
@@ -45,7 +45,7 @@ def test_table_info() -> None:
PRIMARY KEY (company_id)
)
SELECT * FROM 'company' LIMIT 3
SELECT * FROM 'company' LIMIT 3;
company_id company_location
"""
@@ -75,7 +75,7 @@ def test_table_info_w_sample_rows() -> None:
PRIMARY KEY (company_id)
)
SELECT * FROM 'company' LIMIT 2
SELECT * FROM 'company' LIMIT 2;
company_id company_location
@@ -85,7 +85,7 @@ def test_table_info_w_sample_rows() -> None:
PRIMARY KEY (user_id)
)
SELECT * FROM 'user' LIMIT 2
SELECT * FROM 'user' LIMIT 2;
user_id user_name
13 Harrison
14 Chase