Repository navigation
feat: int division operator - #2684
Conversation
Closes #2426 Also makes sure that float division is always producing floats. This is done by casting to float before division. This can be removed when match is implemented and we can check types and use only the appropriate operation.
for more information, see https://pre-commit.ci
| let div_i = l r -> s"ROUND({l} / {r} - 0.5)" | ||
| let div_f = l r -> s"(CAST({l} AS FLOAT) / {r})" |
There was a problem hiding this comment.
I am not sure that these are always correct.
- If
ROUNDis "Bankers' rounding",ROUND(x - 0.5)would not beFROOR(x). - If
lis a type like FLOAT64, I thinkCAST(l AS FLOAT)is not correct. (See PEP 238 cited in this comment Introduce integer division operator//#2426 (comment))
There was a problem hiding this comment.
If ROUND is "Bankers' rounding", ROUND(x - 0.5) would not be FLOOR(x).
I used FLOOR before, but it was giving wrong answers for -13 / 5. I'll run tests on all databases locally now.
If l is a type like FLOAT64, I think CAST(l AS FLOAT) is not correct.
Their argument is that it would not work for complex numbers. I don't think we should concern ourselves with that yet.
There was a problem hiding this comment.
If ROUND is "Bankers' rounding", ROUND(x - 0.5) would not be FLOOR(x).
I used FLOOR before, but it was giving wrong answers for -13 / 5. I'll run tests on all databases locally now.
If l is a type like FLOAT64, I think CAST(l AS FLOAT) is not correct.
Their argument is that it would not work for complex numbers. I don't think we should concern ourselves with that yet.
There was a problem hiding this comment.
On MySQL, x * 1.0 somehow produces different results than CAST(x AS FLOAT). I think it's converting for decimal instead of float.
There was a problem hiding this comment.
Yes, this is correct — MySQL seems to have Decimal division rather than Float division...
| let div = l r -> null | ||
| let mod = l r -> null | ||
| let div_i = l r -> s"FLOOR(FLOOR(ABS({l} / {r})) * SIGN({l} / {r}))" | ||
| let div_f = l r -> s"({l} * 1.0 / {r})" |
There was a problem hiding this comment.
One downside of s-strings is that if l is 5 + 5, then it doesn't understand precedence, and becomes (5 + 5 * 1.0 / 2), so the / 2 only operates on 5 rather than 10.
I guess we handle this, I can check further to confirm.
There was a problem hiding this comment.
Actually this comes up in #2692
(started + unfinished * 1.0 / started)
# and
* 1.0 / 1 - (started ...
There was a problem hiding this comment.
We need more parenthesis!
Maybe move the "binding strength" calculation and parenthesis generation into here. Annotate the strength using some new annotation syntax?.
| let regex_search = text pattern -> s"{text} ~ {pattern}" | ||
| let div_i = l r -> s"trunc({l} / {r})::bigint" | ||
| let round = n_digits column -> s"ROUND(({column})::numeric, {n_digits})" | ||
| let regex_search = text pattern -> s"({text} ~ {pattern})" |
There was a problem hiding this comment.
So good that we have the dialect modules now — adding something goes from "difficult rust" to "easy PRQL"!
| { x_int = 13, x_float = 13.0, k_int = 5, k_float = 5.0 }, | ||
| { x_int = -13, x_float = -13.0, k_int = 5, k_float = 5.0 }, | ||
| { x_int = 13, x_float = 13.0, k_int = -5, k_float = -5.0 }, | ||
| { x_int = -13, x_float = -13.0, k_int = -5, k_float = -5.0 }, |
There was a problem hiding this comment.
I'm guessing this isn't auto-formatted? It's beautiful!
(Maybe that's a side project — align relation literals like this automatically!)
| let div = l r -> null | ||
| let mod = l r -> null | ||
| let div_i = l r -> s"FLOOR(FLOOR(ABS({l} / {r})) * SIGN({l} / {r}))" | ||
| let div_f = l r -> s"({l} * 1.0 / {r})" |
There was a problem hiding this comment.
@aljazerzen how would you feel about:
- Generic dialect is just
l / r - MySQL & MsSql use
s"({l} * 1.0 / {r})"
That way, the default SQL is cleaner, and we keep the guarantees when a dialect is specified. I do still place some weight on having the introductory examples be really easy to read and compare.
The downside is that we don't guarantee the type of division on a generic dialect.
(I'm happy to make the change if you agree)
There was a problem hiding this comment.
I don't like that. Character / in PRQL means something else than / in SQL. I don't think it should be problematic to emit code that looks differently.
All of this would be way more ergonomic if we displayed query results in the book, instead of SQL.
There was a problem hiding this comment.
I don't like that. Character
/in PRQL means something else than/in SQL.
I agree with the principle — if it's different then that's bad.
But it's only different in some minor dialects. My suggestion is to support those iff they're explicitly specified.
Another approach would be to default to DuckDB dialect (or Postgres, or Snowflake, i.e. one which has reasonable behavior here), rather than have the default try to cater to every dialect. Then we could use / safely.
All of this would be way more ergonomic if we displayed query results in the book, instead of SQL.
Showing results would be useful too, but many people are much more used to SQL, and it's much clearer to them to see the actual SQL rather than see the results of a query on a table they're not familiar with.
I get that there's a tradeoff there — to what extent are we "producing SQL" vs "producing query results"? When I speak to people about PRQL, they do say they value the legible SQL output. And if I didn't know about PRQL, I would care about that — it's too difficult to have trust or learn the language otherwise.
I think possibly we're on different sides of this discussion, I hope we can find a synthesis of our perspectives.
There was a problem hiding this comment.
If we could be emitting simpler SQL that still passes the tests, we should definitely simply it. This is not hard now.
I don't strongly feel we should switch to showing query results instead of SQL, just a suggestion.
But I do feel strongly that if we decide on different semantics than SQL, we should compile to something that does implement those semantics correctly, in all edge cases. Otherwise our argument that "PRQL can be used with any SQL database" is simply not true.
We could present this as "PRQL handles edge cases for you".
There was a problem hiding this comment.
I think we agree on the principles:
- PRQL should be correct (not approximately correct)
- We should have legible SQL
...and here they're in conflict.
I probably weigh Legibility more highly than you?
Is there a synthesis of these tradeoffs where we can do both?
- Default compile to DuckDB dialect?
- Default compile to DuckDB on the website & playground?
- Have a small list of supported dialects that work with the generic dialect, and others need to explicitly specify the dialect
WDYT?
| SELECT | ||
| *, | ||
| ((temp_c - 32) / 1.8 - 0) / 100 AS boiling_proportion | ||
| ((temp_c - 32 * 1.0 / 1.8) - 0 * 1.0 / 100 - 0) AS boiling_proportion |
There was a problem hiding this comment.
Re the comment above re generic dialect — I do think this is now harder to read
| @@ -1,3 +1,4 @@ | |||
| version: "2.4" | |||
There was a problem hiding this comment.
This line seems added by accident?
There was a problem hiding this comment.
Nope, it's intentional.
Docker-compose version 3.x was rejecting the file, saying mssql contains entries that are not allowed.
There was a problem hiding this comment.
That is weird. Do you know what version of docker compose you are using?
- Currently the latest docker compose is 2.18.1 (See https://github.com/docker/compose)
- docker compose 2.X is based on the compose spec, and
versionis deprecated in the compose spec. https://github.com/compose-spec/compose-spec/blob/master/spec.md#version-top-level-element
There was a problem hiding this comment.
$ docker-compose --version
docker-compose version 1.25.0, build unknown
There was a problem hiding this comment.
$ docker-compose --version
docker-compose version 1.25.0, build unknown
Oh, your docker-compose version is outdated. compose version 1 is no longer supported, and compose version 2 is widely used these days.
compose v1 is written in Python, and compose v2 is fully rewritten in Go as Docker plugin.
Please uninstall compose v1 and install v2.
https://docs.docker.com/compose/install/
| # TODO: uncomment when updated on latest DuckDB version | ||
| # let div_i = l r -> s"({l} // {r})" |
There was a problem hiding this comment.
DuckDB's // does not work for floats, so I think we can't use this.
duckdb/duckdb#7558
Closes #2426
Also makes sure that float division is always producing floats.
This is done by casting to float before division. This can be removed
when match is implemented and we can check types and use only the
appropriate operation.