-
Notifications
You must be signed in to change notification settings - Fork 367
/
core.jl
850 lines (726 loc) · 29.7 KB
/
core.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
### Common preprocessing
struct OnColRow{T}
row::Int
cols::T
h::Vector{UInt}
OnColRow(row::Union{Signed,Unsigned},
cols::NTuple{<:Any, AbstractVector}, h::Vector{UInt}) =
new{typeof(cols)}(Int(row), cols, h)
end
struct OnCol{T,N} <: AbstractVector{OnColRow{T}}
len::Int
cols::T
h::Vector{UInt}
function OnCol(cs::AbstractVector...)
@assert length(cs) > 1
len = length(cs[1])
@assert all(x -> firstindex(x) == 1, cs)
@assert all(x -> lastindex(x) == len, cs)
new{typeof(cs), length(cs)}(len, cs, UInt[])
end
end
Base.IndexStyle(::Type{<:OnCol}) = Base.IndexLinear()
@inline Base.size(oc::OnCol) = (oc.len,)
@inline function Base.getindex(oc::OnCol, i::Int)
@boundscheck checkbounds(oc, i)
return OnColRow(i, oc.cols, oc.h)
end
Base.hash(ocr1::OnColRow, h::UInt) = throw(MethodError(hash, (ocr1, h)))
@inline Base.hash(ocr1::OnColRow) = @inbounds ocr1.h[ocr1.row]
# Hashing one column at a time is faster since it can use SIMD
function _prehash(oc::OnCol)
h = oc.h
resize!(h, oc.len)
fill!(h, Base.tuplehash_seed)
for col in reverse(oc.cols)
h .= hash.(col, h)
end
end
# TODO: rewrite isequal and isless to use @generated
# or some other approach that would keep them efficient and avoid code duplication
Base.:(==)(x::OnColRow, y::OnColRow) = throw(MethodError(==, (x, y)))
@inline function Base.isequal(ocr1::OnColRow{<:NTuple{2, AbstractVector}},
ocr2::OnColRow{<:NTuple{2, AbstractVector}})
r1 = ocr1.row
c11, c12 = ocr1.cols
r2 = ocr2.row
c21, c22 = ocr2.cols
return @inbounds isequal(c11[r1], c21[r2]) && isequal(c12[r1], c22[r2])
end
Base.isequal(ocr1::OnColRow{<:NTuple{N,AbstractVector}},
ocr2::OnColRow{<:NTuple{N,AbstractVector}}) where {N} =
isequal(ntuple(i -> @inbounds(ocr1.cols[i][ocr1.row]), N),
ntuple(i -> @inbounds(ocr2.cols[i][ocr2.row]), N))
@inline function Base.isless(ocr1::OnColRow{<:NTuple{2, AbstractVector}},
ocr2::OnColRow{<:NTuple{2, AbstractVector}})
r1 = ocr1.row
c11, c12 = ocr1.cols
r2 = ocr2.row
c21, c22 = ocr2.cols
c11r = @inbounds c11[r1]
c12r = @inbounds c12[r1]
c21r = @inbounds c21[r2]
c22r = @inbounds c22[r2]
isless(c11r, c21r) || (isequal(c11r, c21r) && isless(c12r, c22r))
end
@inline Base.isless(ocr1::OnColRow{<:NTuple{N,AbstractVector}},
ocr2::OnColRow{<:NTuple{N,AbstractVector}}) where {N} =
isless(ntuple(i -> @inbounds(ocr1.cols[i][ocr1.row]), N),
ntuple(i -> @inbounds(ocr2.cols[i][ocr2.row]), N))
prepare_on_col() = throw(ArgumentError("at least one on column required when joining"))
prepare_on_col(c::AbstractVector) = c
prepare_on_col(cs::AbstractVector...) = OnCol(cs...)
# Return if it is allowed to use refpool instead of the original array for joining.
# There are multiple conditions that must be met to allow for this.
# If it is allowed we are sure that missing can be used as a sentinel
check_mapping_allowed(short::AbstractVector, refarray_long::AbstractVector,
refpool_long, invrefpool_long) =
!isempty(short) && !isnothing(refpool_long) && !isnothing(invrefpool_long) &&
eltype(refarray_long) <: Union{Signed, Unsigned}
@noinline map_refarray(mapping::AbstractVector, refarray::AbstractVector,
::Val{fi}) where {fi} =
[@inbounds mapping[r - fi + 1] for r in refarray]
function map2refs(x::AbstractVector, invrefpool)
x_refpool = DataAPI.refpool(x)
if x_refpool isa AbstractVector{<:Integer} && 0 <= firstindex(x_refpool) <= 1
# here we know that x_refpool is AbstractVector that allows integer indexing
# and its firstindex must be an integer
# if firstindex is not 0 or 1 then we fallback to slow path for safety reasons
# all refpool we currently know have firstindex 0 or 1
# if there is some very strange firstindex we might run into overflow issues
# below use function barrier as mapping is not type stable
mapping = [get(invrefpool, v, missing) for v in x_refpool]
return map_refarray(mapping, DataAPI.refarray(x), Val(Int(firstindex(x_refpool))))
else
return [get(invrefpool, v, missing) for v in x]
end
end
function preprocess_columns(joiner::DataFrameJoiner)
right_len = length(joiner.dfr_on[!, 1])
left_len = length(joiner.dfl_on[!, 1])
right_shorter = right_len < left_len
left_cols = collect(eachcol(joiner.dfl_on))
right_cols = collect(eachcol(joiner.dfr_on))
# if column of the longer table supports DataAPI.refpool and DataAPI.invrefpool
# remap matching left and right columns to use refs
if right_shorter
for i in eachindex(left_cols, right_cols)
rc = right_cols[i]
lc = left_cols[i]
lc_refs = DataAPI.refarray(lc)
lc_refpool = DataAPI.refpool(lc)
lc_invrefpool = DataAPI.invrefpool(lc)
if check_mapping_allowed(rc, lc_refs, lc_refpool, lc_invrefpool)
right_cols[i] = map2refs(rc, lc_invrefpool)
left_cols[i] = lc_refs
end
end
else
for i in eachindex(left_cols, right_cols)
rc = right_cols[i]
lc = left_cols[i]
rc_refs = DataAPI.refarray(rc)
rc_refpool = DataAPI.refpool(rc)
rc_invrefpool = DataAPI.invrefpool(rc)
if check_mapping_allowed(lc, rc_refs, rc_refpool, rc_invrefpool)
right_cols[i] = rc_refs
left_cols[i] = map2refs(lc, rc_invrefpool)
end
end
end
disallow_sorted = false
for (lc, rc) in zip(left_cols, right_cols)
@assert length(lc) == left_len
@assert length(rc) == right_len
lc_et = nonmissingtype(eltype(lc))
rc_et = nonmissingtype(eltype(rc))
# special case common safe scenarios when eltype between left and right
# column can be different or non-concrete
lc_et <: Real && rc_et <: Real && continue
lc_et <: AbstractString && rc_et <: AbstractString && continue
# otherwise we require non-missing eltype of both sides to be the same and concrete
lc_et === rc_et && isconcretetype(lc_et) && continue
# we disallow using sorted branch for some columns that theoretically
# could be safely sorted (e.g. having Any eltype but holding strings)
# for safety reasons assuming that such cases will be rare in practice
disallow_sorted = true
end
# TODO:
# If DataAPI.invrefpool vectors are found in the "on" columns
# then potentially the following optimizations can be done:
# 1. identify rows in shorter table that should be dropped
# 2. develop custom _innerjoin_sorted and _innerjoin_unsorted that drop rows
# from shorter table that do not match rows from longer table based on
# PooledArray refpool check
# This optimization would significantly complicate the code (especially
# sorted path). It should be added if in practice we find that the use case
# is often enough and that the benefits are significant. The two cases when
# the benefits should be expected are:
# 1. Shorter table is sorted when we drop rows not matching longer table rows
# 2. Shorter table does not have duplicates when we drop rows not matching
# longer table rows
left_col = prepare_on_col(left_cols...)
right_col = prepare_on_col(right_cols...)
return left_col, right_col, right_shorter, disallow_sorted
end
### innerjoin logic
@inline function find_next_range(x::AbstractArray, start::Int, start_value)
stop_value = start_value
n = length(x)
stop = start + 1
while stop <= n
@inbounds stop_value = x[stop]
isequal(start_value, stop_value) || break
stop += 1
end
return stop, stop_value
end
function _innerjoin_sorted(left::AbstractArray, right::AbstractArray)
left_n = length(left)
right_n = length(right)
left_ixs = Int[]
right_ixs = Int[]
(left_n == 0 || right_n == 0) && return left_ixs, right_ixs
# lower bound assuming we get matches
sizehint!(left_ixs, min(left_n, right_n))
sizehint!(right_ixs, min(left_n, right_n))
left_cur = 1
left_val = left[left_cur]
left_new, left_tmp = find_next_range(left, left_cur, left_val)
right_cur = 1
right_val = right[right_cur]
right_new, right_tmp = find_next_range(right, right_cur, right_val)
while left_cur <= left_n && right_cur <= right_n
if isequal(left_val, right_val)
if left_new - left_cur == right_new - right_cur == 1
push!(left_ixs, left_cur)
push!(right_ixs, right_cur)
else
idx = length(left_ixs)
left_range = left_cur:left_new - 1
right_range = right_cur:right_new - 1
to_grow = Base.checked_add(idx, Base.checked_mul(length(left_range),
length(right_range)))
resize!(left_ixs, to_grow)
resize!(right_ixs, to_grow)
@inbounds for right_i in right_range, left_i in left_range
idx += 1
left_ixs[idx] = left_i
right_ixs[idx] = right_i
end
end
left_cur, left_val = left_new, left_tmp
left_new, left_tmp = find_next_range(left, left_cur, left_val)
right_cur, right_val = right_new, right_tmp
right_new, right_tmp = find_next_range(right, right_cur, right_val)
elseif isless(left_val, right_val)
left_cur, left_val = left_new, left_tmp
left_new, left_tmp = find_next_range(left, left_cur, left_val)
else
right_cur, right_val = right_new, right_tmp
right_new, right_tmp = find_next_range(right, right_cur, right_val)
end
end
return left_ixs, right_ixs
end
# optimistically assume that shorter table does not have duplicates in on column
# if this is not the case we call _innerjoin_dup
# which efficiently uses the work already done and continues with the more
# memory expensive algorithm that allows for duplicates
function _innerjoin_unsorted(left::AbstractArray, right::AbstractArray{T}) where {T}
dict = Dict{T, Int}()
right_len = length(right)
# we make sure that:
# * we do not preallocate dict of size larger than half of size of Int
# (this is relevant in 32 bit architectures)
# * dict has at least 2x more slots than the number of values we
# might store in it to avoid reallocations of internal structures when
# we populate it later and to minimize the number of hash collisions;
# typically Dict allows for 16 probes;
# the value of multiplier is heuristic was determined by empirical tests
sizehint!(dict, 2 * min(right_len, typemax(Int) >> 2))
right isa OnCol && _prehash(right)
left isa OnCol && _prehash(left)
for (idx_r, val_r) in enumerate(right)
haskey(dict, val_r) && return _innerjoin_dup(left, right, dict, idx_r)
dict[val_r] = idx_r
end
left_ixs = Int[]
right_ixs = Int[]
# lower bound assuming we get matches
sizehint!(left_ixs, right_len)
sizehint!(right_ixs, right_len)
for (idx_l, val_l) in enumerate(left)
# we know that dict contains only positive values
idx_r = get(dict, val_l, -1)
if idx_r != -1
push!(left_ixs, idx_l)
push!(right_ixs, idx_r)
end
end
return left_ixs, right_ixs
end
extrema_missing(x::AbstractVector{Missing}) = (1, 0)
function extrema_missing(x::AbstractVector{T}) where {T<:Union{Integer, Missing}}
try
return extrema(skipmissing(x))
catch
S = nonmissingtype(T)
return S(1), S(0)
end
end
function _innerjoin_unsorted_int(left::AbstractVector{<:Union{Integer, Missing}},
right::AbstractVector{<:Union{Integer, Missing}})
minv, maxv = extrema_missing(right)
val_range = big(maxv) - big(minv)
if val_range > typemax(Int) - 3 || val_range ÷ 2 > max(64, length(right)) ||
minv < typemin(Int) + 2 || maxv > typemax(Int) - 3
return _innerjoin_unsorted(left, right)
end
offset = 1 - Int(minv) # we are now sure it does not overflow
len = Int(maxv) - Int(minv) + 2
group_map = zeros(Int, len)
@inbounds for (idx_r, val_r) in enumerate(right)
i = val_r === missing ? length(group_map) : Int(val_r) + offset
if group_map[i] > 0
return _innerjoin_dup_int(left, right, group_map, idx_r, offset,
Int(minv), Int(maxv))
end
group_map[i] = idx_r
end
left_ixs = Int[]
right_ixs = Int[]
right_len = length(right)
sizehint!(left_ixs, right_len)
sizehint!(right_ixs, right_len)
@inbounds for (idx_l, val_l) in enumerate(left)
if val_l === missing
idx_r = group_map[end]
if idx_r > 0
push!(left_ixs, idx_l)
push!(right_ixs, idx_r)
end
elseif minv <= val_l <= maxv
idx_r = group_map[Int(val_l) + offset]
if idx_r > 0
push!(left_ixs, idx_l)
push!(right_ixs, idx_r)
end
end
end
return left_ixs, right_ixs
end
# we fall back to general case if we have duplicates
# normally it should happen fast as we reuse work already done
function _innerjoin_dup(left::AbstractArray, right::AbstractArray{T},
dict::Dict{T, Int}, idx_r_start::Int) where {T}
ngroups = idx_r_start - 1
right_len = length(right)
groups = Vector{Int}(undef, right_len)
groups[1:ngroups] = 1:ngroups
@inbounds for idx_r in idx_r_start:right_len
val_r = right[idx_r]
# we know that group ids are positive
group_id = get(dict, val_r, -1)
if group_id == -1
ngroups += 1
groups[idx_r] = ngroups
dict[val_r] = ngroups
else
groups[idx_r] = group_id
end
end
@assert ngroups > 0 # we should not get here with 0-length right
return _innerjoin_postprocess(left, dict, groups, ngroups, right_len)
end
function _innerjoin_dup_int(left::AbstractVector{<:Union{Integer, Missing}},
right::AbstractVector{<:Union{Integer, Missing}},
group_map::Vector{Int}, idx_r_start::Int, offset::Int,
minv::Int, maxv::Int)
ngroups = idx_r_start - 1
right_len = length(right)
groups = Vector{Int}(undef, right_len)
groups[1:ngroups] = 1:ngroups
@inbounds for idx_r in idx_r_start:right_len
val_r = right[idx_r]
i = val_r === missing ? length(group_map) : Int(val_r) + offset
group_map_val = group_map[i]
if group_map_val > 0
groups[idx_r] = group_map_val
else
ngroups += 1
groups[idx_r] = ngroups
group_map[i] = ngroups
end
end
@assert ngroups > 0 # we should not get here with 0-length right
return _innerjoin_postprocess_int(left, group_map, groups, ngroups, right_len,
offset, minv, maxv)
end
function compute_join_indices!(groups::Vector{Int}, ngroups::Int,
starts::Vector, rperm::Vector)
@inbounds for gix in groups
starts[gix] += 1
end
cumsum!(starts, starts)
@inbounds for (i, gix) in enumerate(groups)
rperm[starts[gix]] = i
starts[gix] -= 1
end
push!(starts, length(groups))
return nothing
end
function _innerjoin_postprocess(left::AbstractArray, dict::Dict{T, Int},
groups::Vector{Int}, ngroups::Int,
right_len::Int) where {T}
starts = zeros(Int, ngroups)
rperm = Vector{Int}(undef, right_len)
left_ixs = Int[]
right_ixs = Int[]
# lower bound assuming we get matches
sizehint!(left_ixs, right_len)
sizehint!(right_ixs, right_len)
compute_join_indices!(groups, ngroups, starts, rperm)
n = 0
@inbounds for (idx_l, val_l) in enumerate(left)
group_id = get(dict, val_l, -1)
if group_id != -1
ref_stop = starts[group_id + 1]
l = ref_stop - starts[group_id]
newn = n + l
resize!(left_ixs, newn)
for i in n+1:n+l
left_ixs[i] = idx_l
end
resize!(right_ixs, newn)
for i in 1:l
right_ixs[n + i] = rperm[ref_stop - i + 1]
end
n = newn
end
end
return left_ixs, right_ixs
end
function _innerjoin_postprocess_int(left::AbstractVector{<:Union{Integer, Missing}},
group_map::Vector{Int},
groups::Vector{Int}, ngroups::Int, right_len::Int,
offset::Int, minv::Int, maxv::Int)
starts = zeros(Int, ngroups)
rperm = Vector{Int}(undef, right_len)
left_ixs = Int[]
right_ixs = Int[]
sizehint!(left_ixs, right_len)
sizehint!(right_ixs, right_len)
compute_join_indices!(groups, ngroups, starts, rperm)
n = 0
@inbounds for (idx_l, val_l) in enumerate(left)
if val_l === missing
group_id = group_map[end]
elseif minv <= val_l <= maxv
group_id = group_map[Int(val_l) + offset]
else
group_id = 0
end
if group_id > 0
ref_stop = starts[group_id + 1]
l = ref_stop - starts[group_id]
newn = n + l
resize!(left_ixs, newn)
for i in n+1:n+l
left_ixs[i] = idx_l
end
resize!(right_ixs, newn)
for i in 1:l
right_ixs[n + i] = rperm[ref_stop - i + 1]
end
n = newn
end
end
return left_ixs, right_ixs
end
function find_inner_rows(joiner::DataFrameJoiner)
left_col, right_col, right_shorter, disallow_sorted = preprocess_columns(joiner)
# we treat this case separately so we know we have at least one element later
(isempty(left_col) || isempty(right_col)) && return Int[], Int[]
# if sorting is not disallowed try using a fast algorithm that works
# on sorted columns; if it is not run or errors fall back to the unsorted case
# the try-catch is used to handle the case when columns on which we join
# contain values that are not comparable
if !disallow_sorted
try
if issorted(left_col) && issorted(right_col)
return _innerjoin_sorted(left_col, right_col)
end
catch
# nothing to do - one of the columns is not sortable
end
end
if right_shorter
if left_col isa AbstractVector{<:Union{Integer, Missing}} &&
right_col isa AbstractVector{<:Union{Integer, Missing}}
return _innerjoin_unsorted_int(left_col, right_col)
else
return _innerjoin_unsorted(left_col, right_col)
end
else
if left_col isa AbstractVector{<:Union{Integer, Missing}} &&
right_col isa AbstractVector{<:Union{Integer, Missing}}
return reverse(_innerjoin_unsorted_int(right_col, left_col))
else
return reverse(_innerjoin_unsorted(right_col, left_col))
end
end
error("unreachable reached")
end
### semijoin logic
function _semijoin_sorted(left::AbstractArray, right::AbstractArray,
seen_rows::AbstractVector{Bool})
left_n = length(left)
right_n = length(right)
@assert left_n > 0 && right_n > 0
left_cur = 1
left_val = left[left_cur]
left_new, left_tmp = find_next_range(left, left_cur, left_val)
right_cur = 1
right_val = right[right_cur]
right_new, right_tmp = find_next_range(right, right_cur, right_val)
while left_cur <= left_n && right_cur <= right_n
if isequal(left_val, right_val)
seen_rows[left_cur:left_new - 1] .= true
left_cur, left_val = left_new, left_tmp
left_new, left_tmp = find_next_range(left, left_cur, left_val)
right_cur, right_val = right_new, right_tmp
right_new, right_tmp = find_next_range(right, right_cur, right_val)
elseif isless(left_val, right_val)
left_cur, left_val = left_new, left_tmp
left_new, left_tmp = find_next_range(left, left_cur, left_val)
else
right_cur, right_val = right_new, right_tmp
right_new, right_tmp = find_next_range(right, right_cur, right_val)
end
end
return seen_rows
end
# optimistically assume that shorter table does not have duplicates in on column
# if this is not the case we call _semijoin_dup
# which efficiently uses the work already done and continues with the more
# memory expensive algorithm that allows for duplicates
# note that in semijoin and antijoin we do not have to do it if right table is
# shorter as then we process left table row by row anyway
function _semijoin_unsorted(left::AbstractArray, right::AbstractArray{T},
seen_rows::AbstractVector{Bool},
right_shorter::Bool) where {T}
right_len = length(right)
right isa OnCol && _prehash(right)
left isa OnCol && _prehash(left)
if right_shorter
@assert length(left) == length(seen_rows)
set = Set{T}()
sizehint!(set, 2 * min(right_len, typemax(Int) >> 2))
for val_r in right
push!(set, val_r)
end
@inbounds for (idx_l, val_l) in enumerate(left)
seen_rows[idx_l] = val_l in set
end
else
@assert length(right) == length(seen_rows)
dict = Dict{T, Int}()
sizehint!(dict, 2 * min(right_len, typemax(Int) >> 2))
for (idx_r, val_r) in enumerate(right)
haskey(dict, val_r) && return _semijoin_dup(left, right, dict, idx_r,
seen_rows)
dict[val_r] = idx_r
end
@inbounds for (idx_l, val_l) in enumerate(left)
# we know that dict contains only positive values
idx_r = get(dict, val_l, -1)
if idx_r != -1
seen_rows[idx_r] = true
end
end
end
return seen_rows
end
function _semijoin_unsorted_int(left::AbstractVector{<:Union{Integer, Missing}},
right::AbstractVector{<:Union{Integer, Missing}},
seen_rows::AbstractVector{Bool},
right_shorter::Bool)
minv, maxv = extrema_missing(right)
val_range = big(maxv) - big(minv)
if val_range > typemax(Int) - 3 || val_range ÷ 2 > max(64, length(right)) ||
minv < typemin(Int) + 2 || maxv > typemax(Int) - 3
return _semijoin_unsorted(left, right, seen_rows, right_shorter)
end
offset = 1 - Int(minv) # we are now sure it does not overflow
len = Int(maxv) - Int(minv) + 2
group_map = zeros(Int, len)
if right_shorter
@inbounds for (idx_r, val_r) in enumerate(right)
i = val_r === missing ? length(group_map) : Int(val_r) + offset
group_map[i] = idx_r
end
@inbounds for (idx_l, val_l) in enumerate(left)
if val_l === missing
idx_r = group_map[end]
seen_rows[idx_l] = idx_r > 0
elseif minv <= val_l <= maxv
idx_r = group_map[Int(val_l) + offset]
seen_rows[idx_l] = idx_r > 0
end
end
else
@inbounds for (idx_r, val_r) in enumerate(right)
i = val_r === missing ? length(group_map) : Int(val_r) + offset
if group_map[i] > 0
return _semijoin_dup_int(left, right, group_map, idx_r, offset,
Int(minv), Int(maxv), seen_rows)
end
group_map[i] = idx_r
end
@inbounds for (idx_l, val_l) in enumerate(left)
if val_l === missing
idx_r = group_map[end]
if idx_r > 0
seen_rows[idx_r] = true
end
elseif minv <= val_l <= maxv
idx_r = group_map[Int(val_l) + offset]
if idx_r > 0
seen_rows[idx_r] = true
end
end
end
end
return seen_rows
end
# we fall back to general case if we have duplicates
# normally it should happen fast as we reuse work already done
function _semijoin_dup(left::AbstractArray, right::AbstractArray{T},
dict::Dict{T, Int}, idx_r_start::Int,
seen_rows::AbstractVector{Bool}) where {T}
ngroups = idx_r_start - 1
right_len = length(right)
groups = Vector{Int}(undef, right_len)
groups[1:ngroups] = 1:ngroups
@inbounds for idx_r in idx_r_start:right_len
val_r = right[idx_r]
# we know that group ids are positive
group_id = get(dict, val_r, -1)
if group_id == -1
ngroups += 1
groups[idx_r] = ngroups
dict[val_r] = ngroups
else
groups[idx_r] = group_id
end
end
@assert ngroups > 0 # we should not get here with 0-length right
@assert length(right) == length(seen_rows)
return _semijoin_postprocess(left, dict, groups, ngroups, right_len,
seen_rows)
end
function _semijoin_dup_int(left::AbstractVector{<:Union{Integer, Missing}},
right::AbstractVector{<:Union{Integer, Missing}},
group_map::Vector{Int}, idx_r_start::Int, offset::Int,
minv::Int, maxv::Int, seen_rows::AbstractVector{Bool})
ngroups = idx_r_start - 1
right_len = length(right)
groups = Vector{Int}(undef, right_len)
groups[1:ngroups] = 1:ngroups
@inbounds for idx_r in idx_r_start:right_len
val_r = right[idx_r]
i = val_r === missing ? length(group_map) : Int(val_r) + offset
group_map_val = group_map[i]
if group_map_val > 0
groups[idx_r] = group_map_val
else
ngroups += 1
groups[idx_r] = ngroups
group_map[i] = ngroups
end
end
@assert ngroups > 0 # we should not get here with 0-length right
@assert length(right) == length(seen_rows)
return _semijoin_postprocess_int(left, group_map, groups, ngroups, right_len,
offset, minv, maxv, seen_rows)
end
function _semijoin_postprocess(left::AbstractArray, dict::Dict{T, Int},
groups::Vector{Int}, ngroups::Int, right_len::Int,
seen_rows::AbstractVector{Bool}) where {T}
starts = zeros(Int, ngroups)
rperm = Vector{Int}(undef, right_len)
compute_join_indices!(groups, ngroups, starts, rperm)
@inbounds for (idx_l, val_l) in enumerate(left)
group_id = get(dict, val_l, -1)
if group_id != -1
ref_stop = starts[group_id + 1]
l = ref_stop - starts[group_id]
for i in 1:l
seen_rows[rperm[ref_stop - i + 1]] = true
end
end
end
return seen_rows
end
function _semijoin_postprocess_int(left::AbstractVector{<:Union{Integer, Missing}},
group_map::Vector{Int},
groups::Vector{Int}, ngroups::Int, right_len::Int,
offset::Int, minv::Int, maxv::Int,
seen_rows::AbstractVector{Bool})
starts = zeros(Int, ngroups)
rperm = Vector{Int}(undef, right_len)
compute_join_indices!(groups, ngroups, starts, rperm)
@inbounds for (idx_l, val_l) in enumerate(left)
if val_l === missing
group_id = group_map[end]
elseif minv <= val_l <= maxv
group_id = group_map[Int(val_l) + offset]
else
group_id = 0
end
if group_id > 0
ref_stop = starts[group_id + 1]
l = ref_stop - starts[group_id]
for i in 1:l
seen_rows[rperm[ref_stop - i + 1]] = true
end
end
end
return seen_rows
end
function find_semi_rows(joiner::DataFrameJoiner)
left_col, right_col, right_shorter, disallow_sorted = preprocess_columns(joiner)
seen_rows = falses(length(left_col))
# we treat this case separately so we know we have at least one element later
(isempty(left_col) || isempty(right_col)) && return falses(length(left_col))
# if sorting is not disallowed try using a fast algorithm that works
# on sorted columns; if it is not run or errors fall back to the unsorted case
# the try-catch is used to handle the case when columns on which we join
# contain values that are not comparable
if !disallow_sorted
try
if issorted(left_col) && issorted(right_col)
return _semijoin_sorted(left_col, right_col, seen_rows)
end
catch
# nothing to do - one of the columns is not sortable
end
end
if right_shorter
if left_col isa AbstractVector{<:Union{Integer, Missing}} &&
right_col isa AbstractVector{<:Union{Integer, Missing}}
return _semijoin_unsorted_int(left_col, right_col, seen_rows, right_shorter)
else
return _semijoin_unsorted(left_col, right_col, seen_rows, right_shorter)
end
else
if left_col isa AbstractVector{<:Union{Integer, Missing}} &&
right_col isa AbstractVector{<:Union{Integer, Missing}}
return _semijoin_unsorted_int(right_col, left_col, seen_rows, right_shorter)
else
return _semijoin_unsorted(right_col, left_col, seen_rows, right_shorter)
end
end
error("unreachable reached")
end