Skip to content
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

Allow the construction of a CTE from a string (#42561) #42563

Conversation

dark-panda
Copy link
Contributor

@dark-panda dark-panda commented Jun 22, 2021

Summary

Constructing CTE WITH statements from strings broke some time between Rails 6.0 and 6.1.

See #42561 for details.

It might be a good idea to merge this in as a bug fix to 6-1-stable, since this behaviour was working in 6.0.

@@ -926,11 +926,17 @@ def collect_ctes(children, collector)
when Arel::Nodes::TableAlias
name = child.name
relation = child.relation
when String
Copy link
Member

Choose a reason for hiding this comment

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

I think better than changing this to deal with strings we should build a SQLLiteral node when calling with in the SelectManager like we do with from, group and order.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Perhaps that would be better. I can take a look at that tomorrow how about.

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@rafaelfranca @vlado I took a look at using an SqlLiteral node and the problem we have at that point is that by the time the code reaches where I've patched, we're expecting a node that has a name and relation of some sort, so that we can effectively build #{name} AS #{query}. In the case of an using an SqlLiteral in the SelectManager to wrap a string, you're already presumably in the form of #{name} AS #{query}, so unless you go and parse the contents of the string to separate out the name and the query you're kind of right back to where you started. I don't think you can avoid modifying #collect_ctes without handling at least part of this case.

Another option might be to have a different node type, kind of like the case for joins, where you can also pass things like strings or symbols or whatever, where you pass a StringJoin node in the case of strings or SqlLiterals. You could potentially have StringWith in that case, although it might be necessary to have one for StringWith and StringWith Recursive.

Copy link
Member

Choose a reason for hiding this comment

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

My suggestion is to use SQLLiteral but keep the change in this method, but instead of checking for strings we would check for a SQLLiteral. The reason why I think this change would be better is to avoid the build an AST where the nodes are not always a subclass of Node.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@rafaelfranca ah okay, that's easy enough. I've pushed up another commit with something along these lines then, let's see if that's closer to what we're looking for.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@rafaelfranca any further changes requested here? The aforementioned SQLLiteral changes were made and pushed.

@dark-panda dark-panda force-pushed the fix-42561-allow-construction-of-ctes-from-strings branch from bfa3416 to 65739cf Compare June 27, 2021 21:39
@rails-bot
Copy link

rails-bot bot commented Nov 1, 2021

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
Thank you for your contributions.

@rails-bot rails-bot bot added the stale label Nov 1, 2021
@rails-bot rails-bot bot closed this Nov 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants