Skip to content

feat: int division operator - #2684

Merged
aljazerzen merged 4 commits into
mainfrom
aljaz
Jun 1, 2023
Merged

aljazerzen merged 4 commits into
mainfrom
aljaz

Conversation

@aljazerzen

Copy link
Copy Markdown
Member

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.

aljazerzen and others added 2 commits June 1, 2023 15:43
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.
Comment thread prql-compiler/src/sql/std.sql.prql Outdated
Comment on lines +41 to +42
let div_i = l r -> s"ROUND({l} / {r} - 0.5)"
let div_f = l r -> s"(CAST({l} AS FLOAT) / {r})"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure that these are always correct.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On MySQL, x * 1.0 somehow produces different results than CAST(x AS FLOAT). I think it's converting for decimal instead of float.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is correct — MySQL seems to have Decimal division rather than Float division...

@aljazerzen
aljazerzen merged commit 3ccec92 into main Jun 1, 2023
@aljazerzen
aljazerzen deleted the aljaz branch June 1, 2023 15:57
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})"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually this comes up in #2692

(started + unfinished * 1.0 / started)
# and
* 1.0 / 1 - (started ...

@aljazerzen aljazerzen Jun 2, 2023 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need more parenthesis!

Maybe move the "binding strength" calculation and parenthesis generation into here. Annotate the strength using some new annotation syntax?.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added one option in #2694...

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})"

@max-sixty max-sixty Jun 1, 2023 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So good that we have the dialect modules now — adding something goes from "difficult rust" to "easy PRQL"!

Comment thread prql-compiler/src/tests/test.rs Resolved
{ 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 },

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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})"

@max-sixty max-sixty Jun 1, 2023 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@max-sixty max-sixty Jun 2, 2023 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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".

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re the comment above re generic dialect — I do think this is now harder to read

@@ -1,3 +1,4 @@
version: "2.4"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line seems added by accident?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, it's intentional.

Docker-compose version 3.x was rejecting the file, saying mssql contains entries that are not allowed.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is weird. Do you know what version of docker compose you are using?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$ docker-compose --version
docker-compose version 1.25.0, build unknown

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$ 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/

Comment on lines +85 to +86
# TODO: uncomment when updated on latest DuckDB version
# let div_i = l r -> s"({l} // {r})"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DuckDB's // does not work for floats, so I think we can't use this.
duckdb/duckdb#7558

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Introduce integer division operator //

3 participants