Skip to content

Commit

Permalink
cbindlist
Browse files Browse the repository at this point in the history
add cbind by reference, timing

R prototype of mergelist

wording

use lower overhead funs

stick to int32 for now, correct R_alloc

bmerge C refactor for codecov and one loop for speed

address revealed codecov gaps

refactor vecseq for codecov

seqexp helper, some alloccol export on C

bmerge codecov, types handled in R bmerge already

better comment seqexp

bmerge mult=error #655

multiple new C utils

swap if branches

explain new C utils

comments mostly

reduce conflicts to PR #4386

comment C code

address multiple matches during update-on-join #3747

Revert "address multiple matches during update-on-join #3747"

This reverts commit b64c0c3.

merge.dt has temporarily mult arg, for testing

minor changes to cbindlist c

dev mergelist, for single pair now

add quiet option to cc()

mergelist tests

add check for names to perhaps.dt

rm mult from merge.dt method

rework, clean, polish multer, fix righ and full joins

make full join symmetric

mergepair inner function to loop on

extra check for symmetric

mergelist manual

ensure no df-dt passed where list expected

comments and manual

handle 0 cols tables

more tests

more tests and debugging

move more logic closer to bmerge, simplify mergepair

more tests

revert not used changes

reduce not needed checks, cleanup

copy arg behavior, manual, no tests yet

cbindlist manual, export both

cleanup processing bmerge to dtmatch

test function match order for easier preview

vecseq gets short-circuit

batch test allow browser

big cleanup

remmove unneeded stuff, reduce diff

more cleanup, minor manual fixes

add proper test scripts

Merge branch 'master' into cbind-merge-list

comment out not used code for coverage

more tests, some nocopy opts

rename sql test script, should fix codecov

simplify dtmatch inner branch

more precise copy, now copy only T or F

unused arg not yet in api, wording

comments and refer issues

codecov

hasindex coverage

codecov gap

tests for join using key, cols argument

fix missing import forderv

more tests, improve missing on handling

more tests for order of inner and full join for long keys

new allow.cartesian option, #4383, #914

reduce diff, improve codecov

reduce diff, comments

need more DT, not lists, mergelist 3+ tbls

proper escape heavy check

unit tests

more tests, address overalloc failure

mergelist and cbindlist retain index

manual, examples

fix manual

minor clarify in manual

retain keys, right outer join for snowflake schema joins

duplicates in cbindlist

recycling in cbindlist

escape 0 input in copyCols

empty input handling

closing cbindlist

vectorized _on_ and _join.many_ arg

rename dtmatch to dtmerge

vectorized args: how, mult
push down input validation
add support for cross join, semi join, anti join

full join, reduce overhead for mult=error

mult default value dynamic

fix manual

add "see details" to Rd

mention shared on in arg description

amend feedback from Michael

semi and anti joins will not reorder x columns

Merge branch 'master' into cbind-merge-list

spelling, thx to @jan-glx

check all new funs used and add comments

bugfix, sort=T needed for now

Merge branch 'master' into cbind-merge-list

Update NEWS.md

Merge branch 'master' into cbind-merge-list

Merge branch 'master' into cbind-merge-list

NEWS placement

numbering

ascArg->order

Merge remote-tracking branch 'origin/cbind-merge-list' into cbind-merge-list

attempt to restore from master

Update to stopf() error style

Need isFrame for now

More quality checks: any(!x)->!all(x); use vapply_1{b,c,i}

really restore from master

try to PROTECT() before duplicate()

update error message in test

appease the rchk gods

extraneous space

missing ';'

use catf

simplify perhapsDataTableR

move sqlite.Rraw.manual into other.Rraw

simplify for loop

Merge remote-tracking branch 'origin/cbind-merge-list' into cbind-merge-list
  • Loading branch information
MichaelChirico committed Aug 29, 2024
1 parent 5081d0f commit 239d682
Show file tree
Hide file tree
Showing 5 changed files with 585 additions and 1 deletion.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export(setnafill)
export(.Last.updated)
export(fcoalesce)
export(cbindlist)
export(mergelist)
export(substitute2)
#export(DT) # mtcars |> DT(i,j,by) #4872 #5472

Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

1. In `DT[, variable := value]`, when value is class `POSIXlt`, we automatically coerce it to class `POSIXct` instead, [#1724](https://github.com/Rdatatable/data.table/issues/1724). Thanks to @linzhp for the report, and Benjamin Schwendinger for the fix.

## NEW FEATURES

1. (add example here?) New functions `cbindlist` and `mergelist` have been implemented and exported. Works like `cbind`/`merge` but takes `list` of data.tables on input. `merge` happens in `Reduce` fashion. Supports `how` (_left_, _inner_, _full_, _right_, _semi_, _anti_, _cross_) joins and `mult` argument, closes [#599](https://github.com/Rdatatable/data.table/issues/599) and [#2576](https://github.com/Rdatatable/data.table/issues/2576).

## NOTES

1. Tests run again when some Suggests packages are missing, [#6411](https://github.com/Rdatatable/data.table/issues/6411). Thanks @aadler for the note and @MichaelChirico for the fix.
Expand Down
96 changes: 96 additions & 0 deletions R/mergelist.R
Original file line number Diff line number Diff line change
Expand Up @@ -235,5 +235,101 @@ mergepair = function(lhs, rhs, on, how, mult, lhs.cols=names(lhs), rhs.cols=name
setDT(out)
}

mergelist = function(l, on, cols, how=c("left","inner","full","right","semi","anti","cross"), mult, copy=TRUE, join.many=getOption("datatable.join.many")) {
verbose = getOption("datatable.verbose")
if (verbose)
p = proc.time()[[3L]]
{
if (!is.list(l) || is.data.frame(l))
stopf("'l' must be a list")
if (!all(vapply_1b(l, is.data.table)))
stopf("Every element of 'l' list must be data.table objects")
if (!all(lengths(l)))
stopf("Tables in 'l' argument must be non-zero columns tables")
if (any(vapply_1i(l, function(x) anyDuplicated(names(x)))))
stopf("Some of the tables in 'l' have duplicated column names")
} ## l
if (!isTRUEorFALSE(copy))
stopf("'%s' must be TRUE or FALSE", "copy")
n = length(l)
if (n<2L) {
out = if (!n) as.data.table(l) else l[[1L]]
if (copy) out = copy(out)
if (verbose)
catf("mergelist: merging %d table(s), took %.3fs\n", n, proc.time()[[3L]]-p)
return(out)
}
{
if (!is.list(join.many))
join.many = rep(list(join.many), n-1L)
if (length(join.many)!=n-1L || !all(vapply_1b(join.many, isTRUEorFALSE)))
stopf("'join.many' must be TRUE or FALSE, or a list of such which length must be length(l)-1L")
} ## join.many
{
if (missing(mult))
mult = NULL
if (!is.list(mult))
mult = rep(list(mult), n-1L)
if (length(mult)!=n-1L || !all(vapply_1b(mult, function(x) is.null(x) || (is.character(x) && length(x)==1L && !anyNA(x) && x %chin% c("error","all","first","last")))))
stopf("'mult' must be one of [error, all, first, last] or NULL, or a list of such which length must be length(l)-1L")
} ## mult
{
if (missing(how) || is.null(how))
how = match.arg(how)
if (!is.list(how))
how = rep(list(how), n-1L)
if (length(how)!=n-1L || !all(vapply_1b(how, function(x) is.character(x) && length(x)==1L && !anyNA(x) && x %chin% c("left","inner","full","right","semi","anti","cross"))))
stopf("'how' must be one of [left, inner, full, right, semi, anti, cross], or a list of such which length must be length(l)-1L")
} ## how
{
if (missing(cols) || is.null(cols)) {
cols = vector("list", n)
} else {
if (!is.list(cols))
stopf("'%s' must be a list", "cols")
if (length(cols) != n)
stopf("'cols' must be same length as 'l'")
skip = vapply_1b(cols, is.null)
if (!all(vapply_1b(cols[!skip], function(x) is.character(x) && !anyNA(x) && !anyDuplicated(x))))
stopf("'cols' must be a list of non-zero length, non-NA, non-duplicated, character vectors, or eventually NULLs (all columns)")
if (any(mapply(function(x, icols) !all(icols %chin% names(x)), l[!skip], cols[!skip])))
stopf("'cols' specify columns not present in corresponding table")
}
} ## cols
{
if (missing(on) || is.null(on)) {
on = vector("list", n-1L)
} else {
if (!is.list(on))
on = rep(list(on), n-1L)
if (length(on)!=n-1L || !all(vapply_1b(on, function(x) is.character(x) && !anyNA(x) && !anyDuplicated(x)))) ## length checked in dtmerge
stopf("'on' must be non-NA, non-duplicated, character vector, or a list of such which length must be length(l)-1L")
}
} ## on

l.mem = lapply(l, vapply, address, "")
out = l[[1L]]
out.cols = cols[[1L]]
for (join.i in seq_len(n-1L)) {
rhs.i = join.i + 1L
out = mergepair(
lhs = out, rhs = l[[rhs.i]],
on = on[[join.i]],
how = how[[join.i]], mult = mult[[join.i]],
lhs.cols = out.cols, rhs.cols = cols[[rhs.i]],
copy = FALSE, ## avoid any copies inside, will copy once below
join.many = join.many[[join.i]],
verbose = verbose
)
out.cols = copy(names(out))
}
out.mem = vapply_1c(out, address)
if (copy)
.Call(CcopyCols, out, colnamesInt(out, names(out.mem)[out.mem %chin% unique(unlist(l.mem, recursive=FALSE))]))
if (verbose)
catf("mergelist: merging %d tables, took %.3fs\n", n, proc.time()[[3L]]-p)
out
}

seqexp = function(x) .Call(Cseqexp, x)
perhaps.data.table = function(x) .Call(CperhapsDataTableR, x)
Loading

0 comments on commit 239d682

Please sign in to comment.