atoti.query.cube module

class atoti.query.cube.QueryCube(_name, _hierarchies, _measures, _session)

Query cube.

property hierarchies: atoti._base._base_hierarchies._BaseHierarchies

Hierarchies of the cube.

Return type

~_BaseHierarchies

property levels: atoti.query.levels.QueryLevels

Levels of the cube.

Return type

QueryLevels

property measures: atoti._base._base_measures._Measures

Measures of the cube.

Return type

~_Measures

property name: str

Name of the cube.

Return type

str

query(*measures, condition=None, include_totals=False, levels=None, scenario='Base', timeout=30, **kwargs)

Query the cube to retrieve the value of the passed measures on the given levels.

In JupyterLab with the atoti-jupyterlab plugin installed, query results can be converted to interactive widgets with the Convert to Widget Below action available in the command palette or by right clicking on the representation of the returned Dataframe.

Parameters
  • measures (QueryMeasure) – The measures to query.

  • condition (Union[LevelCondition, MultiCondition, LevelIsInCondition, HierarchyIsInCondition, None]) –

    The filtering condition. Only conditions on level equality with a string are supported.

    Examples

    >>> df = pd.DataFrame(
    ...     columns=["Continent", "Country", "Currency", "Price"],
    ...     data=[
    ...         ("Europe", "France", "EUR", 200.0),
    ...         ("Europe", "Germany", "EUR", 150.0),
    ...         ("Europe", "United Kingdom", "GBP", 120.0),
    ...         ("America", "United states", "USD", 240.0),
    ...         ("America", "Mexico", "MXN", 270.0),
    ...     ],
    ... )
    >>> table = session.read_pandas(
    ...     df,
    ...     keys=["Continent", "Country", "Currency"],
    ...     table_name="Prices",
    ... )
    >>> cube = session.create_cube(table)
    >>> del cube.hierarchies["Continent"]
    >>> del cube.hierarchies["Country"]
    >>> cube.hierarchies["Geography"] = [
    ...     table["Continent"],
    ...     table["Country"],
    ... ]
    >>> session = tt.open_query_session(f"http://localhost:{session.port}")
    >>> cube = session.cubes[cube.name]
    >>> h, l, m = cube.hierarchies, cube.levels, cube.measures
    
    >>> cube.query(
    ...     m["Price.SUM"],
    ...     levels=[l["Country"]],
    ...     condition=l["Continent"] == "Europe",
    ... )
                             Price.SUM
    Continent Country
    Europe    France            200.00
              Germany           150.00
              United Kingdom    120.00
    
    
    >>> cube.query(
    ...     m["Price.SUM"],
    ...     levels=[l["Country"], l["Currency"]],
    ...     condition=(
    ...         (l["Continent"] == "Europe")
    ...         & (l["Currency"] == "EUR")
    ...     ),
    ... )
                               Price.SUM
    Continent Country Currency
    Europe    France  EUR         200.00
              Germany EUR         150.00
    
    >>> cube.query(
    ...     m["Price.SUM"],
    ...     levels=[l["Country"]],
    ...     condition=h["Geography"].isin(
    ...         ("America",), ("Europe", "Germany")
    ...     ),
    ... )
                            Price.SUM
    Continent Country
    America   Mexico           270.00
              United states    240.00
    Europe    Germany          150.00
    

  • include_totals (bool) –

    Whether the returned DataFrame should include the grand total and subtotals. Totals can be useful but they make the DataFrame harder to work with since its index will have some empty values.

    Example

    >>> cube.query(
    ...     m["Price.SUM"],
    ...     levels=[l["Country"], l["Currency"]],
    ...     include_totals=True,
    ... )
                                      Price.SUM
    Continent Country        Currency
    Total                                980.00
    America                              510.00
              Mexico                     270.00
                             MXN         270.00
              United states              240.00
                             USD         240.00
    Europe                               470.00
              France                     200.00
                             EUR         200.00
              Germany                    150.00
                             EUR         150.00
              United Kingdom             120.00
                             GBP         120.00
    

  • levels (Optional[Sequence[QueryLevel]]) – The levels to split on. If None, the value of the measures at the top of the cube is returned.

  • scenario (str) – The scenario to query.

  • timeout (int) – The query timeout in seconds.

Return type

QueryResult