Skip to content

Handle column renaming in makeQueryTemplate and __table #345

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

Merged
merged 4 commits into from
Feb 1, 2023

Conversation

annie
Copy link
Contributor

@annie annie commented Jan 25, 2023

Implement column renaming in stdlib. Specifically, we now set column name overrides based on the names property in node.data.operations, which is added in this PR.

For SQL data sources, we do this by modifying the SELECT clause. For in-memory data sources, we create a copy of the source data and modify it to use the new names.

@annie
Copy link
Contributor Author

annie commented Jan 25, 2023

hm looks like CI is failing because String.replaceAll (which i added in the unit test) isn't available in Node 14. i'll update it to just use .replace.

Copy link
Member

@mbostock mbostock left a comment

Choose a reason for hiding this comment

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

What do you think about making this part of the existing operations.select? E.g.,

operations.select = {
  columns: [
    {column: "foo", as: "foo_alias"},
    {column: "bar", as: "bar_alias"}
  ]
};

and then we’d treat the existing operations as if column and as were the same? I.e., this

operations.select = {
  columns: [
    "foo",
    "bar"
  ]
};

would be the same as

operations.select = {
  columns: [
    {column: "foo", as: "foo"},
    {column: "bar", as: "bar"}
  ]
};

@annie
Copy link
Contributor Author

annie commented Jan 25, 2023

oh interesting! in my first pass i had added names to select as a new property, but that ended up feeling unwieldy – your suggestion seems cleaner though. i'll take a shot at updating it in the monorepo PR and see if i run into any issues.

@annie
Copy link
Contributor Author

annie commented Jan 30, 2023

@mbostock i ran into one major issue while trying to implement your suggestion, which is that it doesn't allow us to preserve the renamed name when a user hides the column. we could potentially get around this by adding a hidden flag to each columns object and toggling that when we hide a column, instead of removing the object from the array, but that would require a fairly significant set of changes and i'm not sure it's worth doing.

given that, i think i'd prefer to stick with this operations.names approach, unless you have strong objections and/or other ideas!

@mbostock
Copy link
Member

@annie does that mean we’re still selecting and returning columns that are hidden when the query is run?

@annie
Copy link
Contributor Author

annie commented Jan 30, 2023

no we don't actually select hidden columns, we just keep the name information for that column in operations.names. that allows us to still show the renamed name in the columns dropdown (where hidden columns are listed in a deselected state), and the column will preserve its renamed name if it's reselected.

@mbostock
Copy link
Member

@annie Got it. Sorry, still waking up! ☕ 👍

@mbostock
Copy link
Member

No further suggestions from me. I’ll leave the reviewing to @libbey-observable and @mkfreeman! Thanks y’all. 🙏

Copy link
Contributor

@libbey-observable libbey-observable left a comment

Choose a reason for hiding this comment

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

This all makes sense and looks good to me!

@@ -659,6 +663,27 @@ export function __table(source, operations) {
Object.fromEntries(operations.select.columns.map((c) => [c, d[c]]))
);
}
if (!primitive && operations.names) {
Copy link
Contributor

@libbey-observable libbey-observable Jan 31, 2023

Choose a reason for hiding this comment

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

Ah, nice, glad it was an easy fix!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the real fix for the original issue you found is actually in this change in Next! but while i was investigating i realized we should also add a check here.

Copy link
Contributor

Choose a reason for hiding this comment

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

aaah! thanks for explaining that.

@annie annie merged commit 085155c into main Feb 1, 2023
@annie annie deleted the annie/column-renaming branch February 1, 2023 20:12
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.

3 participants