-
Notifications
You must be signed in to change notification settings - Fork 222
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add main mathematical functions #3909
Conversation
let asin = column -> s"ASIN({column:0})" | ||
let tan = column -> s"TAN({column:0})" | ||
let atan = column -> s"ATAN({column:0})" | ||
let pow = column exponent -> s"POW({column:0}, {exponent:0})" # TODO: create an operator for that (** or ^ ?) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you're ok with this PR, I reckon we should have some kind of operator for pow
Which one would you prefer?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes agree. No particularly strong view on ^
vs **
, WDYT? (our parser can cope with the initial ambiguity of **
fine)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I personally would go with **
for 3 reasons
- it's in python which is super used in data world (also in JS)
^
makes me think of bitwise XOR in Rust and PythonIt feels more comfortable to type on a QWERTY ^^ I prefer to type twice a key than use shiftnvm I'm stupid
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Maybe **
uses up fewer unique characters, in case we want to use ^
for something else)
Edit: I just saw your reply, OK — let's go with **
...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Super — this is v helpful, we should have done this a while ago.
They all look fine to me. I guess folks aren't using all of these (atan
!) that much, but no harm in having them...
In another PR, we could add some of these to our integration tests. I think they're simple enough that I'm fairly confident they work, but nice to have coverage of them across dialects. A couple of times we've found inconsistencies between dialects this way. If you also want to add them to the unit tests and/or docs, that would be good. Again, am quite confident they work, so not a top priority. |
7a2349c
to
6d2ea0f
Compare
d1899a7
to
fa25dad
Compare
@max-sixty I should be good to review :) |
Great re adding We need to set the associativity here: prql/prqlc/prql-compiler/src/sql/gen_expr.rs Lines 890 to 896 in de975cf
It should be right associative (the only one). And we should add a test that the precedence works; If you prefer to push that to another PR, that's totally fine; we should just comment it out from the parser so it doesn't work yet. |
You're completely right. I'll work on it tonight though I'll have to do some extra work because it's not a |
@max-sixty So after some dive in I'm a bit perplex on how I should make the right associativity work.
to be translated into
instead of
Since there is no "power" operator in Postgres, we can't delegate that to the DB and there is nothing on sqlparser that handles that directly. I tried to use |
OK, good point... this is actually a bit harder than I thought — it's not only adjusting the SQL writer, it's also adjusting the parser here: prql/prqlc/prqlc-parser/src/expr.rs Line 250 in 054d29e
(Previously everything had been left-associative, so all the work I'd done here had been on the SQL writer) I can come back to this and look more. In the meantime, can we comment out the change in the parser so |
grammars/prql-lezer/src/prql.grammar
Outdated
@@ -165,7 +165,7 @@ LambdaParam { identPart TypeDefinition? (":" expression)? } | |||
Comment { "#" ![\n]* } | |||
@precedence { Docblock, Comment } | |||
|
|||
//op_bin_only { "*" | "/" | "//" | "%" | "!=" | ">=" | "<=" | "~=" | ">" | "<" | "??" | "&&" | "||" } | |||
//op_bin_only { "*" | "/" | "//" | "%" | "**" | "!=" | ">=" | "<=" | "~=" | ">" | "<" | "??" | "&&" | "||" } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@vanillajonathan should we remove this line?
Co-authored-by: Maximilian Roos <5635139+max-sixty@users.noreply.github.com>
for more information, see https://pre-commit.ci
Ok I removed the
|
I think this is correct — the We do something similar for |
It would be pretty convenient if all the math functions could be grouped together under a math namespace. Like |
Yup @vanillajonathan I thought about it and agree with you I'd like that. We could have the same for string utils. |
Yes, I agree... |
With all the analytics DB, working on math and dates operations is key.
I will try to work on date / time / durations... types but still need some time for onboarding.
In the meantime if you're ok with this, I would like to first add math functions that are available for almost every dialects. In another PR I'll have a deeper look at string functions and I reckon it should cover pretty much #217 was opened for.
I'll work in a next PR on adding documentation for #1761 (similar to #1775)