Executors
Connects to databases and executes generated SQL queries to fetch results.
An executor executes the generated SQL queries against the database and fetches the results. It is a crucial component in the Text-to-SQL pipeline, as it ensures that the generated SQL queries are valid and return the expected results.
PremSQL supports a native executor for SQLite databases and also supports LangChain’s SQLDatabase as an executor. You can also create custom executors for other database. Let’s start by importing the required libraries:
The execute_sql
method returns a dictionary with the following keys
result
: The result of the SQL query execution.error
: Any error that occurred during the execution.execution_time
: The time taken to execute the SQL query.
You can also use other methods like:
match_sqls()
to match the generated SQL with the ground truth SQL.iterated_execution()
to execute the SQL iteratively and comparing with the ground truth to see the execution time ratio.
Similarly lets use the LangChain’s SQLDatabase as an executor:
Creating Custom Executors
You can create custom executors by inheriting from the BaseExecutor
class and implementing the execute_sql
method. Here’s an example of a custom executor for a PostgreSQL database:
Awesome now that we understand what executors are and how they work,
let’s understand how to evaluate the generated SQL queries using the Text2SQLEvaluator
in the next section.