Skip to content

[DRAFT] feat: support functions #30

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

Merged
merged 13 commits into from
Aug 23, 2022
Merged

Conversation

jaulz
Copy link
Contributor

@jaulz jaulz commented Jul 21, 2022

This PR adds support to create functions. We can of course change the signature but this is just a first proposal because I think it would be a great addition.

@tpetry
Copy link
Owner

tpetry commented Jul 21, 2022

I like the idea how you added support for all the optional modifiers but don't like the mixing of int-indexed and assoc-indexed array when combining the language and more options:

[
   'language' => 'sql',
   'parallel safe'
]

How about making it more phpstan friendly by using only assoc indexed modifiers? Is there one which would not work?

Like:

[
  'language' => 'sql',
  'parallel' => 'safe',
  'leakproof' => false,
  'mutability' => 'stable',
]

@jaulz
Copy link
Contributor Author

jaulz commented Jul 21, 2022

Yep, that's what I originally thought as well but I wasn't sure if we can really support all of the modifiers eventually:
image

However, I just pushed another change where all the different possibilities are handled. Let me know what you think 😊

@tpetry
Copy link
Owner

tpetry commented Jul 21, 2022

Give me some time with both pull requests. They are on my agenda for a long time and I didn't find a good way yet to express all the PG functionality and make it good to use. I will have to play with them and try to remember the problem I had foreseen for my implementation.

@jaulz
Copy link
Contributor Author

jaulz commented Jul 21, 2022

Sure and thanks for considering both of them. I already use this one using my own extension so I thought it might be a good fit for this library as well.

@tpetry tpetry changed the title feat: support functions [DRAFT] feat: support functions Jul 25, 2022
@tpetry
Copy link
Owner

tpetry commented Jul 27, 2022

https://jkatz05.com/post/postgres/postgres-begin-atomic/

Using the atomic by default for PG 14 sounds like a good idea. What do you think @jaulz?

@jaulz
Copy link
Contributor Author

jaulz commented Jul 27, 2022

Oh, that looks super cool, @tpetry. How would you implement the version check? Calling SHOW server_version; and parse it?

@tpetry
Copy link
Owner

tpetry commented Jul 27, 2022

Yes, i did the same:

$version = $this->getConnection()->selectOne('SHOW server_version')->server_version;
$options = match (true) {
$analyze && version_compare($version, '13') >= 0 => 'ANALYZE TRUE, BUFFERS TRUE, SETTINGS TRUE, VERBOSE TRUE, WAL TRUE',
$analyze && version_compare($version, '12') >= 0 => 'ANALYZE TRUE, BUFFERS TRUE, SETTINGS TRUE, VERBOSE TRUE',
$analyze => 'ANALYZE TRUE, BUFFERS TRUE, VERBOSE TRUE',
version_compare($version, '12') >= 0 => 'SETTINGS TRUE, SUMMARY TRUE, VERBOSE TRUE',
default => 'SUMMARY TRUE, VERBOSE TRUE',

@jaulz
Copy link
Contributor Author

jaulz commented Jul 28, 2022

I just noticed that the atomic keyword is part of the body (i.e. after BEGIN). However, I just pushed a change that "smartly" injects the atomic keyword. Let me know what you think.

@tpetry
Copy link
Owner

tpetry commented Aug 5, 2022

I will take some time on the weekend to play with the PR, I didn't forget it ;) I just have very much to do currently.

@jaulz
Copy link
Contributor Author

jaulz commented Aug 5, 2022

No worries and thanks for spending your time at all on it! By the way, I think once we have this we could also think about creating utilities for triggers and maybe even provide Laravel specific triggers (e.g. create trigger to cascade deletions of morphable relations).

@tpetry tpetry force-pushed the feat/compile-functions branch from e7784f6 to 4ae8d3a Compare August 7, 2022 10:24
@tpetry
Copy link
Owner

tpetry commented Aug 7, 2022

I've improved your PR to fit more with the rest of the library and changed the implementation to correctly use the atomic mode. I decided to use it by default for PG14+ as it is a good safety feature.

But I don't like the current option for the null handling. calledonnull: bool doesn't feel correct, for every other term I was able to find their correct naming in the documentation (e.g. they use volatility instead of mutability).

It's missing manual testing for all options currently and I need to create about 50 more unit tests for the final implemenation.

What's your opinion?

@jaulz
Copy link
Contributor Author

jaulz commented Aug 7, 2022

That looks great! I am just wondering whether calledonnull (I would actually prefer calledOnNull) can be described as boolean because that part is actually defined as { CALLED ON NULL INPUT | RETURNS NULL ON NULL INPUT | STRICT } (i.e. STRICT is a third option?).

Would you create tests for all the possible combinations? If yes, I think it could be handy to use phpunit's @dataProvider and just create one test using a data provider which provides the input and the expected output.

@tpetry
Copy link
Owner

tpetry commented Aug 7, 2022

The strict mode is just an alias for the RETURNS NULL ON NULL INPUT mode, so in the end it's only two modes. But I still don't like the calledOnNull name, will have to think about it.

I won't create a test for every combination, that is just too much. Just for every single one, but thats still a lot of tests.

@jaulz
Copy link
Contributor Author

jaulz commented Aug 17, 2022

I was just about to ask if you need a hand to implement the tests but saw that are you already implemented a couple of them. Are any tests missing from your side where I can help directly?

@tpetry
Copy link
Owner

tpetry commented Aug 17, 2022

I am almost finished, will need to review it again and write the documentation. Will finish everything this week ;)

@tpetry tpetry force-pushed the feat/compile-functions branch from 834d6af to 207190a Compare August 23, 2022 19:09
@tpetry tpetry force-pushed the feat/compile-functions branch from 207190a to fbf7cd0 Compare August 23, 2022 19:12
@tpetry tpetry merged commit e8a0184 into tpetry:master Aug 23, 2022
@tpetry
Copy link
Owner

tpetry commented Aug 23, 2022

It's been released with 0.20.0. Thanks for your pull request.

It was a lot of work getting this fully done but without your draft, I wouldn't have had the time. Sorry for being so slow.

@jaulz
Copy link
Contributor Author

jaulz commented Aug 24, 2022

Very cool, thanks a lot for your hard work and no worries! 😊

@jaulz jaulz deleted the feat/compile-functions branch August 24, 2022 13:57
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.

2 participants