Skip to content

Commit

Permalink
docs: expand ibis-for-sql-users set operator examples (#10395)
Browse files Browse the repository at this point in the history
Co-authored-by: Phillip Cloud <[email protected]>
  • Loading branch information
IndexSeek and cpcloud authored Oct 30, 2024
1 parent e1f9124 commit 5c14965
Showing 1 changed file with 37 additions and 3 deletions.
40 changes: 37 additions & 3 deletions docs/tutorials/ibis-for-sql-users.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -1151,7 +1151,12 @@ engine.

To appear.

## Unions
## Set operators

Set operations are a common SQL idiom for combining or comparing rows
from different relations. Ibis supports the following set operations:

### Unions

SQL dialects often support two kinds of `UNION` operations:

Expand All @@ -1163,13 +1168,42 @@ The Ibis `union` function by default is a `UNION ALL`, and you can set
`distinct=True` to get the normal `UNION` behavior:

```{python}
expr1 = t1.limit(10)
expr2 = t1.limit(10, offset=10)
#| code-fold: true
sales_2023 = ibis.table(
dict(product_name="string", order_quantity="int64"),
name="sales_2023",
)
sales_2024 = ibis.table(
dict(product_name="string", order_quantity="int64"),
name="sales_2024",
)
```

```{python}
expr1 = sales_2023.limit(10)
expr2 = sales_2024.limit(10, offset=10)
expr = expr1.union(expr2)
ibis.to_sql(expr)
```

### Intersection and difference

SQL dialects support `INTERSECT` and `EXCEPT` operators that returns rows that
are in both tables or only in one table, respectively.

In Ibis, we can perform these operations using `intersection` and `difference`.

```{python}
expr = ibis.difference(sales_2023, sales_2024)
ibis.to_sql(expr)
```

```{python}
expr = ibis.intersect(sales_2023, sales_2024)
ibis.to_sql(expr)
```

## Esoterica

This area will be the spillover for miscellaneous SQL concepts and how
Expand Down

0 comments on commit 5c14965

Please sign in to comment.