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
Nested CTE is not rendered in top level query #50
Comments
Hi thanks for your feedback, actually all CTEs should be attached to the main query and not to any other CTE or subquery. so you should rewrite your programs as the following:
let me know if this work. |
Yeah that method does work. With the way I've structured my solution, each query only has access to things they are directly related to. CTE2 has a reference to CTE1. Main only has a reference to CTE2. Would you take a pull request for having the CTEs pulled from sub queries up to the main query during compilation? |
I think pulling the CTEs to the top level automatically may lead to confusion in certain cases, for example when a CTE is referenced by more than one query, this may lead to adding the same CTE more than once in the main query. one example would be:
Maybe leaving this to the developer is more appropriate, anyway we can keep the discussion open and get more feedback. for the moment we can enhance the usage examples in the docs, if you find so please open an issue there. |
I understand where you're coming from. However, the current behaviour has already led to developer confusion because I added a CTE in the query where the CTE is actually referenced and this wasn't rendered in the subsequent SQL. Maybe this needs to throw an exception when SqlKata detects a nested CTE? In terms of the example above, that's not quite what I had in mind and I agree that example would lead to developer confusion. Maybe it was just an omission but cte1 was not added to cte2 & cte3 using the With syntax. What this might look like:
To avoid the duplication aspects, Cte1 would be added once due to an Object reference equality check. As you say, maybe it is more appropriate to leave it to the developer, the current behaviour allows SqlKata to produce unexpected and broken SQL. Anyway, just a suggestion, thanks for taking the time to reply. |
@SaltyDH, I will reconsider this, I am convinced with your approach more honestly, do you have a PR for this feature ? |
@BoostIO funded this issue with $20. Visit this issue on Issuehunt |
@edokan has started working. Visit this issue on Issuehunt |
@edokan has submitted output. Visit this issue on Issuehunt |
Fixes #50 Nested CTE is not rendered in top level query
@ahmad-moussawi has rewarded. Visit this issue on Issuehunt |
Amazing work on the library, been really enjoying using it for my use case.
My scenario has two CTEs. Due to program flow, I am attaching the first CTE to the second CTE query, expecting that both CTEs would be rendered in the top level query.
Given the following code:
Current behaviour:
Rendered SQL:
As you can see above, whilst CTE1 is not rendered in the SQL, the bindings do filter up which has the side effect of throwing off the correct parameter values for the query (the query doesn't execute so its a mute point) If I use AddComponent to reattach CTE1 manually to the mainQuery, I end up with 3 bindings as the CTE1 parameter binding is promoted a second time.
Expected behaviour:
The text was updated successfully, but these errors were encountered: