Skip to content

Commit f07f865

Browse files
committed
add issorted to groupby
1 parent 80988aa commit f07f865

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

src/sort/groupby.jl

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,19 @@ _columns(ds::GroupBy) = _columns(ds.parent)
2323
index(ds::GroupBy) = index(ds.parent)
2424
Base.parent(ds::GroupBy) = ds.parent
2525

26-
function groupby(ds::Dataset, cols::MultiColumnIndex; alg = HeapSortAlg(), rev = false, mapformats::Bool = true, stable = true)
26+
function groupby(ds::Dataset, cols::MultiColumnIndex; alg = HeapSortAlg(), rev = false, mapformats::Bool = true, stable = true, issorted = false)
2727
# @assert !isgrouped(ds) "`groupby` is not yet implemented for already grouped data sets"
2828
colsidx = index(ds)[cols]
29-
a = _sortperm(ds, cols, rev, a = alg, mapformats = mapformats, stable = stable)
30-
GroupBy(ds,colsidx, a[2], a[1], a[3])
29+
if issorted
30+
selected_columns, ranges, last_valid_index = _find_starts_of_groups(ds::Dataset, colsidx, nrow(ds) < typemax(Int32) ? Val(Int32) : Val(Int64))
31+
return GroupBy(ds, colsidx, 1:nrow(ds), ranges, last_valid_index)
32+
else
33+
a = _sortperm(ds, cols, rev, a = alg, mapformats = mapformats, stable = stable)
34+
GroupBy(ds,colsidx, a[2], a[1], a[3])
35+
end
3136
end
3237

33-
groupby(ds::Dataset, col::ColumnIndex; alg = HeapSortAlg(), rev = false, mapformats::Bool = true, stable = true) = groupby(ds, [col], alg = alg, rev = rev, mapformats = mapformats, stable = stable)
38+
groupby(ds::Dataset, col::ColumnIndex; alg = HeapSortAlg(), rev = false, mapformats::Bool = true, stable = true, issorted = false) = groupby(ds, [col], alg = alg, rev = rev, mapformats = mapformats, stable = stable, issorted = issorted)
3439

3540
function _threaded_permute_for_groupby(x, perm)
3641
if DataAPI.refpool(x) !== nothing

0 commit comments

Comments
 (0)