Skip to content

Commit b103af8

Browse files
committed
docs: Clarify the cube.sql() function with an example
1 parent 471e10c commit b103af8

File tree

3 files changed

+81
-18
lines changed

3 files changed

+81
-18
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,17 @@ cube(`all_sales`, {
193193
});
194194
```
195195

196+
<ReferenceBox>
197+
198+
Currently, [`{cube.sql()}` function][ref-cube-sql-func] is not supported in YAML data models.
199+
Please [track this issue](https://github.com/cube-js/cube/issues/7484).
200+
201+
As a workaround, you can use JavaScript data models, put a SQL query in a
202+
[Jinja](/product/data-modeling/dynamic/jinja#jinja) variable, or load it from
203+
the [template context](/reference/python/cube#templatecontext-class).
204+
205+
</ReferenceBox>
206+
196207
Another use case of the Data Blending approach would be when you want to chart
197208
some measures (business related) together and see how they correlate.
198209

@@ -236,3 +247,5 @@ const queries = [
236247

237248
const resultSet = await cubeApi.load(queries);
238249
```
250+
251+
[ref-cube-sql-func]: /product/data-modeling/syntax#cubesql-function

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

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -90,21 +90,6 @@ cube(`users`, {
9090

9191
Then you can derive the `teachers` and `students` cubes from `users`:
9292

93-
<CodeTabs>
94-
95-
```yaml
96-
cubes:
97-
- name: teachers
98-
extends: users
99-
sql: >
100-
SELECT * FROM {users.sql()} WHERE type = 'teacher'
101-
102-
- name: students
103-
extends: users
104-
sql: >
105-
SELECT * FROM {users.sql()} WHERE type = 'student'
106-
```
107-
10893
```javascript
10994
cube(`teachers`, {
11095
extends: users,
@@ -125,11 +110,9 @@ cube(`students`, {
125110
});
126111
```
127112

128-
</CodeTabs>
129-
130113
<ReferenceBox>
131114

132-
Currently, `{cube.sql()}` is not supported in YAML data models.
115+
Currently, [`{cube.sql()}` function][ref-cube-sql-func] is not supported in YAML data models.
133116
Please [track this issue](https://github.com/cube-js/cube/issues/7484).
134117

135118
As a workaround, you can use JavaScript data models, put a SQL query in a
@@ -180,3 +163,5 @@ cube(`lessons`, {
180163
[ref-schema-advanced-extend]:
181164
/product/data-modeling/concepts/code-reusability-extending-cubes
182165
[ref-schema-ref-cubes-extends]: /reference/data-model/cube#extends
166+
167+
[ref-cube-sql-func]: /product/data-modeling/syntax#cubesql-function

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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,69 @@ LEFT JOIN contacts "contacts"
539539
ON "users".contact_id = "contacts".id
540540
```
541541

542+
### `{cube.sql()}` function
543+
544+
When defining a cube, you can reference the `sql` parameter of another cube,
545+
effectively reusing the SQL query it's defined on. This is particularly useful
546+
when defining [polymorphic cubes][ref-polymorphism] or using [data blending][ref-data-blending].
547+
548+
Consider the following data model:
549+
550+
```javascript
551+
cube(`organisms`, {
552+
sql_table: `organisms`
553+
})
554+
555+
cube(`animals`, {
556+
sql: `
557+
SELECT *
558+
FROM ${organisms.sql()}
559+
WHERE kingdom = 'animals'
560+
`
561+
})
562+
563+
cube(`dogs`, {
564+
sql: `
565+
SELECT *
566+
FROM ${animals.sql()}
567+
WHERE species = 'dogs'
568+
`,
569+
570+
measures: {
571+
count: {
572+
type: `count`
573+
}
574+
}
575+
})
576+
```
577+
578+
If you query for `dogs.count`, Cube will generate the following SQL:
579+
580+
```sql
581+
SELECT count(*) "dogs__count"
582+
FROM (
583+
SELECT *
584+
FROM (
585+
SELECT *
586+
FROM organisms
587+
WHERE kingdom = 'animals'
588+
)
589+
WHERE species = 'dogs'
590+
) AS "dogs"
591+
```
592+
593+
<ReferenceBox>
594+
595+
Currently, `{cube.sql()}` function is only supported in JavaScript data models.
596+
It is not supported in YAML data models.
597+
Please [track this issue](https://github.com/cube-js/cube/issues/7484).
598+
599+
As a workaround, you can use JavaScript data models, put a SQL query in a
600+
[Jinja](/product/data-modeling/dynamic/jinja#jinja) variable, or load it from
601+
the [template context](/reference/python/cube#templatecontext-class).
602+
603+
</ReferenceBox>
604+
542605
### Non-SQL references
543606

544607
Outside [SQL expressions][self-sql-expressions], `column` is not recognized
@@ -629,3 +692,5 @@ defining dynamic data models.
629692
[ref-default-granularities]: /product/data-modeling/concepts#time-dimensions
630693
[ref-custom-granularities]: /reference/data-model/dimensions#granularities
631694
[ref-style-guide]: /guides/style-guide
695+
[ref-polymorphism]: /product/data-modeling/concepts/polymorphic-cubes
696+
[ref-data-blending]: /product/data-modeling/concepts/data-blending

0 commit comments

Comments
 (0)