-
Notifications
You must be signed in to change notification settings - Fork 15
Implement Rename transform #15
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
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
ac9f1ac
wip: implement Rename transform
Omar-Elrefaei 8b77cba
wip: implement Rename transform
Omar-Elrefaei e2a8921
Merge remote-tracking branch 'origin/master'
Omar-Elrefaei 1013e40
fix
Omar-Elrefaei 8d3207e
Actually address brought up issues
Omar-Elrefaei 078e649
refactor and add a pairs of strings constructor
Omar-Elrefaei cb6e7fb
add tests
Omar-Elrefaei 650ddc2
confirm to Tables.jl spec
Omar-Elrefaei 5f8e0dd
assert that all requested renames exist
Omar-Elrefaei 4bb7689
update
Omar-Elrefaei aec00fb
make it work with a single Pair, and add test
Omar-Elrefaei 6ca7888
Update src/transforms/rename.jl
juliohm a34fed3
Update src/transforms/rename.jl
juliohm 6787a01
Update src/transforms/rename.jl
juliohm b72f999
Update src/transforms/rename.jl
juliohm 8c330eb
Update src/transforms/rename.jl
juliohm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,7 @@ export | |
# built-in | ||
Select, | ||
Reject, | ||
Rename, | ||
Identity, | ||
Center, | ||
Scale, | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# ------------------------------------------------------------------ | ||
# Licensed under the MIT License. See LICENSE in the project root. | ||
# ------------------------------------------------------------------ | ||
|
||
""" | ||
Rename(:col₁ => :newcol₁, :col₂ => :newcol₂, ..., :col₁ => :newcolₙ) | ||
|
||
The transform that renames `col₁` to `newcol₁`, `col₂` to `newcol₂`, ... | ||
""" | ||
struct Rename <: Stateless | ||
names::Dict{Symbol,Symbol} | ||
end | ||
|
||
pairsyms(x::Pair) = Symbol(first(x)) => Symbol(last(x)) | ||
|
||
Rename(names::Pair) = pairsyms(names) |> Dict |> Rename | ||
Rename(names...) = pairsyms.(names) |> Dict |> Rename | ||
|
||
function apply(transform::Rename, table) | ||
_rename(transform.names, table) | ||
end | ||
|
||
function revert(transform::Rename, table, cache) | ||
# reversing the key-value pairs of the Dict | ||
newnames = Dict(new => old for (old, new) in transform.names) | ||
_rename(newnames, table) |> first | ||
end | ||
|
||
|
||
function _rename(names, table) | ||
oldnames = Tables.columnnames(table) | ||
|
||
# check if requested renames exist in the table | ||
@assert keys(names) ⊆ oldnames "invalid column names" | ||
|
||
# use new names if necessary | ||
newnames = map(oldnames) do oldname | ||
juliohm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
oldname in keys(names) ? names[oldname] : oldname | ||
end | ||
|
||
cols = Tables.columns(table) | ||
vals = [Tables.getcolumn(cols, name) for name in oldnames] | ||
𝒯 = (; zip(newnames, vals)...) |> Tables.materializer(table) | ||
|
||
𝒯, nothing | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.