Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove scopedenum for EnumX #444

Merged
merged 2 commits into from
May 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

name = "Arrow"
uuid = "69666777-d1a9-59fb-9406-91d4454c9d45"
Expand All @@ -27,6 +26,7 @@ CodecLz4 = "5ba52731-8f18-5e0d-9241-30f10d1ec561"
CodecZstd = "6b39b394-51ab-5f42-8807-6242bab2b4c2"
DataAPI = "9a962f9c-6df0-11e9-0e5d-c546b8b5ee8a"
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
EnumX = "4e289a0a-7415-4d19-859d-a7e5c4648b56"
LoggingExtras = "e6f89c97-d47a-5376-807f-9c37f3926c36"
Mmap = "a63ad114-7e13-5084-954f-fe012c677804"
PooledArrays = "2dfb63ee-cc39-5dd5-95bd-886bf059d720"
Expand All @@ -43,6 +43,7 @@ BitIntegers = "0.2, 0.3"
CodecLz4 = "0.4"
CodecZstd = "0.7"
DataAPI = "1"
EnumX = "1"
FilePathsBase = "0.9"
LoggingExtras = "0.4, 1"
PooledArrays = "0.5, 1.0"
Expand All @@ -59,11 +60,10 @@ DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
FilePathsBase = "48062228-2e41-5def-b9a4-89aafe57970f"
JSON3 = "0f8b85d8-7281-11e9-16c2-39a750bddbf1"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
StructTypes = "856f2bd8-1eba-4b0a-8007-ebc267875bd4"
Sockets = "6462fe0b-24de-5631-8697-dd941f90decc"
StructTypes = "856f2bd8-1eba-4b0a-8007-ebc267875bd4"
TOML = "fa267f1f-6049-4f14-aa54-33bafae1ed76"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["CategoricalArrays", "DataFrames", "FilePathsBase", "JSON3", "Random", "StructTypes",
"TOML", "Test", "Sockets"]
test = ["CategoricalArrays", "DataFrames", "FilePathsBase", "JSON3", "Random", "StructTypes", "TOML", "Test", "Sockets"]
108 changes: 1 addition & 107 deletions src/FlatBuffers/FlatBuffers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const VOffsetT = UInt16
const VtableMetadataFields = 2

basetype(::Enum) = UInt8
basetype(::Type{T}) where {T <: Enum{S}} where {S} = S

function readbuffer(t::AbstractVector{UInt8}, pos::Integer, ::Type{Bool})
@inbounds b = t[pos + 1]
Expand All @@ -49,111 +50,4 @@ function Base.show(io::IO, x::TableOrStruct)
end
end

abstract type ScopedEnum{T<:Integer} <: Enum{T} end

macro scopedenum(T, syms...)
if isempty(syms)
throw(ArgumentError("no arguments given for ScopedEnum $T"))
end
basetype = Int32
typename = T
if isa(T, Expr) && T.head === :(::) && length(T.args) == 2 && isa(T.args[1], Symbol)
typename = T.args[1]
basetype = Core.eval(__module__, T.args[2])
if !isa(basetype, DataType) || !(basetype <: Integer) || !isbitstype(basetype)
throw(ArgumentError("invalid base type for ScopedEnum $typename, $T=::$basetype; base type must be an integer primitive type"))
end
elseif !isa(T, Symbol)
throw(ArgumentError("invalid type expression for ScopedEnum $T"))
end
values = basetype[]
seen = Set{Symbol}()
namemap = Dict{basetype,Symbol}()
lo = hi = 0
i = zero(basetype)
hasexpr = false

if length(syms) == 1 && syms[1] isa Expr && syms[1].head === :block
syms = syms[1].args
end
for s in syms
s isa LineNumberNode && continue
if isa(s, Symbol)
if i == typemin(basetype) && !isempty(values)
throw(ArgumentError("overflow in value \"$s\" of ScopedEnum $typename"))
end
elseif isa(s, Expr) &&
(s.head === :(=) || s.head === :kw) &&
length(s.args) == 2 && isa(s.args[1], Symbol)
i = Core.eval(__module__, s.args[2]) # allow exprs, e.g. uint128"1"
if !isa(i, Integer)
throw(ArgumentError("invalid value for ScopedEnum $typename, $s; values must be integers"))
end
i = convert(basetype, i)
s = s.args[1]
hasexpr = true
else
throw(ArgumentError(string("invalid argument for ScopedEnum ", typename, ": ", s)))
end
if !Base.isidentifier(s)
throw(ArgumentError("invalid name for ScopedEnum $typename; \"$s\" is not a valid identifier"))
end
if hasexpr && haskey(namemap, i)
throw(ArgumentError("both $s and $(namemap[i]) have value $i in ScopedEnum $typename; values must be unique"))
end
namemap[i] = s
push!(values, i)
if s in seen
throw(ArgumentError("name \"$s\" in ScopedEnum $typename is not unique"))
end
push!(seen, s)
if length(values) == 1
lo = hi = i
else
lo = min(lo, i)
hi = max(hi, i)
end
i += oneunit(i)
end
defs = Expr(:block)
if isa(typename, Symbol)
for (i, sym) in namemap
push!(defs.args, :(const $(esc(sym)) = $(esc(typename))($i)))
end
end
_typename_str = string(typename)
mod = Symbol(_typename_str, last(_typename_str) == 's' ? "es" : "s")
syms = Tuple(Base.values(namemap))
blk = quote
module $(esc(mod))
export $(esc(typename))
# enum definition
primitive type $(esc(typename)) <: ScopedEnum{$(basetype)} $(sizeof(basetype) * 8) end
function $(esc(typename))(x::Integer)
$(Base.Enums.membershiptest(:x, values)) || Base.Enums.enum_argument_error($(Expr(:quote, typename)), x)
return Core.bitcast($(esc(typename)), convert($(basetype), x))
end
if isdefined(Base.Enums, :namemap)
Base.Enums.namemap(::Type{$(esc(typename))}) = $(esc(namemap))
end
Base.typemin(x::Type{$(esc(typename))}) = $(esc(typename))($lo)
Base.typemax(x::Type{$(esc(typename))}) = $(esc(typename))($hi)
let insts = (Any[ $(esc(typename))(v) for v in $values ]...,)
Base.instances(::Type{$(esc(typename))}) = insts
end
FlatBuffers.basetype(::$(esc(typename))) = $(basetype)
FlatBuffers.basetype(::Type{$(esc(typename))}) = $(basetype)
$defs
#Base.getproperty(::Type{$(esc(typename))}, sym::Symbol) = sym in $syms ? getfield($(esc(mod)), sym) : getfield($(esc(typename)), sym)
export $(syms...)
end
end
push!(blk.args, :nothing)
blk.head = :toplevel
push!(blk.args, :(using .$mod))
# Return the newly created module, since it's (now) meant to be user visible.
push!(blk.args, esc(mod))
return blk
end

end # module
10 changes: 5 additions & 5 deletions src/arraytypes/arraytypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ function toarrowvector(x, i=1, de=Dict{Int64, Any}(), ded=DictEncoding[], meta=g
@debugv 3 x
A = arrowvector(x, i, 0, 0, de, ded, meta; compression=compression, kw...)
if compression isa LZ4FrameCompressor
A = compress(Meta.CompressionTypes.LZ4_FRAME, compression, A)
A = compress(Meta.CompressionType.LZ4_FRAME, compression, A)
elseif compression isa Vector{LZ4FrameCompressor}
A = compress(Meta.CompressionTypes.LZ4_FRAME, compression[Threads.threadid()], A)
A = compress(Meta.CompressionType.LZ4_FRAME, compression[Threads.threadid()], A)
elseif compression isa ZstdCompressor
A = compress(Meta.CompressionTypes.ZSTD, compression, A)
A = compress(Meta.CompressionType.ZSTD, compression, A)
elseif compression isa Vector{ZstdCompressor}
A = compress(Meta.CompressionTypes.ZSTD, compression[Threads.threadid()], A)
A = compress(Meta.CompressionType.ZSTD, compression[Threads.threadid()], A)
end
@debugv 2 "converted top-level column to arrow format: $(typeof(A))"
@debugv 3 A
Expand Down Expand Up @@ -99,7 +99,7 @@ Base.size(v::NullVector) = (length(v.data),)
Base.getindex(v::NullVector{T}, i::Int) where {T} = ArrowTypes.fromarrow(T, getindex(v.data, i))

arrowvector(::NullKind, x, i, nl, fi, de, ded, meta; kw...) = NullVector{eltype(x)}(MissingVector(length(x)), isnothing(meta) ? nothing : toidict(meta))
compress(Z::Meta.CompressionType, comp, v::NullVector) =
compress(Z::Meta.CompressionType.T, comp, v::NullVector) =
Compressed{Z, NullVector}(v, CompressedBuffer[], length(v), length(v), Compressed[])

function makenodesbuffers!(col::NullVector, fieldnodes, fieldbuffers, bufferoffset, alignment)
Expand Down
2 changes: 1 addition & 1 deletion src/arraytypes/bool.jl
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ function arrowvector(::BoolKind, x, i, nl, fi, de, ded, meta; kw...)
return BoolVector{eltype(x)}(bytes, 1, validity, len, meta)
end

function compress(Z::Meta.CompressionType, comp, p::P) where {P <: BoolVector}
function compress(Z::Meta.CompressionType.T, comp, p::P) where {P <: BoolVector}
len = length(p)
nc = nullcount(p)
validity = compress(Z, comp, p.validity)
Expand Down
6 changes: 3 additions & 3 deletions src/arraytypes/compressed.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,16 @@ Base.eltype(c::Compressed{Z, A}) where {Z, A} = eltype(A)
getmetadata(x::Compressed) = getmetadata(x.data)
compressiontype(c::Compressed{Z}) where {Z} = Z

function compress(Z::Meta.CompressionType, comp, x::Array)
function compress(Z::Meta.CompressionType.T, comp, x::Array)
GC.@preserve x begin
y = unsafe_wrap(Array, convert(Ptr{UInt8}, pointer(x)), sizeof(x))
return CompressedBuffer(transcode(comp, y), length(y))
end
end

compress(Z::Meta.CompressionType, comp, x) = compress(Z, comp, convert(Array, x))
compress(Z::Meta.CompressionType.T, comp, x) = compress(Z, comp, convert(Array, x))

compress(Z::Meta.CompressionType, comp, v::ValidityBitmap) =
compress(Z::Meta.CompressionType.T, comp, v::ValidityBitmap) =
v.nc == 0 ? CompressedBuffer(UInt8[], 0) : compress(Z, comp, view(v.bytes, v.pos:(v.pos + cld(v.ℓ, 8) - 1)))

function makenodesbuffers!(col::Compressed, fieldnodes, fieldbuffers, bufferoffset, alignment)
Expand Down
2 changes: 1 addition & 1 deletion src/arraytypes/dictencoding.jl
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ function Base.copy(x::DictEncoded{T, S}) where {T, S}
return PooledArray(PooledArrays.RefArray(refs), Dict{T, S}(val => i for (i, val) in enumerate(pool)), pool)
end

function compress(Z::Meta.CompressionType, comp, x::A) where {A <: DictEncoded}
function compress(Z::Meta.CompressionType.T, comp, x::A) where {A <: DictEncoded}
len = length(x)
nc = nullcount(x)
validity = compress(Z, comp, x.validity)
Expand Down
2 changes: 1 addition & 1 deletion src/arraytypes/fixedsizelist.jl
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ function arrowvector(::FixedSizeListKind{N, T}, x, i, nl, fi, de, ded, meta; kw.
return FixedSizeList{S, typeof(data)}(UInt8[], validity, data, len, meta)
end

function compress(Z::Meta.CompressionType, comp, x::FixedSizeList{T, A}) where {T, A}
function compress(Z::Meta.CompressionType.T, comp, x::FixedSizeList{T, A}) where {T, A}
len = length(x)
nc = nullcount(x)
validity = compress(Z, comp, x.validity)
Expand Down
2 changes: 1 addition & 1 deletion src/arraytypes/list.jl
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ function arrowvector(::ListKind, x, i, nl, fi, de, ded, meta; largelists::Bool=f
return List{T, eltype(flat.inds), typeof(data)}(UInt8[], validity, offsets, data, len, meta)
end

function compress(Z::Meta.CompressionType, comp, x::List{T, O, A}) where {T, O, A}
function compress(Z::Meta.CompressionType.T, comp, x::List{T, O, A}) where {T, O, A}
len = length(x)
nc = nullcount(x)
validity = compress(Z, comp, x.validity)
Expand Down
2 changes: 1 addition & 1 deletion src/arraytypes/map.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function arrowvector(::MapKind, x, i, nl, fi, de, ded, meta; largelists::Bool=fa
return Map{withmissing(ET, Dict{K, V}), eltype(flat.inds), typeof(data)}(validity, offsets, data, len, meta)
end

function compress(Z::Meta.CompressionType, comp, x::A) where {A <: Map}
function compress(Z::Meta.CompressionType.T, comp, x::A) where {A <: Map}
len = length(x)
nc = nullcount(x)
validity = compress(Z, comp, x.validity)
Expand Down
2 changes: 1 addition & 1 deletion src/arraytypes/primitive.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function arrowvector(::PrimitiveKind, x, i, nl, fi, de, ded, meta; kw...)
return Primitive(eltype(x), UInt8[], validity, x, length(x), meta)
end

function compress(Z::Meta.CompressionType, comp, p::P) where {P <: Primitive}
function compress(Z::Meta.CompressionType.T, comp, p::P) where {P <: Primitive}
len = length(p)
nc = nullcount(p)
validity = compress(Z, comp, p.validity)
Expand Down
2 changes: 1 addition & 1 deletion src/arraytypes/struct.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ function arrowvector(::StructKind, x, i, nl, fi, de, ded, meta; kw...)
return Struct{withmissing(eltype(x), namedtupletype(T, data)), typeof(data)}(validity, data, len, meta)
end

function compress(Z::Meta.CompressionType, comp, x::A) where {A <: Struct}
function compress(Z::Meta.CompressionType.T, comp, x::A) where {A <: Struct}
len = length(x)
nc = nullcount(x)
validity = compress(Z, comp, x.validity)
Expand Down
12 changes: 6 additions & 6 deletions src/arraytypes/unions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# Union arrays
# need a custom representation of Union types since arrow unions
# are ordered, and possibly indirected via separate typeIds array
# here, T is Meta.UnionModes.Dense or Meta.UnionModes.Sparse,
# here, T is Meta.UnionMode.Dense or Meta.UnionMode.Sparse,
# typeIds is a NTuple{N, Int32}, and U is a Tuple{...} of the
# unioned types
struct UnionT{T, typeIds, U}
Expand Down Expand Up @@ -93,7 +93,7 @@ end

# convenience wrappers for signaling that an array shoudld be written
# as with dense/sparse union arrow buffers
struct DenseUnionVector{T, U} <: AbstractVector{UnionT{Meta.UnionModes.Dense, nothing, U}}
struct DenseUnionVector{T, U} <: AbstractVector{UnionT{Meta.UnionMode.Dense, nothing, U}}
itr::T
end

Expand All @@ -118,7 +118,7 @@ function todense(::Type{UnionT{T, typeIds, U}}, x) where {T, typeIds, U}
return types, offsets, data
end

struct SparseUnionVector{T, U} <: AbstractVector{UnionT{Meta.UnionModes.Sparse, nothing, U}}
struct SparseUnionVector{T, U} <: AbstractVector{UnionT{Meta.UnionMode.Sparse, nothing, U}}
itr::T
end

Expand Down Expand Up @@ -160,7 +160,7 @@ Base.@propagate_inbounds function Base.getindex(A::ToSparseUnion{T}, i::Integer)
return @inbounds x isa T ? x : ArrowTypes.default(T)
end

function compress(Z::Meta.CompressionType, comp, x::A) where {A <: DenseUnion}
function compress(Z::Meta.CompressionType.T, comp, x::A) where {A <: DenseUnion}
len = length(x)
nc = nullcount(x)
typeIds = compress(Z, comp, x.typeIds)
Expand Down Expand Up @@ -214,7 +214,7 @@ arrowvector(::UnionKind, x::Union{DenseUnion, SparseUnion}, i, nl, fi, de, ded,

function arrowvector(::UnionKind, x, i, nl, fi, de, ded, meta; kw...)
UT = eltype(x)
if unionmode(UT) == Meta.UnionModes.Dense
if unionmode(UT) == Meta.UnionMode.Dense
x = x isa DenseUnionVector ? x.itr : x
typeids, offsets, data = todense(UT, x)
data2 = map(y -> arrowvector(y[2], i, nl + 1, y[1], de, ded, nothing; kw...), enumerate(data))
Expand All @@ -229,7 +229,7 @@ function arrowvector(::UnionKind, x, i, nl, fi, de, ded, meta; kw...)
end
end

function compress(Z::Meta.CompressionType, comp, x::A) where {A <: SparseUnion}
function compress(Z::Meta.CompressionType.T, comp, x::A) where {A <: SparseUnion}
len = length(x)
nc = nullcount(x)
typeIds = compress(Z, comp, x.typeIds)
Expand Down
Loading