Skip to content

Commit ec0173d

Browse files
pass for style
1 parent b068504 commit ec0173d

File tree

1 file changed

+53
-54
lines changed

1 file changed

+53
-54
lines changed

R/mergelist.R

Lines changed: 53 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -145,96 +145,95 @@ dtmerge = function(x, i, on, how, mult, join.many, void=FALSE, verbose) {
145145

146146
# atomic join between two tables
147147
mergepair = function(lhs, rhs, on, how, mult, lhs.cols=names(lhs), rhs.cols=names(rhs), copy=TRUE, join.many=TRUE, verbose=FALSE) {
148-
semianti = how=="semi" || how=="anti"
149-
innerfull = how=="inner" || how=="full"
150-
{
151-
if (how!="cross") {
152-
if (is.null(on)) {
153-
if (how=="left" || semianti) on = key(rhs)
154-
else if (how=="right") on = key(lhs)
155-
else if (innerfull) on = onkeys(key(lhs), key(rhs))
156-
if (is.null(on))
157-
stopf("'on' is missing and necessary key is not present")
158-
}
159-
if (any(bad.on <- !on %chin% names(lhs)))
160-
stopf("'on' argument specify columns to join [%s] that are not present in LHS table [%s]", brackify(on[bad.on]), brackify(names(lhs)))
161-
if (any(bad.on <- !on %chin% names(rhs)))
162-
stopf("'on' argument specify columns to join [%s] that are not present in RHS table [%s]", brackify(on[bad.on]), brackify(names(rhs)))
163-
} else if (is.null(on)) {
164-
on = character() ## cross join only
165-
}
166-
} ## on
167-
{
168-
if (how!="right") {
169-
jnfm = lhs; fm.cols = lhs.cols; jnto = rhs; to.cols = rhs.cols
170-
} else {
171-
jnfm = rhs; fm.cols = rhs.cols; jnto = lhs; to.cols = lhs.cols
148+
semi_or_anti = how == "semi" || how == "anti"
149+
inner_or_full = how == "inner" || how == "full"
150+
151+
if (how != "cross") {
152+
if (is.null(on)) {
153+
if (how == "left" || semi_or_anti) on = key(rhs)
154+
else if (how == "right") on = key(lhs)
155+
else if (inner_or_full) on = onkeys(key(lhs), key(rhs))
156+
if (is.null(on))
157+
stopf("'on' is missing and necessary key is not present")
172158
}
173-
} ## join-to and join-from tables and columns (right outer join swap)
159+
if (any(bad.on <- !on %chin% names(lhs)))
160+
stopf("'on' argument specifies columns to join [%s] that are not present in %s table [%s]", brackify(on[bad.on]), "LHS", brackify(names(lhs)))
161+
if (any(bad.on <- !on %chin% names(rhs)))
162+
stopf("'on' argument specifies columns to join [%s] that are not present in %s table [%s]", brackify(on[bad.on]), "RHS", brackify(names(rhs)))
163+
} else if (is.null(on)) {
164+
on = character() ## cross join only
165+
}
166+
167+
## join-to and join-from tables and columns (right outer join-->swap)
168+
if (how != "right") {
169+
join_from = lhs; from_cols = lhs.cols; join_to = rhs; to_cols = rhs.cols
170+
} else {
171+
join_from = rhs; from_cols = rhs.cols; join_to = lhs; to_cols = lhs.cols
172+
}
174173

175174
## ensure symmetric join for inner|full join, apply mult on both tables, bmerge do only 'x' table
176-
cp.i = FALSE ## copy marker of out.i
177-
if ((innerfull) && !is.null(mult) && (mult=="first" || mult=="last")) {
178-
jnfm = fdistinct(jnfm, on=on, mult=mult, cols=fm.cols, copy=FALSE) ## might not copy when already unique by 'on'
179-
cp.i = nrow(jnfm)!=nrow(lhs) ## nrow(lhs) bc how='inner|full' so jnfm=lhs
180-
} else if (how=="inner" && (is.null(mult) || mult=="error")) { ## we do this branch only to raise error from bmerge, we cannot use forder to just find duplicates because those duplicates might not have matching rows in another table, full join checks mult='error' during two non-void bmerges
181-
dtmerge(x=jnfm, i=jnto, on=on, how=how, mult=mult, verbose=verbose, join.many=join.many, void=TRUE)
175+
copy_i = FALSE ## copy marker of out.i
176+
if (inner_or_full && !is.null(mult) && (mult == "first" || mult == "last")) {
177+
join_from = fdistinct(join_from, on=on, mult=mult, cols=from_cols, copy=FALSE) ## might not copy when already unique by 'on'
178+
copy_i = nrow(join_from) != nrow(lhs) ## nrow(lhs) bc how='inner|full' so join_from=lhs
179+
} else if (how == "inner" && (is.null(mult) || mult == "error")) { ## we do this branch only to raise error from bmerge, we cannot use forder to just find duplicates because those duplicates might not have matching rows in another table, full join checks mult='error' during two non-void bmerges
180+
dtmerge(x=join_from, i=join_to, on=on, how=how, mult=mult, verbose=verbose, join.many=join.many, void=TRUE)
182181
}
183182

184183
## binary merge
185-
ans = dtmerge(x=jnto, i=jnfm, on=on, how=how, mult=mult, verbose=verbose, join.many=join.many)
184+
ans = dtmerge(x=join_to, i=join_from, on=on, how=how, mult=mult, verbose=verbose, join.many=join.many)
186185

187186
## make i side
188187
out.i = if (is.null(ans$irows))
189-
.shallow(jnfm, cols=someCols(jnfm, fm.cols, keep=on, retain.order=semianti), retain.key=TRUE)
188+
.shallow(join_from, cols=someCols(join_from, from_cols, keep=on, retain.order=semi_or_anti), retain.key=TRUE)
190189
else
191-
.Call(CsubsetDT, jnfm, ans$irows, someCols(jnfm, fm.cols, keep=on, retain.order=semianti))
192-
cp.i = cp.i || !is.null(ans$irows)
190+
.Call(CsubsetDT, join_from, ans$irows, someCols(join_from, from_cols, keep=on, retain.order=semi_or_anti))
191+
copy_i = copy_i || !is.null(ans$irows)
193192

194193
## make x side
195-
if (semianti) {
196-
out.x = list(); cp.x = TRUE
194+
if (semi_or_anti) {
195+
out.x = list(); copy_x = TRUE
197196
} else {
198197
out.x = if (is.null(ans$xrows)) ## as of now xrows cannot be NULL #4409 thus nocov below
199-
internal_error("dtmerge()$xrows returned NULL, #4409 been resolved but related code has not been updated?") #.shallow(jnto, cols=someCols(jnto, to.cols, drop=on), retain.key=TRUE) # nocov ## as of now nocov does not make difference r-lib/covr#279
198+
internal_error("dtmerge()$xrows returned NULL, #4409 been resolved but related code has not been updated?") #.shallow(join_to, cols=someCols(join_to, to_cols, drop=on), retain.key=TRUE) # nocov ## as of now nocov does not make difference r-lib/covr#279
200199
else
201-
.Call(CsubsetDT, jnto, ans$xrows, someCols(jnto, to.cols, drop=on))
202-
cp.x = !is.null(ans$xrows)
200+
.Call(CsubsetDT, join_to, ans$xrows, someCols(join_to, to_cols, drop=on))
201+
copy_x = !is.null(ans$xrows)
203202
## ensure no duplicated column names in merge results
204-
if (any(dup.i<-names(out.i) %chin% names(out.x)))
203+
if (any(dup.i <- names(out.i) %chin% names(out.x)))
205204
stopf("merge result has duplicated column names, use 'cols' argument or rename columns in 'l' tables, duplicated column(s): %s", brackify(names(out.i)[dup.i]))
206205
}
207206

208207
## stack i and x
209-
if (how!="full") {
210-
if (!cp.i && copy) out.i = copy(out.i)
211-
#if (!cp.x && copy) out.x = copy(out.x) ## as of now cp.x always TRUE, search for #4409 here
208+
if (how != "full") {
209+
if (!copy_i && copy) out.i = copy(out.i)
210+
#if (!copy_x && copy) out.x = copy(out.x) ## as of now copy_x always TRUE, search for #4409 here
212211
out = .Call(Ccbindlist, list(out.i, out.x), FALSE)
213-
if (how=="right") setcolorder(out, neworder=c(on, names(out.x))) ## arrange columns: i.on, x.cols, i.cols
212+
if (how == "right") setcolorder(out, neworder=c(on, names(out.x))) ## arrange columns: i.on, x.cols, i.cols
214213
} else { # how=="full"
215214
## we made left join side above, proceed to right join side, so swap tbls
216-
jnfm = rhs; fm.cols = rhs.cols; jnto = lhs; to.cols = lhs.cols
215+
join_from = rhs; from_cols = rhs.cols; join_to = lhs; to_cols = lhs.cols
217216

218217
cp.r = FALSE
219-
if (!is.null(mult) && (mult=="first" || mult=="last")) {
220-
jnfm = fdistinct(jnfm, on=on, mult=mult, cols=fm.cols, copy=FALSE)
221-
cp.r = nrow(jnfm)!=nrow(rhs) ## nrow(rhs) bc jnfm=rhs
218+
if (!is.null(mult) && (mult == "first" || mult == "last")) {
219+
join_from = fdistinct(join_from, on=on, mult=mult, cols=from_cols, copy=FALSE)
220+
cp.r = nrow(join_from) != nrow(rhs) ## nrow(rhs) bc join_from=rhs
222221
} ## mult=="error" check was made on one side already, below we do on the second side, test 101.43
223222

224223
## binary merge anti join
225-
bns = dtmerge(x=jnto, i=jnfm, on=on, how="anti", mult=if (!is.null(mult) && mult!="all") mult, verbose=verbose, join.many=join.many)
224+
bns = dtmerge(x=join_to, i=join_from, on=on, how="anti", mult=if (!is.null(mult) && mult!="all") mult, verbose=verbose, join.many=join.many)
226225

227226
## make anti join side
228227
out.r = if (is.null(bns$irows))
229-
.shallow(jnfm, cols=someCols(jnfm, fm.cols, keep=on), retain.key=TRUE) ## retain.key is used only in the edge case when !nrow(out.i)
228+
.shallow(join_from, cols=someCols(join_from, from_cols, keep=on), retain.key=TRUE) ## retain.key is used only in the edge case when !nrow(out.i)
230229
else
231-
.Call(CsubsetDT, jnfm, bns$irows, someCols(jnfm, fm.cols, keep=on))
230+
.Call(CsubsetDT, join_from, bns$irows, someCols(join_from, from_cols, keep=on))
232231
cp.r = cp.r || !is.null(bns$irows)
233232

234233
## short circuit to avoid rbindlist to empty sets and retains keys
235234
if (!nrow(out.r)) { ## possibly also !nrow(out.i)
236-
if (!cp.i && copy) out.i = copy(out.i)
237-
#if (!cp.x && copy) out.x = copy(out.x) ## as of now cp.x always TRUE, search for #4409 here
235+
if (!copy_i && copy) out.i = copy(out.i)
236+
#if (!copy_x && copy) out.x = copy(out.x) ## as of now copy_x always TRUE, search for #4409 here
238237
out = .Call(Ccbindlist, list(out.i, out.x), FALSE)
239238
} else if (!nrow(out.i)) { ## but not !nrow(out.r)
240239
if (!cp.r && copy) out.r = copy(out.r)

0 commit comments

Comments
 (0)