Skip to content

Commit

Permalink
docs(book): add a section about division operators (#3603)
Browse files Browse the repository at this point in the history
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Maximilian Roos <[email protected]>
  • Loading branch information
3 people authored Oct 4, 2023
1 parent 0b943ad commit 1c56448
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
19 changes: 19 additions & 0 deletions web/book/src/reference/syntax/operators.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,25 @@ operations and for function calls (see the discussion below.)
| or | <code>\|\|</code> | 9 | left-to-right |
| function call | | 10 | |

## Division and integer division

The `/` operator performs division that always returns a float value, while the
`//` operator does integer division (truncated division) that always returns an
integer value.

```prql
prql target:sql.sqlite
from [
{a = 5, b = 2},
{a = 5, b = -2},
]
select {
div_out = a / b,
int_div_out = a // b,
}
```

## Coalesce

We can coalesce values with an `??` operator. Coalescing takes either the first
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
source: web/book/tests/documentation/book.rs
expression: "prql target:sql.sqlite\n\nfrom [\n {a = 5, b = 2},\n {a = 5, b = -2},\n]\nselect {\n div_out = a / b,\n int_div_out = a // b,\n}\n"
---
WITH table_0 AS (
SELECT
5 AS a,
2 AS b
UNION
ALL
SELECT
5 AS a,
-2 AS b
)
SELECT
(a * 1.0 / b) AS div_out,
ROUND(ABS(a / b) - 0.5) * SIGN(a) * SIGN(b) AS int_div_out
FROM
table_0

0 comments on commit 1c56448

Please sign in to comment.