-
Notifications
You must be signed in to change notification settings - Fork 1k
Introduce datatable.unique.names policy for duplicate handling in setnames() #4044 #7647
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,6 +35,32 @@ | |
| table_name, brackify(duplicate_names), domain=NA) | ||
| } | ||
|
|
||
| process_name_policy = function(names_vec) { | ||
| policy = getOption("datatable.unique.names", "off") | ||
|
|
||
| if (is.null(policy) || policy == "off") return(names_vec) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. on a second thought |
||
|
|
||
| allowed = c("warn", "error", "rename") | ||
| if (!policy %in% allowed) { | ||
| warningf("Invalid value for 'datatable.unique.names': [%s]. Falling back to 'off'. Allowed values are: 'off', 'warn', 'error', 'rename'.", as.character(policy)) | ||
| return(names_vec) | ||
| } | ||
|
|
||
| if (anyDuplicated(names_vec)) { | ||
| dups = unique(names_vec[duplicated(names_vec)]) | ||
| msg = sprintf("Duplicate column names created: %s. This may cause ambiguity.", brackify(dups)) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this problematic for a table like |
||
|
|
||
| if (policy == "warn") { | ||
| warningf(msg) | ||
| } else if (policy == "error") { | ||
| stopf(msg) | ||
| } else if (policy == "rename") { | ||
| return(make.unique(names_vec)) | ||
| } | ||
| } | ||
| return(names_vec) | ||
| } | ||
|
|
||
| duplicated_values = function(x) { | ||
| # fast anyDuplicated for the typical/non-error case; second duplicated() pass for (usually) error case | ||
| if (!anyDuplicated(x)) return(vector(typeof(x))) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21520,3 +21520,14 @@ test(2365.1, melt(df_melt, id.vars=1:2), melt(dt_melt, id.vars=1:2)) | |
| df_dcast = data.frame(a = c("x", "y"), b = 1:2, v = 3:4) | ||
| dt_dcast = data.table(a = c("x", "y"), b = 1:2, v = 3:4) | ||
| test(2365.2, dcast(df_dcast, a ~ b, value.var = "v"), dcast(dt_dcast, a ~ b, value.var = "v")) | ||
|
|
||
| #4044 | ||
| DT = as.data.table(iris) | ||
| options(datatable.unique.names = "off") | ||
| test(2366.1, names(setnames(copy(DT), "Petal.Length", "Sepal.Length")), c("Sepal.Length", "Sepal.Width", "Sepal.Length", "Petal.Width", "Species")) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we now the nice options parameter in |
||
| options(datatable.unique.names = "warn") | ||
| test(2366.2, names(setnames(copy(DT), "Petal.Length", "Sepal.Length")), c("Sepal.Length", "Sepal.Width", "Sepal.Length", "Petal.Width", "Species"), warning = "Duplicate column names created") | ||
| options(datatable.unique.names = "error") | ||
| test(2366.3, setnames(copy(DT), "Petal.Length", "Sepal.Length"), error = "Duplicate column names created") | ||
| options(datatable.unique.names = "rename") | ||
| test(2366.4, names(setnames(copy(DT), "Petal.Length", "Sepal.Length")), c("Sepal.Length", "Sepal.Width", "Sepal.Length.1", "Petal.Width", "Species")) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missing newline |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -105,6 +105,12 @@ | |
| \item{\code{datatable.enlist}}{Experimental feature. Default is \code{NULL}. If set to a function | ||
| (e.g., \code{list}), the \code{j} expression can return a \code{list}, which will then | ||
| be "enlisted" into columns in the result.} | ||
| \item{\code{datatable.unique.names}}{A character string, default \code{"off"}. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it should somehow state that this currently only holds for setnames and not other functions that could create duplicate names like |
||
| Controls the behavior when operations (like \code{setnames}) would result in | ||
| duplicate column names. Can be \code{"off"} (silently allow duplicates), | ||
| \code{"warn"} (issue a warning), \code{"error"} (halt with an error), | ||
| or \code{"rename"} (automatically fix duplicates using \code{make.unique}). | ||
| Invalid values will trigger a warning and fall back to \code{"off"}.} | ||
| } | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please dont delete comments