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

Preserve columns names when SELECT * and JOIN #268

Merged
merged 19 commits into from
Mar 19, 2024
Merged

Preserve columns names when SELECT * and JOIN #268

merged 19 commits into from
Mar 19, 2024

Conversation

andicuko
Copy link
Contributor

@andicuko andicuko commented Feb 20, 2024

It fixes this issue.

The fix:

When constructing the relation from the ast query, we pass a parameter preserve_input_names: bool to try_from_tables_with_joins and then to try_from_table_with_joins depending weather the there is a Wildcard in the SelectItem or not.

In try_from_table_with_joins after the Join (that is created as usual with automatically generated names for fields) a Map is also created where if preserve_input_names is True the original fields names of the Join inputs inserted back but only if they don't create naming collision.

Notice that if SELECT * FROM tableA JOIN tableB USING(id) the id column name can be found as a field name along with all the other non ambiguous original column names in the resulting relation whereas SELECT * FROM tableA JOIN tableB ON(tableA.id=tableB.id) the id field name is no longer present anymore since it is ambiguous, however. the field will still be present but with a generated field name.

.field_inputs()
.map(|(f, i)| (i, f.into()))
.collect();
let composed_columns = all_columns.and_then(join_columns.clone());
Copy link
Contributor

Choose a reason for hiding this comment

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

Where are you using this ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure to understand, here the only difference from before is that the name of join columns which I change it from new_columns to join_columns

.collect();
composed_columns.and_then(original_not_ambiguous_columns)
} else {
composed_columns
Copy link
Contributor

Choose a reason for hiding this comment

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

You're relying on the previously defined composed column here ? It's a bit hard to read

@@ -251,6 +251,7 @@ impl<'a, T: QueryToRelationTranslator + Copy + Clone> VisitedQueryRelations<'a,
fn try_from_table_with_joins(
Copy link
Contributor

Choose a reason for hiding this comment

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

This function is becoming too complex, can you try to split it in independent sub-functions with explicit names ?

.chain(self.field_inputs().filter_map(|(name, id)| {
}).collect::<Vec<_>>();

let fields_not_in_vec = self
Copy link
Contributor

Choose a reason for hiding this comment

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

This is really obscure, can you separate well between name preservation and collision management?

src/hierarchy.rs Outdated
/// It creates a new hierarchy with elements for which the tail of their
/// path is not ambiguous. In the new hierarchy, only the tails of the original
/// path are used as a path.
pub fn non_ambiguous_tails(&self) -> Hierarchy<T> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove this from this file

@ngrislain ngrislain merged commit aba5b5e into main Mar 19, 2024
1 check passed
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.

Select * on a JOIN should not rename fields
2 participants