From 4b1249d921aa3abdf05a8cc804df9cac536759c9 Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Tue, 28 May 2019 01:33:58 +0800 Subject: [PATCH 01/37] initial work on data.table::coalesce --- R/coalesce.R | 34 ++++++++++++++++++++++ src/coalesce.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++++ src/data.table.h | 3 ++ src/init.c | 1 + 4 files changed, 114 insertions(+) create mode 100644 R/coalesce.R create mode 100644 src/coalesce.c diff --git a/R/coalesce.R b/R/coalesce.R new file mode 100644 index 0000000000..3668b3f0fa --- /dev/null +++ b/R/coalesce.R @@ -0,0 +1,34 @@ +coalesce <- function(x, ...) { + if (!anyNA(x) || missing(..1)) return(x) + + count_na = sum(is.na(x)) + + values = list(...) + + nn = length(x) + # can be lengths() in R 3.2.0 + lengths = vapply_1i(values, length) + if (any(idx <- lengths > 1L & lengths < nn)) { + first_idx = which(idx) + stop("Replacement ", first_idx, " has length ", lengths[first_idx], + " but length(x) is ", lx, ". Only singletons will be recycled") + } + + x_type = typeof(x) + x_factor = inherits(x, 'factor') + + types = vapply_1c(values, typeof) + if (!all(idx <- x_type == types)) { + first_idx = which(!idx) + stop("Replacement ", first_idx, " has internal type ", types[first_idx], + " but typeof(x) is '", x_type, "'. Please coerce before coalescing.") + } + if (x_type == 'integer' && !all(idx <- x_factor == vapply_1b(values, inherits, 'factor'))) { + first_idx = which(!idx) + stop("Replacement ", first_idx, ' is ', if (x_factor) 'not ' else '', + "a factor, but x is", if (x_factor) "." else "not.", + " Please ensure type consistency on your inputs.") + } + + .Call(Ccoalesce, x, values, nn, count_na) +} \ No newline at end of file diff --git a/src/coalesce.c b/src/coalesce.c new file mode 100644 index 0000000000..f9eab4f0d4 --- /dev/null +++ b/src/coalesce.c @@ -0,0 +1,76 @@ +#include "data.table.h" + +SEXP coalesce(SEXP x, SEXP values, SEXP n, SEXP count_na) { + int JJ = LENGTH(values), nprotect = 0, NN = asInteger(n), KK = asInteger(count_na); + + switch(TYPEOF(x)) { + case LGLSXP: { + int *base_ptr = LOGICAL(x); + + SEXP out = PROTECT(allocVector(LGLSXP, NN)); nprotect++; + int *out_ptr = LOGICAL(out); + + for (int i = 0; i < NN; i++) { + Rprintf("KK: %d\ti: %d\n", KK, i); + if (KK > 0 && base_ptr[i] == NA_LOGICAL) { + int j = 0; + while (j < JJ) { + Rprintf("LOOPING values"); + SEXP this_j = VECTOR_ELT(values, j); + int take_idx; + if (LENGTH(this_j) == 1) take_idx = 0; else take_idx = i; + int this_replacement = LOGICAL(this_j)[take_idx]; + if (this_replacement != NA_LOGICAL) { + out_ptr[i] = this_replacement; + KK--; + break; + } + j++; + } + if (j == JJ) out_ptr[i] = NA_LOGICAL; + } else out_ptr[i] = base_ptr[i]; + } + UNPROTECT(nprotect); + return(out); + } + case INTSXP: { + int *base_ptr = INTEGER(x); + + SEXP out = PROTECT(allocVector(INTSXP, NN)); nprotect++; + int *out_ptr = INTEGER(out); + + int i = 0; + + Rprintf("starting loop\n"); + while (KK > 0 && i < NN) { + Rprintf("Current NA count: %d\tCurrent row: %d\n", KK, i); + if (base_ptr[i] == NA_LOGICAL) { + Rprintf("Found NA!\n"); + int j = 0; + while (j < JJ) { + int this_replacement = LOGICAL(VECTOR_ELT(values, j))[i]; + if (this_replacement != NA_LOGICAL) { + Rprintf("Found replacement!\n"); + out_ptr[i] = this_replacement; + KK--; + break; + } + j++; + } + if (j == JJ) { + Rprintf("Giving up & remaining NA\n"); + out_ptr[i] = NA_LOGICAL; + } + } else { + Rprintf("Not missing\n"); + out_ptr[i] = base_ptr[i]; + } + i++; + } + UNPROTECT(nprotect); + return(out); + } + default: + error("Unrecognized type."); + } +} diff --git a/src/data.table.h b/src/data.table.h index 97a7dc559e..88b0a91d8c 100644 --- a/src/data.table.h +++ b/src/data.table.h @@ -199,3 +199,6 @@ SEXP nafillR(SEXP obj, SEXP type, SEXP fill, SEXP inplace, SEXP cols, SEXP verbo // between.c SEXP between(SEXP x, SEXP lower, SEXP upper, SEXP bounds); + +// coalesce.c +SEXP coalesce(SEXP x, SEXP values, SEXP n, SEXP count_na); diff --git a/src/init.c b/src/init.c index c6ed2337cd..abe4d77518 100644 --- a/src/init.c +++ b/src/init.c @@ -168,6 +168,7 @@ R_CallMethodDef callMethods[] = { {"CnafillR", (DL_FUNC) &nafillR, -1}, {"CcolnamesInt", (DL_FUNC) &colnamesInt, -1}, {"CinitLastUpdated", (DL_FUNC) &initLastUpdated, -1}, +{"Ccoalesce", (DL_FUNC) &coalesce, -1}, {NULL, NULL, 0} }; From a67a399f73873cdeb05a07b71411f4895d2ff43f Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Wed, 29 May 2019 00:41:44 +0800 Subject: [PATCH 02/37] basic working version done; to be benchmarked & iterated --- NAMESPACE | 1 + R/coalesce.R | 4 +- src/coalesce.c | 135 +++++++++++++++++++++++++++++++++++++---------- src/data.table.h | 2 +- 4 files changed, 110 insertions(+), 32 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 747ae77ab0..7fadac8c49 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -32,6 +32,7 @@ export(rleidv) export(rowid) export(rowidv) export(uniqueN) +export(coalesce) export(setDTthreads, getDTthreads) # set operators export(fintersect) diff --git a/R/coalesce.R b/R/coalesce.R index 3668b3f0fa..6ddccf0eec 100644 --- a/R/coalesce.R +++ b/R/coalesce.R @@ -1,8 +1,6 @@ coalesce <- function(x, ...) { if (!anyNA(x) || missing(..1)) return(x) - count_na = sum(is.na(x)) - values = list(...) nn = length(x) @@ -30,5 +28,5 @@ coalesce <- function(x, ...) { " Please ensure type consistency on your inputs.") } - .Call(Ccoalesce, x, values, nn, count_na) + .Call(Ccoalesce, x, values) } \ No newline at end of file diff --git a/src/coalesce.c b/src/coalesce.c index f9eab4f0d4..9bf60c004c 100644 --- a/src/coalesce.c +++ b/src/coalesce.c @@ -1,71 +1,150 @@ #include "data.table.h" -SEXP coalesce(SEXP x, SEXP values, SEXP n, SEXP count_na) { - int JJ = LENGTH(values), nprotect = 0, NN = asInteger(n), KK = asInteger(count_na); +SEXP coalesce(SEXP x, SEXP values) { + int JJ = LENGTH(values), nprotect = 0, NN = LENGTH(x); + // pass over x in parallel to count NA + int na_count = 0; switch(TYPEOF(x)) { case LGLSXP: { - int *base_ptr = LOGICAL(x); + int *restrict x_ptr = LOGICAL(x); + #pragma omp parallel for schedule(dynamic) reduction(+:na_count) + for (int i = 0; i < NN; i++) na_count += (int) (x_ptr[i] == NA_LOGICAL); + } break; + case INTSXP: { + int *restrict x_ptr = INTEGER(x); + #pragma omp parallel for schedule(dynamic) reduction(+:na_count) + for (int i = 0; i < NN; i++) na_count += (int) (x_ptr[i] == NA_INTEGER); + } break; + case REALSXP: { + double *restrict x_ptr = REAL(x); + #pragma omp parallel for schedule(dynamic) reduction(+:na_count) + for (int i = 0; i < NN; i++) na_count += (int) (ISNA(x_ptr[i])); + } break; + case STRSXP: { + for (int i = 0; i < NN; i++) na_count += (int) (STRING_ELT(x, i) == NA_STRING); + } break; + default: error("Incompatible type"); + } + + switch(TYPEOF(x)) { + case LGLSXP: { + int *x_ptr = LOGICAL(x); SEXP out = PROTECT(allocVector(LGLSXP, NN)); nprotect++; int *out_ptr = LOGICAL(out); for (int i = 0; i < NN; i++) { - Rprintf("KK: %d\ti: %d\n", KK, i); - if (KK > 0 && base_ptr[i] == NA_LOGICAL) { + if (na_count > 0 && x_ptr[i] == NA_LOGICAL) { int j = 0; while (j < JJ) { - Rprintf("LOOPING values"); SEXP this_j = VECTOR_ELT(values, j); int take_idx; - if (LENGTH(this_j) == 1) take_idx = 0; else take_idx = i; + if (LENGTH(this_j) == 1) { + take_idx = 0; + } else { + take_idx = i; + } int this_replacement = LOGICAL(this_j)[take_idx]; if (this_replacement != NA_LOGICAL) { out_ptr[i] = this_replacement; - KK--; + na_count--; break; } j++; } if (j == JJ) out_ptr[i] = NA_LOGICAL; - } else out_ptr[i] = base_ptr[i]; + } else out_ptr[i] = x_ptr[i]; } UNPROTECT(nprotect); return(out); } case INTSXP: { - int *base_ptr = INTEGER(x); + int *x_ptr = INTEGER(x); SEXP out = PROTECT(allocVector(INTSXP, NN)); nprotect++; int *out_ptr = INTEGER(out); - int i = 0; + for (int i = 0; i < NN; i++) { + if (na_count > 0 && x_ptr[i] == NA_INTEGER) { + int j = 0; + while (j < JJ) { + SEXP this_j = VECTOR_ELT(values, j); + int take_idx; + if (LENGTH(this_j) == 1) { + take_idx = 0; + } else { + take_idx = i; + } + int this_replacement = INTEGER(this_j)[take_idx]; + if (this_replacement != NA_INTEGER) { + out_ptr[i] = this_replacement; + na_count--; + break; + } + j++; + } + if (j == JJ) out_ptr[i] = NA_INTEGER; + } else out_ptr[i] = x_ptr[i]; + } + UNPROTECT(nprotect); + return(out); + } + case REALSXP: { + double *x_ptr = REAL(x); - Rprintf("starting loop\n"); - while (KK > 0 && i < NN) { - Rprintf("Current NA count: %d\tCurrent row: %d\n", KK, i); - if (base_ptr[i] == NA_LOGICAL) { - Rprintf("Found NA!\n"); + SEXP out = PROTECT(allocVector(REALSXP, NN)); nprotect++; + double *out_ptr = REAL(out); + + for (int i = 0; i < NN; i++) { + if (na_count > 0 && ISNA(x_ptr[i])) { int j = 0; while (j < JJ) { - int this_replacement = LOGICAL(VECTOR_ELT(values, j))[i]; - if (this_replacement != NA_LOGICAL) { - Rprintf("Found replacement!\n"); + SEXP this_j = VECTOR_ELT(values, j); + int take_idx; + if (LENGTH(this_j) == 1) { + take_idx = 0; + } else { + take_idx = i; + } + double this_replacement = REAL(this_j)[take_idx]; + if (!ISNA(this_replacement)) { out_ptr[i] = this_replacement; - KK--; + na_count--; break; } j++; } - if (j == JJ) { - Rprintf("Giving up & remaining NA\n"); - out_ptr[i] = NA_LOGICAL; + if (j == JJ) out_ptr[i] = NA_REAL; + } else out_ptr[i] = x_ptr[i]; + } + UNPROTECT(nprotect); + return(out); + } + case STRSXP: { + SEXP out = PROTECT(allocVector(STRSXP, NN)); nprotect++; + + for (int i = 0; i < NN; i++) { + if (na_count > 0 && STRING_ELT(x, i) == NA_STRING) { + int j = 0; + while (j < JJ) { + SEXP this_j = VECTOR_ELT(values, j); + int take_idx; + if (LENGTH(this_j) == 1) { + take_idx = 0; + } else { + take_idx = i; + } + SEXP this_replacement = STRING_ELT(this_j, take_idx); + if (this_replacement != NA_STRING) { + SET_STRING_ELT(out, i, this_replacement); + na_count--; + break; + } + j++; } - } else { - Rprintf("Not missing\n"); - out_ptr[i] = base_ptr[i]; - } - i++; + if (j == JJ) SET_STRING_ELT(out, i, NA_STRING); + } else SET_STRING_ELT(out, i, STRING_ELT(x, i)); } UNPROTECT(nprotect); return(out); diff --git a/src/data.table.h b/src/data.table.h index 88b0a91d8c..b0461760aa 100644 --- a/src/data.table.h +++ b/src/data.table.h @@ -201,4 +201,4 @@ SEXP nafillR(SEXP obj, SEXP type, SEXP fill, SEXP inplace, SEXP cols, SEXP verbo SEXP between(SEXP x, SEXP lower, SEXP upper, SEXP bounds); // coalesce.c -SEXP coalesce(SEXP x, SEXP values, SEXP n, SEXP count_na); +SEXP coalesce(SEXP x, SEXP values); From 7cd97581c9ff70917f139da53b4ccf8eda3b0307 Mon Sep 17 00:00:00 2001 From: jangorecki Date: Wed, 29 May 2019 11:30:07 +0530 Subject: [PATCH 03/37] resolving mmy feedback in PR --- src/coalesce.c | 82 +++++++++++++++----------------------------------- 1 file changed, 25 insertions(+), 57 deletions(-) diff --git a/src/coalesce.c b/src/coalesce.c index 9bf60c004c..4dce5ce316 100644 --- a/src/coalesce.c +++ b/src/coalesce.c @@ -1,51 +1,42 @@ #include "data.table.h" SEXP coalesce(SEXP x, SEXP values) { - int JJ = LENGTH(values), nprotect = 0, NN = LENGTH(x); - - // pass over x in parallel to count NA - int na_count = 0; - switch(TYPEOF(x)) { + int JJ = LENGTH(values), protecti = 0; + uint64_t NN = LENGTH(x), na_count = 0;; + + switch(TYPEOF(x)) { // pass over x in parallel to count NA case LGLSXP: { int *restrict x_ptr = LOGICAL(x); #pragma omp parallel for schedule(dynamic) reduction(+:na_count) - for (int i = 0; i < NN; i++) na_count += (int) (x_ptr[i] == NA_LOGICAL); + for (uint64_t i = 0; i < NN; i++) na_count += (int) (x_ptr[i] == NA_LOGICAL); } break; case INTSXP: { int *restrict x_ptr = INTEGER(x); #pragma omp parallel for schedule(dynamic) reduction(+:na_count) - for (int i = 0; i < NN; i++) na_count += (int) (x_ptr[i] == NA_INTEGER); + for (uint64_t i = 0; i < NN; i++) na_count += (int) (x_ptr[i] == NA_INTEGER); } break; case REALSXP: { double *restrict x_ptr = REAL(x); #pragma omp parallel for schedule(dynamic) reduction(+:na_count) - for (int i = 0; i < NN; i++) na_count += (int) (ISNA(x_ptr[i])); + for (uint64_t i = 0; i < NN; i++) na_count += (int) (ISNA(x_ptr[i])); } break; case STRSXP: { - for (int i = 0; i < NN; i++) na_count += (int) (STRING_ELT(x, i) == NA_STRING); + for (uint64_t i = 0; i < NN; i++) na_count += (int) (STRING_ELT(x, i) == NA_STRING); } break; default: error("Incompatible type"); } - + switch(TYPEOF(x)) { case LGLSXP: { int *x_ptr = LOGICAL(x); - - SEXP out = PROTECT(allocVector(LGLSXP, NN)); nprotect++; + SEXP out = PROTECT(allocVector(LGLSXP, NN)); protecti++; int *out_ptr = LOGICAL(out); - - for (int i = 0; i < NN; i++) { + for (uint64_t i = 0; i < NN; i++) { if (na_count > 0 && x_ptr[i] == NA_LOGICAL) { int j = 0; while (j < JJ) { SEXP this_j = VECTOR_ELT(values, j); - int take_idx; - if (LENGTH(this_j) == 1) { - take_idx = 0; - } else { - take_idx = i; - } - int this_replacement = LOGICAL(this_j)[take_idx]; + int this_replacement = LOGICAL(this_j)[LENGTH(this_j)==1 ? 0 : i]; if (this_replacement != NA_LOGICAL) { out_ptr[i] = this_replacement; na_count--; @@ -56,27 +47,19 @@ SEXP coalesce(SEXP x, SEXP values) { if (j == JJ) out_ptr[i] = NA_LOGICAL; } else out_ptr[i] = x_ptr[i]; } - UNPROTECT(nprotect); + UNPROTECT(protecti); return(out); } case INTSXP: { int *x_ptr = INTEGER(x); - - SEXP out = PROTECT(allocVector(INTSXP, NN)); nprotect++; + SEXP out = PROTECT(allocVector(INTSXP, NN)); protecti++; int *out_ptr = INTEGER(out); - - for (int i = 0; i < NN; i++) { + for (uint64_t i = 0; i < NN; i++) { if (na_count > 0 && x_ptr[i] == NA_INTEGER) { int j = 0; while (j < JJ) { SEXP this_j = VECTOR_ELT(values, j); - int take_idx; - if (LENGTH(this_j) == 1) { - take_idx = 0; - } else { - take_idx = i; - } - int this_replacement = INTEGER(this_j)[take_idx]; + int this_replacement = INTEGER(this_j)[LENGTH(this_j)==1 ? 0 : i]; if (this_replacement != NA_INTEGER) { out_ptr[i] = this_replacement; na_count--; @@ -87,27 +70,19 @@ SEXP coalesce(SEXP x, SEXP values) { if (j == JJ) out_ptr[i] = NA_INTEGER; } else out_ptr[i] = x_ptr[i]; } - UNPROTECT(nprotect); + UNPROTECT(protecti); return(out); } case REALSXP: { double *x_ptr = REAL(x); - - SEXP out = PROTECT(allocVector(REALSXP, NN)); nprotect++; + SEXP out = PROTECT(allocVector(REALSXP, NN)); protecti++; double *out_ptr = REAL(out); - - for (int i = 0; i < NN; i++) { + for (uint64_t i = 0; i < NN; i++) { if (na_count > 0 && ISNA(x_ptr[i])) { int j = 0; while (j < JJ) { SEXP this_j = VECTOR_ELT(values, j); - int take_idx; - if (LENGTH(this_j) == 1) { - take_idx = 0; - } else { - take_idx = i; - } - double this_replacement = REAL(this_j)[take_idx]; + double this_replacement = REAL(this_j)[LENGTH(this_j)==1 ? 0 : i]; if (!ISNA(this_replacement)) { out_ptr[i] = this_replacement; na_count--; @@ -118,24 +93,17 @@ SEXP coalesce(SEXP x, SEXP values) { if (j == JJ) out_ptr[i] = NA_REAL; } else out_ptr[i] = x_ptr[i]; } - UNPROTECT(nprotect); + UNPROTECT(protecti); return(out); } case STRSXP: { - SEXP out = PROTECT(allocVector(STRSXP, NN)); nprotect++; - - for (int i = 0; i < NN; i++) { + SEXP out = PROTECT(allocVector(STRSXP, NN)); protecti++; + for (uint64_t i = 0; i < NN; i++) { if (na_count > 0 && STRING_ELT(x, i) == NA_STRING) { int j = 0; while (j < JJ) { SEXP this_j = VECTOR_ELT(values, j); - int take_idx; - if (LENGTH(this_j) == 1) { - take_idx = 0; - } else { - take_idx = i; - } - SEXP this_replacement = STRING_ELT(this_j, take_idx); + SEXP this_replacement = STRING_ELT(this_j, LENGTH(this_j)==1 ? 0 : i); if (this_replacement != NA_STRING) { SET_STRING_ELT(out, i, this_replacement); na_count--; @@ -146,7 +114,7 @@ SEXP coalesce(SEXP x, SEXP values) { if (j == JJ) SET_STRING_ELT(out, i, NA_STRING); } else SET_STRING_ELT(out, i, STRING_ELT(x, i)); } - UNPROTECT(nprotect); + UNPROTECT(protecti); return(out); } default: From 9c02162ae69a652981b25c986fc3729dddd87a12 Mon Sep 17 00:00:00 2001 From: jangorecki Date: Wed, 29 May 2019 11:34:45 +0530 Subject: [PATCH 04/37] proper pragma omp def --- src/coalesce.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/coalesce.c b/src/coalesce.c index 4dce5ce316..a7512c0556 100644 --- a/src/coalesce.c +++ b/src/coalesce.c @@ -7,17 +7,17 @@ SEXP coalesce(SEXP x, SEXP values) { switch(TYPEOF(x)) { // pass over x in parallel to count NA case LGLSXP: { int *restrict x_ptr = LOGICAL(x); - #pragma omp parallel for schedule(dynamic) reduction(+:na_count) + #pragma omp parallel for reduction(+:na_count) num_threads(getDTthreads()) for (uint64_t i = 0; i < NN; i++) na_count += (int) (x_ptr[i] == NA_LOGICAL); } break; case INTSXP: { int *restrict x_ptr = INTEGER(x); - #pragma omp parallel for schedule(dynamic) reduction(+:na_count) + #pragma omp parallel for reduction(+:na_count) num_threads(getDTthreads()) for (uint64_t i = 0; i < NN; i++) na_count += (int) (x_ptr[i] == NA_INTEGER); } break; case REALSXP: { double *restrict x_ptr = REAL(x); - #pragma omp parallel for schedule(dynamic) reduction(+:na_count) + #pragma omp parallel for reduction(+:na_count) num_threads(getDTthreads()) for (uint64_t i = 0; i < NN; i++) na_count += (int) (ISNA(x_ptr[i])); } break; case STRSXP: { From 890cd1a0028398046153df0051115c043997f26e Mon Sep 17 00:00:00 2001 From: jangorecki Date: Wed, 29 May 2019 14:08:39 +0530 Subject: [PATCH 05/37] rewrite coalesce for less passes --- R/coalesce.R | 17 +++- src/coalesce.c | 231 ++++++++++++++++++++++++++++------------------- src/data.table.h | 3 +- src/init.c | 1 + 4 files changed, 156 insertions(+), 96 deletions(-) diff --git a/R/coalesce.R b/R/coalesce.R index 6ddccf0eec..b599e08e60 100644 --- a/R/coalesce.R +++ b/R/coalesce.R @@ -1,5 +1,5 @@ -coalesce <- function(x, ...) { - if (!anyNA(x) || missing(..1)) return(x) +coalesce = function(x, ..., inplace=FALSE) { + if (missing(..1)) return(x) values = list(...) @@ -28,5 +28,14 @@ coalesce <- function(x, ...) { " Please ensure type consistency on your inputs.") } - .Call(Ccoalesce, x, values) -} \ No newline at end of file + ans = .Call(Ccoalesce, x, values, inplace) + if (inplace) invisible(ans) else ans +} +setcoalesce = function(x, ...) { + if (missing(..1)) return(invisible(x)) + invisible(coalesce(x, ..., inplace=TRUE)) +} +#whichna.character = function(x) { +# stopifnot(is.character(x)) +# .Call(Cwhichna_charR, x) +#} diff --git a/src/coalesce.c b/src/coalesce.c index a7512c0556..456855e3b9 100644 --- a/src/coalesce.c +++ b/src/coalesce.c @@ -1,123 +1,172 @@ #include "data.table.h" -SEXP coalesce(SEXP x, SEXP values) { - int JJ = LENGTH(values), protecti = 0; - uint64_t NN = LENGTH(x), na_count = 0;; +// low overhead helpers +void which_int(int *x, int nx, int *out, int *nout, int val) { + int *buf = malloc(nx*sizeof(buf)); + if (!buf) error("%s: Unable to allocate memory", __func__); // # nocov + int j=0; + for (int i=0; i 0 && x_ptr[i] == NA_LOGICAL) { - int j = 0; - while (j < JJ) { - SEXP this_j = VECTOR_ELT(values, j); - int this_replacement = LOGICAL(this_j)[LENGTH(this_j)==1 ? 0 : i]; - if (this_replacement != NA_LOGICAL) { - out_ptr[i] = this_replacement; - na_count--; - break; - } - j++; + for (int i=0; i 0 && x_ptr[i] == NA_INTEGER) { - int j = 0; - while (j < JJ) { - SEXP this_j = VECTOR_ELT(values, j); - int this_replacement = INTEGER(this_j)[LENGTH(this_j)==1 ? 0 : i]; - if (this_replacement != NA_INTEGER) { - out_ptr[i] = this_replacement; - na_count--; - break; - } - j++; + for (int i=0; i 0 && ISNA(x_ptr[i])) { - int j = 0; - while (j < JJ) { - SEXP this_j = VECTOR_ELT(values, j); - double this_replacement = REAL(this_j)[LENGTH(this_j)==1 ? 0 : i]; - if (!ISNA(this_replacement)) { - out_ptr[i] = this_replacement; - na_count--; - break; - } - j++; + for (int i=0; i 0 && STRING_ELT(x, i) == NA_STRING) { - int j = 0; - while (j < JJ) { - SEXP this_j = VECTOR_ELT(values, j); - SEXP this_replacement = STRING_ELT(this_j, LENGTH(this_j)==1 ? 0 : i); - if (this_replacement != NA_STRING) { - SET_STRING_ELT(out, i, this_replacement); - na_count--; - break; - } - j++; + for (int i=0; i Date: Wed, 29 May 2019 17:22:34 +0530 Subject: [PATCH 06/37] int64 support, precaalc value lens and re-use --- src/coalesce.c | 60 +++++++++++++++++++++++++++++++------------------- 1 file changed, 37 insertions(+), 23 deletions(-) diff --git a/src/coalesce.c b/src/coalesce.c index 456855e3b9..1066b25e39 100644 --- a/src/coalesce.c +++ b/src/coalesce.c @@ -1,6 +1,5 @@ #include "data.table.h" -// low overhead helpers void which_int(int *x, int nx, int *out, int *nout, int val) { int *buf = malloc(nx*sizeof(buf)); if (!buf) error("%s: Unable to allocate memory", __func__); // # nocov @@ -89,7 +88,7 @@ SEXP coalesce(SEXP x, SEXP values, SEXP inplace) { which_int(INTEGER(x), nx, iwhich, &nwhich, NA_INTEGER); } break; case REALSXP: { - which_double(REAL(x), nx, iwhich, &nwhich, NA_REAL); + which_double(REAL(x), nx, iwhich, &nwhich, inherits(x,"integer64") ? NA_INT64_D : NA_REAL); } break; case STRSXP: { SEXP val = PROTECT(allocNAVector(STRSXP, 1)); protecti++; @@ -100,6 +99,11 @@ SEXP coalesce(SEXP x, SEXP values, SEXP inplace) { if (nwhich==0) return(binplace ? x : duplicate(x)); + int *valueslens = malloc(JJ*sizeof(valueslens)); + if (!valueslens) error("%s: Unable to allocate memory", __func__); // # nocov + for (int j=0; j Date: Wed, 29 May 2019 18:03:32 +0530 Subject: [PATCH 07/37] add alt interface for dots to coalesce, as requested in #3424 --- R/coalesce.R | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/R/coalesce.R b/R/coalesce.R index b599e08e60..3546a895d5 100644 --- a/R/coalesce.R +++ b/R/coalesce.R @@ -1,7 +1,11 @@ -coalesce = function(x, ..., inplace=FALSE) { - if (missing(..1)) return(x) +coalesce = function(x, ..., .dots=NULL, .inplace=FALSE) { + if (is.null(.dots) && missing(..1)) return(x) - values = list(...) + values = if (!is.null(.dots)) { + if (!missing(..1)) stop("provide either '...' or '.dots' argument, not both") + if (!is.list(.dots)) stop("argument '.dots' requires a list input") + .dots + } else list(...) nn = length(x) # can be lengths() in R 3.2.0 @@ -28,12 +32,11 @@ coalesce = function(x, ..., inplace=FALSE) { " Please ensure type consistency on your inputs.") } - ans = .Call(Ccoalesce, x, values, inplace) - if (inplace) invisible(ans) else ans + ans = .Call(Ccoalesce, x, values, .inplace) + if (.inplace) invisible(ans) else ans } -setcoalesce = function(x, ...) { - if (missing(..1)) return(invisible(x)) - invisible(coalesce(x, ..., inplace=TRUE)) +setcoalesce = function(x, ..., .dots=NULL) { + invisible(coalesce(x, ..., .dots=.dots, .inplace=TRUE)) } #whichna.character = function(x) { # stopifnot(is.character(x)) From eb1d9d17cb7a1b957b306b4067f6e5c4850ef927 Mon Sep 17 00:00:00 2001 From: jangorecki Date: Wed, 29 May 2019 20:42:48 +0530 Subject: [PATCH 08/37] remove extra allocation, add verbose --- src/coalesce.c | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/coalesce.c b/src/coalesce.c index 1066b25e39..8670eca60e 100644 --- a/src/coalesce.c +++ b/src/coalesce.c @@ -1,61 +1,52 @@ #include "data.table.h" void which_int(int *x, int nx, int *out, int *nout, int val) { - int *buf = malloc(nx*sizeof(buf)); - if (!buf) error("%s: Unable to allocate memory", __func__); // # nocov int j=0; for (int i=0; i Date: Wed, 29 May 2019 22:06:06 +0530 Subject: [PATCH 09/37] simplify which_char --- src/coalesce.c | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/src/coalesce.c b/src/coalesce.c index 8670eca60e..9546c05306 100644 --- a/src/coalesce.c +++ b/src/coalesce.c @@ -31,19 +31,10 @@ void which_double(double *x, int nx, int *out, int *nout, double val) { } void which_char(SEXP x, int nx, int *out, int *nout, SEXP val) { int j=0; - if (STRING_ELT(val, 0) == NA_STRING) { - for (int i=0; i Date: Thu, 30 May 2019 12:23:11 +0530 Subject: [PATCH 10/37] reorg code, which_eq double NaN handling --- R/coalesce.R | 12 +++-- src/coalesce.c | 94 +++++++++------------------------- src/data.table.h | 9 +++- src/init.c | 3 +- src/utils.c | 131 +++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 173 insertions(+), 76 deletions(-) create mode 100644 src/utils.c diff --git a/R/coalesce.R b/R/coalesce.R index 3546a895d5..9fb1ae5392 100644 --- a/R/coalesce.R +++ b/R/coalesce.R @@ -38,7 +38,11 @@ coalesce = function(x, ..., .dots=NULL, .inplace=FALSE) { setcoalesce = function(x, ..., .dots=NULL) { invisible(coalesce(x, ..., .dots=.dots, .inplace=TRUE)) } -#whichna.character = function(x) { -# stopifnot(is.character(x)) -# .Call(Cwhichna_charR, x) -#} +which_eq.double = function(x, value, negate=FALSE) { + stopifnot(is.double(x), is.double(value)) + .Call(Cwhich_eq_doubleR, x, value, negate) +} +which_eq.character = function(x, value, negate=FALSE) { + stopifnot(is.character(x), is.character(value)) + .Call(Cwhich_eq_charR, x, value, negate) +} diff --git a/src/coalesce.c b/src/coalesce.c index 9546c05306..1cef54ab0f 100644 --- a/src/coalesce.c +++ b/src/coalesce.c @@ -1,62 +1,9 @@ #include "data.table.h" -void which_int(int *x, int nx, int *out, int *nout, int val) { - int j=0; - for (int i=0; i Date: Thu, 30 May 2019 12:23:59 +0530 Subject: [PATCH 11/37] ncoalesce not yet exported --- NAMESPACE | 1 - 1 file changed, 1 deletion(-) diff --git a/NAMESPACE b/NAMESPACE index 7fadac8c49..747ae77ab0 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -32,7 +32,6 @@ export(rleidv) export(rowid) export(rowidv) export(uniqueN) -export(coalesce) export(setDTthreads, getDTthreads) # set operators export(fintersect) From 9e2cf15b667a2ebe87b65b56a118bd1d8bdf2cd7 Mon Sep 17 00:00:00 2001 From: jangorecki Date: Thu, 30 May 2019 13:28:47 +0530 Subject: [PATCH 12/37] coalesce input validation push down to C --- R/coalesce.R | 29 ++--------------------------- src/coalesce.c | 9 ++++++++- src/data.table.h | 4 +++- src/utils.c | 30 +++++++++++++++++++++++------- 4 files changed, 36 insertions(+), 36 deletions(-) diff --git a/R/coalesce.R b/R/coalesce.R index 9fb1ae5392..1e1eb7fc82 100644 --- a/R/coalesce.R +++ b/R/coalesce.R @@ -1,37 +1,12 @@ coalesce = function(x, ..., .dots=NULL, .inplace=FALSE) { if (is.null(.dots) && missing(..1)) return(x) - + values = if (!is.null(.dots)) { if (!missing(..1)) stop("provide either '...' or '.dots' argument, not both") if (!is.list(.dots)) stop("argument '.dots' requires a list input") .dots } else list(...) - - nn = length(x) - # can be lengths() in R 3.2.0 - lengths = vapply_1i(values, length) - if (any(idx <- lengths > 1L & lengths < nn)) { - first_idx = which(idx) - stop("Replacement ", first_idx, " has length ", lengths[first_idx], - " but length(x) is ", lx, ". Only singletons will be recycled") - } - - x_type = typeof(x) - x_factor = inherits(x, 'factor') - - types = vapply_1c(values, typeof) - if (!all(idx <- x_type == types)) { - first_idx = which(!idx) - stop("Replacement ", first_idx, " has internal type ", types[first_idx], - " but typeof(x) is '", x_type, "'. Please coerce before coalescing.") - } - if (x_type == 'integer' && !all(idx <- x_factor == vapply_1b(values, inherits, 'factor'))) { - first_idx = which(!idx) - stop("Replacement ", first_idx, ' is ', if (x_factor) 'not ' else '', - "a factor, but x is", if (x_factor) "." else "not.", - " Please ensure type consistency on your inputs.") - } - + ans = .Call(Ccoalesce, x, values, .inplace) if (.inplace) invisible(ans) else ans } diff --git a/src/coalesce.c b/src/coalesce.c index 1cef54ab0f..61e4680613 100644 --- a/src/coalesce.c +++ b/src/coalesce.c @@ -3,7 +3,14 @@ SEXP coalesce(SEXP x, SEXP values, SEXP inplace) { int JJ = length(values), protecti = 0, nx = length(x); - bool binplace = trueFalseR(inplace); + if (!isTrueFalse(inplace)) error("%s: argument '.inplace' must be TRUE or FALSE", __func__); + bool binplace = LOGICAL(inplace)[0]; + + int firstLenMiss = lenMiss(values, nx, true); + if (firstLenMiss >= 0) error("%s: Replacement %d has length %d but length(x) is %d. Only singletons will be recycled.", __func__, firstLenMiss+1, length(VECTOR_ELT(values, firstLenMiss)), nx); + + int firstTypeMiss = typeMiss(values, TYPEOF(x)); + if (firstTypeMiss >= 0) error("%s: Replacement %d has internal type %s but typeof(x) is %s. Please coerce before coalescing", __func__, firstTypeMiss+1, type2char(TYPEOF(VECTOR_ELT(values, firstTypeMiss))), type2char(TYPEOF(x))); double tic = 0; const bool verbose = GetVerbose(); diff --git a/src/data.table.h b/src/data.table.h index 8e02e553f1..6ccca9e0ad 100644 --- a/src/data.table.h +++ b/src/data.table.h @@ -201,7 +201,9 @@ SEXP nafillR(SEXP obj, SEXP type, SEXP fill, SEXP inplace, SEXP cols, SEXP verbo SEXP between(SEXP x, SEXP lower, SEXP upper, SEXP bounds); // utils.c -bool trueFalseR(SEXP x); +bool isTrueFalse(SEXP x); +int lenMiss(SEXP x, int n, bool scalar); +int typeMiss(SEXP x, SEXPTYPE type); void which_eq_int(int *x, int nx, int *out, int *nout, int val, bool negate); void which_eq_double(double *x, int nx, int *out, int *nout, double val, bool negate); void which_eq_char(SEXP x, int nx, int *out, int *nout, SEXP val, bool negate); diff --git a/src/utils.c b/src/utils.c index ee024a9ac2..4453a589b8 100644 --- a/src/utils.c +++ b/src/utils.c @@ -1,12 +1,28 @@ #include "data.table.h" /* - * input validation and extraction + * input validation */ -bool trueFalseR(SEXP x) { - if (!isLogical(x) || length(x)!=1 || LOGICAL(x)[0]==NA_LOGICAL) - error("internal error: argument must be TRUE or FALSE"); // # nocov - return LOGICAL(x)[0]; +bool isTrueFalse(SEXP x) { + return !isLogical(x) || length(x)!=1 || LOGICAL(x)[0]==NA_LOGICAL; +} +int lenMiss(SEXP x, int n, bool scalar) { + if (!isNewList(x)) error("x must be a list"); + int nx = length(x); + for (int i=0; i Date: Thu, 30 May 2019 13:49:12 +0530 Subject: [PATCH 13/37] fix input validation --- src/coalesce.c | 2 ++ src/utils.c | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/coalesce.c b/src/coalesce.c index 61e4680613..e95da0ec23 100644 --- a/src/coalesce.c +++ b/src/coalesce.c @@ -3,6 +3,8 @@ SEXP coalesce(SEXP x, SEXP values, SEXP inplace) { int JJ = length(values), protecti = 0, nx = length(x); + if (!isVectorAtomic(x)) error("%s argument 'x' must be atomic vector", __func__); + if (!isTrueFalse(inplace)) error("%s: argument '.inplace' must be TRUE or FALSE", __func__); bool binplace = LOGICAL(inplace)[0]; diff --git a/src/utils.c b/src/utils.c index 4453a589b8..f4b7c81931 100644 --- a/src/utils.c +++ b/src/utils.c @@ -4,7 +4,7 @@ * input validation */ bool isTrueFalse(SEXP x) { - return !isLogical(x) || length(x)!=1 || LOGICAL(x)[0]==NA_LOGICAL; + return isLogical(x) && length(x)==1 && LOGICAL(x)[0]!=NA_LOGICAL; } int lenMiss(SEXP x, int n, bool scalar) { if (!isNewList(x)) error("x must be a list"); From 7f86a33f2db15e947cb83afc66201f769c886af9 Mon Sep 17 00:00:00 2001 From: jangorecki Date: Fri, 31 May 2019 09:38:28 +0530 Subject: [PATCH 14/37] coalesce API cleanup --- R/coalesce.R | 15 +++++++++++---- src/coalesce.c | 6 +++--- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/R/coalesce.R b/R/coalesce.R index 1e1eb7fc82..ce31d01c8c 100644 --- a/R/coalesce.R +++ b/R/coalesce.R @@ -1,4 +1,4 @@ -coalesce = function(x, ..., .dots=NULL, .inplace=FALSE) { +coalesce = function(x, ..., .dots=NULL) { if (is.null(.dots) && missing(..1)) return(x) values = if (!is.null(.dots)) { @@ -7,11 +7,18 @@ coalesce = function(x, ..., .dots=NULL, .inplace=FALSE) { .dots } else list(...) - ans = .Call(Ccoalesce, x, values, .inplace) - if (.inplace) invisible(ans) else ans + .Call(Ccoalesce, x, values, FALSE) } setcoalesce = function(x, ..., .dots=NULL) { - invisible(coalesce(x, ..., .dots=.dots, .inplace=TRUE)) + if (is.null(.dots) && missing(..1)) return(x) + + values = if (!is.null(.dots)) { + if (!missing(..1)) stop("provide either '...' or '.dots' argument, not both") + if (!is.list(.dots)) stop("argument '.dots' requires a list input") + .dots + } else list(...) + + invisible(.Call(Ccoalesce, x, values, TRUE)) } which_eq.double = function(x, value, negate=FALSE) { stopifnot(is.double(x), is.double(value)) diff --git a/src/coalesce.c b/src/coalesce.c index e95da0ec23..7adce5e483 100644 --- a/src/coalesce.c +++ b/src/coalesce.c @@ -48,15 +48,15 @@ SEXP coalesce(SEXP x, SEXP values, SEXP inplace) { int *valueslens = malloc(JJ*sizeof(valueslens)); if (!valueslens) error("%s: Unable to allocate memory", __func__); // # nocov - for (int j=0; j Date: Fri, 31 May 2019 12:43:37 +0530 Subject: [PATCH 15/37] own int64 funs, which_eq wrapper --- R/coalesce.R | 9 ++---- src/coalesce.c | 21 ++++++++------ src/data.table.h | 4 +-- src/init.c | 3 +- src/utils.c | 73 ++++++++++++++++++++++++++++++++++++------------ 5 files changed, 73 insertions(+), 37 deletions(-) diff --git a/R/coalesce.R b/R/coalesce.R index ce31d01c8c..5f9fc9d07b 100644 --- a/R/coalesce.R +++ b/R/coalesce.R @@ -20,11 +20,6 @@ setcoalesce = function(x, ..., .dots=NULL) { invisible(.Call(Ccoalesce, x, values, TRUE)) } -which_eq.double = function(x, value, negate=FALSE) { - stopifnot(is.double(x), is.double(value)) - .Call(Cwhich_eq_doubleR, x, value, negate) -} -which_eq.character = function(x, value, negate=FALSE) { - stopifnot(is.character(x), is.character(value)) - .Call(Cwhich_eq_charR, x, value, negate) +which_eq = function(x, value, negate=FALSE) { + .Call(Cwhich_eqR, x, value, negate) } diff --git a/src/coalesce.c b/src/coalesce.c index 7adce5e483..b4d8025efa 100644 --- a/src/coalesce.c +++ b/src/coalesce.c @@ -30,7 +30,11 @@ SEXP coalesce(SEXP x, SEXP values, SEXP inplace) { which_eq_int(INTEGER(x), nx, iwhich, &nwhich, NA_INTEGER, false); } break; case REALSXP: { - which_eq_double(REAL(x), nx, iwhich, &nwhich, inherits(x,"integer64") ? NA_INT64_D : NA_REAL, false); + if (inherits(x,"integer64")) { + which_eq_int64((int64_t *)REAL(x), nx, iwhich, &nwhich, NA_INTEGER64, false); + } else { + which_eq_double(REAL(x), nx, iwhich, &nwhich, NA_REAL, false); + } } break; case STRSXP: { which_eq_char(x, nx, iwhich, &nwhich, NA_STRING, false); @@ -46,10 +50,6 @@ SEXP coalesce(SEXP x, SEXP values, SEXP inplace) { return(binplace ? x : duplicate(x)); } - int *valueslens = malloc(JJ*sizeof(valueslens)); - if (!valueslens) error("%s: Unable to allocate memory", __func__); // # nocov - for (int j=0; j Date: Sat, 1 Jun 2019 16:07:17 +0530 Subject: [PATCH 16/37] unify error messages --- src/coalesce.c | 6 +++--- src/utils.c | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/coalesce.c b/src/coalesce.c index b4d8025efa..3719cef724 100644 --- a/src/coalesce.c +++ b/src/coalesce.c @@ -3,7 +3,7 @@ SEXP coalesce(SEXP x, SEXP values, SEXP inplace) { int JJ = length(values), protecti = 0, nx = length(x); - if (!isVectorAtomic(x)) error("%s argument 'x' must be atomic vector", __func__); + if (!isVectorAtomic(x)) error("%s: argument 'x' must be atomic vector", __func__); if (!isTrueFalse(inplace)) error("%s: argument '.inplace' must be TRUE or FALSE", __func__); bool binplace = LOGICAL(inplace)[0]; @@ -40,7 +40,7 @@ SEXP coalesce(SEXP x, SEXP values, SEXP inplace) { which_eq_char(x, nx, iwhich, &nwhich, NA_STRING, false); } break; default: { - error("Incompatible type"); + error("%s: Incompatible type", __func__); } } if (verbose) Rprintf("%s: which NA took %.3fs\n", __func__, omp_get_wtime()-tic); @@ -134,7 +134,7 @@ SEXP coalesce(SEXP x, SEXP values, SEXP inplace) { } } break; default: { - error("Unrecognized type."); + error("%s: Incompatible type.", __func__); } } if (verbose) Rprintf("%s: loop over x NAs indices took %.3fs\n", __func__, omp_get_wtime()-tic); diff --git a/src/utils.c b/src/utils.c index b08c263821..7f9ff31298 100644 --- a/src/utils.c +++ b/src/utils.c @@ -142,9 +142,9 @@ void which_eq_int64(int64_t *x, int nx, int *out, int *nout, int64_t val, bool n SEXP which_eqR(SEXP x, SEXP val, SEXP negate) { int protecti = 0; - if (!isVectorAtomic(x)) error("%s argument 'x' must be atomic vector", __func__); + if (!isVectorAtomic(x)) error("%s: argument 'x' must be atomic vector", __func__); - if (TYPEOF(x)!=TYPEOF(val)) error("%s argument 'value' must of the same type as argument 'x'", __func__); + if (TYPEOF(x)!=TYPEOF(val)) error("%s: argument 'value' must of the same type as argument 'x'", __func__); if (!isTrueFalse(negate)) error("%s: argument 'negate' must be TRUE or FALSE", __func__); bool bnegate = LOGICAL(negate)[0]; @@ -172,7 +172,7 @@ SEXP which_eqR(SEXP x, SEXP val, SEXP negate) { which_eq_char(x, nx, iwhich, &nwhich, STRING_ELT(val, 0), bnegate); } break; default: { - error("Incompatible type"); + error("%s: Incompatible type", __func__); } } From 36de4f90b1d35efc28290ad63f32f2466b882a09 Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Sun, 2 Jun 2019 15:50:44 +0800 Subject: [PATCH 17/37] add tests and manual page, first pass --- NAMESPACE | 1 + NEWS.md | 2 ++ inst/tests/tests.Rraw | 59 +++++++++++++++++++++++++++++++++++++++++++ man/coalesce.Rd | 28 ++++++++++++++++++++ src/coalesce.c | 8 +++--- 5 files changed, 94 insertions(+), 4 deletions(-) create mode 100644 man/coalesce.Rd diff --git a/NAMESPACE b/NAMESPACE index d96ab5cb28..b6d77a26df 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -53,6 +53,7 @@ export(frollsum) export(nafill) export(setnafill) export(.Last.updated) +export(coalesce) S3method("[", data.table) S3method("[<-", data.table) diff --git a/NEWS.md b/NEWS.md index 3da712bc7f..8e32291c1a 100644 --- a/NEWS.md +++ b/NEWS.md @@ -92,6 +92,8 @@ 15. `DT[order(col)[1:5], ...]` (i.e. where `i` is a compound expression involving `order()`) is now optimized to use `data.table`'s multithreaded `forder`, [#1921](https://github.com/Rdatatable/data.table/issues/1921). This example is not a fully optimal top-N query since the full ordering is still computed. The improvement is that the call to `order()` is computed faster for any `i` expression using `order`. +16. New function `coalesce` for efficiently replacing missing values according to a prioritized list of candidates (_a la_ SQL), [#3424](https://github.com/Rdatatable/data.table/issues/3424) See [#3608](https://github.com/Rdatatable/data.table/pull/3608) for benchmarks. + #### BUG FIXES 1. `first`, `last`, `head` and `tail` by group no longer error in some cases, [#2030](https://github.com/Rdatatable/data.table/issues/2030) [#3462](https://github.com/Rdatatable/data.table/issues/3462). Thanks to @franknarf1 for reporting. diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index ea6107383f..e1e8a52eb4 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -14977,6 +14977,65 @@ DT = data.table( test(2054, DT[order(C)[1:5], B, verbose=TRUE], c('b', 'b', 'c', 'c', 'a'), output = 'order optimisation is on') +# #3424 -- coalesce function +bool = c(TRUE, NA, FALSE) +bool_val = c(TRUE, TRUE, FALSE) +int = c(1L, 2L, NA_integer_, 4L) +int_val = 1:4 +num = c(1, 2, NA_real_, 4) +num_val = c(1, 2, 3, 4) +str = c('a', NA_character_, 'b', NA_character_) +str_val = c('a', 'b', 'b', 'b') +fkt = factor(str) +fkt_val = factor(str_val) + +## singleton replacements +test(2055.01, coalesce(bool, TRUE), bool_val) +test(2055.02, coalesce(bool, NA, TRUE), bool_val) +test(2055.03, coalesce(int, 3L), int_val) +test(2055.04, coalesce(int, NA_integer_, 3L), int_val) +test(2055.05, coalesce(num, 3), num_val) +test(2055.06, coalesce(num, NA_real_, 3), num_val) +test(2055.07, coalesce(str, 'b'), str_val) +test(2055.08, coalesce(str, NA_character_, 'b'), str_val) +test(2055.09, coalesce(fkt, factor('b', levels = c('a', 'b'))), fkt_val) +test(2055.10, coalesce(fkt, NA_integer_, factor('b', levels = c('a', 'b'))), fkt_val) + +## vector replacements +test(2055.11, coalesce(bool, rep(TRUE, 3L)), bool_val) +test(2055.12, coalesce(bool, rep(NA, 3L), rep(TRUE, 3L)), bool_val) +test(2055.13, coalesce(int, rep(3L, 4L)), int_val) +test(2055.14, coalesce(int, rep(NA_integer_, 4L), rep(3L, 4L)), int_val) +test(2055.15, coalesce(num, rep(3, 4L)), num_val) +test(2055.16, coalesce(num, rep(NA_real_, 4L), rep(3, 4L)), num_val) +test(2055.17, coalesce(str, rep('b', 4L)), str_val) +test(2055.18, coalesce(str, rep(NA_character_, 4L), rep('b', 4L)), str_val) +test(2055.19, coalesce(fkt, factor(rep('b', 4L), levels = c('a', 'b'))), fkt_val) +test(2055.20, coalesce(fkt, rep(NA_integer_, 4L), factor(rep('b', 4L), levels = c('a', 'b'))), fkt_val) + +## .dots argument +test(2055.21, coalesce(bool, .dots = list(NA, TRUE)), bool_val) + +## edge cases/checks +test(2055.22, coalesce(bool), bool) +test(2055.23, coalesce(fkt), fkt) +test(2055.24, coalesce(bool, 1L), error = 'internal type integer') +#test(2055.25, coalesce(bool, NA_integer_), error = 'internal type integer') +#test(2055.26, coalesce(fkt, 1L), error = '') +test(2055.27, coalesce(fkt, 'b'), error = 'internal type character') +test(2055.28, coalesce(str, factor('b')), error = 'internal type integer') +test(2055.29, coalesce(bool, TRUE, .dots = list(FALSE)), error = "either '...' or '.dots'") +test(2055.30, coalesce(bool, .dots = TRUE), error = "'.dots' requires a list") +test(2055.31, coalesce(list(1), list(2)), error = "'x' must be an atomic vector") +test(2055.32, coalesce(bool, c(TRUE, FALSE)), error = "Only singletons will be recycled") +test(2055.33, coalesce(as.raw(0), as.raw(1)), error = "Incompatible type: raw") + +## verbose output +old = options(datatable.verbose = TRUE) +test(2055.34, coalesce(bool, TRUE), bool_val, output = 'which NA took.*duplicate.*took.*loop over NA indices of x took') +test(2055.35, coalesce(bool_val, TRUE), bool_val, output = 'no NAs in input, skip') +options(old) + ################################### # Add new tests above this line # diff --git a/man/coalesce.Rd b/man/coalesce.Rd new file mode 100644 index 0000000000..38def36072 --- /dev/null +++ b/man/coalesce.Rd @@ -0,0 +1,28 @@ +\name{coalesce} +\alias{coalesce} +\title{ Coalescing missing values } +\description{ + Fill in missing values in a vector by successively pulling from candidate vectors in order (\emph{a la} the ANSI SQL function of the same name). + +} +\usage{ + coalesce(x, \dots, .dots=NULL) +} +\arguments{ + \item{x}{ A vector of values } + \item{\dots}{ Vectors to be used successively as replacements for missing values of \code{x}; singletons are also accepted. } + \item{.dots}{ Alternative interface to specify replacements by supplying a list directly. } +} +\details{ + +} + +\value{ + +} +\examples{ + +} +\keyword{ data } + + diff --git a/src/coalesce.c b/src/coalesce.c index 3719cef724..c3f06e16b9 100644 --- a/src/coalesce.c +++ b/src/coalesce.c @@ -3,7 +3,7 @@ SEXP coalesce(SEXP x, SEXP values, SEXP inplace) { int JJ = length(values), protecti = 0, nx = length(x); - if (!isVectorAtomic(x)) error("%s: argument 'x' must be atomic vector", __func__); + if (!isVectorAtomic(x)) error("%s: argument 'x' must be an atomic vector", __func__); if (!isTrueFalse(inplace)) error("%s: argument '.inplace' must be TRUE or FALSE", __func__); bool binplace = LOGICAL(inplace)[0]; @@ -40,7 +40,7 @@ SEXP coalesce(SEXP x, SEXP values, SEXP inplace) { which_eq_char(x, nx, iwhich, &nwhich, NA_STRING, false); } break; default: { - error("%s: Incompatible type", __func__); + error("%s: Incompatible type: %s", __func__, type2char(TYPEOF(x))); } } if (verbose) Rprintf("%s: which NA took %.3fs\n", __func__, omp_get_wtime()-tic); @@ -134,10 +134,10 @@ SEXP coalesce(SEXP x, SEXP values, SEXP inplace) { } } break; default: { - error("%s: Incompatible type.", __func__); + error("Internal error in %s -- incompatible type should %s should have been caught by now; please report.", __func__, type2char(TYPEOF(x))); // # nocov } } - if (verbose) Rprintf("%s: loop over x NAs indices took %.3fs\n", __func__, omp_get_wtime()-tic); + if (verbose) Rprintf("%s: loop over NA indices of x took %.3fs\n", __func__, omp_get_wtime()-tic); UNPROTECT(protecti); return out; From fdf719c874262722ed4ea40fd1670fba4cfc356b Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Sun, 2 Jun 2019 21:55:11 +0800 Subject: [PATCH 18/37] more placeholder tests --- inst/tests/tests.Rraw | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index e1e8a52eb4..1c31096a33 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -15022,18 +15022,20 @@ test(2055.23, coalesce(fkt), fkt) test(2055.24, coalesce(bool, 1L), error = 'internal type integer') #test(2055.25, coalesce(bool, NA_integer_), error = 'internal type integer') #test(2055.26, coalesce(fkt, 1L), error = '') -test(2055.27, coalesce(fkt, 'b'), error = 'internal type character') -test(2055.28, coalesce(str, factor('b')), error = 'internal type integer') -test(2055.29, coalesce(bool, TRUE, .dots = list(FALSE)), error = "either '...' or '.dots'") -test(2055.30, coalesce(bool, .dots = TRUE), error = "'.dots' requires a list") -test(2055.31, coalesce(list(1), list(2)), error = "'x' must be an atomic vector") -test(2055.32, coalesce(bool, c(TRUE, FALSE)), error = "Only singletons will be recycled") -test(2055.33, coalesce(as.raw(0), as.raw(1)), error = "Incompatible type: raw") +#test(2055.27, coalesce(num, 3L), error = 'internal type integer') +#test(2055.28, coalesce(int, 3), error = 'internal type double') +test(2055.29, coalesce(fkt, 'b'), error = 'internal type character') +test(2055.30, coalesce(str, factor('b')), error = 'internal type integer') +test(2055.31, coalesce(bool, TRUE, .dots = list(FALSE)), error = "either '...' or '.dots'") +test(2055.32, coalesce(bool, .dots = TRUE), error = "'.dots' requires a list") +test(2055.33, coalesce(list(1), list(2)), error = "'x' must be an atomic vector") +test(2055.34, coalesce(bool, c(TRUE, FALSE)), error = "Only singletons will be recycled") +test(2055.35, coalesce(as.raw(0), as.raw(1)), error = "Incompatible type: raw") ## verbose output old = options(datatable.verbose = TRUE) -test(2055.34, coalesce(bool, TRUE), bool_val, output = 'which NA took.*duplicate.*took.*loop over NA indices of x took') -test(2055.35, coalesce(bool_val, TRUE), bool_val, output = 'no NAs in input, skip') +test(2055.36, coalesce(bool, TRUE), bool_val, output = 'which NA took.*duplicate.*took.*loop over NA indices of x took') +test(2055.37, coalesce(bool_val, TRUE), bool_val, output = 'no NAs in input, skip') options(old) From 008ccc669748544302e8da582711010b02ecd1fe Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Sun, 2 Jun 2019 22:18:09 +0800 Subject: [PATCH 19/37] integer64 tests --- inst/tests/tests.Rraw | 49 +++++++++++++++++++++++++++---------------- 1 file changed, 31 insertions(+), 18 deletions(-) diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index 1c31096a33..aef53e5792 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -15010,34 +15010,47 @@ test(2055.15, coalesce(num, rep(3, 4L)), num_val) test(2055.16, coalesce(num, rep(NA_real_, 4L), rep(3, 4L)), num_val) test(2055.17, coalesce(str, rep('b', 4L)), str_val) test(2055.18, coalesce(str, rep(NA_character_, 4L), rep('b', 4L)), str_val) -test(2055.19, coalesce(fkt, factor(rep('b', 4L), levels = c('a', 'b'))), fkt_val) -test(2055.20, coalesce(fkt, rep(NA_integer_, 4L), factor(rep('b', 4L), levels = c('a', 'b'))), fkt_val) +test(2055.19, coalesce(fkt, factor(rep('b', 4L), levels=c('a', 'b'))), fkt_val) +test(2055.20, coalesce(fkt, rep(NA_integer_, 4L), factor(rep('b', 4L), levels=c('a', 'b'))), fkt_val) ## .dots argument -test(2055.21, coalesce(bool, .dots = list(NA, TRUE)), bool_val) +test(2055.21, coalesce(bool, .dots=list(NA, TRUE)), bool_val) ## edge cases/checks test(2055.22, coalesce(bool), bool) test(2055.23, coalesce(fkt), fkt) -test(2055.24, coalesce(bool, 1L), error = 'internal type integer') -#test(2055.25, coalesce(bool, NA_integer_), error = 'internal type integer') -#test(2055.26, coalesce(fkt, 1L), error = '') -#test(2055.27, coalesce(num, 3L), error = 'internal type integer') -#test(2055.28, coalesce(int, 3), error = 'internal type double') -test(2055.29, coalesce(fkt, 'b'), error = 'internal type character') -test(2055.30, coalesce(str, factor('b')), error = 'internal type integer') -test(2055.31, coalesce(bool, TRUE, .dots = list(FALSE)), error = "either '...' or '.dots'") -test(2055.32, coalesce(bool, .dots = TRUE), error = "'.dots' requires a list") -test(2055.33, coalesce(list(1), list(2)), error = "'x' must be an atomic vector") -test(2055.34, coalesce(bool, c(TRUE, FALSE)), error = "Only singletons will be recycled") -test(2055.35, coalesce(as.raw(0), as.raw(1)), error = "Incompatible type: raw") +test(2055.24, coalesce(bool, 1L), error='internal type integer') +#test(2055.25, coalesce(bool, NA_integer_), error='internal type integer') +#test(2055.26, coalesce(fkt, 1L), error='') +#test(2055.27, coalesce(num, 3L), error='internal type integer') +#test(2055.28, coalesce(int, 3), error='internal type double') +test(2055.29, coalesce(fkt, 'b'), error='internal type character') +test(2055.30, coalesce(str, factor('b')), error='internal type integer') +test(2055.31, coalesce(bool, TRUE, .dots=list(FALSE)), error="either '...' or '.dots'") +test(2055.32, coalesce(bool, .dots=TRUE), error="'.dots' requires a list") +test(2055.33, coalesce(list(1), list(2)), error="'x' must be an atomic vector") +test(2055.34, coalesce(bool, c(TRUE, FALSE)), error="Only singletons will be recycled") +test(2055.35, coalesce(as.raw(0), as.raw(1)), error="Incompatible type: raw") ## verbose output -old = options(datatable.verbose = TRUE) -test(2055.36, coalesce(bool, TRUE), bool_val, output = 'which NA took.*duplicate.*took.*loop over NA indices of x took') -test(2055.37, coalesce(bool_val, TRUE), bool_val, output = 'no NAs in input, skip') +old = options(datatable.verbose=TRUE) +test(2055.36, coalesce(bool, TRUE), bool_val, output='which NA took.*duplicate.*took.*loop over NA indices of x took') +test(2055.37, coalesce(bool_val, TRUE), bool_val, output='no NAs in input, skip') options(old) +## integer64 tests +if (test_bit64) { + int64 = as.integer64(int) + int64_val = as.integer64(1:4) + + test(2055.38, coalesce(int64, as.integer64(3)), int64_val) + test(2055.39, coalesce(int64, as.integer64(NA), as.integer64(3)), int64_val) + test(2055.40, coalesce(int64, as.integer64(rep(3, 4L))), int64_val) + + #test(2055.41, coalesce(int64, 1), error='') + test(2055.42, coalesce(int64, 1L), error = 'internal type integer') +} + ################################### # Add new tests above this line # From bbc71e27347adb8c453cca6c3f702c09c266b4b7 Mon Sep 17 00:00:00 2001 From: jangorecki Date: Mon, 3 Jun 2019 09:58:11 +0530 Subject: [PATCH 20/37] reord test for easier dev and git merge --- inst/tests/coalesce.Rraw | 88 ++++++++++++++++++++++++++++++++++++++++ inst/tests/tests.Rraw | 74 --------------------------------- tests/coalesce.R | 2 + 3 files changed, 90 insertions(+), 74 deletions(-) create mode 100644 inst/tests/coalesce.Rraw create mode 100644 tests/coalesce.R diff --git a/inst/tests/coalesce.Rraw b/inst/tests/coalesce.Rraw new file mode 100644 index 0000000000..295814db31 --- /dev/null +++ b/inst/tests/coalesce.Rraw @@ -0,0 +1,88 @@ +require(methods) + +if (exists("test.data.table", .GlobalEnv, inherits=FALSE)) { + if (!identical(suppressWarnings(packageDescription("data.table")), NA)) { + remove.packages("data.table") + stop("This is dev mode but data.table was installed. Uninstalled it. Please q() this R session and try cc() again. The installed namespace causes problems in dev mode for the S4 tests.\n") + } + if ((tt<-compiler::enableJIT(-1))>0) + cat("This is dev mode and JIT is enabled (level ", tt, ") so there will be a brief pause around the first test.\n", sep="") +} else { + require(data.table) + test = data.table:::test + #x = data.table:::x +} + +# #3424 -- coalesce function +bool = c(TRUE, NA, FALSE) +bool_val = c(TRUE, TRUE, FALSE) +int = c(1L, 2L, NA_integer_, 4L) +int_val = 1:4 +num = c(1, 2, NA_real_, 4) +num_val = c(1, 2, 3, 4) +str = c('a', NA_character_, 'b', NA_character_) +str_val = c('a', 'b', 'b', 'b') +fkt = factor(str) +fkt_val = factor(str_val) + +## singleton replacements +test(2055.01, coalesce(bool, TRUE), bool_val) +test(2055.02, coalesce(bool, NA, TRUE), bool_val) +test(2055.03, coalesce(int, 3L), int_val) +test(2055.04, coalesce(int, NA_integer_, 3L), int_val) +test(2055.05, coalesce(num, 3), num_val) +test(2055.06, coalesce(num, NA_real_, 3), num_val) +test(2055.07, coalesce(str, 'b'), str_val) +test(2055.08, coalesce(str, NA_character_, 'b'), str_val) +test(2055.09, coalesce(fkt, factor('b', levels = c('a', 'b'))), fkt_val) +test(2055.10, coalesce(fkt, NA_integer_, factor('b', levels = c('a', 'b'))), fkt_val) + +## vector replacements +test(2055.11, coalesce(bool, rep(TRUE, 3L)), bool_val) +test(2055.12, coalesce(bool, rep(NA, 3L), rep(TRUE, 3L)), bool_val) +test(2055.13, coalesce(int, rep(3L, 4L)), int_val) +test(2055.14, coalesce(int, rep(NA_integer_, 4L), rep(3L, 4L)), int_val) +test(2055.15, coalesce(num, rep(3, 4L)), num_val) +test(2055.16, coalesce(num, rep(NA_real_, 4L), rep(3, 4L)), num_val) +test(2055.17, coalesce(str, rep('b', 4L)), str_val) +test(2055.18, coalesce(str, rep(NA_character_, 4L), rep('b', 4L)), str_val) +test(2055.19, coalesce(fkt, factor(rep('b', 4L), levels=c('a', 'b'))), fkt_val) +test(2055.20, coalesce(fkt, rep(NA_integer_, 4L), factor(rep('b', 4L), levels=c('a', 'b'))), fkt_val) + +## .dots argument +test(2055.21, coalesce(bool, .dots=list(NA, TRUE)), bool_val) + +## edge cases/checks +test(2055.22, coalesce(bool), bool) +test(2055.23, coalesce(fkt), fkt) +test(2055.24, coalesce(bool, 1L), error='internal type integer') +#test(2055.25, coalesce(bool, NA_integer_), error='internal type integer') +#test(2055.26, coalesce(fkt, 1L), error='') +#test(2055.27, coalesce(num, 3L), error='internal type integer') +#test(2055.28, coalesce(int, 3), error='internal type double') +test(2055.29, coalesce(fkt, 'b'), error='internal type character') +test(2055.30, coalesce(str, factor('b')), error='internal type integer') +test(2055.31, coalesce(bool, TRUE, .dots=list(FALSE)), error="either '...' or '.dots'") +test(2055.32, coalesce(bool, .dots=TRUE), error="'.dots' requires a list") +test(2055.33, coalesce(list(1), list(2)), error="'x' must be an atomic vector") +test(2055.34, coalesce(bool, c(TRUE, FALSE)), error="Only singletons will be recycled") +test(2055.35, coalesce(as.raw(0), as.raw(1)), error="Incompatible type: raw") + +## verbose output +old = options(datatable.verbose=TRUE) +test(2055.36, coalesce(bool, TRUE), bool_val, output='which NA took.*duplicate.*took.*loop over NA indices of x took') +test(2055.37, coalesce(bool_val, TRUE), bool_val, output='no NAs in input, skip') +options(old) + +## integer64 tests +if (test_bit64) { + int64 = as.integer64(int) + int64_val = as.integer64(1:4) + + test(2055.38, coalesce(int64, as.integer64(3)), int64_val) + test(2055.39, coalesce(int64, as.integer64(NA), as.integer64(3)), int64_val) + test(2055.40, coalesce(int64, as.integer64(rep(3, 4L))), int64_val) + + #test(2055.41, coalesce(int64, 1), error='') + test(2055.42, coalesce(int64, 1L), error = 'internal type integer') +} diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index aef53e5792..ea6107383f 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -14977,80 +14977,6 @@ DT = data.table( test(2054, DT[order(C)[1:5], B, verbose=TRUE], c('b', 'b', 'c', 'c', 'a'), output = 'order optimisation is on') -# #3424 -- coalesce function -bool = c(TRUE, NA, FALSE) -bool_val = c(TRUE, TRUE, FALSE) -int = c(1L, 2L, NA_integer_, 4L) -int_val = 1:4 -num = c(1, 2, NA_real_, 4) -num_val = c(1, 2, 3, 4) -str = c('a', NA_character_, 'b', NA_character_) -str_val = c('a', 'b', 'b', 'b') -fkt = factor(str) -fkt_val = factor(str_val) - -## singleton replacements -test(2055.01, coalesce(bool, TRUE), bool_val) -test(2055.02, coalesce(bool, NA, TRUE), bool_val) -test(2055.03, coalesce(int, 3L), int_val) -test(2055.04, coalesce(int, NA_integer_, 3L), int_val) -test(2055.05, coalesce(num, 3), num_val) -test(2055.06, coalesce(num, NA_real_, 3), num_val) -test(2055.07, coalesce(str, 'b'), str_val) -test(2055.08, coalesce(str, NA_character_, 'b'), str_val) -test(2055.09, coalesce(fkt, factor('b', levels = c('a', 'b'))), fkt_val) -test(2055.10, coalesce(fkt, NA_integer_, factor('b', levels = c('a', 'b'))), fkt_val) - -## vector replacements -test(2055.11, coalesce(bool, rep(TRUE, 3L)), bool_val) -test(2055.12, coalesce(bool, rep(NA, 3L), rep(TRUE, 3L)), bool_val) -test(2055.13, coalesce(int, rep(3L, 4L)), int_val) -test(2055.14, coalesce(int, rep(NA_integer_, 4L), rep(3L, 4L)), int_val) -test(2055.15, coalesce(num, rep(3, 4L)), num_val) -test(2055.16, coalesce(num, rep(NA_real_, 4L), rep(3, 4L)), num_val) -test(2055.17, coalesce(str, rep('b', 4L)), str_val) -test(2055.18, coalesce(str, rep(NA_character_, 4L), rep('b', 4L)), str_val) -test(2055.19, coalesce(fkt, factor(rep('b', 4L), levels=c('a', 'b'))), fkt_val) -test(2055.20, coalesce(fkt, rep(NA_integer_, 4L), factor(rep('b', 4L), levels=c('a', 'b'))), fkt_val) - -## .dots argument -test(2055.21, coalesce(bool, .dots=list(NA, TRUE)), bool_val) - -## edge cases/checks -test(2055.22, coalesce(bool), bool) -test(2055.23, coalesce(fkt), fkt) -test(2055.24, coalesce(bool, 1L), error='internal type integer') -#test(2055.25, coalesce(bool, NA_integer_), error='internal type integer') -#test(2055.26, coalesce(fkt, 1L), error='') -#test(2055.27, coalesce(num, 3L), error='internal type integer') -#test(2055.28, coalesce(int, 3), error='internal type double') -test(2055.29, coalesce(fkt, 'b'), error='internal type character') -test(2055.30, coalesce(str, factor('b')), error='internal type integer') -test(2055.31, coalesce(bool, TRUE, .dots=list(FALSE)), error="either '...' or '.dots'") -test(2055.32, coalesce(bool, .dots=TRUE), error="'.dots' requires a list") -test(2055.33, coalesce(list(1), list(2)), error="'x' must be an atomic vector") -test(2055.34, coalesce(bool, c(TRUE, FALSE)), error="Only singletons will be recycled") -test(2055.35, coalesce(as.raw(0), as.raw(1)), error="Incompatible type: raw") - -## verbose output -old = options(datatable.verbose=TRUE) -test(2055.36, coalesce(bool, TRUE), bool_val, output='which NA took.*duplicate.*took.*loop over NA indices of x took') -test(2055.37, coalesce(bool_val, TRUE), bool_val, output='no NAs in input, skip') -options(old) - -## integer64 tests -if (test_bit64) { - int64 = as.integer64(int) - int64_val = as.integer64(1:4) - - test(2055.38, coalesce(int64, as.integer64(3)), int64_val) - test(2055.39, coalesce(int64, as.integer64(NA), as.integer64(3)), int64_val) - test(2055.40, coalesce(int64, as.integer64(rep(3, 4L))), int64_val) - - #test(2055.41, coalesce(int64, 1), error='') - test(2055.42, coalesce(int64, 1L), error = 'internal type integer') -} - ################################### # Add new tests above this line # diff --git a/tests/coalesce.R b/tests/coalesce.R new file mode 100644 index 0000000000..8a2b9fab91 --- /dev/null +++ b/tests/coalesce.R @@ -0,0 +1,2 @@ +require(data.table) +test.data.table(script="coalesce.Rraw") From 0b6ac676bf37d31ec1589f2c1db4e60e5775a6c3 Mon Sep 17 00:00:00 2001 From: jangorecki Date: Mon, 3 Jun 2019 10:23:43 +0530 Subject: [PATCH 21/37] move gh comments to test script --- inst/tests/coalesce.Rraw | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/inst/tests/coalesce.Rraw b/inst/tests/coalesce.Rraw index 295814db31..82cb8bde87 100644 --- a/inst/tests/coalesce.Rraw +++ b/inst/tests/coalesce.Rraw @@ -56,9 +56,12 @@ test(2055.21, coalesce(bool, .dots=list(NA, TRUE)), bool_val) test(2055.22, coalesce(bool), bool) test(2055.23, coalesce(fkt), fkt) test(2055.24, coalesce(bool, 1L), error='internal type integer') +#TODO should we error here, or convert any NA type to the right NA? #test(2055.25, coalesce(bool, NA_integer_), error='internal type integer') +#TODO we don't have any robustness around factor/integer as yet #test(2055.26, coalesce(fkt, 1L), error='') #test(2055.27, coalesce(num, 3L), error='internal type integer') +#TODO two more placeholder tests -- do we really want to error here? or worth to add isReallyReal-type tests? numeric->integer should always be doable right? #test(2055.28, coalesce(int, 3), error='internal type double') test(2055.29, coalesce(fkt, 'b'), error='internal type character') test(2055.30, coalesce(str, factor('b')), error='internal type integer') @@ -83,6 +86,7 @@ if (test_bit64) { test(2055.39, coalesce(int64, as.integer64(NA), as.integer64(3)), int64_val) test(2055.40, coalesce(int64, as.integer64(rep(3, 4L))), int64_val) + #TODO this line current runs and gives a nonsense answer #test(2055.41, coalesce(int64, 1), error='') test(2055.42, coalesce(int64, 1L), error = 'internal type integer') } From 4979f1fb369c1ba987a4c30c42da2508303fba00 Mon Sep 17 00:00:00 2001 From: jangorecki Date: Mon, 3 Jun 2019 10:33:11 +0530 Subject: [PATCH 22/37] fix check errors --- inst/tests/coalesce.Rraw | 11 ++++++++++- man/coalesce.Rd | 14 ++++++++------ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/inst/tests/coalesce.Rraw b/inst/tests/coalesce.Rraw index 82cb8bde87..1d648af3e9 100644 --- a/inst/tests/coalesce.Rraw +++ b/inst/tests/coalesce.Rraw @@ -10,7 +10,16 @@ if (exists("test.data.table", .GlobalEnv, inherits=FALSE)) { } else { require(data.table) test = data.table:::test - #x = data.table:::x + setcoalesce = data.table:::setcoalesce + which_eq = data.table:::which_eq +} + +sugg = c( + "bit64" +) +for (s in sugg) { + assign(paste0("test_",s), loaded<-suppressWarnings(suppressMessages(require(s, character.only=TRUE)))) + if (!loaded) cat("\n**** Suggested package",s,"is not installed. Tests using it will be skipped.\n\n") } # #3424 -- coalesce function diff --git a/man/coalesce.Rd b/man/coalesce.Rd index 38def36072..b9a449a6e6 100644 --- a/man/coalesce.Rd +++ b/man/coalesce.Rd @@ -1,9 +1,9 @@ \name{coalesce} \alias{coalesce} +\alias{setcoalesce} \title{ Coalescing missing values } \description{ - Fill in missing values in a vector by successively pulling from candidate vectors in order (\emph{a la} the ANSI SQL function of the same name). - +Fill in missing values in a vector by successively pulling from candidate vectors in order (\emph{a la} the ANSI SQL function of the same name). } \usage{ coalesce(x, \dots, .dots=NULL) @@ -14,14 +14,16 @@ \item{.dots}{ Alternative interface to specify replacements by supplying a list directly. } } \details{ - +Factor type is not yet supported. } - \value{ - +Atomic vector of same type as \code{x} provided on input, having \code{NA} values replaced by corresponding non-\code{NA} values of arguments provided to \dots. } \examples{ - +x = c(11L, NA, 13L, NA, 15L, NA) +y = c(NA, 12L, 5L, NA, NA, NA) +z = c(11L, NA, 1L, 14L, NA, NA) +coalesce(x, y, z) } \keyword{ data } From 55c6be2197fd154bc29e4e965ff86451be1b4b10 Mon Sep 17 00:00:00 2001 From: jangorecki Date: Mon, 3 Jun 2019 10:54:31 +0530 Subject: [PATCH 23/37] early escape for no input --- R/coalesce.R | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/R/coalesce.R b/R/coalesce.R index 5f9fc9d07b..da05b078cb 100644 --- a/R/coalesce.R +++ b/R/coalesce.R @@ -1,5 +1,5 @@ coalesce = function(x, ..., .dots=NULL) { - if (is.null(.dots) && missing(..1)) return(x) + if (is.null(.dots) && missing(..1)) return(copy(x)) values = if (!is.null(.dots)) { if (!missing(..1)) stop("provide either '...' or '.dots' argument, not both") @@ -7,6 +7,8 @@ coalesce = function(x, ..., .dots=NULL) { .dots } else list(...) + if (!length(values)) return(copy(x)) + .Call(Ccoalesce, x, values, FALSE) } setcoalesce = function(x, ..., .dots=NULL) { @@ -18,6 +20,8 @@ setcoalesce = function(x, ..., .dots=NULL) { .dots } else list(...) + if (!length(values)) return(x) + invisible(.Call(Ccoalesce, x, values, TRUE)) } which_eq = function(x, value, negate=FALSE) { From 5e9e85c9d3eebad2bd919f6b3dcb42367c86c935 Mon Sep 17 00:00:00 2001 From: jangorecki Date: Mon, 3 Jun 2019 12:18:54 +0530 Subject: [PATCH 24/37] setcoalesce and which_eq test coverage --- inst/tests/coalesce.Rraw | 156 +++++++++++++++++++++++++-------------- src/utils.c | 2 +- 2 files changed, 101 insertions(+), 57 deletions(-) diff --git a/inst/tests/coalesce.Rraw b/inst/tests/coalesce.Rraw index 1d648af3e9..e6ea09ea3b 100644 --- a/inst/tests/coalesce.Rraw +++ b/inst/tests/coalesce.Rraw @@ -22,7 +22,7 @@ for (s in sugg) { if (!loaded) cat("\n**** Suggested package",s,"is not installed. Tests using it will be skipped.\n\n") } -# #3424 -- coalesce function +# coalesce #3424 bool = c(TRUE, NA, FALSE) bool_val = c(TRUE, TRUE, FALSE) int = c(1L, 2L, NA_integer_, 4L) @@ -33,69 +33,113 @@ str = c('a', NA_character_, 'b', NA_character_) str_val = c('a', 'b', 'b', 'b') fkt = factor(str) fkt_val = factor(str_val) - -## singleton replacements -test(2055.01, coalesce(bool, TRUE), bool_val) -test(2055.02, coalesce(bool, NA, TRUE), bool_val) -test(2055.03, coalesce(int, 3L), int_val) -test(2055.04, coalesce(int, NA_integer_, 3L), int_val) -test(2055.05, coalesce(num, 3), num_val) -test(2055.06, coalesce(num, NA_real_, 3), num_val) -test(2055.07, coalesce(str, 'b'), str_val) -test(2055.08, coalesce(str, NA_character_, 'b'), str_val) -test(2055.09, coalesce(fkt, factor('b', levels = c('a', 'b'))), fkt_val) -test(2055.10, coalesce(fkt, NA_integer_, factor('b', levels = c('a', 'b'))), fkt_val) - -## vector replacements -test(2055.11, coalesce(bool, rep(TRUE, 3L)), bool_val) -test(2055.12, coalesce(bool, rep(NA, 3L), rep(TRUE, 3L)), bool_val) -test(2055.13, coalesce(int, rep(3L, 4L)), int_val) -test(2055.14, coalesce(int, rep(NA_integer_, 4L), rep(3L, 4L)), int_val) -test(2055.15, coalesce(num, rep(3, 4L)), num_val) -test(2055.16, coalesce(num, rep(NA_real_, 4L), rep(3, 4L)), num_val) -test(2055.17, coalesce(str, rep('b', 4L)), str_val) -test(2055.18, coalesce(str, rep(NA_character_, 4L), rep('b', 4L)), str_val) -test(2055.19, coalesce(fkt, factor(rep('b', 4L), levels=c('a', 'b'))), fkt_val) -test(2055.20, coalesce(fkt, rep(NA_integer_, 4L), factor(rep('b', 4L), levels=c('a', 'b'))), fkt_val) - -## .dots argument -test(2055.21, coalesce(bool, .dots=list(NA, TRUE)), bool_val) - -## edge cases/checks -test(2055.22, coalesce(bool), bool) -test(2055.23, coalesce(fkt), fkt) -test(2055.24, coalesce(bool, 1L), error='internal type integer') +# singleton replacements +test(2055.001, coalesce(bool, TRUE), bool_val) +test(2055.002, coalesce(bool, NA, TRUE), bool_val) +test(2055.003, coalesce(int, 3L), int_val) +test(2055.004, coalesce(int, NA_integer_, 3L), int_val) +test(2055.005, coalesce(num, 3), num_val) +test(2055.006, coalesce(num, NA_real_, 3), num_val) +test(2055.007, coalesce(str, 'b'), str_val) +test(2055.008, coalesce(str, NA_character_, 'b'), str_val) +test(2055.009, coalesce(fkt, factor('b', levels = c('a', 'b'))), fkt_val) +test(2055.010, coalesce(fkt, NA_integer_, factor('b', levels = c('a', 'b'))), fkt_val) +# vector replacements +test(2055.011, coalesce(bool, rep(TRUE, 3L)), bool_val) +test(2055.012, coalesce(bool, rep(NA, 3L), rep(TRUE, 3L)), bool_val) +test(2055.013, coalesce(int, rep(3L, 4L)), int_val) +test(2055.014, coalesce(int, rep(NA_integer_, 4L), rep(3L, 4L)), int_val) +test(2055.015, coalesce(num, rep(3, 4L)), num_val) +test(2055.016, coalesce(num, rep(NA_real_, 4L), rep(3, 4L)), num_val) +test(2055.017, coalesce(str, rep('b', 4L)), str_val) +test(2055.018, coalesce(str, rep(NA_character_, 4L), rep('b', 4L)), str_val) +test(2055.019, coalesce(fkt, factor(rep('b', 4L), levels=c('a', 'b'))), fkt_val) +test(2055.020, coalesce(fkt, rep(NA_integer_, 4L), factor(rep('b', 4L), levels=c('a', 'b'))), fkt_val) +# .dots argument +test(2055.021, coalesce(bool, .dots=list(NA, TRUE)), bool_val) +# floating point extras +x = c(11L, NA, 13L, NA, 15L, NaN, NA, NA, NA)+0.1 +y = c(NA, 12L, 5L, NA, NA, 16L, NaN, Inf, NA)+0.1 +z = c(11L, NA, 1L, 14L, NA, 16L, 1L, 2L, NA)+0.1 +test(2055.041, coalesce(x, y, z), c(11:15,NaN,NaN,Inf,NA)+0.1) +# edge cases/checks +test(2055.051, coalesce(bool), bool) +test(2055.052, coalesce(fkt), fkt) +test(2055.053, coalesce(bool, 1L), error='internal type integer') #TODO should we error here, or convert any NA type to the right NA? -#test(2055.25, coalesce(bool, NA_integer_), error='internal type integer') +#test(2055.054, coalesce(bool, NA_integer_), error='internal type integer') #TODO we don't have any robustness around factor/integer as yet -#test(2055.26, coalesce(fkt, 1L), error='') -#test(2055.27, coalesce(num, 3L), error='internal type integer') +#test(2055.055, coalesce(fkt, 1L), error='') +#test(2055.056, coalesce(num, 3L), error='internal type integer') #TODO two more placeholder tests -- do we really want to error here? or worth to add isReallyReal-type tests? numeric->integer should always be doable right? -#test(2055.28, coalesce(int, 3), error='internal type double') -test(2055.29, coalesce(fkt, 'b'), error='internal type character') -test(2055.30, coalesce(str, factor('b')), error='internal type integer') -test(2055.31, coalesce(bool, TRUE, .dots=list(FALSE)), error="either '...' or '.dots'") -test(2055.32, coalesce(bool, .dots=TRUE), error="'.dots' requires a list") -test(2055.33, coalesce(list(1), list(2)), error="'x' must be an atomic vector") -test(2055.34, coalesce(bool, c(TRUE, FALSE)), error="Only singletons will be recycled") -test(2055.35, coalesce(as.raw(0), as.raw(1)), error="Incompatible type: raw") - -## verbose output +#test(2055.057, coalesce(int, 3), error='internal type double') +test(2055.058, coalesce(fkt, 'b'), error='internal type character') +test(2055.059, coalesce(str, factor('b')), error='internal type integer') +test(2055.060, coalesce(bool, TRUE, .dots=list(FALSE)), error="either '...' or '.dots'") +test(2055.061, coalesce(bool, .dots=TRUE), error="'.dots' requires a list") +test(2055.062, coalesce(list(1), list(2)), error="'x' must be an atomic vector") +test(2055.063, coalesce(bool, c(TRUE, FALSE)), error="Only singletons will be recycled") +test(2055.064, coalesce(as.raw(0), as.raw(1)), error="Incompatible type: raw") +test(2055.065, coalesce(bool, .dots=list()), bool) +# verbose output old = options(datatable.verbose=TRUE) -test(2055.36, coalesce(bool, TRUE), bool_val, output='which NA took.*duplicate.*took.*loop over NA indices of x took') -test(2055.37, coalesce(bool_val, TRUE), bool_val, output='no NAs in input, skip') +test(2055.101, coalesce(bool, TRUE), bool_val, output='which NA took.*duplicate.*took.*loop over NA indices of x took') +test(2055.102, coalesce(bool_val, TRUE), bool_val, output='no NAs in input, skip') options(old) - -## integer64 tests +# integer64 tests if (test_bit64) { int64 = as.integer64(int) int64_val = as.integer64(1:4) + test(2055.201, coalesce(int64, as.integer64(3)), int64_val) + test(2055.202, coalesce(int64, as.integer64(NA), as.integer64(3)), int64_val) + test(2055.203, coalesce(int64, as.integer64(rep(3, 4L))), int64_val) + #TODO this line current runs and gives a nonsense answer + #test(2055.204, coalesce(int64, 1), error='') + test(2055.205, coalesce(int64, 1L), error = 'internal type integer') +} - test(2055.38, coalesce(int64, as.integer64(3)), int64_val) - test(2055.39, coalesce(int64, as.integer64(NA), as.integer64(3)), int64_val) - test(2055.40, coalesce(int64, as.integer64(rep(3, 4L))), int64_val) +# setcoalesce +x = c(11L, NA, 13L, NA, 15L, NA) +y = c(NA, 12L, 5L, NA, NA, NA) +z = c(11L, NA, 1L, 14L, NA, NA) +xx = copy(x) +xx_addr = address(xx) +setcoalesce(xx, y, z) +test(2055.501, xx_addr, address(xx)) +test(2055.502, xx, c(11:15, NA)) +xx = copy(x) +xx_addr = address(xx) +setcoalesce(xx, .dots=list()) +test(2055.503, xx_addr, address(xx)) +test(2055.504, xx, x) +test(2055.505, address(setcoalesce(xx)), xx_addr) +test(2055.506, setcoalesce(xx, TRUE, .dots=list(FALSE)), error="either '...' or '.dots'") +test(2055.507, setcoalesce(xx, .dots=TRUE), error="'.dots' requires a list") - #TODO this line current runs and gives a nonsense answer - #test(2055.41, coalesce(int64, 1), error='') - test(2055.42, coalesce(int64, 1L), error = 'internal type integer') +# which_eq +test(2056.01, which_eq(c(TRUE, NA, FALSE, FALSE), FALSE, negate=TRUE), 1:2) +test(2056.02, which_eq(c(TRUE, NA, FALSE, FALSE), NA, negate=TRUE), c(1L,3L,4L)) +test(2056.03, which_eq(c(TRUE, TRUE), TRUE, negate=TRUE), integer()) +test(2056.04, which_eq(c(1L, NA, 5L), 5L, negate=TRUE), c(1L,2L)) +test(2056.05, which_eq(c(1L, NA, 5L), NA_integer_, negate=TRUE), c(1L,3L)) +test(2056.06, which_eq(c(1L, 1L), 1L, negate=TRUE), integer()) +test(2056.07, which_eq(c("a", NA, "r", "a"), "a", negate=TRUE), 2:3) +test(2056.08, which_eq(c("a", NA, "r", "a"), "r", negate=TRUE), c(1L,2L,4L)) +test(2056.09, which_eq(c("a","a"), "a", negate=TRUE), integer()) +test(2056.10, which_eq(c(1.1, NA, 3.1), NA_real_, negate=TRUE), c(1L,3L)) +test(2056.11, which_eq(c(NA_real_, NA_real_), NA_real_, negate=TRUE), integer()) +test(2056.12, which_eq(c(1.1, NaN, 3.1), NaN, negate=TRUE), c(1L,3L)) +test(2056.13, which_eq(c(NaN, NaN), NaN, negate=TRUE), integer()) +test(2056.14, which_eq(c(1.1, Inf, 3.1), Inf, negate=TRUE), c(1L,3L)) +test(2056.15, which_eq(c(Inf, Inf), Inf, negate=TRUE), integer()) +test(2056.16, which_eq(c(NA, 12L, 5L, NA, NA, 16L, NaN, Inf, NA)+0.1, NaN), 7L) +test(2056.17, which_eq(c(NA, 12L, 5L, NA, NA, 16L, NaN, Inf, NA)+0.1, Inf), 8L) +if (test_bit64) { + test(2056.51, which_eq(bit64::as.integer64(c(1L, NA, 5L)), bit64::as.integer64(5L), negate=TRUE), c(1L,2L)) + test(2056.52, which_eq(bit64::as.integer64(c(1L, NA, 5L)), bit64::as.integer64(NA_integer_), negate=TRUE), c(1L,3L)) + test(2056.53, which_eq(bit64::as.integer64(c(1L, 1L)), bit64::as.integer64(1L), negate=TRUE), integer()) } +test(2056.81, which_eq(list(1L,2L), 1L), error="argument 'x' must be atomic vector") +test(2056.82, which_eq(c(1:2), 1), error="argument 'value' must of the same type as argument 'x'") +test(2056.83, which_eq(c(1:2), 1L, negate=1L), error="argument 'negate' must be TRUE or FALSE") +test(2056.84, which_eq(as.raw(0), as.raw(1)), error="Incompatible type: raw") diff --git a/src/utils.c b/src/utils.c index 7f9ff31298..061154ced1 100644 --- a/src/utils.c +++ b/src/utils.c @@ -172,7 +172,7 @@ SEXP which_eqR(SEXP x, SEXP val, SEXP negate) { which_eq_char(x, nx, iwhich, &nwhich, STRING_ELT(val, 0), bnegate); } break; default: { - error("%s: Incompatible type", __func__); + error("%s: Incompatible type: %s", __func__, type2char(TYPEOF(x))); } } From 7116086b6d3d4a128bf2225aefd45596b29851d8 Mon Sep 17 00:00:00 2001 From: jangorecki Date: Mon, 3 Jun 2019 21:55:13 +0530 Subject: [PATCH 25/37] resolve pending unit tests related to classes and factor levels --- inst/tests/coalesce.Rraw | 23 ++++++++++++----------- src/coalesce.c | 17 +++++++++++++++-- src/data.table.h | 4 +++- src/utils.c | 25 ++++++++++++++++++++++++- 4 files changed, 54 insertions(+), 15 deletions(-) diff --git a/inst/tests/coalesce.Rraw b/inst/tests/coalesce.Rraw index e6ea09ea3b..e860a8f1b0 100644 --- a/inst/tests/coalesce.Rraw +++ b/inst/tests/coalesce.Rraw @@ -43,7 +43,7 @@ test(2055.006, coalesce(num, NA_real_, 3), num_val) test(2055.007, coalesce(str, 'b'), str_val) test(2055.008, coalesce(str, NA_character_, 'b'), str_val) test(2055.009, coalesce(fkt, factor('b', levels = c('a', 'b'))), fkt_val) -test(2055.010, coalesce(fkt, NA_integer_, factor('b', levels = c('a', 'b'))), fkt_val) +test(2055.010, coalesce(fkt, factor(NA_integer_, levels=c("a","b")), factor('b', levels = c('a', 'b'))), fkt_val) # vector replacements test(2055.011, coalesce(bool, rep(TRUE, 3L)), bool_val) test(2055.012, coalesce(bool, rep(NA, 3L), rep(TRUE, 3L)), bool_val) @@ -54,7 +54,7 @@ test(2055.016, coalesce(num, rep(NA_real_, 4L), rep(3, 4L)), num_val) test(2055.017, coalesce(str, rep('b', 4L)), str_val) test(2055.018, coalesce(str, rep(NA_character_, 4L), rep('b', 4L)), str_val) test(2055.019, coalesce(fkt, factor(rep('b', 4L), levels=c('a', 'b'))), fkt_val) -test(2055.020, coalesce(fkt, rep(NA_integer_, 4L), factor(rep('b', 4L), levels=c('a', 'b'))), fkt_val) +test(2055.020, coalesce(fkt, factor(rep(NA_integer_, 4L), levels=c("a","b")), factor(rep('b', 4L), levels=c('a', 'b'))), fkt_val) # .dots argument test(2055.021, coalesce(bool, .dots=list(NA, TRUE)), bool_val) # floating point extras @@ -62,17 +62,19 @@ x = c(11L, NA, 13L, NA, 15L, NaN, NA, NA, NA)+0.1 y = c(NA, 12L, 5L, NA, NA, 16L, NaN, Inf, NA)+0.1 z = c(11L, NA, 1L, 14L, NA, 16L, 1L, 2L, NA)+0.1 test(2055.041, coalesce(x, y, z), c(11:15,NaN,NaN,Inf,NA)+0.1) +# factor of different levels +x = factor(c('a','b',NA,NA,'b')) +y = factor(c('b','b','a',NA,'b')) +z = factor(c('a',NA,NA,'d','a')) +test(2055.042, coalesce(x, y, z), error='has different factor levels') # edge cases/checks test(2055.051, coalesce(bool), bool) test(2055.052, coalesce(fkt), fkt) test(2055.053, coalesce(bool, 1L), error='internal type integer') -#TODO should we error here, or convert any NA type to the right NA? -#test(2055.054, coalesce(bool, NA_integer_), error='internal type integer') -#TODO we don't have any robustness around factor/integer as yet -#test(2055.055, coalesce(fkt, 1L), error='') -#test(2055.056, coalesce(num, 3L), error='internal type integer') -#TODO two more placeholder tests -- do we really want to error here? or worth to add isReallyReal-type tests? numeric->integer should always be doable right? -#test(2055.057, coalesce(int, 3), error='internal type double') +test(2055.054, coalesce(bool, NA_integer_), error='internal type integer') +test(2055.055, coalesce(fkt, 1L), error='does not inherit factor class') +test(2055.056, coalesce(num, 3L), error='internal type integer') +test(2055.057, coalesce(int, 3), error='internal type double') test(2055.058, coalesce(fkt, 'b'), error='internal type character') test(2055.059, coalesce(str, factor('b')), error='internal type integer') test(2055.060, coalesce(bool, TRUE, .dots=list(FALSE)), error="either '...' or '.dots'") @@ -93,8 +95,7 @@ if (test_bit64) { test(2055.201, coalesce(int64, as.integer64(3)), int64_val) test(2055.202, coalesce(int64, as.integer64(NA), as.integer64(3)), int64_val) test(2055.203, coalesce(int64, as.integer64(rep(3, 4L))), int64_val) - #TODO this line current runs and gives a nonsense answer - #test(2055.204, coalesce(int64, 1), error='') + test(2055.204, coalesce(int64, 1), error='does not inherit interger64') test(2055.205, coalesce(int64, 1L), error = 'internal type integer') } diff --git a/src/coalesce.c b/src/coalesce.c index c3f06e16b9..000a1b8b02 100644 --- a/src/coalesce.c +++ b/src/coalesce.c @@ -8,12 +8,25 @@ SEXP coalesce(SEXP x, SEXP values, SEXP inplace) { if (!isTrueFalse(inplace)) error("%s: argument '.inplace' must be TRUE or FALSE", __func__); bool binplace = LOGICAL(inplace)[0]; - int firstLenMiss = lenMiss(values, nx, true); - if (firstLenMiss >= 0) error("%s: Replacement %d has length %d but length(x) is %d. Only singletons will be recycled.", __func__, firstLenMiss+1, length(VECTOR_ELT(values, firstLenMiss)), nx); + int firstLengthMiss = lengthMiss(values, nx, true); + if (firstLengthMiss >= 0) error("%s: Replacement %d has length %d but length(x) is %d. Only singletons will be recycled.", __func__, firstLengthMiss+1, length(VECTOR_ELT(values, firstLengthMiss)), nx); int firstTypeMiss = typeMiss(values, TYPEOF(x)); if (firstTypeMiss >= 0) error("%s: Replacement %d has internal type %s but typeof(x) is %s. Please coerce before coalescing", __func__, firstTypeMiss+1, type2char(TYPEOF(VECTOR_ELT(values, firstTypeMiss))), type2char(TYPEOF(x))); + if (INHERITS(x, char_integer64)) { + int firstClassMiss = classMiss(values, char_integer64); + if (firstClassMiss >= 0) error("%s: Replacement %d does not inherit interger64 class while 'x' do. Please align classes before coalescing", __func__, firstClassMiss+1); + } else if (INHERITS(x, char_Date)) { + int firstClassMiss = classMiss(values, char_Date); + if (firstClassMiss >= 0) error("%s: Replacement %d does not inherit Date class while 'x' do. Please align classes before coalescing", __func__, firstClassMiss+1); + } else if (INHERITS(x, char_factor)) { + int firstClassMiss = classMiss(values, char_factor); + if (firstClassMiss >= 0) error("%s: Replacement %d does not inherit factor class while 'x' do. Please align classes before coalescing", __func__, firstClassMiss+1); + int firstLevelsMiss = levelsMiss(values, getAttrib(x, R_LevelsSymbol)); + if (firstLevelsMiss >= 0) error("%s: Replacement %d has different factor levels than 'x'. Coerce to character or align factor levels before coalescing", __func__, firstLevelsMiss+1); + } + double tic = 0; const bool verbose = GetVerbose(); diff --git a/src/data.table.h b/src/data.table.h index c4732ae806..9095fa736d 100644 --- a/src/data.table.h +++ b/src/data.table.h @@ -203,8 +203,10 @@ SEXP between(SEXP x, SEXP lower, SEXP upper, SEXP bounds); // utils.c bool isTrueFalse(SEXP x); -int lenMiss(SEXP x, int n, bool scalar); +int lengthMiss(SEXP x, int n, bool scalar); int typeMiss(SEXP x, SEXPTYPE type); +int classMiss(SEXP x, SEXP char_class); +int levelsMiss(SEXP x, SEXP levels); void which_eq_int(int *x, int nx, int *out, int *nout, int val, bool negate); void which_eq_double(double *x, int nx, int *out, int *nout, double val, bool negate); void which_eq_char(SEXP x, int nx, int *out, int *nout, SEXP val, bool negate); diff --git a/src/utils.c b/src/utils.c index 061154ced1..266fcacb76 100644 --- a/src/utils.c +++ b/src/utils.c @@ -6,7 +6,7 @@ bool isTrueFalse(SEXP x) { return isLogical(x) && length(x)==1 && LOGICAL(x)[0]!=NA_LOGICAL; } -int lenMiss(SEXP x, int n, bool scalar) { +int lengthMiss(SEXP x, int n, bool scalar) { if (!isNewList(x)) error("x must be a list"); int nx = length(x); for (int i=0; i Date: Tue, 4 Jun 2019 15:39:47 +0530 Subject: [PATCH 26/37] check classMiss only for x class, add nanotime tests --- inst/tests/coalesce.Rraw | 102 +++++++++++++++++++++++++-------------- src/coalesce.c | 13 ++--- src/data.table.h | 1 + src/utils.c | 15 ++++++ 4 files changed, 86 insertions(+), 45 deletions(-) diff --git a/inst/tests/coalesce.Rraw b/inst/tests/coalesce.Rraw index e860a8f1b0..6b5527c309 100644 --- a/inst/tests/coalesce.Rraw +++ b/inst/tests/coalesce.Rraw @@ -15,7 +15,8 @@ if (exists("test.data.table", .GlobalEnv, inherits=FALSE)) { } sugg = c( - "bit64" + "bit64", + "nanotime" ) for (s in sugg) { assign(paste0("test_",s), loaded<-suppressWarnings(suppressMessages(require(s, character.only=TRUE)))) @@ -33,6 +34,12 @@ str = c('a', NA_character_, 'b', NA_character_) str_val = c('a', 'b', 'b', 'b') fkt = factor(str) fkt_val = factor(str_val) +date = as.Date(int, origin="1970-01-01") +date_val = as.Date(int_val, origin="1970-01-01") +idate = as.IDate(int, origin="1970-01-01") +idate_val = as.IDate(int_val, origin="1970-01-01") +itime = as.ITime(int) +itime_val = as.ITime(int_val) # singleton replacements test(2055.001, coalesce(bool, TRUE), bool_val) test(2055.002, coalesce(bool, NA, TRUE), bool_val) @@ -44,59 +51,82 @@ test(2055.007, coalesce(str, 'b'), str_val) test(2055.008, coalesce(str, NA_character_, 'b'), str_val) test(2055.009, coalesce(fkt, factor('b', levels = c('a', 'b'))), fkt_val) test(2055.010, coalesce(fkt, factor(NA_integer_, levels=c("a","b")), factor('b', levels = c('a', 'b'))), fkt_val) +test(2055.011, coalesce(date, as.Date("1970-01-04")), date_val) +test(2055.012, coalesce(date, as.Date(NA), as.Date("1970-01-04")), date_val) +test(2055.013, coalesce(idate, as.IDate("1970-01-04")), idate_val) +test(2055.014, coalesce(idate, as.IDate(NA), as.IDate("1970-01-04")), idate_val) +test(2055.015, coalesce(itime, as.ITime(3L)), itime_val) +test(2055.016, coalesce(itime, as.ITime(NA), as.ITime(3L)), itime_val) # vector replacements -test(2055.011, coalesce(bool, rep(TRUE, 3L)), bool_val) -test(2055.012, coalesce(bool, rep(NA, 3L), rep(TRUE, 3L)), bool_val) -test(2055.013, coalesce(int, rep(3L, 4L)), int_val) -test(2055.014, coalesce(int, rep(NA_integer_, 4L), rep(3L, 4L)), int_val) -test(2055.015, coalesce(num, rep(3, 4L)), num_val) -test(2055.016, coalesce(num, rep(NA_real_, 4L), rep(3, 4L)), num_val) -test(2055.017, coalesce(str, rep('b', 4L)), str_val) -test(2055.018, coalesce(str, rep(NA_character_, 4L), rep('b', 4L)), str_val) -test(2055.019, coalesce(fkt, factor(rep('b', 4L), levels=c('a', 'b'))), fkt_val) -test(2055.020, coalesce(fkt, factor(rep(NA_integer_, 4L), levels=c("a","b")), factor(rep('b', 4L), levels=c('a', 'b'))), fkt_val) +test(2055.051, coalesce(bool, rep(TRUE, 3L)), bool_val) +test(2055.052, coalesce(bool, rep(NA, 3L), rep(TRUE, 3L)), bool_val) +test(2055.053, coalesce(int, rep(3L, 4L)), int_val) +test(2055.054, coalesce(int, rep(NA_integer_, 4L), rep(3L, 4L)), int_val) +test(2055.055, coalesce(num, rep(3, 4L)), num_val) +test(2055.056, coalesce(num, rep(NA_real_, 4L), rep(3, 4L)), num_val) +test(2055.057, coalesce(str, rep('b', 4L)), str_val) +test(2055.058, coalesce(str, rep(NA_character_, 4L), rep('b', 4L)), str_val) +test(2055.059, coalesce(fkt, factor(rep('b', 4L), levels=c('a', 'b'))), fkt_val) +test(2055.060, coalesce(fkt, factor(rep(NA_integer_, 4L), levels=c("a","b")), factor(rep('b', 4L), levels=c('a', 'b'))), fkt_val) +test(2055.061, coalesce(date, rep(as.Date("1970-01-04"), 4L)), date_val) +test(2055.062, coalesce(date, rep(as.Date(NA), 4L), rep(as.Date("1970-01-04"), 4L)), date_val) +test(2055.063, coalesce(idate, rep(as.IDate("1970-01-04"), 4L)), idate_val) +test(2055.064, coalesce(idate, rep(as.IDate(NA), 4L), rep(as.IDate("1970-01-04"), 4L)), idate_val) +test(2055.065, coalesce(itime, rep(as.ITime(3L), 4L)), itime_val) +test(2055.066, coalesce(itime, rep(as.ITime(NA), 4L), rep(as.ITime(3L), 4L)), itime_val) # .dots argument -test(2055.021, coalesce(bool, .dots=list(NA, TRUE)), bool_val) +test(2055.101, coalesce(bool, .dots=list(NA, TRUE)), bool_val) # floating point extras x = c(11L, NA, 13L, NA, 15L, NaN, NA, NA, NA)+0.1 y = c(NA, 12L, 5L, NA, NA, 16L, NaN, Inf, NA)+0.1 z = c(11L, NA, 1L, 14L, NA, 16L, 1L, 2L, NA)+0.1 -test(2055.041, coalesce(x, y, z), c(11:15,NaN,NaN,Inf,NA)+0.1) +test(2055.151, coalesce(x, y, z), c(11:15,NaN,NaN,Inf,NA)+0.1) # factor of different levels x = factor(c('a','b',NA,NA,'b')) y = factor(c('b','b','a',NA,'b')) z = factor(c('a',NA,NA,'d','a')) -test(2055.042, coalesce(x, y, z), error='has different factor levels') +test(2055.152, coalesce(x, y, z), error='has different factor levels') # edge cases/checks -test(2055.051, coalesce(bool), bool) -test(2055.052, coalesce(fkt), fkt) -test(2055.053, coalesce(bool, 1L), error='internal type integer') -test(2055.054, coalesce(bool, NA_integer_), error='internal type integer') -test(2055.055, coalesce(fkt, 1L), error='does not inherit factor class') -test(2055.056, coalesce(num, 3L), error='internal type integer') -test(2055.057, coalesce(int, 3), error='internal type double') -test(2055.058, coalesce(fkt, 'b'), error='internal type character') -test(2055.059, coalesce(str, factor('b')), error='internal type integer') -test(2055.060, coalesce(bool, TRUE, .dots=list(FALSE)), error="either '...' or '.dots'") -test(2055.061, coalesce(bool, .dots=TRUE), error="'.dots' requires a list") -test(2055.062, coalesce(list(1), list(2)), error="'x' must be an atomic vector") -test(2055.063, coalesce(bool, c(TRUE, FALSE)), error="Only singletons will be recycled") -test(2055.064, coalesce(as.raw(0), as.raw(1)), error="Incompatible type: raw") -test(2055.065, coalesce(bool, .dots=list()), bool) +test(2055.201, coalesce(bool), bool) +test(2055.202, coalesce(fkt), fkt) +test(2055.203, coalesce(bool, 1L), error='internal type integer') +test(2055.204, coalesce(bool, NA_integer_), error='internal type integer') +test(2055.205, coalesce(fkt, 1L), error='does not inherit factor class') +test(2055.206, coalesce(num, 3L), error='internal type integer') +test(2055.207, coalesce(int, 3), error='internal type double') +test(2055.208, coalesce(fkt, 'b'), error='internal type character') +test(2055.209, coalesce(str, factor('b')), error='internal type integer') +test(2055.210, coalesce(bool, TRUE, .dots=list(FALSE)), error="either '...' or '.dots'") +test(2055.211, coalesce(bool, .dots=TRUE), error="'.dots' requires a list") +test(2055.212, coalesce(list(1), list(2)), error="'x' must be an atomic vector") +test(2055.213, coalesce(bool, c(TRUE, FALSE)), error="Only singletons will be recycled") +test(2055.214, coalesce(as.raw(0), as.raw(1)), error="Incompatible type: raw") +test(2055.215, coalesce(bool, .dots=list()), bool) # verbose output old = options(datatable.verbose=TRUE) -test(2055.101, coalesce(bool, TRUE), bool_val, output='which NA took.*duplicate.*took.*loop over NA indices of x took') -test(2055.102, coalesce(bool_val, TRUE), bool_val, output='no NAs in input, skip') +test(2055.251, coalesce(bool, TRUE), bool_val, output='which NA took.*duplicate.*took.*loop over NA indices of x took') +test(2055.252, coalesce(bool_val, TRUE), bool_val, output='no NAs in input, skip') options(old) # integer64 tests if (test_bit64) { int64 = as.integer64(int) int64_val = as.integer64(1:4) - test(2055.201, coalesce(int64, as.integer64(3)), int64_val) - test(2055.202, coalesce(int64, as.integer64(NA), as.integer64(3)), int64_val) - test(2055.203, coalesce(int64, as.integer64(rep(3, 4L))), int64_val) - test(2055.204, coalesce(int64, 1), error='does not inherit interger64') - test(2055.205, coalesce(int64, 1L), error = 'internal type integer') + test(2055.301, coalesce(int64, as.integer64(3)), int64_val) + test(2055.302, coalesce(int64, as.integer64(NA), as.integer64(3)), int64_val) + test(2055.303, coalesce(int64, as.integer64(rep(3, 4L))), int64_val) + test(2055.304, coalesce(int64, 1), error='does not inherit integer64') + test(2055.305, coalesce(int64, 1L), error = 'internal type integer') +} +# nanotime tests +if (test_nanotime) { + nt = nanotime(int) + nt_val = nanotime(1:4) + #TODO test fail to properly test: eddelbuettel/nanotime#46 + test(2055.401, coalesce(nt, nanotime(3L)), nt_val) + test(2055.402, coalesce(nt, nanotime(NA), nanotime(3L)), nt_val) + test(2055.403, coalesce(nt, nanotime(rep(3, 4L))), nt_val) + test(2055.404, coalesce(nt, 1), error='does not inherit nanotime') + test(2055.405, coalesce(nt, 1L), error = 'internal type integer') } # setcoalesce diff --git a/src/coalesce.c b/src/coalesce.c index 000a1b8b02..49ae85321f 100644 --- a/src/coalesce.c +++ b/src/coalesce.c @@ -14,15 +14,10 @@ SEXP coalesce(SEXP x, SEXP values, SEXP inplace) { int firstTypeMiss = typeMiss(values, TYPEOF(x)); if (firstTypeMiss >= 0) error("%s: Replacement %d has internal type %s but typeof(x) is %s. Please coerce before coalescing", __func__, firstTypeMiss+1, type2char(TYPEOF(VECTOR_ELT(values, firstTypeMiss))), type2char(TYPEOF(x))); - if (INHERITS(x, char_integer64)) { - int firstClassMiss = classMiss(values, char_integer64); - if (firstClassMiss >= 0) error("%s: Replacement %d does not inherit interger64 class while 'x' do. Please align classes before coalescing", __func__, firstClassMiss+1); - } else if (INHERITS(x, char_Date)) { - int firstClassMiss = classMiss(values, char_Date); - if (firstClassMiss >= 0) error("%s: Replacement %d does not inherit Date class while 'x' do. Please align classes before coalescing", __func__, firstClassMiss+1); - } else if (INHERITS(x, char_factor)) { - int firstClassMiss = classMiss(values, char_factor); - if (firstClassMiss >= 0) error("%s: Replacement %d does not inherit factor class while 'x' do. Please align classes before coalescing", __func__, firstClassMiss+1); + SEXP x_class = findClass(x); + int firstClassMiss = classMiss(values, x_class); + if (firstClassMiss >= 0) error("%s: Replacement %d does not inherit %s class while 'x' do. Please align classes before coalescing", __func__, firstClassMiss+1, CHAR(x_class)); + if (x_class == char_factor) { int firstLevelsMiss = levelsMiss(values, getAttrib(x, R_LevelsSymbol)); if (firstLevelsMiss >= 0) error("%s: Replacement %d has different factor levels than 'x'. Coerce to character or align factor levels before coalescing", __func__, firstLevelsMiss+1); } diff --git a/src/data.table.h b/src/data.table.h index 9095fa736d..b38c537af7 100644 --- a/src/data.table.h +++ b/src/data.table.h @@ -205,6 +205,7 @@ SEXP between(SEXP x, SEXP lower, SEXP upper, SEXP bounds); bool isTrueFalse(SEXP x); int lengthMiss(SEXP x, int n, bool scalar); int typeMiss(SEXP x, SEXPTYPE type); +SEXP findClass(SEXP x); int classMiss(SEXP x, SEXP char_class); int levelsMiss(SEXP x, SEXP levels); void which_eq_int(int *x, int nx, int *out, int *nout, int val, bool negate); diff --git a/src/utils.c b/src/utils.c index 266fcacb76..0ba9f5882d 100644 --- a/src/utils.c +++ b/src/utils.c @@ -24,8 +24,23 @@ int typeMiss(SEXP x, SEXPTYPE type) { } return -1; } +SEXP findClass(SEXP x) { + SEXP vclass = getAttrib(x, R_ClassSymbol); + if (!isString(vclass)) return R_NilValue; + for (int i=0; i Date: Tue, 4 Jun 2019 18:01:35 +0530 Subject: [PATCH 27/37] update manual --- man/coalesce.Rd | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/man/coalesce.Rd b/man/coalesce.Rd index b9a449a6e6..549f757263 100644 --- a/man/coalesce.Rd +++ b/man/coalesce.Rd @@ -3,18 +3,18 @@ \alias{setcoalesce} \title{ Coalescing missing values } \description{ -Fill in missing values in a vector by successively pulling from candidate vectors in order (\emph{a la} the ANSI SQL function of the same name). +Fill in missing values in a vector by successively pulling from candidate vectors in order (\emph{a la} the ANSI SQL function). } \usage{ coalesce(x, \dots, .dots=NULL) } \arguments{ - \item{x}{ A vector of values } - \item{\dots}{ Vectors to be used successively as replacements for missing values of \code{x}; singletons are also accepted. } - \item{.dots}{ Alternative interface to specify replacements by supplying a list directly. } + \item{x}{ An atomic vector of values. } + \item{\dots}{ Vectors of same class as \code{x} to be used successively as replacements for missing values of \code{x}; singletons are also accepted. } + \item{.dots}{ List of vectors, an alternative interface to specify \dots argument. } } \details{ -Factor type is not yet supported. +Factor type is supported only when factor levels are equal. } \value{ Atomic vector of same type as \code{x} provided on input, having \code{NA} values replaced by corresponding non-\code{NA} values of arguments provided to \dots. @@ -26,5 +26,3 @@ z = c(11L, NA, 1L, 14L, NA, NA) coalesce(x, y, z) } \keyword{ data } - - From a7054f7d229a0d373158175eb4b36ff124aacd0f Mon Sep 17 00:00:00 2001 From: jangorecki Date: Tue, 4 Jun 2019 18:54:43 +0530 Subject: [PATCH 28/37] test coverage and posixct tests --- inst/tests/coalesce.Rraw | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/inst/tests/coalesce.Rraw b/inst/tests/coalesce.Rraw index 6b5527c309..abf4909353 100644 --- a/inst/tests/coalesce.Rraw +++ b/inst/tests/coalesce.Rraw @@ -40,6 +40,8 @@ idate = as.IDate(int, origin="1970-01-01") idate_val = as.IDate(int_val, origin="1970-01-01") itime = as.ITime(int) itime_val = as.ITime(int_val) +posix = as.POSIXct(int, origin="1970-01-01") +posix_val = as.POSIXct(int_val, origin="1970-01-01") # singleton replacements test(2055.001, coalesce(bool, TRUE), bool_val) test(2055.002, coalesce(bool, NA, TRUE), bool_val) @@ -57,6 +59,8 @@ test(2055.013, coalesce(idate, as.IDate("1970-01-04")), idate_val) test(2055.014, coalesce(idate, as.IDate(NA), as.IDate("1970-01-04")), idate_val) test(2055.015, coalesce(itime, as.ITime(3L)), itime_val) test(2055.016, coalesce(itime, as.ITime(NA), as.ITime(3L)), itime_val) +test(2055.017, coalesce(posix, as.POSIXct(3L, origin="1970-01-01")), posix_val) +test(2055.018, coalesce(posix, as.POSIXct(NA_integer_, origin="1970-01-01"), as.POSIXct(3L, origin="1970-01-01")), posix_val) # vector replacements test(2055.051, coalesce(bool, rep(TRUE, 3L)), bool_val) test(2055.052, coalesce(bool, rep(NA, 3L), rep(TRUE, 3L)), bool_val) @@ -74,6 +78,8 @@ test(2055.063, coalesce(idate, rep(as.IDate("1970-01-04"), 4L)), idate_val) test(2055.064, coalesce(idate, rep(as.IDate(NA), 4L), rep(as.IDate("1970-01-04"), 4L)), idate_val) test(2055.065, coalesce(itime, rep(as.ITime(3L), 4L)), itime_val) test(2055.066, coalesce(itime, rep(as.ITime(NA), 4L), rep(as.ITime(3L), 4L)), itime_val) +test(2055.067, coalesce(posix, as.POSIXct(rep(3L, 4L), origin="1970-01-01")), posix_val) +test(2055.068, coalesce(posix, as.POSIXct(rep(NA_integer_, 4L), origin="1970-01-01"), as.POSIXct(rep(3L, 4L), origin="1970-01-01")), posix_val) # .dots argument test(2055.101, coalesce(bool, .dots=list(NA, TRUE)), bool_val) # floating point extras @@ -102,6 +108,7 @@ test(2055.212, coalesce(list(1), list(2)), error="'x' must be an atomic vector") test(2055.213, coalesce(bool, c(TRUE, FALSE)), error="Only singletons will be recycled") test(2055.214, coalesce(as.raw(0), as.raw(1)), error="Incompatible type: raw") test(2055.215, coalesce(bool, .dots=list()), bool) +test(2055.216, coalesce(structure(c(1:2,NA,4L), class=c("a")), c(NA,NA,3L,4L)), structure(1:4, class="a")) # verbose output old = options(datatable.verbose=TRUE) test(2055.251, coalesce(bool, TRUE), bool_val, output='which NA took.*duplicate.*took.*loop over NA indices of x took') @@ -111,9 +118,9 @@ options(old) if (test_bit64) { int64 = as.integer64(int) int64_val = as.integer64(1:4) - test(2055.301, coalesce(int64, as.integer64(3)), int64_val) - test(2055.302, coalesce(int64, as.integer64(NA), as.integer64(3)), int64_val) - test(2055.303, coalesce(int64, as.integer64(rep(3, 4L))), int64_val) + test(2055.301, as.character(coalesce(int64, as.integer64(3))), as.character(int64_val)) # why as.character see nanotime tests below + test(2055.302, as.character(coalesce(int64, as.integer64(NA), as.integer64(3))), as.character(int64_val)) + test(2055.303, as.character(coalesce(int64, as.integer64(rep(3, 4L)))), as.character(int64_val)) test(2055.304, coalesce(int64, 1), error='does not inherit integer64') test(2055.305, coalesce(int64, 1L), error = 'internal type integer') } @@ -121,12 +128,11 @@ if (test_bit64) { if (test_nanotime) { nt = nanotime(int) nt_val = nanotime(1:4) - #TODO test fail to properly test: eddelbuettel/nanotime#46 - test(2055.401, coalesce(nt, nanotime(3L)), nt_val) - test(2055.402, coalesce(nt, nanotime(NA), nanotime(3L)), nt_val) - test(2055.403, coalesce(nt, nanotime(rep(3, 4L))), nt_val) - test(2055.404, coalesce(nt, 1), error='does not inherit nanotime') - test(2055.405, coalesce(nt, 1L), error = 'internal type integer') + #test(2055.401, as.character(coalesce(nt, nanotime(3L))), as.character(nt_val)) # aas.character due to eddelbuettel/nanotime#46 + #test(2055.402, as.character(coalesce(nt, nanotime(NA), nanotime(3L))), as.character(nt_val)) + #test(2055.403, as.character(coalesce(nt, nanotime(rep(3, 4L)))), as.character(nt_val)) + #test(2055.404, coalesce(nt, 1), error='does not inherit nanotime') + #test(2055.405, coalesce(nt, 1L), error = 'internal type integer') } # setcoalesce From 149906049a419e3312aadc27edb8dfbf3ae17b4e Mon Sep 17 00:00:00 2001 From: jangorecki Date: Wed, 5 Jun 2019 10:22:07 +0530 Subject: [PATCH 29/37] handle nanotime class in coalesce, explained in eddelbuettel/nanotime#48 --- inst/tests/coalesce.Rraw | 10 +++++----- src/coalesce.c | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/inst/tests/coalesce.Rraw b/inst/tests/coalesce.Rraw index abf4909353..622a4c20d3 100644 --- a/inst/tests/coalesce.Rraw +++ b/inst/tests/coalesce.Rraw @@ -128,11 +128,11 @@ if (test_bit64) { if (test_nanotime) { nt = nanotime(int) nt_val = nanotime(1:4) - #test(2055.401, as.character(coalesce(nt, nanotime(3L))), as.character(nt_val)) # aas.character due to eddelbuettel/nanotime#46 - #test(2055.402, as.character(coalesce(nt, nanotime(NA), nanotime(3L))), as.character(nt_val)) - #test(2055.403, as.character(coalesce(nt, nanotime(rep(3, 4L)))), as.character(nt_val)) - #test(2055.404, coalesce(nt, 1), error='does not inherit nanotime') - #test(2055.405, coalesce(nt, 1L), error = 'internal type integer') + test(2055.401, as.character(coalesce(nt, nanotime(3L))), as.character(nt_val)) # aas.character due to eddelbuettel/nanotime#46 + test(2055.402, as.character(coalesce(nt, nanotime(NA), nanotime(3L))), as.character(nt_val)) + test(2055.403, as.character(coalesce(nt, nanotime(rep(3, 4L)))), as.character(nt_val)) + test(2055.404, coalesce(nt, 1), error='does not inherit nanotime') + test(2055.405, coalesce(nt, 1L), error = 'internal type integer') } # setcoalesce diff --git a/src/coalesce.c b/src/coalesce.c index 49ae85321f..b14a775f21 100644 --- a/src/coalesce.c +++ b/src/coalesce.c @@ -38,7 +38,7 @@ SEXP coalesce(SEXP x, SEXP values, SEXP inplace) { which_eq_int(INTEGER(x), nx, iwhich, &nwhich, NA_INTEGER, false); } break; case REALSXP: { - if (inherits(x,"integer64")) { + if (INHERITS(x,char_integer64) || INHERITS(x,char_nanotime)) { which_eq_int64((int64_t *)REAL(x), nx, iwhich, &nwhich, NA_INTEGER64, false); } else { which_eq_double(REAL(x), nx, iwhich, &nwhich, NA_REAL, false); @@ -100,7 +100,7 @@ SEXP coalesce(SEXP x, SEXP values, SEXP inplace) { } } break; case REALSXP: { - if (inherits(x,"integer64")) { // integer64 + if (INHERITS(x,char_integer64) || INHERITS(x,char_nanotime)) { // integer64 and nanotime int64_t *out_ptr = (int64_t *)REAL(out); for (int i=0; i Date: Wed, 5 Jun 2019 10:23:44 +0530 Subject: [PATCH 30/37] Hugh minor feedback to manual --- man/coalesce.Rd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/man/coalesce.Rd b/man/coalesce.Rd index 549f757263..0079641a21 100644 --- a/man/coalesce.Rd +++ b/man/coalesce.Rd @@ -9,7 +9,7 @@ Fill in missing values in a vector by successively pulling from candidate vector coalesce(x, \dots, .dots=NULL) } \arguments{ - \item{x}{ An atomic vector of values. } + \item{x}{ An atomic vector. } \item{\dots}{ Vectors of same class as \code{x} to be used successively as replacements for missing values of \code{x}; singletons are also accepted. } \item{.dots}{ List of vectors, an alternative interface to specify \dots argument. } } From b9265841edecc6d8d3d0e5428f0bc622a147ba16 Mon Sep 17 00:00:00 2001 From: jangorecki Date: Wed, 5 Jun 2019 10:29:14 +0530 Subject: [PATCH 31/37] which_eq nanotime support --- inst/tests/coalesce.Rraw | 13 +++++++++---- src/utils.c | 2 +- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/inst/tests/coalesce.Rraw b/inst/tests/coalesce.Rraw index 622a4c20d3..09bf3cc752 100644 --- a/inst/tests/coalesce.Rraw +++ b/inst/tests/coalesce.Rraw @@ -128,7 +128,7 @@ if (test_bit64) { if (test_nanotime) { nt = nanotime(int) nt_val = nanotime(1:4) - test(2055.401, as.character(coalesce(nt, nanotime(3L))), as.character(nt_val)) # aas.character due to eddelbuettel/nanotime#46 + test(2055.401, as.character(coalesce(nt, nanotime(3L))), as.character(nt_val)) # as.character due to eddelbuettel/nanotime#46 test(2055.402, as.character(coalesce(nt, nanotime(NA), nanotime(3L))), as.character(nt_val)) test(2055.403, as.character(coalesce(nt, nanotime(rep(3, 4L)))), as.character(nt_val)) test(2055.404, coalesce(nt, 1), error='does not inherit nanotime') @@ -172,9 +172,14 @@ test(2056.15, which_eq(c(Inf, Inf), Inf, negate=TRUE), integer()) test(2056.16, which_eq(c(NA, 12L, 5L, NA, NA, 16L, NaN, Inf, NA)+0.1, NaN), 7L) test(2056.17, which_eq(c(NA, 12L, 5L, NA, NA, 16L, NaN, Inf, NA)+0.1, Inf), 8L) if (test_bit64) { - test(2056.51, which_eq(bit64::as.integer64(c(1L, NA, 5L)), bit64::as.integer64(5L), negate=TRUE), c(1L,2L)) - test(2056.52, which_eq(bit64::as.integer64(c(1L, NA, 5L)), bit64::as.integer64(NA_integer_), negate=TRUE), c(1L,3L)) - test(2056.53, which_eq(bit64::as.integer64(c(1L, 1L)), bit64::as.integer64(1L), negate=TRUE), integer()) + test(2056.51, which_eq(as.integer64(c(1L, NA, 5L)), as.integer64(5L), negate=TRUE), c(1L,2L)) + test(2056.52, which_eq(as.integer64(c(1L, NA, 5L)), as.integer64(NA_integer_), negate=TRUE), c(1L,3L)) + test(2056.53, which_eq(as.integer64(c(1L, 1L)), as.integer64(1L), negate=TRUE), integer()) +} +if (test_nanotime) { + test(2056.61, which_eq(nanotime(c(1L, NA, 5L)), nanotime(5L), negate=TRUE), c(1L,2L)) + test(2056.62, which_eq(nanotime(c(1L, NA, 5L)), nanotime(NA_integer_), negate=TRUE), c(1L,3L)) + test(2056.63, which_eq(nanotime(c(1L, 1L)), nanotime(1L), negate=TRUE), integer()) } test(2056.81, which_eq(list(1L,2L), 1L), error="argument 'x' must be atomic vector") test(2056.82, which_eq(c(1:2), 1), error="argument 'value' must of the same type as argument 'x'") diff --git a/src/utils.c b/src/utils.c index 0ba9f5882d..fd8e453747 100644 --- a/src/utils.c +++ b/src/utils.c @@ -200,7 +200,7 @@ SEXP which_eqR(SEXP x, SEXP val, SEXP negate) { which_eq_int(INTEGER(x), nx, iwhich, &nwhich, INTEGER(val)[0], bnegate); } break; case REALSXP: { - if (inherits(x,"integer64")) { + if (INHERITS(x,char_integer64) || INHERITS(x,char_nanotime)) { which_eq_int64((int64_t *)REAL(x), nx, iwhich, &nwhich, ((int64_t *)REAL(val))[0], bnegate); } else { which_eq_double(REAL(x), nx, iwhich, &nwhich, REAL(val)[0], bnegate); From 9c386ef59155dccd4522fc7117d8996a48f874f3 Mon Sep 17 00:00:00 2001 From: mattdowle Date: Mon, 17 Jun 2019 18:59:00 -0700 Subject: [PATCH 32/37] major simplification (rewrite) and parallelized. (Was +641 -2 lines) --- R/coalesce.R | 5 +- inst/tests/coalesce.Rraw | 187 --------------------------------- inst/tests/tests.Rraw | 129 +++++++++++++++++++++++ src/coalesce.c | 193 +++++++++++----------------------- src/data.table.h | 15 +-- src/init.c | 1 - src/utils.c | 222 --------------------------------------- tests/coalesce.R | 2 - 8 files changed, 197 insertions(+), 557 deletions(-) delete mode 100644 inst/tests/coalesce.Rraw delete mode 100644 src/utils.c delete mode 100644 tests/coalesce.R diff --git a/R/coalesce.R b/R/coalesce.R index da05b078cb..acb1296a70 100644 --- a/R/coalesce.R +++ b/R/coalesce.R @@ -11,6 +11,7 @@ coalesce = function(x, ..., .dots=NULL) { .Call(Ccoalesce, x, values, FALSE) } + setcoalesce = function(x, ..., .dots=NULL) { if (is.null(.dots) && missing(..1)) return(x) @@ -24,6 +25,4 @@ setcoalesce = function(x, ..., .dots=NULL) { invisible(.Call(Ccoalesce, x, values, TRUE)) } -which_eq = function(x, value, negate=FALSE) { - .Call(Cwhich_eqR, x, value, negate) -} + diff --git a/inst/tests/coalesce.Rraw b/inst/tests/coalesce.Rraw deleted file mode 100644 index 09bf3cc752..0000000000 --- a/inst/tests/coalesce.Rraw +++ /dev/null @@ -1,187 +0,0 @@ -require(methods) - -if (exists("test.data.table", .GlobalEnv, inherits=FALSE)) { - if (!identical(suppressWarnings(packageDescription("data.table")), NA)) { - remove.packages("data.table") - stop("This is dev mode but data.table was installed. Uninstalled it. Please q() this R session and try cc() again. The installed namespace causes problems in dev mode for the S4 tests.\n") - } - if ((tt<-compiler::enableJIT(-1))>0) - cat("This is dev mode and JIT is enabled (level ", tt, ") so there will be a brief pause around the first test.\n", sep="") -} else { - require(data.table) - test = data.table:::test - setcoalesce = data.table:::setcoalesce - which_eq = data.table:::which_eq -} - -sugg = c( - "bit64", - "nanotime" -) -for (s in sugg) { - assign(paste0("test_",s), loaded<-suppressWarnings(suppressMessages(require(s, character.only=TRUE)))) - if (!loaded) cat("\n**** Suggested package",s,"is not installed. Tests using it will be skipped.\n\n") -} - -# coalesce #3424 -bool = c(TRUE, NA, FALSE) -bool_val = c(TRUE, TRUE, FALSE) -int = c(1L, 2L, NA_integer_, 4L) -int_val = 1:4 -num = c(1, 2, NA_real_, 4) -num_val = c(1, 2, 3, 4) -str = c('a', NA_character_, 'b', NA_character_) -str_val = c('a', 'b', 'b', 'b') -fkt = factor(str) -fkt_val = factor(str_val) -date = as.Date(int, origin="1970-01-01") -date_val = as.Date(int_val, origin="1970-01-01") -idate = as.IDate(int, origin="1970-01-01") -idate_val = as.IDate(int_val, origin="1970-01-01") -itime = as.ITime(int) -itime_val = as.ITime(int_val) -posix = as.POSIXct(int, origin="1970-01-01") -posix_val = as.POSIXct(int_val, origin="1970-01-01") -# singleton replacements -test(2055.001, coalesce(bool, TRUE), bool_val) -test(2055.002, coalesce(bool, NA, TRUE), bool_val) -test(2055.003, coalesce(int, 3L), int_val) -test(2055.004, coalesce(int, NA_integer_, 3L), int_val) -test(2055.005, coalesce(num, 3), num_val) -test(2055.006, coalesce(num, NA_real_, 3), num_val) -test(2055.007, coalesce(str, 'b'), str_val) -test(2055.008, coalesce(str, NA_character_, 'b'), str_val) -test(2055.009, coalesce(fkt, factor('b', levels = c('a', 'b'))), fkt_val) -test(2055.010, coalesce(fkt, factor(NA_integer_, levels=c("a","b")), factor('b', levels = c('a', 'b'))), fkt_val) -test(2055.011, coalesce(date, as.Date("1970-01-04")), date_val) -test(2055.012, coalesce(date, as.Date(NA), as.Date("1970-01-04")), date_val) -test(2055.013, coalesce(idate, as.IDate("1970-01-04")), idate_val) -test(2055.014, coalesce(idate, as.IDate(NA), as.IDate("1970-01-04")), idate_val) -test(2055.015, coalesce(itime, as.ITime(3L)), itime_val) -test(2055.016, coalesce(itime, as.ITime(NA), as.ITime(3L)), itime_val) -test(2055.017, coalesce(posix, as.POSIXct(3L, origin="1970-01-01")), posix_val) -test(2055.018, coalesce(posix, as.POSIXct(NA_integer_, origin="1970-01-01"), as.POSIXct(3L, origin="1970-01-01")), posix_val) -# vector replacements -test(2055.051, coalesce(bool, rep(TRUE, 3L)), bool_val) -test(2055.052, coalesce(bool, rep(NA, 3L), rep(TRUE, 3L)), bool_val) -test(2055.053, coalesce(int, rep(3L, 4L)), int_val) -test(2055.054, coalesce(int, rep(NA_integer_, 4L), rep(3L, 4L)), int_val) -test(2055.055, coalesce(num, rep(3, 4L)), num_val) -test(2055.056, coalesce(num, rep(NA_real_, 4L), rep(3, 4L)), num_val) -test(2055.057, coalesce(str, rep('b', 4L)), str_val) -test(2055.058, coalesce(str, rep(NA_character_, 4L), rep('b', 4L)), str_val) -test(2055.059, coalesce(fkt, factor(rep('b', 4L), levels=c('a', 'b'))), fkt_val) -test(2055.060, coalesce(fkt, factor(rep(NA_integer_, 4L), levels=c("a","b")), factor(rep('b', 4L), levels=c('a', 'b'))), fkt_val) -test(2055.061, coalesce(date, rep(as.Date("1970-01-04"), 4L)), date_val) -test(2055.062, coalesce(date, rep(as.Date(NA), 4L), rep(as.Date("1970-01-04"), 4L)), date_val) -test(2055.063, coalesce(idate, rep(as.IDate("1970-01-04"), 4L)), idate_val) -test(2055.064, coalesce(idate, rep(as.IDate(NA), 4L), rep(as.IDate("1970-01-04"), 4L)), idate_val) -test(2055.065, coalesce(itime, rep(as.ITime(3L), 4L)), itime_val) -test(2055.066, coalesce(itime, rep(as.ITime(NA), 4L), rep(as.ITime(3L), 4L)), itime_val) -test(2055.067, coalesce(posix, as.POSIXct(rep(3L, 4L), origin="1970-01-01")), posix_val) -test(2055.068, coalesce(posix, as.POSIXct(rep(NA_integer_, 4L), origin="1970-01-01"), as.POSIXct(rep(3L, 4L), origin="1970-01-01")), posix_val) -# .dots argument -test(2055.101, coalesce(bool, .dots=list(NA, TRUE)), bool_val) -# floating point extras -x = c(11L, NA, 13L, NA, 15L, NaN, NA, NA, NA)+0.1 -y = c(NA, 12L, 5L, NA, NA, 16L, NaN, Inf, NA)+0.1 -z = c(11L, NA, 1L, 14L, NA, 16L, 1L, 2L, NA)+0.1 -test(2055.151, coalesce(x, y, z), c(11:15,NaN,NaN,Inf,NA)+0.1) -# factor of different levels -x = factor(c('a','b',NA,NA,'b')) -y = factor(c('b','b','a',NA,'b')) -z = factor(c('a',NA,NA,'d','a')) -test(2055.152, coalesce(x, y, z), error='has different factor levels') -# edge cases/checks -test(2055.201, coalesce(bool), bool) -test(2055.202, coalesce(fkt), fkt) -test(2055.203, coalesce(bool, 1L), error='internal type integer') -test(2055.204, coalesce(bool, NA_integer_), error='internal type integer') -test(2055.205, coalesce(fkt, 1L), error='does not inherit factor class') -test(2055.206, coalesce(num, 3L), error='internal type integer') -test(2055.207, coalesce(int, 3), error='internal type double') -test(2055.208, coalesce(fkt, 'b'), error='internal type character') -test(2055.209, coalesce(str, factor('b')), error='internal type integer') -test(2055.210, coalesce(bool, TRUE, .dots=list(FALSE)), error="either '...' or '.dots'") -test(2055.211, coalesce(bool, .dots=TRUE), error="'.dots' requires a list") -test(2055.212, coalesce(list(1), list(2)), error="'x' must be an atomic vector") -test(2055.213, coalesce(bool, c(TRUE, FALSE)), error="Only singletons will be recycled") -test(2055.214, coalesce(as.raw(0), as.raw(1)), error="Incompatible type: raw") -test(2055.215, coalesce(bool, .dots=list()), bool) -test(2055.216, coalesce(structure(c(1:2,NA,4L), class=c("a")), c(NA,NA,3L,4L)), structure(1:4, class="a")) -# verbose output -old = options(datatable.verbose=TRUE) -test(2055.251, coalesce(bool, TRUE), bool_val, output='which NA took.*duplicate.*took.*loop over NA indices of x took') -test(2055.252, coalesce(bool_val, TRUE), bool_val, output='no NAs in input, skip') -options(old) -# integer64 tests -if (test_bit64) { - int64 = as.integer64(int) - int64_val = as.integer64(1:4) - test(2055.301, as.character(coalesce(int64, as.integer64(3))), as.character(int64_val)) # why as.character see nanotime tests below - test(2055.302, as.character(coalesce(int64, as.integer64(NA), as.integer64(3))), as.character(int64_val)) - test(2055.303, as.character(coalesce(int64, as.integer64(rep(3, 4L)))), as.character(int64_val)) - test(2055.304, coalesce(int64, 1), error='does not inherit integer64') - test(2055.305, coalesce(int64, 1L), error = 'internal type integer') -} -# nanotime tests -if (test_nanotime) { - nt = nanotime(int) - nt_val = nanotime(1:4) - test(2055.401, as.character(coalesce(nt, nanotime(3L))), as.character(nt_val)) # as.character due to eddelbuettel/nanotime#46 - test(2055.402, as.character(coalesce(nt, nanotime(NA), nanotime(3L))), as.character(nt_val)) - test(2055.403, as.character(coalesce(nt, nanotime(rep(3, 4L)))), as.character(nt_val)) - test(2055.404, coalesce(nt, 1), error='does not inherit nanotime') - test(2055.405, coalesce(nt, 1L), error = 'internal type integer') -} - -# setcoalesce -x = c(11L, NA, 13L, NA, 15L, NA) -y = c(NA, 12L, 5L, NA, NA, NA) -z = c(11L, NA, 1L, 14L, NA, NA) -xx = copy(x) -xx_addr = address(xx) -setcoalesce(xx, y, z) -test(2055.501, xx_addr, address(xx)) -test(2055.502, xx, c(11:15, NA)) -xx = copy(x) -xx_addr = address(xx) -setcoalesce(xx, .dots=list()) -test(2055.503, xx_addr, address(xx)) -test(2055.504, xx, x) -test(2055.505, address(setcoalesce(xx)), xx_addr) -test(2055.506, setcoalesce(xx, TRUE, .dots=list(FALSE)), error="either '...' or '.dots'") -test(2055.507, setcoalesce(xx, .dots=TRUE), error="'.dots' requires a list") - -# which_eq -test(2056.01, which_eq(c(TRUE, NA, FALSE, FALSE), FALSE, negate=TRUE), 1:2) -test(2056.02, which_eq(c(TRUE, NA, FALSE, FALSE), NA, negate=TRUE), c(1L,3L,4L)) -test(2056.03, which_eq(c(TRUE, TRUE), TRUE, negate=TRUE), integer()) -test(2056.04, which_eq(c(1L, NA, 5L), 5L, negate=TRUE), c(1L,2L)) -test(2056.05, which_eq(c(1L, NA, 5L), NA_integer_, negate=TRUE), c(1L,3L)) -test(2056.06, which_eq(c(1L, 1L), 1L, negate=TRUE), integer()) -test(2056.07, which_eq(c("a", NA, "r", "a"), "a", negate=TRUE), 2:3) -test(2056.08, which_eq(c("a", NA, "r", "a"), "r", negate=TRUE), c(1L,2L,4L)) -test(2056.09, which_eq(c("a","a"), "a", negate=TRUE), integer()) -test(2056.10, which_eq(c(1.1, NA, 3.1), NA_real_, negate=TRUE), c(1L,3L)) -test(2056.11, which_eq(c(NA_real_, NA_real_), NA_real_, negate=TRUE), integer()) -test(2056.12, which_eq(c(1.1, NaN, 3.1), NaN, negate=TRUE), c(1L,3L)) -test(2056.13, which_eq(c(NaN, NaN), NaN, negate=TRUE), integer()) -test(2056.14, which_eq(c(1.1, Inf, 3.1), Inf, negate=TRUE), c(1L,3L)) -test(2056.15, which_eq(c(Inf, Inf), Inf, negate=TRUE), integer()) -test(2056.16, which_eq(c(NA, 12L, 5L, NA, NA, 16L, NaN, Inf, NA)+0.1, NaN), 7L) -test(2056.17, which_eq(c(NA, 12L, 5L, NA, NA, 16L, NaN, Inf, NA)+0.1, Inf), 8L) -if (test_bit64) { - test(2056.51, which_eq(as.integer64(c(1L, NA, 5L)), as.integer64(5L), negate=TRUE), c(1L,2L)) - test(2056.52, which_eq(as.integer64(c(1L, NA, 5L)), as.integer64(NA_integer_), negate=TRUE), c(1L,3L)) - test(2056.53, which_eq(as.integer64(c(1L, 1L)), as.integer64(1L), negate=TRUE), integer()) -} -if (test_nanotime) { - test(2056.61, which_eq(nanotime(c(1L, NA, 5L)), nanotime(5L), negate=TRUE), c(1L,2L)) - test(2056.62, which_eq(nanotime(c(1L, NA, 5L)), nanotime(NA_integer_), negate=TRUE), c(1L,3L)) - test(2056.63, which_eq(nanotime(c(1L, 1L)), nanotime(1L), negate=TRUE), integer()) -} -test(2056.81, which_eq(list(1L,2L), 1L), error="argument 'x' must be atomic vector") -test(2056.82, which_eq(c(1:2), 1), error="argument 'value' must of the same type as argument 'x'") -test(2056.83, which_eq(c(1:2), 1L, negate=1L), error="argument 'negate' must be TRUE or FALSE") -test(2056.84, which_eq(as.raw(0), as.raw(1)), error="Incompatible type: raw") diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index cb4f07fe9d..d93589fbfe 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -15063,6 +15063,135 @@ DT = data.table(a=1) test(2059.1, rbindlist(list(DT,1)), error="Item 2 of input is not a data.frame, data.table or list") test(2059.2, rbindlist(DT), error="Input is data.table but should be a plain list of items to be stacked") +# coalesce, #3424 +bool = c(TRUE, NA, FALSE) +bool_val = c(TRUE, TRUE, FALSE) +int = c(1L, 2L, NA_integer_, 4L) +int_val = 1:4 +num = c(1, 2, NA_real_, 4) +num_val = c(1, 2, 3, 4) +str = c('a', NA_character_, 'b', NA_character_) +str_val = c('a', 'b', 'b', 'b') +fkt = factor(str) +fkt_val = factor(str_val) +date = as.Date(int, origin="1970-01-01") +date_val = as.Date(int_val, origin="1970-01-01") +idate = as.IDate(int, origin="1970-01-01") +idate_val = as.IDate(int_val, origin="1970-01-01") +itime = as.ITime(int) +itime_val = as.ITime(int_val) +posix = as.POSIXct(int, origin="1970-01-01") +posix_val = as.POSIXct(int_val, origin="1970-01-01") +# singleton replacements +test(2060.001, coalesce(bool, TRUE), bool_val) +test(2060.002, coalesce(bool, NA, TRUE), bool_val) +test(2060.003, coalesce(int, 3L), int_val) +test(2060.004, coalesce(int, NA_integer_, 3L), int_val) +test(2060.005, coalesce(num, 3), num_val) +test(2060.006, coalesce(num, NA_real_, 3), num_val) +test(2060.007, coalesce(str, 'b'), str_val) +test(2060.008, coalesce(str, NA_character_, 'b'), str_val) +test(2060.009, coalesce(fkt, factor('b', levels = c('a', 'b'))), fkt_val) +test(2060.010, coalesce(fkt, factor(NA_integer_, levels=c("a","b")), factor('b', levels = c('a', 'b'))), fkt_val) +test(2060.011, coalesce(date, as.Date("1970-01-04")), date_val) +test(2060.012, coalesce(date, as.Date(NA), as.Date("1970-01-04")), date_val) +test(2060.013, coalesce(idate, as.IDate("1970-01-04")), idate_val) +test(2060.014, coalesce(idate, as.IDate(NA), as.IDate("1970-01-04")), idate_val) +test(2060.015, coalesce(itime, as.ITime(3L)), itime_val) +test(2060.016, coalesce(itime, as.ITime(NA), as.ITime(3L)), itime_val) +test(2060.017, coalesce(posix, as.POSIXct(3L, origin="1970-01-01")), posix_val) +test(2060.018, coalesce(posix, as.POSIXct(NA_integer_, origin="1970-01-01"), as.POSIXct(3L, origin="1970-01-01")), posix_val) +# vector replacements +test(2060.051, coalesce(bool, rep(TRUE, 3L)), bool_val) +test(2060.052, coalesce(bool, rep(NA, 3L), rep(TRUE, 3L)), bool_val) +test(2060.053, coalesce(int, rep(3L, 4L)), int_val) +test(2060.054, coalesce(int, rep(NA_integer_, 4L), rep(3L, 4L)), int_val) +test(2060.055, coalesce(num, rep(3, 4L)), num_val) +test(2060.056, coalesce(num, rep(NA_real_, 4L), rep(3, 4L)), num_val) +test(2060.057, coalesce(str, rep('b', 4L)), str_val) +test(2060.058, coalesce(str, rep(NA_character_, 4L), rep('b', 4L)), str_val) +test(2060.059, coalesce(fkt, factor(rep('b', 4L), levels=c('a', 'b'))), fkt_val) +test(2060.060, coalesce(fkt, factor(rep(NA_integer_, 4L), levels=c("a","b")), factor(rep('b', 4L), levels=c('a', 'b'))), fkt_val) +test(2060.061, coalesce(date, rep(as.Date("1970-01-04"), 4L)), date_val) +test(2060.062, coalesce(date, rep(as.Date(NA), 4L), rep(as.Date("1970-01-04"), 4L)), date_val) +test(2060.063, coalesce(idate, rep(as.IDate("1970-01-04"), 4L)), idate_val) +test(2060.064, coalesce(idate, rep(as.IDate(NA), 4L), rep(as.IDate("1970-01-04"), 4L)), idate_val) +test(2060.065, coalesce(itime, rep(as.ITime(3L), 4L)), itime_val) +test(2060.066, coalesce(itime, rep(as.ITime(NA), 4L), rep(as.ITime(3L), 4L)), itime_val) +test(2060.067, coalesce(posix, as.POSIXct(rep(3L, 4L), origin="1970-01-01")), posix_val) +test(2060.068, coalesce(posix, as.POSIXct(rep(NA_integer_, 4L), origin="1970-01-01"), as.POSIXct(rep(3L, 4L), origin="1970-01-01")), posix_val) +# .dots argument +test(2060.101, coalesce(bool, .dots=list(NA, TRUE)), bool_val) +# floating point extras +x = c(11L, NA, 13L, NA, 15L, NaN, NA, NA, NA)+0.1 +y = c(NA, 12L, 5L, NA, NA, 16L, NaN, Inf, NA)+0.1 +z = c(11L, NA, 1L, 14L, NA, 16L, 1L, 2L, NA)+0.1 +test(2060.151, coalesce(x, y, z), c(11:15,NaN,NaN,Inf,NA)+0.1) +# factor of different levels +x = factor(c('a','b',NA,NA,'b')) +y = factor(c('b','b','a',NA,'b')) +z = factor(c('a',NA,NA,'d','a')) +test(2060.152, coalesce(x, y, z), error="Item 3 is a factor but its levels are not identical to the first item's levels") +# edge cases/checks +test(2060.201, coalesce(bool), bool) +test(2060.202, coalesce(fkt), fkt) +test(2060.203, coalesce(bool, 1L), error='internal type integer') +test(2060.204, coalesce(bool, NA_integer_), error='internal type integer') +test(2060.205, coalesce(fkt, 1L), error='Item 1 is a factor but item 2 is not a factor. When factors are involved, all items must be factor') +test(2060.206, coalesce(num, 3L), error='internal type integer') +test(2060.207, coalesce(int, 3), error='internal type double') +test(2060.208, coalesce(fkt, 'b'), error='Item 1 is a factor but item 2 is not a factor. When factors are involved, all items must be factor.') +test(2060.209, coalesce(str, factor('b')), error='Item 2 is a factor but item 1 is not a factor. When factors are involved, all items must be factor') +test(2060.210, coalesce(bool, TRUE, .dots=list(FALSE)), error="either '...' or '.dots'") +test(2060.211, coalesce(bool, .dots=TRUE), error="'.dots' requires a list") +test(2060.212, coalesce(list(1), list(2)), error="'x' must be an atomic vector") +test(2060.213, coalesce(bool, c(TRUE, FALSE)), error="Item 2 is length 2 but the first item is length 10. Only singletons are recycled") +test(2060.214, coalesce(as.raw(0), as.raw(1)), error="Unsupported type: raw") +test(2060.215, coalesce(bool, .dots=list()), bool) +test(2060.216, coalesce(structure(c(1:2,NA,4L), class=c("a")), c(NA,NA,3L,4L)), structure(1:4, class="a")) +# verbose output +old = options(datatable.verbose=TRUE) +test(2060.251, coalesce(bool, TRUE), bool_val, output='which NA took.*duplicate.*took.*loop over NA indices of x took') +test(2060.252, coalesce(bool_val, TRUE), bool_val, output='no NAs in input, skip') +options(old) +# integer64 tests +if (test_bit64) { + int64 = as.integer64(int) + int64_val = as.integer64(1:4) + test(2060.301, as.character(coalesce(int64, as.integer64(3))), as.character(int64_val)) # why as.character see nanotime tests below + test(2060.302, as.character(coalesce(int64, as.integer64(NA), as.integer64(3))), as.character(int64_val)) + test(2060.303, as.character(coalesce(int64, as.integer64(rep(3, 4L)))), as.character(int64_val)) + test(2060.304, coalesce(int64, 1), error='does not inherit integer64') + test(2060.305, coalesce(int64, 1L), error = 'internal type integer') +} +# nanotime tests +if (test_nanotime) { + nt = nanotime(int) + nt_val = nanotime(1:4) + test(2060.401, as.character(coalesce(nt, nanotime(3L))), as.character(nt_val)) # as.character due to eddelbuettel/nanotime#46 + test(2060.402, as.character(coalesce(nt, nanotime(NA), nanotime(3L))), as.character(nt_val)) + test(2060.403, as.character(coalesce(nt, nanotime(rep(3, 4L)))), as.character(nt_val)) + test(2060.404, coalesce(nt, 1), error='does not inherit nanotime') + test(2060.405, coalesce(nt, 1L), error = 'internal type integer') +} +# setcoalesce +x = c(11L, NA, 13L, NA, 15L, NA) +y = c(NA, 12L, 5L, NA, NA, NA) +z = c(11L, NA, 1L, 14L, NA, NA) +xx = copy(x) +xx_addr = address(xx) +setcoalesce(xx, y, z) +test(2060.501, xx_addr, address(xx)) +test(2060.502, xx, c(11:15, NA)) +xx = copy(x) +xx_addr = address(xx) +setcoalesce(xx, .dots=list()) +test(2060.503, xx_addr, address(xx)) +test(2060.504, xx, x) +test(2060.505, address(setcoalesce(xx)), xx_addr) +test(2060.506, setcoalesce(xx, TRUE, .dots=list(FALSE)), error="either '...' or '.dots'") +test(2060.507, setcoalesce(xx, .dots=TRUE), error="'.dots' requires a list") + ################################### # Add new tests above this line # diff --git a/src/coalesce.c b/src/coalesce.c index b14a775f21..3f5894476c 100644 --- a/src/coalesce.c +++ b/src/coalesce.c @@ -1,152 +1,87 @@ #include "data.table.h" -SEXP coalesce(SEXP x, SEXP values, SEXP inplace) { - int JJ = length(values), protecti = 0, nx = length(x); +SEXP coalesce(SEXP x, SEXP values, SEXP inplaceArg) { - if (!isVectorAtomic(x)) error("%s: argument 'x' must be an atomic vector", __func__); - - if (!isTrueFalse(inplace)) error("%s: argument '.inplace' must be TRUE or FALSE", __func__); - bool binplace = LOGICAL(inplace)[0]; - - int firstLengthMiss = lengthMiss(values, nx, true); - if (firstLengthMiss >= 0) error("%s: Replacement %d has length %d but length(x) is %d. Only singletons will be recycled.", __func__, firstLengthMiss+1, length(VECTOR_ELT(values, firstLengthMiss)), nx); - - int firstTypeMiss = typeMiss(values, TYPEOF(x)); - if (firstTypeMiss >= 0) error("%s: Replacement %d has internal type %s but typeof(x) is %s. Please coerce before coalescing", __func__, firstTypeMiss+1, type2char(TYPEOF(VECTOR_ELT(values, firstTypeMiss))), type2char(TYPEOF(x))); - - SEXP x_class = findClass(x); - int firstClassMiss = classMiss(values, x_class); - if (firstClassMiss >= 0) error("%s: Replacement %d does not inherit %s class while 'x' do. Please align classes before coalescing", __func__, firstClassMiss+1, CHAR(x_class)); - if (x_class == char_factor) { - int firstLevelsMiss = levelsMiss(values, getAttrib(x, R_LevelsSymbol)); - if (firstLevelsMiss >= 0) error("%s: Replacement %d has different factor levels than 'x'. Coerce to character or align factor levels before coalescing", __func__, firstLevelsMiss+1); - } - - double tic = 0; + if (!isVectorAtomic(x)) error("argument 'x' must be an atomic vector"); + if (!IS_TRUE_OR_FALSE(inplaceArg)) error("argument '.inplace' must be TRUE or FALSE"); + const bool inplace = LOGICAL(inplaceArg)[0]; const bool verbose = GetVerbose(); + const int nval = length(values); - int *iwhich = malloc(nx*sizeof(iwhich)); // only nwhich will be filled, remaining space will store garbage - if (!iwhich) error("%s: Unable to allocate memory", __func__); // # nocov - int nwhich=0; + const bool factor = isFactor(x); + const int nrow = length(x); - if (verbose) tic = omp_get_wtime(); - switch(TYPEOF(x)) { - case LGLSXP: { - which_eq_int(LOGICAL(x), nx, iwhich, &nwhich, NA_LOGICAL, false); - } break; - case INTSXP: { - which_eq_int(INTEGER(x), nx, iwhich, &nwhich, NA_INTEGER, false); - } break; - case REALSXP: { - if (INHERITS(x,char_integer64) || INHERITS(x,char_nanotime)) { - which_eq_int64((int64_t *)REAL(x), nx, iwhich, &nwhich, NA_INTEGER64, false); + for (int i=0; i Date: Mon, 17 Jun 2019 23:16:52 -0700 Subject: [PATCH 33/37] singletons by using final; passing tests --- inst/tests/tests.Rraw | 24 +++++------ src/coalesce.c | 92 +++++++++++++++++++++++++++++++------------ 2 files changed, 77 insertions(+), 39 deletions(-) diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index d93589fbfe..2baf895d09 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -43,6 +43,7 @@ if (exists("test.data.table", .GlobalEnv, inherits=FALSE)) { replace_dot_alias = data.table:::replace_dot_alias rollup.data.table = data.table:::rollup.data.table selfrefok = data.table:::selfrefok + setcoalesce = data.table:::setcoalesce setdiff_ = data.table:::setdiff_ setreordervec = data.table:::setreordervec setrev = data.table:::setrev @@ -15135,11 +15136,11 @@ test(2060.152, coalesce(x, y, z), error="Item 3 is a factor but its levels are n # edge cases/checks test(2060.201, coalesce(bool), bool) test(2060.202, coalesce(fkt), fkt) -test(2060.203, coalesce(bool, 1L), error='internal type integer') -test(2060.204, coalesce(bool, NA_integer_), error='internal type integer') +test(2060.203, coalesce(bool, 1L), error='Item 2 is type integer but the first item is type logical. Please coerce before coalescing') +test(2060.204, coalesce(bool, NA_integer_), error='Item 2 is type integer but the first item is type logical.') test(2060.205, coalesce(fkt, 1L), error='Item 1 is a factor but item 2 is not a factor. When factors are involved, all items must be factor') -test(2060.206, coalesce(num, 3L), error='internal type integer') -test(2060.207, coalesce(int, 3), error='internal type double') +test(2060.206, coalesce(num, 3L), error='Item 2 is type integer but the first item is type double') +test(2060.207, coalesce(int, 3), error='Item 2 is type double but the first item is type integer') test(2060.208, coalesce(fkt, 'b'), error='Item 1 is a factor but item 2 is not a factor. When factors are involved, all items must be factor.') test(2060.209, coalesce(str, factor('b')), error='Item 2 is a factor but item 1 is not a factor. When factors are involved, all items must be factor') test(2060.210, coalesce(bool, TRUE, .dots=list(FALSE)), error="either '...' or '.dots'") @@ -15148,12 +15149,7 @@ test(2060.212, coalesce(list(1), list(2)), error="'x' must be an atomic vector") test(2060.213, coalesce(bool, c(TRUE, FALSE)), error="Item 2 is length 2 but the first item is length 10. Only singletons are recycled") test(2060.214, coalesce(as.raw(0), as.raw(1)), error="Unsupported type: raw") test(2060.215, coalesce(bool, .dots=list()), bool) -test(2060.216, coalesce(structure(c(1:2,NA,4L), class=c("a")), c(NA,NA,3L,4L)), structure(1:4, class="a")) -# verbose output -old = options(datatable.verbose=TRUE) -test(2060.251, coalesce(bool, TRUE), bool_val, output='which NA took.*duplicate.*took.*loop over NA indices of x took') -test(2060.252, coalesce(bool_val, TRUE), bool_val, output='no NAs in input, skip') -options(old) +test(2060.216, coalesce(structure(c(1:2,NA,4L), class=c("a")), c(NA,NA,3L,4L)),, error="Item 2 has a different class than item 1") # integer64 tests if (test_bit64) { int64 = as.integer64(int) @@ -15161,8 +15157,8 @@ if (test_bit64) { test(2060.301, as.character(coalesce(int64, as.integer64(3))), as.character(int64_val)) # why as.character see nanotime tests below test(2060.302, as.character(coalesce(int64, as.integer64(NA), as.integer64(3))), as.character(int64_val)) test(2060.303, as.character(coalesce(int64, as.integer64(rep(3, 4L)))), as.character(int64_val)) - test(2060.304, coalesce(int64, 1), error='does not inherit integer64') - test(2060.305, coalesce(int64, 1L), error = 'internal type integer') + test(2060.304, coalesce(int64, 1), error='Item 2 has a different class than item 1') + test(2060.305, coalesce(int64, 1L), error = 'Item 2 is type integer but the first item is type double') } # nanotime tests if (test_nanotime) { @@ -15171,8 +15167,8 @@ if (test_nanotime) { test(2060.401, as.character(coalesce(nt, nanotime(3L))), as.character(nt_val)) # as.character due to eddelbuettel/nanotime#46 test(2060.402, as.character(coalesce(nt, nanotime(NA), nanotime(3L))), as.character(nt_val)) test(2060.403, as.character(coalesce(nt, nanotime(rep(3, 4L)))), as.character(nt_val)) - test(2060.404, coalesce(nt, 1), error='does not inherit nanotime') - test(2060.405, coalesce(nt, 1L), error = 'internal type integer') + test(2060.404, coalesce(nt, 1), error='Item 2 has a different class than item 1') + test(2060.405, coalesce(nt, 1L), error = 'Item 2 is type integer but the first item is type double') } # setcoalesce x = c(11L, NA, 13L, NA, 15L, NA) diff --git a/src/coalesce.c b/src/coalesce.c index 3f5894476c..061b048b18 100644 --- a/src/coalesce.c +++ b/src/coalesce.c @@ -7,27 +7,24 @@ SEXP coalesce(SEXP x, SEXP values, SEXP inplaceArg) { const bool inplace = LOGICAL(inplaceArg)[0]; const bool verbose = GetVerbose(); const int nval = length(values); - - const bool factor = isFactor(x); + const bool factor=isFactor(x); const int nrow = length(x); - for (int i=0; i Date: Mon, 17 Jun 2019 23:33:37 -0700 Subject: [PATCH 34/37] fixed missing error() argument, failed just on windows --- src/coalesce.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/coalesce.c b/src/coalesce.c index 061b048b18..baf5cbfdd9 100644 --- a/src/coalesce.c +++ b/src/coalesce.c @@ -25,7 +25,7 @@ SEXP coalesce(SEXP x, SEXP values, SEXP inplaceArg) { if (!R_compute_identical(getAttrib(x, R_ClassSymbol), getAttrib(item, R_ClassSymbol), 0)) error("Item %d has a different class than item 1.", i+2); if (length(item)!=1 && length(item)!=nrow) - error("Item %d is length %d but the first item is length %d. Only singletons are recycled.", i+2, length(item)); + error("Item %d is length %d but the first item is length %d. Only singletons are recycled.", i+2, length(item), nrow); } if (!inplace) x = PROTECT(duplicate(x)); if (verbose) Rprintf("coalesce copied x (inplace=FALSE)\n"); From ffaa3b4075a95d04038674c42314ecbe6cb6e742 Mon Sep 17 00:00:00 2001 From: mattdowle Date: Mon, 17 Jun 2019 23:58:13 -0700 Subject: [PATCH 35/37] actually fixed this time --- inst/tests/tests.Rraw | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index 2baf895d09..06825a7155 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -15146,7 +15146,7 @@ test(2060.209, coalesce(str, factor('b')), error='Item 2 is a factor but item 1 test(2060.210, coalesce(bool, TRUE, .dots=list(FALSE)), error="either '...' or '.dots'") test(2060.211, coalesce(bool, .dots=TRUE), error="'.dots' requires a list") test(2060.212, coalesce(list(1), list(2)), error="'x' must be an atomic vector") -test(2060.213, coalesce(bool, c(TRUE, FALSE)), error="Item 2 is length 2 but the first item is length 10. Only singletons are recycled") +test(2060.213, coalesce(bool, c(TRUE, FALSE)), error="Item 2 is length 2 but the first item is length 3. Only singletons are recycled") test(2060.214, coalesce(as.raw(0), as.raw(1)), error="Unsupported type: raw") test(2060.215, coalesce(bool, .dots=list()), bool) test(2060.216, coalesce(structure(c(1:2,NA,4L), class=c("a")), c(NA,NA,3L,4L)),, error="Item 2 has a different class than item 1") From 0acc6364aaaf3d985c8569cfc1db0efccfa2406a Mon Sep 17 00:00:00 2001 From: mattdowle Date: Tue, 18 Jun 2019 02:11:17 -0700 Subject: [PATCH 36/37] news item benchmark and removed .dots --- NEWS.md | 28 ++++++++++++++++++++++------ R/coalesce.R | 43 ++++++++++++++++++++++--------------------- inst/tests/tests.Rraw | 17 +++++++---------- man/coalesce.Rd | 25 +++++++++++++++++++++---- 4 files changed, 72 insertions(+), 41 deletions(-) diff --git a/NEWS.md b/NEWS.md index 1d0ce898b2..63a1a259bf 100644 --- a/NEWS.md +++ b/NEWS.md @@ -100,19 +100,35 @@ # default 4 threads on a laptop with 16GB RAM and 8 logical CPU ids = as.vector(outer(LETTERS, LETTERS, paste0)) - system.time(DT1 <- CJ(ids, 1:500000)) # 3.9GB; 340m rows - # user system elapsed + system.time( CJ(ids, 1:500000) ) # 3.9GB; 340m rows + # user system elapsed (seconds) # 3.000 0.817 3.798 # was # 1.800 0.832 2.190 # now - ids = as.factor(ids) - system.time(DT2 <- CJ(ids, 1:500000)) # 2.6GB; 340m rows - # user system elapsed + # ids = as.factor(ids) + system.time( CJ(ids, 1:500000) ) # 2.6GB; 340m rows + # user system elapsed (seconds) # 1.779 0.534 2.293 # was # 0.357 0.763 0.292 # now ``` -18. New function `coalesce` for efficiently replacing missing values according to a prioritized list of candidates (_a la_ SQL), [#3424](https://github.com/Rdatatable/data.table/issues/3424) See [#3608](https://github.com/Rdatatable/data.table/pull/3608) for benchmarks. +18. New function `coalesce(x, ...)` has been written in C, and is multithreaded for numeric and factor types. It replaces missing values according to a prioritized list of candidates (as per SQL COALESCE, `dplyr::coalesce`, and `hutils::coalesce`), [#3424](https://github.com/Rdatatable/data.table/issues/3424). It accepts any number of vectors in several forms. For example, given three vectors `x`, `y`, and `z`, where each `NA` in `x` is to be replaced by the corresponding value in `y` if that is non-NA, else the corresponding value in `z`, the following equivalent forms are all accepted: `coalesce(x,y,z)`, `coalesce(x,list(y,z))`, and `coalesce(list(x,y,z))`. + + ```R + # default 4 threads on a laptop with 16GB RAM and 8 logical CPU + N = 100e6 + x = replicate(5, {x=sample(N); x[sample(N, N/2)]=NA; x}, simplify=FALSE) # 2GB + y1 = do.call(dplyr::coalesce, x)) + y2 = do.call(hutils::coalesce, x)) + y3 = do.call(data.table::coalesce, x)) + # user system elapsed (seconds) + # 4.935 1.876 6.810 # dplyr::coalesce + # 3.122 0.831 3.956 # hutils::coalesce + # 0.915 0.099 0.379 # data.table::coalesce + identical(y1,y2) && identical(y1,y3) + # TRUE + ``` + #### BUG FIXES diff --git a/R/coalesce.R b/R/coalesce.R index acb1296a70..a6db718bb7 100644 --- a/R/coalesce.R +++ b/R/coalesce.R @@ -1,28 +1,29 @@ -coalesce = function(x, ..., .dots=NULL) { - if (is.null(.dots) && missing(..1)) return(copy(x)) - - values = if (!is.null(.dots)) { - if (!missing(..1)) stop("provide either '...' or '.dots' argument, not both") - if (!is.list(.dots)) stop("argument '.dots' requires a list input") - .dots - } else list(...) - - if (!length(values)) return(copy(x)) +coalesce = function(x, ...) { + if (missing(..1)) { + if (is.list(x)) { + if (length(x)<=1L) return(x[[1L]]) + values = x[-1L] + x = x[[1L]] + } else return(x) + } else { + if (!missing(..2)) values = list(...) + else values = if (is.list(..1)) ..1 else list(..1) + } .Call(Ccoalesce, x, values, FALSE) } -setcoalesce = function(x, ..., .dots=NULL) { - if (is.null(.dots) && missing(..1)) return(x) - - values = if (!is.null(.dots)) { - if (!missing(..1)) stop("provide either '...' or '.dots' argument, not both") - if (!is.list(.dots)) stop("argument '.dots' requires a list input") - .dots - } else list(...) - - if (!length(values)) return(x) - +setcoalesce = function(x, ...) { + if (missing(..1)) { + if (is.list(x)) { + if (length(x)<=1L) return(x[[1L]]) + values = x[-1L] + x = x[[1L]] + } else return(x) + } else { + if (!missing(..2)) values = list(...) + else values = if (is.list(..1)) ..1 else list(..1) + } invisible(.Call(Ccoalesce, x, values, TRUE)) } diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index 06825a7155..3cc3a498db 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -15121,18 +15121,19 @@ test(2060.065, coalesce(itime, rep(as.ITime(3L), 4L)), itime_val) test(2060.066, coalesce(itime, rep(as.ITime(NA), 4L), rep(as.ITime(3L), 4L)), itime_val) test(2060.067, coalesce(posix, as.POSIXct(rep(3L, 4L), origin="1970-01-01")), posix_val) test(2060.068, coalesce(posix, as.POSIXct(rep(NA_integer_, 4L), origin="1970-01-01"), as.POSIXct(rep(3L, 4L), origin="1970-01-01")), posix_val) -# .dots argument -test(2060.101, coalesce(bool, .dots=list(NA, TRUE)), bool_val) +test(2060.101, coalesce(bool, list(NA, TRUE)), bool_val) # floating point extras x = c(11L, NA, 13L, NA, 15L, NaN, NA, NA, NA)+0.1 y = c(NA, 12L, 5L, NA, NA, 16L, NaN, Inf, NA)+0.1 z = c(11L, NA, 1L, 14L, NA, 16L, 1L, 2L, NA)+0.1 -test(2060.151, coalesce(x, y, z), c(11:15,NaN,NaN,Inf,NA)+0.1) +test(2060.151, coalesce(x, y, z), ans<-c(11:15,NaN,NaN,Inf,NA)+0.1) +test(2060.152, coalesce(list(x, y, z)), ans) +test(2060.153, coalesce(x, list(y,z)), ans) # factor of different levels x = factor(c('a','b',NA,NA,'b')) y = factor(c('b','b','a',NA,'b')) z = factor(c('a',NA,NA,'d','a')) -test(2060.152, coalesce(x, y, z), error="Item 3 is a factor but its levels are not identical to the first item's levels") +test(2060.154, coalesce(x, y, z), error="Item 3 is a factor but its levels are not identical to the first item's levels") # edge cases/checks test(2060.201, coalesce(bool), bool) test(2060.202, coalesce(fkt), fkt) @@ -15143,12 +15144,10 @@ test(2060.206, coalesce(num, 3L), error='Item 2 is type integer but the first it test(2060.207, coalesce(int, 3), error='Item 2 is type double but the first item is type integer') test(2060.208, coalesce(fkt, 'b'), error='Item 1 is a factor but item 2 is not a factor. When factors are involved, all items must be factor.') test(2060.209, coalesce(str, factor('b')), error='Item 2 is a factor but item 1 is not a factor. When factors are involved, all items must be factor') -test(2060.210, coalesce(bool, TRUE, .dots=list(FALSE)), error="either '...' or '.dots'") -test(2060.211, coalesce(bool, .dots=TRUE), error="'.dots' requires a list") test(2060.212, coalesce(list(1), list(2)), error="'x' must be an atomic vector") test(2060.213, coalesce(bool, c(TRUE, FALSE)), error="Item 2 is length 2 but the first item is length 3. Only singletons are recycled") test(2060.214, coalesce(as.raw(0), as.raw(1)), error="Unsupported type: raw") -test(2060.215, coalesce(bool, .dots=list()), bool) +test(2060.215, coalesce(bool, list()), bool) test(2060.216, coalesce(structure(c(1:2,NA,4L), class=c("a")), c(NA,NA,3L,4L)),, error="Item 2 has a different class than item 1") # integer64 tests if (test_bit64) { @@ -15181,12 +15180,10 @@ test(2060.501, xx_addr, address(xx)) test(2060.502, xx, c(11:15, NA)) xx = copy(x) xx_addr = address(xx) -setcoalesce(xx, .dots=list()) +setcoalesce(xx, list()) test(2060.503, xx_addr, address(xx)) test(2060.504, xx, x) test(2060.505, address(setcoalesce(xx)), xx_addr) -test(2060.506, setcoalesce(xx, TRUE, .dots=list(FALSE)), error="either '...' or '.dots'") -test(2060.507, setcoalesce(xx, .dots=TRUE), error="'.dots' requires a list") ################################### diff --git a/man/coalesce.Rd b/man/coalesce.Rd index 0079641a21..0e4a4a9295 100644 --- a/man/coalesce.Rd +++ b/man/coalesce.Rd @@ -3,15 +3,15 @@ \alias{setcoalesce} \title{ Coalescing missing values } \description{ -Fill in missing values in a vector by successively pulling from candidate vectors in order (\emph{a la} the ANSI SQL function). +Fill in missing values in a vector by successively pulling from candidate vectors in order. As per the ANSI SQL function COALESCE, \code{dplyr::coalesce} and \code{hutils::coalesce}. +Written in C, and multithreaded for numeric and factor types. } \usage{ - coalesce(x, \dots, .dots=NULL) + coalesce(x, \dots) } \arguments{ - \item{x}{ An atomic vector. } + \item{x}{ An atomic vector. Or, if \dots is missing, x may be a \code{list} of atomic vectors. } \item{\dots}{ Vectors of same class as \code{x} to be used successively as replacements for missing values of \code{x}; singletons are also accepted. } - \item{.dots}{ List of vectors, an alternative interface to specify \dots argument. } } \details{ Factor type is supported only when factor levels are equal. @@ -24,5 +24,22 @@ x = c(11L, NA, 13L, NA, 15L, NA) y = c(NA, 12L, 5L, NA, NA, NA) z = c(11L, NA, 1L, 14L, NA, NA) coalesce(x, y, z) +coalesce(list(x,y,z)) # same +coalesce(x, list(y,z)) # same + +\dontrun{ +# default 4 threads on a laptop with 16GB RAM and 8 logical CPU +N = 100e6 +x = replicate(5, {x=sample(N); x[sample(N, N/2)]=NA; x}, simplify=FALSE) # 2GB +system.time(y1 <- do.call(dplyr::coalesce, x)) +system.time(y2 <- do.call(hutils::coalesce, x)) +system.time(y3 <- do.call(data.table::coalesce, x)) +identical(y1,y2) && identical(y1,y3) +# user system elapsed (seconds) +# 4.935 1.876 6.810 # dplyr v0.8.1 +# 3.122 0.831 3.956 # hutils v1.5.0 +# 0.915 0.099 0.379 # data.table v1.12.4 +} } \keyword{ data } + From c228c7e492a5b6fe8eacb7bab48deee0527408ea Mon Sep 17 00:00:00 2001 From: mattdowle Date: Tue, 18 Jun 2019 02:38:48 -0700 Subject: [PATCH 37/37] coverage --- inst/tests/tests.Rraw | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index 3cc3a498db..a8c813d0f1 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -15129,11 +15129,15 @@ z = c(11L, NA, 1L, 14L, NA, 16L, 1L, 2L, NA)+0.1 test(2060.151, coalesce(x, y, z), ans<-c(11:15,NaN,NaN,Inf,NA)+0.1) test(2060.152, coalesce(list(x, y, z)), ans) test(2060.153, coalesce(x, list(y,z)), ans) +test(2060.154, coalesce(list(x)), x) +test(2060.155, setcoalesce(list(x)), x) +test(2060.156, setcoalesce(list(x,y,z)), ans) +test(2060.157, x, ans) # setcoalesce updated the first item (x) by reference # factor of different levels x = factor(c('a','b',NA,NA,'b')) y = factor(c('b','b','a',NA,'b')) z = factor(c('a',NA,NA,'d','a')) -test(2060.154, coalesce(x, y, z), error="Item 3 is a factor but its levels are not identical to the first item's levels") +test(2060.180, coalesce(x, y, z), error="Item 3 is a factor but its levels are not identical to the first item's levels") # edge cases/checks test(2060.201, coalesce(bool), bool) test(2060.202, coalesce(fkt), fkt)