Skip to content

Commit f50ef6c

Browse files
author
Pietro Vertechi
authored
implement lazy row (JuliaArrays#24)
1 parent 23b4400 commit f50ef6c

File tree

4 files changed

+36
-6
lines changed

4 files changed

+36
-6
lines changed

src/StructArrays.jl

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ include("structarray.jl")
88
include("utils.jl")
99
include("collect.jl")
1010
include("sort.jl")
11+
include("lazy.jl")
1112

1213
function __init__()
1314
Requires.@require Tables="bd369af6-aec1-5ad0-b16a-f7cc5008161c" include("tables.jl")
@@ -16,6 +17,7 @@ function __init__()
1617
end
1718
Requires.@require WeakRefStrings="ea10d353-3f73-51f8-a26c-33c1cb351aa5" begin
1819
isstringarray(::WeakRefStrings.StringArray) = true
20+
arrayof(::Type{T}, d) where {T<:AbstractString} = WeakRefStrings.StringArray{T}(d)
1921
end
2022
end
2123

src/collect.jl

+13-6
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
1-
struct StructArrayInitializer{F}
1+
arrayof(S, d) = Array{S}(undef, d)
2+
3+
struct StructArrayInitializer{F, G}
24
unwrap::F
5+
arrayof::G
36
end
4-
StructArrayInitializer() = StructArrayInitializer(t -> false)
7+
StructArrayInitializer(unwrap = t->false) = StructArrayInitializer(unwrap, arrayof)
58

69
const default_initializer = StructArrayInitializer()
710

8-
(s::StructArrayInitializer)(S, d) = StructArray{S}(undef, d; unwrap = s.unwrap)
11+
function (s::StructArrayInitializer)(S, d)
12+
ai = ArrayInitializer(s.unwrap, s.arrayof)
13+
buildfromschema(typ -> ai(typ, d), S)
14+
end
915

10-
struct ArrayInitializer{F}
16+
struct ArrayInitializer{F, G}
1117
unwrap::F
18+
arrayof::G
1219
end
13-
ArrayInitializer() = ArrayInitializer(t -> false)
20+
ArrayInitializer(unwrap = t->false) = ArrayInitializer(unwrap, arrayof)
1421

15-
(s::ArrayInitializer)(S, d) = _undef_array(S, d; unwrap = s.unwrap)
22+
(s::ArrayInitializer)(S, d) = s.unwrap(S) ? buildfromschema(typ -> s(typ, d), S) : s.arrayof(S, d)
1623

1724
_reshape(v, itr, ::Base.HasShape) = reshape(v, axes(itr))
1825
_reshape(v, itr, ::Union{Base.HasLength, Base.SizeUnknown}) = v

src/lazy.jl

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
struct LazyRow{T, N, C, I}
2+
columns::StructArray{T, N, C} # a `Columns` object
3+
index::I
4+
end
5+
6+
Base.getproperty(c::LazyRow, nm::Symbol) = getproperty(getfield(c, 1), nm)[getfield(c, 2)]
7+
Base.setproperty!(c::LazyRow, nm::Symbol, val) = (getproperty(getfield(c, 1), nm)[getfield(c, 2)] = val; nothing)
8+
Base.propertynames(c::LazyRow) = propertynames(getfield(c, 1))
9+
10+
staticschema(::Type{<:LazyRow{T}}) where {T} = staticschema(T)
11+
buildfromschema(f, ::Type{<:LazyRow{T}}) where {T} = buildfromschema(f, T)
12+
13+
iscompatible(::Type{<:LazyRow{S}}, ::Type{StructArray{T, N, C}}) where {S, T, N, C} =
14+
iscompatible(S, StructArray{T, N, C})
15+
16+
(s::ArrayInitializer)(::Type{<:LazyRow{T}}, d) where {T} = buildfromschema(typ -> s(typ, d), T)

test/runtests.jl

+5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using StructArrays
2+
using StructArrays: LazyRow
23
import Tables, PooledArrays, WeakRefStrings
34
using Test
45

@@ -251,6 +252,10 @@ collect_fieldarrays_rec(t) = StructArrays.collect_fieldarrays(t, initializer = i
251252
@test collect_fieldarrays_rec(v) == StructArray((a = [1, 1.2], b = Int[2, 3]))
252253
@test typeof(collect_fieldarrays_rec(v)) == typeof(StructArray((a = [1, 1.2], b = Int[2, 3])))
253254

255+
s = StructArray(a = [1, 2], b = [3, 4])
256+
@test StructArrays.collect_fieldarrays(LazyRow(s, i) for i in eachindex(s)) == s
257+
@test collect_fieldarrays_rec(LazyRow(s, i) for i in eachindex(s)) == s
258+
254259
v = [(a = 1, b = 2), (a = 1.2, b = "3")]
255260
@test collect_fieldarrays_rec(v) == StructArray((a = [1, 1.2], b = Any[2, "3"]))
256261
@test typeof(collect_fieldarrays_rec(v)) == typeof(StructArray((a = [1, 1.2], b = Any[2, "3"])))

0 commit comments

Comments
 (0)