Data Object - answerrocket/answerrocket-python-client GitHub Wiki
Overview
The data
object provides access to a variety of data-related features, including executing SQL against a database connection, as well as obtaining information about a dataset and its domain model.
If multiple datasets have the same name and are accessible to the current user, then the first one will be returned.
The following example creates an instance of the Max SDK, gets a reference to the data
object, and calls the get_dataset_id()
function.
from answer_rocket import AnswerRocketClient
max = AnswerRocketClient()
data = max.data
dataset_id = data.get_dataset_id("pasta")
Functions
execute_sql_query()
Executes a SQL query against the specified database connection.
execute_sql_query(database_id: UUID, sql_query: str, row_limit: Optional[int] = None, copilot_id: Optional[UUID] = None, copilot_skill_id: Optional[UUID] = None) -> ExecuteSqlQueryResult
Parameters:
database_id: The UUID of the database connection. This can obtained from the Max UI.
sql_query: The SQL query to execute against the database / schema.
Note that only SQL _queries_ will be run, and SQL _commands_ (like DROP DATABASE) are not allowed.
row_limit: An optional row limit to be applied to the SQL results.
copilot_id: An optional copilot UUID that broadens access to the specified database. If the user has access to the copilot, then they do not need direct access to the database in order to execute the SQL query.
copilot_skill_id: An optional copilot skill UUID that broadens access to the specified database. If the user has access to the copilot skill, then they do not need direct access to the database in order to execute the SQL query.
Returns: returns an instance of the ExecuteSqlQueryResult object (see below for details).
Examples:
execute_sql_query_result = max.data.execute_sql_query("9c569fcb-d47f-46f0-ac87-0ea8ea6882a5", "SELECT * FROM transactions WHERE year = 2023", 100)
if not execute_sql_query_result.success:
print(execute_sql_query_result.error)
print(execute_sql_query_result.code)
else:
print(execute_sql_query_result.df)
ExecuteSqlQueryResult object
Fields:
success: A boolean indicating if the query was successful
code: An optional integer error code
error: An optional string error message
df: A Pandas data frame containing the results of the query (or None if an error occurred).
execute_rql_query()
Executes a RQL (Rocket Query Language) query against the specified dataset.
execute_rql_query(dataset_id: UUID, rql_query: str, row_limit: Optional[int] = None) -> ExecuteRqlQueryResult
Parameters:
dataset_id: The UUID of the dataset. This can obtained from the Max UI.
rql_query: The RQL query to execute against the dataset.
row_limit: An optional row limit to be applied to the generated SQL.
copilot_id: An optional copilot UUID that will broaden access to the specified dataset.
Returns: returns an instance of the ExecuteRqlQueryResult object (see below for details).
Examples:
execute_rql_query_result = max.data.execute_rql_query("2405863e-b87b-4cca-abbc-e8d740b7d84d", "@out sales.amount GROUP BY date.year")
if not execute_rql_query_result.success:
print(execute_rql_query_result.error)
print(execute_rql_query_result.code)
else:
print(execute_rql_query_result.df)
rql_script_response = execute_rql_query_result.rql_script_response
print(rql_script_response["sql"])
ExecuteRqlQueryResult object
Fields:
success: A boolean indicating if the query was successful
code: An optional integer error code
error: An optional string error message
rql_script_response: The RQL script response, which includes the generated SQL
get_dataset_id()
Gets a dataset ID by dataset name. A case-insensitive search is performed on the datasets that the user has access to.
get_dataset_id(dataset_name: String) -> Optional[UUID]
Parameters:
dataset_name: The name of the dataset. A case-insensitive search is performed.
Returns: the UUID of the matching dataset, or None if not found.
get_dataset()
Gets a dataset by dataset ID.
get_dataset_id(dataset_id: UUID, copilot_id: Optional[UUID] = None) -> Optional[MaxDataset]
Parameters:
dataset_id: The UUID of the dataset.
copilot_id: An optional copilot UUID that broadens access to the specified dataset. If the user has access to the copilot, then they do not need direct access to the dataset.
Returns: a MaxDataset object, or None if not found.
get_domain_object()
Gets a domain object by ID.
get_domain_object(dataset_id: UUID, domain_object_id: str) -> DomainObjectResult
Parameters:
dataset_id: The UUID of the dataset. See the get_dataset_id() function.
domain_object_id: The string ID of the domain object.
Returns: returns an instance of the DomainObjectResult object (see below for details).
Examples:
dataset_id = get_dataset_id("sales")
domain_object_result = data.get_domain_object(dataset_id, "fact_sales__sales_amount")
if domain_object_result.success:
print(domain_object_result.domain_object)
else:
print(domain_object_result.error)
get_domain_object_by_name()
Gets a domain object by name.
get_domain_object_name(dataset_id: UUID, rql_name: str) -> : DomainObjectResult
Parameters:
dataset_id: The UUID of the dataset. See the get_dataset_id() function.
rql_name: The RQL name of the domain object to retrieve.
Returns: returns an instance of the DomainObjectResult object (see below for details).
Examples:
dataset_id = get_dataset_id("sales")
domain_object_result = data.get_domain_object_by_name(dataset_id, "transactions.amount")
if domain_object_result.success:
print(domain_object_result.domain_object)
else:
print(domain_object_result.error)
DomainObjectResult object
Fields:
success: A boolean indicating if the query was successful
code: An optional integer error code
error: An optional string error message
domain_object: The domain object retrieved (or None if not found)
reload_dataset()
Reloads a dataset, which includes generating dimension value files.
reload_dataset(self, dataset_id: Optional[UUID] = None, database_id: Optional[UUID] = None, table_names: Optional[List[str]] = None) -> MaxMutationResponse:
Parameters:
dataset_id: The UUID of the dataset. Can be None if databaseId is specified.
database_id: The UUID of the database. If specified, then all datasets for the database will be reloaded.
tables_names: An optional list of table names to reload. If not specified, then all tables for the dataset will be processed.
Returns: returns a MaxMutationResponse object.
Examples:
dataset_id = get_dataset_id("sales")
response = data.reload_dataset(dataset_id)
if response.success:
print("Dataset was reloaded")
else:
print("Error reloading dataset: " + response.error)
update_dataset_date_range()
Modifies the dataset start date and end date.
update_dataset_date_range(self, dataset_id: UUID, min_date: str, max_date: str) -> MaxMutationResponse:
Parameters:
dataset_id: The UUID of the dataset.
min_date: The start date of the date range in ISO date time format
max_date: The end date of the date range in ISO date time format
Returns: returns a MaxMutationResponse object.
Examples:
dataset_id = get_dataset_id("sales")
response = data.update_dataset_date_range(dataset_id, "2019-02-01T00:00:00Z", "2023-05-01T00:00:00Z")