Skip to content

Commit 544daf3

Browse files
committed
Add an example of multiple primary keys
1 parent df271c5 commit 544daf3

File tree

2 files changed

+90
-5
lines changed

2 files changed

+90
-5
lines changed

docs/pages/product/data-modeling/concepts.mdx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,10 @@ cubes:
232232
233233
</CodeTabs>
234234
235+
Note that the `id` dimension is defined as a [primary key][ref-ref-primary-key].
236+
It is also possible to have more than one primary key dimension in a cube if
237+
you'd like them all to be parts of a composite key.
238+
235239
[The `line_items` table](#top) also has a couple of dimensions which can be
236240
represented as follows:
237241

@@ -729,5 +733,6 @@ Pre-Aggregations][ref-caching-preaggs-intro].
729733
[ref-pmc]: /product/deployment/cloud/deployment-types#production-multi-cluster
730734
[ref-ref-time-dimensions]: /reference/data-model/types-and-formats#time-1
731735
[ref-ref-dimension-granularities]: /reference/data-model/dimensions#granularities
736+
[ref-ref-primary-key]: /reference/data-model/dimensions#primary_key
732737
[ref-custom-granularity-recipe]: /guides/recipes/data-modeling/custom-granularity
733738
[ref-proxy-granularity]: /product/data-modeling/concepts/calculated-members#time-dimension-granularity

docs/pages/reference/data-model/dimensions.mdx

Lines changed: 85 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -264,17 +264,18 @@ cubes:
264264
265265
### `primary_key`
266266

267-
Specify which dimension is a primary key for a cube. The default value is
267+
Specify if a dimension is a primary key for a cube. The default value is
268268
`false`.
269269

270270
A primary key is used to make [joins][ref-schema-ref-joins] work properly.
271271

272-
<WarningBox>
272+
<InfoBox>
273273

274-
Setting `primary_key` to `true` will change the default value of `public`
275-
parameter to `false`. If you still want `public` to be `true`, set it manually.
274+
Setting `primary_key` to `true` will change the default value of the [`public`
275+
parameter](#public) to `false`. If you still want `public` to be `true`, set it
276+
explicitly.
276277

277-
</WarningBox>
278+
</InfoBox>
278279

279280
<CodeTabs>
280281

@@ -306,6 +307,85 @@ cubes:
306307
307308
</CodeTabs>
308309
310+
It is possible to have more than one primary key dimension in a cube if you'd
311+
like them all to be parts of a composite key:
312+
313+
<CodeTabs>
314+
315+
```javascript
316+
cube(`products`, {
317+
sql: `
318+
SELECT 1 AS column_a, 1 AS column_b UNION ALL
319+
SELECT 2 AS column_a, 1 AS column_b UNION ALL
320+
SELECT 1 AS column_a, 2 AS column_b UNION ALL
321+
SELECT 2 AS column_a, 2 AS column_b
322+
`,
323+
324+
dimensions: {
325+
composite_key_a: {
326+
sql: `column_a`,
327+
type: `number`,
328+
primary_key: true
329+
},
330+
331+
composite_key_b: {
332+
sql: `column_b`,
333+
type: `number`,
334+
primary_key: true
335+
}
336+
},
337+
338+
measures: {
339+
count: {
340+
type: `count`
341+
}
342+
}
343+
})
344+
```
345+
346+
```yaml
347+
cubes:
348+
- name: products
349+
sql: >
350+
SELECT 1 AS column_a, 1 AS column_b UNION ALL
351+
SELECT 2 AS column_a, 1 AS column_b UNION ALL
352+
SELECT 1 AS column_a, 2 AS column_b UNION ALL
353+
SELECT 2 AS column_a, 2 AS column_b
354+
355+
dimensions:
356+
- name: composite_key_a
357+
sql: column_a
358+
type: number
359+
primary_key: true
360+
361+
- name: composite_key_b
362+
sql: column_b
363+
type: number
364+
primary_key: true
365+
366+
measures:
367+
- name: count
368+
type: count
369+
```
370+
371+
</CodeTabs>
372+
373+
Querying the `count` measure of the cube shown above will generate the following
374+
SQL to the upstream data source:
375+
376+
```sql
377+
SELECT
378+
count(
379+
CAST("product".column_a as TEXT) || CAST("product".column_b as TEXT)
380+
) "product__count"
381+
FROM (
382+
SELECT 1 AS column_a, 1 AS column_b UNION ALL
383+
SELECT 2 AS column_a, 1 AS column_b UNION ALL
384+
SELECT 1 AS column_a, 2 AS column_b UNION ALL
385+
SELECT 2 AS column_a, 2 AS column_b
386+
) AS "product"
387+
```
388+
309389
### `propagate_filters_to_sub_query`
310390

311391
When this statement is set to `true`, the filters applied to the query will be

0 commit comments

Comments
 (0)