Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 4 additions & 103 deletions doc/user/content/transform-data/idiomatic-materialize-sql/any.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,72 +51,16 @@ array/list/map contains duplicates, include [`DISTINCT`](/sql/select/#select-dis
<td><blue>Materialize SQL</blue></td>
<td class="copyableCode">

**If no duplicates exist in the unnested field:** Use a Common Table
Expression (CTE) to [`UNNEST()`](/sql/functions/#unnest) the array of values and
perform the equi-join on the unnested values.

<br>
<div style="background-color: var(--code-block)">

```mzsql
-- array_field contains no duplicates.--

WITH my_expanded_values AS
(SELECT UNNEST(array_field) AS fieldZ FROM tableB)
SELECT a.fieldA, ...
FROM tableA a
JOIN my_expanded_values t ON a.fieldZ = t.fieldZ
;
```

</td>
</tr>
<tr>
<td><blue>Materialize SQL</blue></td>
<td class="copyableCode">

**Duplicates may exist in the unnested field:** Use a Common Table
Expression (CTE) to [`DISTINCT`](/sql/select/#select-distinct)
[`UNNEST()`](/sql/functions/#unnest) the array of values and perform the
equi-join on the unnested values.

<br>
<div style="background-color: var(--code-block)">


```mzsql
-- array_field may contain duplicates.--

WITH my_expanded_values AS
(SELECT DISTINCT UNNEST(array_field) AS fieldZ FROM tableB)
SELECT a.fieldA, ...
FROM tableA a
JOIN my_expanded_values t ON a.fieldZ = t.fieldZ
;
```
{{% include-from-yaml data="idiomatic_mzsql/patterns_general" name="any-equi-join" field="syntax_idiomatic" %}}

</td>
</tr>
<tr>
<td><red>Anti-pattern</red> ❌</td>
<td>

<red>Avoid the use of [`ANY(...)` function](/sql/functions/#expression-bool_op-any) for equi-join
conditions.</red>

<br>
<div style="background-color: var(--code-block)">

```nofmt
-- Anti-pattern. Avoid. --
SELECT a.fieldA, ...
FROM tableA a, tableB b
WHERE a.fieldZ = ANY(b.array_field) -- Anti-pattern. Avoid.
;
{{% include-from-yaml data="idiomatic_mzsql/patterns_general" name="any-equi-join" field="syntax_anti_pattern" %}}

```

</div>
</td>
</tr>

Expand Down Expand Up @@ -155,35 +99,7 @@ with the `orders` table on the unnested values.
<td><blue>Materialize SQL</blue> ✅</td>
<td class="copyableCode">

***If no duplicates in the unnested field***

```mzsql
-- sales_items.items contains no duplicates. --

WITH individual_sales_items AS
(SELECT unnest(items) as item, week_of FROM sales_items)
SELECT s.week_of, o.order_id, o.item, o.quantity
FROM orders o
JOIN individual_sales_items s ON o.item = s.item
WHERE date_trunc('week', o.order_date) = s.week_of
ORDER BY s.week_of, o.order_id, o.item, o.quantity
;
```

***To omit duplicates that may exist in the unnested field***

```mzsql
-- sales_items.items may contains duplicates --

WITH individual_sales_items AS
(SELECT DISTINCT unnest(items) as item, week_of FROM sales_items)
SELECT s.week_of, o.order_id, o.item, o.quantity
FROM orders o
JOIN individual_sales_items s ON o.item = s.item
WHERE date_trunc('week', o.order_date) = s.week_of
ORDER BY s.week_of, o.order_id, o.item, o.quantity
;
```
{{% include-from-yaml data="idiomatic_mzsql/patterns_general" name="any-equi-join" field="example_idiomatic" %}}

</td>
</tr>
Expand All @@ -192,22 +108,7 @@ ORDER BY s.week_of, o.order_id, o.item, o.quantity
<td><red>Anti-pattern</red> ❌</td>
<td>

<red>Avoid the use of [`ANY()`](/sql/functions/#expression-bool_op-any) for the equi-join condition.</red>

<br>
<div style="background-color: var(--code-block)">

```nofmt
-- Anti-pattern. Avoid. --
SELECT s.week_of, o.order_id, o.item, o.quantity
FROM orders o
JOIN sales_items s ON o.item = ANY(s.items)
WHERE date_trunc('week', o.order_date) = s.week_of
ORDER BY s.week_of, o.order_id, o.item, o.quantity
;
```

</div>
{{% include-from-yaml data="idiomatic_mzsql/patterns_general" name="any-equi-join" field="example_anti_pattern" %}}

</td>
</tr>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ performance.

### Query Patterns

{{% idiomatic-sql/general-syntax-table %}}
{{< yaml-table data="idiomatic_mzsql/patterns_general"
columns="pattern_title,syntax_idiomatic" >}}

### Examples

{{% idiomatic-sql/general-example-table %}}
{{< yaml-table data="idiomatic_mzsql/patterns_general"
columns="pattern_title,example_idiomatic" >}}

## Window Functions
{{< callout >}}
Expand All @@ -34,11 +36,13 @@ performance.

### Query Patterns

{{% idiomatic-sql/window-functions-syntax-table %}}
{{< yaml-table data="idiomatic_mzsql/patterns_window_functions"
columns="pattern_title,syntax_idiomatic" >}}

### Examples

{{% idiomatic-sql/window-functions-example-table %}}
{{< yaml-table data="idiomatic_mzsql/patterns_window_functions"
columns="pattern_title,example_idiomatic" >}}

## See also

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,7 @@ in a subquery.
<td><blue>Materialize SQL</blue></td>
<td class="copyableCode">

Use a subquery that uses the [MIN()](/sql/functions/#min) or
[MAX()](/sql/functions/#max) aggregate function.

<br>
<div style="background-color: var(--code-block)">

```mzsql
SELECT tableA.fieldA, tableA.fieldB, minmax.Z
FROM tableA,
(SELECT fieldA,
MIN(fieldZ),
MAX(fieldZ)
FROM tableA
GROUP BY fieldA) minmax
WHERE tableA.fieldA = minmax.fieldA
ORDER BY fieldA ... ;
```
{{% include-from-yaml data="idiomatic_mzsql/patterns_window_functions" name="first-value" field="syntax_idiomatic" %}}

</td>
</tr>
Expand All @@ -64,23 +48,8 @@ ORDER BY fieldA ... ;
<td><red>Anti-pattern</red> ❌</td>
<td>

<red>Avoid the use of [`FIRST_VALUE() OVER (PARTITION BY ... ORDER BY ...)`
window function](/sql/functions/#first_value) for first value within groups
queries.</red>
{{% include-from-yaml data="idiomatic_mzsql/patterns_window_functions" name="first-value" field="syntax_anti_pattern" %}}

<br>
<div style="background-color: var(--code-block)">

```nofmt
-- Anti-pattern. Avoid. --
SELECT fieldA, fieldB,
FIRST_VALUE(fieldZ) OVER (PARTITION BY fieldA ORDER BY ...),
FIRST_VALUE(fieldZ) OVER (PARTITION BY fieldA ORDER BY ... DESC)
FROM tableA
ORDER BY fieldA, ...;
```

</div>
</td>
</tr>

Expand Down Expand Up @@ -139,45 +108,16 @@ value if ordered by ascending price values).
<td><blue>Materialize SQL</blue> ✅</td>
<td class="copyableCode">

```mzsql
SELECT o.order_id, minmax.lowest_price, o.item, o.price,
o.price - minmax.lowest_price AS diff_lowest_price
FROM orders_view o,
(SELECT order_id,
MIN(price) AS lowest_price
FROM orders_view
GROUP BY order_id) minmax
WHERE o.order_id = minmax.order_id
ORDER BY o.order_id, o.item;
```
{{% include-from-yaml data="idiomatic_mzsql/patterns_window_functions" name="first-value" field="extra_example_idiomatic_min" %}}

</td>
</tr>
<tr>
<td><red>Anti-pattern</red> ❌</td>
<td>

<red>Avoid the use of [`FIRST_VALUE() OVER (PARTITION BY ... ORDER BY ...)`
window function](/sql/functions/#first_value) for first value within groups queries.</red>

<br>
<div style="background-color: var(--code-block)">


```nofmt
-- Anti-pattern --
SELECT order_id,
FIRST_VALUE(price)
OVER (PARTITION BY order_id ORDER BY price) AS lowest_price,
item,
price,
price - FIRST_VALUE(price)
OVER (PARTITION BY order_id ORDER BY price) AS diff_lowest_price
FROM orders_view
ORDER BY order_id, item;
```
{{% include-from-yaml data="idiomatic_mzsql/patterns_window_functions" name="first-value" field="extra_example_anti_pattern_min" %}}

</div>
</td>
</tr>
</tbody>
Expand All @@ -203,49 +143,18 @@ value if ordered by descending price values).
<td><blue>Materialize SQL</blue> ✅</td>
<td class="copyableCode">

```mzsql
SELECT o.order_id, minmax.highest_price, o.item, o.price,
o.price - minmax.highest_price AS diff_highest_price
FROM orders_view o,
(SELECT order_id,
MAX(price) AS highest_price
FROM orders_view
GROUP BY order_id) minmax
WHERE o.order_id = minmax.order_id
ORDER BY o.order_id, o.item;
```
{{% include-from-yaml data="idiomatic_mzsql/patterns_window_functions" name="first-value" field="extra_example_idiomatic_max" %}}

</td>
</tr>
<tr>
<td><red>Anti-pattern</red> ❌</td>
<td>

<red>Avoid the use of [`FIRST_VALUE() OVER (PARTITION BY ... ORDER BY ...)`
window function](/sql/functions/#first_value) for first value within groups
queries.</red>

<br>
<div style="background-color: var(--code-block)">


```nofmt
-- Anti-pattern --
SELECT order_id,
FIRST_VALUE(price)
OVER (PARTITION BY order_id ORDER BY price DESC) AS highest_price,
item,
price,
price - FIRST_VALUE(price)
OVER (PARTITION BY order_id ORDER BY price DESC) AS diff_highest_price
FROM orders_view
ORDER BY order_id, item;
```
{{% include-from-yaml data="idiomatic_mzsql/patterns_window_functions" name="first-value" field="extra_example_anti_pattern_max" %}}

</div>
</td>
</tr>

</tbody>
</table>

Expand All @@ -257,7 +166,7 @@ in the order and these prices. The example uses a subquery that groups by the
`order_id` and selects `MIN(price)` as the lowest price (i.e., first
value if ordered by price values) and `MAX(price)` as the
highest price (i.e., first
value if ordered by descending price values)
value if ordered by descending price values).

<table>
<thead>
Expand All @@ -271,52 +180,16 @@ value if ordered by descending price values)
<td><blue>Materialize SQL</blue> ✅</td>
<td class="copyableCode">


```mzsql
SELECT o.order_id, minmax.lowest_price, minmax.highest_price, o.item, o.price,
o.price - minmax.lowest_price AS diff_lowest_price,
o.price - minmax.highest_price AS diff_highest_price
FROM orders_view o,
(SELECT order_id,
MIN(price) AS lowest_price,
MAX(price) AS highest_price
FROM orders_view
GROUP BY order_id) minmax
WHERE o.order_id = minmax.order_id
ORDER BY o.order_id, o.item;
```
{{% include-from-yaml data="idiomatic_mzsql/patterns_window_functions" name="first-value" field="example_idiomatic" %}}

</td>
</tr>
<tr>
<td><red>Anti-pattern</red> ❌</td>
<td>

<red>Avoid the use of [`FIRST_VALUE() OVER (PARTITION BY ... ORDER BY ...)`
window function](/sql/functions/#first_value) for first value within groups
queries.</red>

<br>
<div style="background-color: var(--code-block)">


```nofmt
-- Anti-pattern --
SELECT order_id,
FIRST_VALUE(price)
OVER (PARTITION BY order_id ORDER BY price) AS lowest_price,
FIRST_VALUE(price)
OVER (PARTITION BY order_id ORDER BY price DESC) AS highest_price,
item,
price,
price - FIRST_VALUE(price)
OVER (PARTITION BY order_id ORDER BY price) AS diff_lowest_price,
price - FIRST_VALUE(price)
OVER (PARTITION BY order_id ORDER BY price DESC) AS diff_highest_price
FROM orders_view
ORDER BY order_id, item;
```
</div>
{{% include-from-yaml data="idiomatic_mzsql/patterns_window_functions" name="first-value" field="example_anti_pattern" %}}

</td>
</tr>
</tbody>
Expand Down
Loading
Loading