-
Notifications
You must be signed in to change notification settings - Fork 3
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
Add support for RECURSIVE withs #110
Conversation
8f9b30d
to
d1b0551
Compare
d1b0551
to
11631a0
Compare
@@ -127,10 +127,12 @@ module Database.PostgreSQL.PQTypes.SQL.Builder | |||
, sqlLimit | |||
, sqlDistinct | |||
, sqlWith | |||
, sqlWithRecursive |
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 need to read up on how WITH RECURSIVE works, but in the meantime I have an excellent code review delay tactic: could you add a test that uses sqlWithRecursive
?
(to be fair I do honestly think we should have a test)
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 must fix something first, but will gladly add tests.
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.
BTW the code looks sensible, but I'm not too familiar with the inner workings of this library.
Who else can we get to take a look in @arybczak's absence 🤔
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.
There you go, I've added a simple test showcasing the recursive with ! I don't really know who else is familiar enough with the library, your name was on the suggested reviewers, that's why I bothered you in the first place !
6b4a67d
to
2090101
Compare
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.
What happens if you do
sqlWithRecursive ...
sqlWith ...
...
?
Looks to me like the last sqlWith
will overwrite the Recursive
tag of the first sqlWithRecursive
, right?
The simplest solution here would be to mark all WITH
clauses recursive, right?
@@ -443,6 +452,16 @@ checkAndRememberMaterializationSupport = do | |||
fetchOne runIdentity | |||
liftIO $ writeIORef withMaterializedSupported (isRight res) | |||
|
|||
-- This function has to be called as one of first things in your program |
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.
Isn't this feature supported in pretty much all PostgreSQL versions? I checked and 9.6 looks to have it.
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.
They started in 8.4 according to Wikipedia, but I don't know what are the older versions supported by hpqtypes-extra. If we don't need to check for the feature, I'll be glad to remove these !
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.
We don't really care for anything older than 11 at this point ;)
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.
Removed then.
@arybczak : Indeed, it would get overridden. I didn't think about it, shame on me. As for marking ALL CTEs as recursive, apart from the obvious "adding code that is not always explicitly needed" issue (I guess we can live with that one), I just would like for @marco44 to confirm that there is no overhead / issue we didn't think about in putting "RECURSIVE" everywhere. |
If you're building a list of CTE expressions, and one in the list is recursive, you need to add the recursive keyword to the whole CTE block, so if you have a bunch of sqlWith, sqlWithRecursive... the sqlWithRecursive "wins", you need to add the keyword. You're telling PostgreSQL that any of the subqueries may be recursive, so if it seens some UNION/UNION ALL constructs, it has to inspect them differently. Making all CTEs recursive in the whole app (if I understood correctly the question) seems bad to me:
|
All right, lemme find a better way of handling this then. |
This version keeps |
|
||
class SqlWith a where | ||
sqlWith1 :: a -> SQL -> SQL -> Materialized -> a | ||
instance Semigroup Recursive where |
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.
You can put a comment here that explains why it works like this 🙂
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.
Added !
ccfe023
to
2f7cf86
Compare
I'm going to need some recursive powered CTE, but hpqtypes-extra doesn't offer support for these neat things.
This PR propose adding support for them. It's a bit tricky, because the syntax demands that "RECURSIVE" be right after the "WITH" and cannot target a singular CTE.