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

Properly qualify relations when in Relation#union #334

Merged
merged 1 commit into from
May 15, 2019

Conversation

ianks
Copy link
Collaborator

@ianks ianks commented May 15, 2019

When Sequel performs a union of two datasets, it will alias the resulting union as t1 by default. As a result, select_append anything that required a qualified column will not work after performing a union.

To fix this, this PR ensures we qualify the schema when performing a union. If relation_a and relation_b are of the Relation::Name, then we use that name as the alias. If they are different, we default the alias to #{relation_a.name}__#{relation_b.name}. This is not technically required, but IMO makes the relation more easily inspectable/debuggable, and is more resistant to cases when user's manually qualify columns.

opts = { alias: alias_name.to_sym, **options }

new_schema = schema.qualified(opts[:alias])
new_schema.(new(dataset.__send__(__method__, relation.dataset, opts, &block)))
Copy link
Member

Choose a reason for hiding this comment

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

new(dataset.__send__(__method__, relation.dataset, opts, &block), schema: new_schema)

would be better here


it 'qualifies the table as the concatenated relation names' do
result = relation1.union(relation2)
expect_to_have_qualified_name(result, :users__tasks)
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure if we even need to test this but anyway

@flash-gordon flash-gordon merged commit 9e8b753 into rom-rb:master May 15, 2019
@flash-gordon
Copy link
Member

Good catch

dgollahon pushed a commit to dgollahon/rom-sql that referenced this pull request Jun 3, 2023
- When merging relations with virtual columns the qualified projection (added in rom-rb#334) causes the virtual column to be moved into the outer select. This means that column will always be hardcoded to `NULL` instead of selecting from the result of the union. This is a [not-that-esoteric technique with unions](https://stackoverflow.com/a/39766372). This also worked in ROM prior to rom-rb#334.

This PR is really an issue/bug report just in the form of a failing test. I don't have a good idea as to what the right solutin is but I figured it would be helpful to at least have a very concrete demonstration of the issue.
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