Skip to main content

Views

The best practice is to use views to specify explicit join paths for SQL API queries. While BI tools would see a view as a table, in fact, no materialization is performed until Cube is queried through the SQL API. At the query time, Cube will try to maximize member pushdown, so only required parts of the view are materialized at query time. Cube also solves fan and chasm traps based on the dimensions selected in the query, so if measure aggregation types are properly set up, you will see correct results in BI tools even though cubes and views are seen just as tables. Consider the following data model:
With this data model, here’s how you can get orders count by users’ cities:

CROSS JOIN and __cubeJoinField

The SQL API also supports joins through the __cubeJoinField virtual column, which allows to control how specific cubes are joined. This is considered an advanced functionality, and views should be used where possible. Join can also be done through CROSS JOIN. Usage of CROSS JOIN or __cubeJoinField instructs Cube to perform join as it’s defined in a data model while using provided cubes as join hints. For example, the following query joins the orders and products tables under the hood on orders.product_id = products.id, exactly the same way as the REST (JSON) API query does:
Or through CROSS JOIN:
In the resulting query plan, you won’t see any joins as you can’t see those for REST (JSON) API queries either:
This feature allows you to join cubes even joined transitively only. In most of the BI tools you’d use __cubeJoinField to define joins between cube tables. In tools that allow defining custom SQL datasets, you can use joined tables as a dataset SQL. For example:
Please note we use aliasing to avoid name clashing between cube members in a resulting data set. In this case, wrapped SQL will be properly processed by Cube, pushing down all operations to Cube query:
We can see this by introspecting the EXPLAIN plan for this query:
Please note that, even if product_description is in the inner selection, it isn’t evaluated in the final query as it isn’t used in any way.

Joining views on a shared dimension

When you join two views on a dimension that resolves to the same underlying cube member and group by that dimension, Cube doesn’t perform a row-level join. Instead it merges them into a single multi-fact query: each view becomes its own aggregating subquery and the results are stitched together on the shared key, so measures from both views are combined without fan-out.
The JOIN type (INNER, LEFT, RIGHT, FULL) controls which keys are kept. This requires the Tesseract SQL planner and only applies to grouped queries whose GROUP BY is the join key. See multi-fact views for the full explanation.