Skip to main content

Use case

Some datasets are spread across more than one database. Take an online store: its orders live in the store’s own database, while customer records, and the orders placed before the store went online, live in an ERP system. Reports need both: revenue by customer, revenue by country, and all orders, old and new, in one result set. The SQL API can query across these databases, but only in some query shapes. This recipe shows every shape, which ones work, and why.

How Cube queries across data sources

Without a rollup_join, a single Cube query runs against one data source. A query that spans data sources works only in one of two ways:
  • Post-processing. The SQL API splits the query into several Cube queries, each against one data source, and combines their results in query post-processing. This works for a union, and for a join of grouped results.
  • Pre-aggregations. A rollup_join pre-aggregation joins rollups from different data sources in Cube Store, so one Cube query can read members of both data sources. rollup_join is currently in Preview, and its API may change.
Everything else fails, including a regular join of two cubes from different data sources.

Configuration

Define the two connections as multiple data sources. The default source needs no name. Every other source gets one, and the full list goes in CUBEJS_DATASOURCES:
A rollup_join also needs Cube Store.

Data modeling

The model has three cubes and one view:
  • orders reads the store’s orders from the default data source.
  • legacy_orders reads the older orders from the erp data source. It has the same members as orders, so the two can be appended.
  • customers reads customer records from the erp data source. Point a cube at a named data source with data_source.
  • The sales view includes members of orders and customers, so it spans both data sources.
The source dimension is a constant that tells which database a row came from. The pre-aggregations at the bottom of orders and customers define a rollup_join that serves revenue by status and country:

Querying

Connect to the SQL API to run the queries below.

Appending rows with UNION ALL

Append orders and legacy_orders with UNION ALL:
Each SELECT runs against its own database, and the SQL API appends the results. A WHERE clause in a SELECT reaches that database, so it limits what the database scans. Wrap the union in a CTE to aggregate over both databases:
UNION, an outer ORDER BY, and an outer LIMIT also work.
Only additive measures can be combined across data sources. Summing a sum or a count from each data source is correct. Non-additive measures, such as count_distinct, avg, or percentiles, cannot be combined from per-source results: summing distinct counts double-counts values present in both databases. No error is raised, so report these measures per data source.

Joining grouped results

To relate rows across data sources, join two grouped subqueries on a regular column. Here, revenue per customer from the default data source is joined to customer names from the erp data source:
Each subquery is a separate Cube query, and the SQL API joins their results. Both subqueries need GROUP BY. If either subquery is ungrouped, as in SELECT customer_id, revenue FROM orders, the query fails with Use __cubeJoinField to join Cubes.

Joining cubes in one query

A join of two cubes through CROSS JOIN or __cubeJoinField is one Cube query, so it can read only one data source:
The same query works when it uses only members that the orders_by_country rollup_join covers. Cube Store then serves it from the joined rollups:
To relate members that no rollup_join covers, such as customers.name, join grouped results instead.

Querying a view that spans data sources

A view member takes the data source of its cube. A query to the sales view that uses only members of orders reads one data source, so it works, and the SQL API runs it as one Cube query:
A query that uses members of both data sources, such as customers_name and revenue, fails with the same rollupJoin error as a join of cubes. It works only when a rollup_join covers all of its members, as orders_by_country covers customers_country and revenue:
A view query reads the data sources of every cube on the join path to its members, not only the cubes of those members. A query that uses only customers_name also fails, because the sales view reaches customers through orders.

Expressions over both data sources

The SQL API runs an expression such as CASE or LOWER over a view through SQL pushdown, which sends the expression to the data source together with the query. Pushdown targets one data source, so an expression that reads members of both data sources fails:
It works when a rollup_join covers the members, because Cube Store then runs the expression:

Checking how a query runs

Run EXPLAIN on a query to see how the SQL API runs it. In a workbook, the Generated SQL tab of an inspected query shows the SQL that Cube sends to the data source. In the EXPLAIN output:
  • A Union or Join over several CubeScan nodes means post-processing. Each CubeScan is one Cube query against one data source.
  • CubeScanWrappedSql means SQL pushdown. The plan shows the SQL sent to the data source, or to Cube Store when a pre-aggregation serves the query.
A CubeScan with a request names each cube, not its data source. Read it together with the data_source of each cube. A pushed-down query names where it runs: in the physical plan, its CubeScanExecutionPlan, SQL: line ends with JSON, and the SQL follows on the next lines:
dataSource is the data source that runs the SQL, and preAggregations lists the IDs of the pre-aggregations it reads, such as orders.main. When Cube Store runs it, external is true and dataSource is null.

Limitations

  • Each Cube query is capped by the row limit. Post-processing combines the results of separate Cube queries, and each of them returns at most CUBESQL_NON_STREAMING_QUERY_MAX_ROW_LIMIT rows. It defaults to CUBEJS_DB_QUERY_LIMIT, which is 50,000. The cap applies before the results are combined. With streaming mode enabled, a query with no LIMIT is streamed and not capped, unless Cube Store serves it from a pre-aggregation. Either way, aggregate inside each SELECT or subquery, as in the examples above. Do not union or join raw rows.
  • SQL pushdown targets one data source. A union, a join, or an expression cannot be pushed down across data sources. A union and a join of grouped results run in post-processing instead. An expression over both data sources needs a rollup_join.
  • Non-additive measures cannot be combined across data sources. See the warning above.
  • The joined rollup of a rollup_join needs an index on the join key. Without the id_index on customers_by_id, Cube Store fails with Can't find index to join table.
  • The join SQL of a rollup_join must reference members. Write {CUBE.customer_id} = {customers.id}, not {CUBE}.customer_id = {customers}.id. A join on raw columns fails with From members are not found in join.
  • A cube name that starts with pg_ needs a schema. The SQL API routes such a name to pg_catalog, and the query fails with Table or CTE with name 'pg_...' not found. Qualify the name with the schema that holds cubes, or avoid the prefix.
See Joining data from multiple data sources for a rollup_join walkthrough.