SqlAlchemyBaseView#
Tip
To learn how to use db-ally with databases see How-To: Use SQL databases with db-ally.
dbally.SqlAlchemyBaseView
#
Bases: MethodsBaseView
Base class for views that use SQLAlchemy to generate SQL queries.
Creates a new instance of the SQL view.
PARAMETER | DESCRIPTION |
---|---|
sqlalchemy_engine |
SQLAlchemy engine to use for executing the queries.
TYPE:
|
Source code in src/dbally/views/sqlalchemy_base.py
ask
async
#
ask(query: str, llm: LLM, event_tracker: Optional[EventTracker] = None, n_retries: int = 3, dry_run: bool = False, llm_options: Optional[LLMOptions] = None) -> ViewExecutionResult
Executes the query and returns the result. It generates the IQL query from the natural language query and applies the filters to the view. It retries the process in case of errors.
PARAMETER | DESCRIPTION |
---|---|
query |
The natural language query to execute.
TYPE:
|
llm |
The LLM used to execute the query.
TYPE:
|
event_tracker |
The event tracker used to audit the query execution.
TYPE:
|
n_retries |
The number of retries to execute the query in case of errors.
TYPE:
|
dry_run |
If True, the query will not be used to fetch data from the datasource.
TYPE:
|
llm_options |
Options to use for the LLM.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
ViewExecutionResult
|
The result of the query. |
RAISES | DESCRIPTION |
---|---|
ViewExecutionError
|
When an error occurs while executing the view. |
Source code in src/dbally/views/structured.py
list_similarity_indexes
#
Lists all the similarity indexes used by the view.
RETURNS | DESCRIPTION |
---|---|
Dict[AbstractSimilarityIndex, List[IndexLocation]]
|
Mapping of similarity indexes to their locations in the (view_name, filter_name, argument_name) format. |
Source code in src/dbally/views/structured.py
list_few_shots
#
list_few_shots() -> List[FewShotExample]
List all examples to be injected into few-shot prompt.
RETURNS | DESCRIPTION |
---|---|
List[FewShotExample]
|
List of few-shot examples |
get_iql_generator
#
get_iql_generator() -> IQLGenerator
Returns the IQL generator for the view.
RETURNS | DESCRIPTION |
---|---|
IQLGenerator
|
IQL generator for the view. |
list_filters
#
list_filters() -> List[ExposedFunction]
List filters in the given view
RETURNS | DESCRIPTION |
---|---|
List[ExposedFunction]
|
Filters defined inside the View and decorated with |
Source code in src/dbally/views/methods_base.py
list_aggregations
#
list_aggregations() -> List[ExposedFunction]
List aggregations in the given view
RETURNS | DESCRIPTION |
---|---|
List[ExposedFunction]
|
Aggregations defined inside the View and decorated with |
Source code in src/dbally/views/methods_base.py
list_methods_by_decorator
classmethod
#
list_methods_by_decorator(decorator: Callable) -> List[ExposedFunction]
Lists all methods decorated with the given decorator.
PARAMETER | DESCRIPTION |
---|---|
decorator |
The decorator to filter the methods
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
List[ExposedFunction]
|
List of exposed methods |
Source code in src/dbally/views/methods_base.py
call_filter_method
async
#
call_filter_method(func: FunctionCall) -> Any
Converts a IQL FunctonCall filter to a method call. If the method is a coroutine, it will be awaited.
PARAMETER | DESCRIPTION |
---|---|
func |
IQL FunctionCall node
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Any
|
The result of the method call |
Source code in src/dbally/views/methods_base.py
call_aggregation_method
async
#
call_aggregation_method(func: FunctionCall) -> Any
Converts a IQL FunctonCall aggregation to a method call. If the method is a coroutine, it will be awaited.
PARAMETER | DESCRIPTION |
---|---|
func |
IQL FunctionCall node
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Any
|
The result of the method call |
Source code in src/dbally/views/methods_base.py
get_select
abstractmethod
#
Creates the initial SqlAlchemy select object which will be used to build the query.
Source code in src/dbally/views/sqlalchemy_base.py
apply_filters
async
#
Applies the chosen filters to the view.
PARAMETER | DESCRIPTION |
---|---|
filters |
IQLQuery object representing the filters to apply.
TYPE:
|
Source code in src/dbally/views/sqlalchemy_base.py
apply_aggregation
async
#
Applies the chosen aggregation to the view.
PARAMETER | DESCRIPTION |
---|---|
aggregation |
IQLQuery object representing the aggregation to apply.
TYPE:
|
Source code in src/dbally/views/sqlalchemy_base.py
execute
#
execute(dry_run: bool = False) -> ViewExecutionResult
Executes the generated SQL query and returns the results.
PARAMETER | DESCRIPTION |
---|---|
dry_run |
If True, only adds the SQL query to the context field without executing the query.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
ViewExecutionResult
|
Results of the query where |