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

migrate ArrowTypes code that depends on Sockets and UUIDs to extensions #500

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
10 changes: 8 additions & 2 deletions src/ArrowTypes/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,17 @@
name = "ArrowTypes"
uuid = "31f734f8-188a-4ce0-8406-c8a06bd891cd"
authors = ["quinnj <[email protected]>"]
version = "2.3.0"
version = "2.4.0"

[deps]

[weakdeps]
Sockets = "6462fe0b-24de-5631-8697-dd941f90decc"
UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"

[extensions]
ArrowTypesSocketsExt = "Sockets"
ArrowTypesUUIDsExt = "UUIDs"

[compat]
julia = "1.0"
julia = "1.9"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not keep the existing Julia version compat and do this instead? https://pkgdocs.julialang.org/v1/creating-packages/#Transition-from-normal-dependency-to-extension

45 changes: 45 additions & 0 deletions src/ArrowTypes/ext/ArrowTypesSocketsExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "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
#
# 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.

module ArrowTypesSocketsExt

import ArrowTypes
import Sockets: IPv4, IPv6, IPAddr, InetAddr

ArrowTypes.ArrowKind(::Type{IPv4}) = ArrowTypes.PrimitiveKind()
ArrowTypes.ArrowType(::Type{IPv4}) = UInt32
ArrowTypes.toarrow(x::IPv4) = x.host
const IPV4_SYMBOL = Symbol("JuliaLang.IPv4")
ArrowTypes.arrowname(::Type{IPv4}) = IPV4_SYMBOL
ArrowTypes.JuliaType(::Val{IPV4_SYMBOL}) = IPv4
ArrowTypes.fromarrow(::Type{IPv4}, x::Integer) = IPv4(x)

ArrowTypes.ArrowKind(::Type{IPv6}) = ArrowTypes.FixedSizeListKind{16,UInt8}()
ArrowTypes.ArrowType(::Type{IPv6}) = NTuple{16,UInt8}
ArrowTypes.toarrow(x::IPv6) = reinterpret(NTuple{16,UInt8}, x.host)
const IPV6_SYMBOL = Symbol("JuliaLang.IPv6")
ArrowTypes.arrowname(::Type{IPv6}) = IPV6_SYMBOL
ArrowTypes.JuliaType(::Val{IPV6_SYMBOL}) = IPv6
ArrowTypes.fromarrow(::Type{IPv6}, x::NTuple{16,UInt8}) = IPv6(reinterpret(UInt128, x))

const INET_ADDR_SYMBOL = Symbol("JuliaLang.InetAddr")
ArrowTypes.arrowname(::Type{<:InetAddr}) = INET_ADDR_SYMBOL
ArrowTypes.JuliaType(
::Val{INET_ADDR_SYMBOL},
::Type{@NamedTuple{host::T, port::UInt16}},
) where {T<:IPAddr} = InetAddr{T}

end # module ArrowTypesSocketsExt
30 changes: 30 additions & 0 deletions src/ArrowTypes/ext/ArrowTypesUUIDsExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "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
#
# 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.

module ArrowTypesUUIDsExt

import ArrowTypes
import UUIDs: UUID

ArrowTypes.ArrowKind(::Type{UUID}) = ArrowTypes.FixedSizeListKind{16,UInt8}()
ArrowTypes.ArrowType(::Type{UUID}) = NTuple{16,UInt8}
ArrowTypes.toarrow(x::UUID) = reinterpret(NTuple{16,UInt8}, x.value)
const UUID_SYMBOL = Symbol("JuliaLang.UUID")
ArrowTypes.arrowname(::Type{UUID}) = UUID_SYMBOL
ArrowTypes.JuliaType(::Val{UUID_SYMBOL}) = UUID
ArrowTypes.fromarrow(::Type{UUID}, x::NTuple{16,UInt8}) = UUID(reinterpret(UInt128, x))

end # module ArrowTypesUUIDsExt
43 changes: 0 additions & 43 deletions src/ArrowTypes/src/ArrowTypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ in order to signal how they should be serialized in the arrow format.
"""
module ArrowTypes

using Sockets
using UUIDs

export ArrowKind,
NullKind,
PrimitiveKind,
Expand Down Expand Up @@ -261,46 +258,6 @@ getsize(::FixedSizeListKind{N,T}) where {N,T} = N

ArrowKind(::Type{NTuple{N,T}}) where {N,T} = FixedSizeListKind{N,T}()

ArrowKind(::Type{UUID}) = FixedSizeListKind{16,UInt8}()
ArrowType(::Type{UUID}) = NTuple{16,UInt8}
toarrow(x::UUID) = _cast(NTuple{16,UInt8}, x.value)
const UUIDSYMBOL = Symbol("JuliaLang.UUID")
arrowname(::Type{UUID}) = UUIDSYMBOL
JuliaType(::Val{UUIDSYMBOL}) = UUID
fromarrow(::Type{UUID}, x::NTuple{16,UInt8}) = UUID(_cast(UInt128, x))

ArrowKind(::Type{IPv4}) = PrimitiveKind()
ArrowType(::Type{IPv4}) = UInt32
toarrow(x::IPv4) = x.host
const IPV4_SYMBOL = Symbol("JuliaLang.IPv4")
arrowname(::Type{IPv4}) = IPV4_SYMBOL
JuliaType(::Val{IPV4_SYMBOL}) = IPv4
fromarrow(::Type{IPv4}, x::Integer) = IPv4(x)

ArrowKind(::Type{IPv6}) = FixedSizeListKind{16,UInt8}()
ArrowType(::Type{IPv6}) = NTuple{16,UInt8}
toarrow(x::IPv6) = _cast(NTuple{16,UInt8}, x.host)
const IPV6_SYMBOL = Symbol("JuliaLang.IPv6")
arrowname(::Type{IPv6}) = IPV6_SYMBOL
JuliaType(::Val{IPV6_SYMBOL}) = IPv6
fromarrow(::Type{IPv6}, x::NTuple{16,UInt8}) = IPv6(_cast(UInt128, x))

function _cast(::Type{Y}, x)::Y where {Y}
y = Ref{Y}()
_unsafe_cast!(y, Ref(x), 1)
return y[]
end

function _unsafe_cast!(y::Ref{Y}, x::Ref, n::Integer) where {Y}
X = eltype(x)
GC.@preserve x y begin
ptr_x = Base.unsafe_convert(Ptr{X}, x)
ptr_y = Base.unsafe_convert(Ptr{Y}, y)
unsafe_copyto!(Ptr{X}(ptr_y), ptr_x, n)
end
return y
end

"StructKind data are stored in separate buffers for each field of the struct"
struct StructKind <: ArrowKind end

Expand Down
30 changes: 22 additions & 8 deletions src/ArrowTypes/test/tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -100,31 +100,45 @@ end
@test K == ArrowTypes.FixedSizeListKind{3,UInt8}()

u = UUID(rand(UInt128))
ubytes = ArrowTypes._cast(NTuple{16,UInt8}, u.value)
ubytes = reinterpret(NTuple{16,UInt8}, u.value)
@test ArrowTypes.ArrowKind(u) == ArrowTypes.FixedSizeListKind{16,UInt8}()
@test ArrowTypes.ArrowType(UUID) == NTuple{16,UInt8}
@test ArrowTypes.toarrow(u) == ubytes
@test ArrowTypes.arrowname(UUID) == ArrowTypes.UUIDSYMBOL
@test ArrowTypes.JuliaType(Val(ArrowTypes.UUIDSYMBOL)) == UUID
UUID_SYMBOL = Symbol("JuliaLang.UUID")
@test ArrowTypes.arrowname(UUID) == UUID_SYMBOL
@test ArrowTypes.JuliaType(Val(UUID_SYMBOL)) == UUID
@test ArrowTypes.fromarrow(UUID, ubytes) == u

ip4 = IPv4(rand(UInt32))
@test ArrowTypes.ArrowKind(ip4) == PrimitiveKind()
@test ArrowTypes.ArrowType(IPv4) == UInt32
@test ArrowTypes.toarrow(ip4) == ip4.host
@test ArrowTypes.arrowname(IPv4) == ArrowTypes.IPV4_SYMBOL
@test ArrowTypes.JuliaType(Val(ArrowTypes.IPV4_SYMBOL)) == IPv4
IPV4_SYMBOL = Symbol("JuliaLang.IPv4")
@test ArrowTypes.arrowname(IPv4) == IPV4_SYMBOL
@test ArrowTypes.JuliaType(Val(IPV4_SYMBOL)) == IPv4
@test ArrowTypes.fromarrow(IPv4, ip4.host) == ip4

ip6 = IPv6(rand(UInt128))
ip6_ubytes = ArrowTypes._cast(NTuple{16,UInt8}, ip6.host)
ip6_ubytes = reinterpret(NTuple{16,UInt8}, ip6.host)
@test ArrowTypes.ArrowKind(ip6) == ArrowTypes.FixedSizeListKind{16,UInt8}()
@test ArrowTypes.ArrowType(IPv6) == NTuple{16,UInt8}
@test ArrowTypes.toarrow(ip6) == ip6_ubytes
@test ArrowTypes.arrowname(IPv6) == ArrowTypes.IPV6_SYMBOL
@test ArrowTypes.JuliaType(Val(ArrowTypes.IPV6_SYMBOL)) == IPv6
IPV6_SYMBOL = Symbol("JuliaLang.IPv6")
@test ArrowTypes.arrowname(IPv6) == IPV6_SYMBOL
@test ArrowTypes.JuliaType(Val(IPV6_SYMBOL)) == IPv6
@test ArrowTypes.fromarrow(IPv6, ip6_ubytes) == ip6

INET_ADDR_SYMBOL = Symbol("JuliaLang.InetAddr")
@test ArrowTypes.arrowname(Sockets.InetAddr{IPv4}) == INET_ADDR_SYMBOL
@test ArrowTypes.JuliaType(
Val(INET_ADDR_SYMBOL),
@NamedTuple{host::IPv4, port::UInt16},
) == Sockets.InetAddr{IPv4}
@test ArrowTypes.JuliaType(
Val(INET_ADDR_SYMBOL),
@NamedTuple{host::IPv6, port::UInt16},
) == Sockets.InetAddr{IPv6}

nt = (id=1, name="bob")
@test ArrowTypes.ArrowKind(NamedTuple) == ArrowTypes.StructKind()
@test ArrowTypes.fromarrow(typeof(nt), nt) === nt
Expand Down
Loading