Skip to content

Commit 412d86f

Browse files
committed
add supporting modify!(views,...)
1 parent 19002e8 commit 412d86f

File tree

1 file changed

+47
-6
lines changed

1 file changed

+47
-6
lines changed

src/dataset/modify.jl

+47-6
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,12 @@ end
287287
modify(origninal_ds::AbstractDataset, @nospecialize(args...)) = modify!(copy(origninal_ds), args...)
288288

289289
modify!(ds::Dataset) = parent(ds)
290-
function modify!(ds::Dataset, @nospecialize(args...))
291-
idx_cpy = Index(copy(index(ds).lookup), copy(index(ds).names), copy(index(ds).format))
290+
function modify!(ds::AbstractDataset, @nospecialize(args...))
291+
if ds isa SubDataset
292+
idx_cpy = copy(index(parent(ds)))
293+
else
294+
idx_cpy = Index(copy(index(ds).lookup), copy(index(ds).names), copy(index(ds).format))
295+
end
292296
if isgrouped(ds)
293297
norm_var = normalize_modify_multiple!(idx_cpy, index(ds), args...)
294298
allnewvars = map(x -> x.second.second, norm_var)
@@ -339,9 +343,33 @@ end
339343
function _resize_result!(ds, _res, newcol)
340344
resize_col = _is_scalar(_res, nrow(ds))
341345
if resize_col
342-
ds[!, newcol] = fill!(Tables.allocatecolumn(typeof(_res), nrow(ds)), _res)
346+
if ds isa SubDataset
347+
if haskey(index(ds), newcol)
348+
ds[:, newcol] = fill!(Tables.allocatecolumn(typeof(_res), nrow(ds)), _res)
349+
elseif !haskey(index(parent(ds)), newcol)
350+
parent(ds)[!, newcol] = missings(typeof(_res), nrow(parent(ds)))
351+
_update_subindex!(index(ds), index(parent(ds)), newcol)
352+
ds[:, newcol] = fill!(Tables.allocatecolumn(typeof(_res), nrow(ds)), _res)
353+
else
354+
throw(ArgumentError("modifing a parent's column which doesn't appear in SubDataset is not allowed"))
355+
end
356+
else
357+
ds[!, newcol] = fill!(Tables.allocatecolumn(typeof(_res), nrow(ds)), _res)
358+
end
343359
else
344-
ds[!, newcol] = _res
360+
if ds isa SubDataset
361+
if haskey(index(ds), mslast)
362+
ds[:, newcol] = _res
363+
elseif !haskey(index(parent(ds)), newcol)
364+
parent(ds)[!, newcol] = missings(eltype(_res), nrow(parent(ds)))
365+
_update_subindex!(index(ds), index(parent(ds)), newcol)
366+
ds[:, newcol] = _res
367+
else
368+
throw(ArgumentError("modifing a parent's column which doesn't appear in SubDataset is not allowed"))
369+
end
370+
else
371+
ds[!, newcol] = _res
372+
end
345373
end
346374
end
347375

@@ -377,10 +405,23 @@ function _modify_f_barrier(ds, msfirst, mssecond, mslast)
377405
end
378406
elseif (mssecond isa Expr) && mssecond.head == :BYROW
379407
try
380-
ds[!, mslast] = byrow(ds, mssecond.args[1], msfirst; mssecond.args[2]...)
408+
if ds isa SubDataset
409+
_res = byrow(ds, mssecond.args[1], msfirst; mssecond.args[2]...)
410+
if haskey(index(ds), mslast)
411+
ds[:, mslast] = _res
412+
elseif !haskey(index(parent(ds)), mslast)
413+
parent(ds)[!, mslast] = missings(eltype(_res), nrow(parent(ds)))
414+
_update_subindex!(index(ds), index(parent(ds)), mslast)
415+
ds[:, mslast] = _res
416+
else
417+
throw(ArgumentError("modifing a parent's column which doesn't appear in SubDataset is not allowed"))
418+
end
419+
else
420+
ds[!, mslast] = byrow(ds, mssecond.args[1], msfirst; mssecond.args[2]...)
421+
end
381422
catch e
382423
if e isa MethodError
383-
throw(ArgumentError("output of `byrow` operation must be a vector"))
424+
throw(ArgumentError("There is problem in your `byrow`, make sure that the output of `byrow` is a vector"))
384425
end
385426
rethrow(e)
386427
end

0 commit comments

Comments
 (0)