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 arollup_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_joinpre-aggregation joins rollups from different data sources in Cube Store, so one Cube query can read members of both data sources.rollup_joinis currently in Preview, and its API may change.
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 inCUBEJS_DATASOURCES:
rollup_join also needs Cube Store.
Data modeling
The model has three cubes and one view:ordersreads the store’s orders from the default data source.legacy_ordersreads the older orders from theerpdata source. It has the same members asorders, so the two can be appended.customersreads customer records from theerpdata source. Point a cube at a named data source withdata_source.- The
salesview includes members ofordersandcustomers, so it spans both data sources.
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:
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.
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 theerp data source:
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 throughCROSS JOIN or __cubeJoinField is one Cube query,
so it can read only one data source:
orders_by_country
rollup_join covers. Cube Store then serves it from the joined rollups:
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 thesales view that
uses only members of orders reads one data source, so it works, and the SQL API
runs it as one Cube query:
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:
customers_name also fails, because the sales view reaches customers through
orders.
Expressions over both data sources
The SQL API runs an expression such asCASE 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:
rollup_join covers the members, because Cube Store then runs the
expression:
Checking how a query runs
RunEXPLAIN 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
UnionorJoinover severalCubeScannodes means post-processing. EachCubeScanis one Cube query against one data source. CubeScanWrappedSqlmeans SQL pushdown. The plan shows the SQL sent to the data source, or to Cube Store when a pre-aggregation serves the query.
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_LIMITrows. It defaults toCUBEJS_DB_QUERY_LIMIT, which is50,000. The cap applies before the results are combined. With streaming mode enabled, a query with noLIMITis streamed and not capped, unless Cube Store serves it from a pre-aggregation. Either way, aggregate inside eachSELECTor 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_joinneeds an index on the join key. Without theid_indexoncustomers_by_id, Cube Store fails withCan't find index to join table. - The join SQL of a
rollup_joinmust reference members. Write{CUBE.customer_id} = {customers.id}, not{CUBE}.customer_id = {customers}.id. A join on raw columns fails withFrom members are not found in join. - A cube name that starts with
pg_needs a schema. The SQL API routes such a name topg_catalog, and the query fails withTable or CTE with name 'pg_...' not found. Qualify the name with the schema that holds cubes, or avoid the prefix.
rollup_join walkthrough.