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

Add binary interval parsing #237

Merged
merged 5 commits into from
Oct 15, 2021
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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "LibPQ"
uuid = "194296ae-ab2e-5f79-8cd4-7183a0a5a0d1"
license = "MIT"
version = "1.8.1"
version = "1.9.0"

[deps]
BinaryProvider = "b99e7846-7c00-51b0-8f62-c81ae34c0232"
Expand Down
41 changes: 41 additions & 0 deletions src/parsing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,47 @@ function _interval_regex()
return Regex(String(take!(io)))
end

function _split_period(period::T, ::Type{P}) where {T<:Period,P<:Period}
q, r = divrem(period, convert(T, P(1)))
return P(q), r
end

# Splits internal interval types into the expected period types from
# https://www.postgresql.org/docs/10/datatype-datetime.html#DATATYPE-INTERVAL-INPUT
function _split_periods(months, days, microseconds)
periods = Period[]

push!(periods, _split_period(months, Year)...)
push!(periods, _split_period(days, Week)...)

seconds, ms = _split_period(microseconds, Second)
minutes, seconds = _split_period(seconds, Minute)
hours, minutes = _split_period(minutes, Hour)

push!(periods, hours, minutes, seconds, ms)
filter!(!iszero, periods)

return Dates.CompoundPeriod(periods)
end

# Parse binary into postgres interval
function Base.parse(
::Type{Dates.CompoundPeriod}, pqv::PQBinaryValue{PQ_SYSTEM_TYPES[:interval]}
)
current_pointer = data_pointer(pqv)

microsecond = Microsecond(ntoh(unsafe_load(Ptr{Int64}(current_pointer))))
current_pointer += sizeof(Int64)

day = Day(ntoh(unsafe_load(Ptr{Int32}(current_pointer))))
current_pointer += sizeof(Int32)

month = Month(ntoh(unsafe_load(Ptr{Int32}(current_pointer))))

# Split combined periods to match the output from text queries
return _split_periods(Month(month), Day(day), Microsecond(microsecond))
end

# parse the iso_8601 interval output format
# https://www.postgresql.org/docs/10/datatype-datetime.html#DATATYPE-INTERVAL-OUTPUT
function pqparse(::Type{Dates.CompoundPeriod}, str::AbstractString)
Expand Down
6 changes: 5 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1153,7 +1153,6 @@ end
binary_not_implemented_types = [
Decimal,
Time,
Dates.CompoundPeriod,
Array,
OffsetArray,
]
Expand Down Expand Up @@ -1235,6 +1234,11 @@ end
("INTERVAL '1000 years 7 weeks'", Dates.CompoundPeriod(Period[Year(1000), Day(7 * 7)])),
("INTERVAL '1 day -1 hour'", Dates.CompoundPeriod(Period[Day(1), Hour(-1)])),
("INTERVAL '-1 month 1 day'", Dates.CompoundPeriod(Period[Month(-1), Day(1)])),
# With fields specification
("INTERVAL '1 day 2:03:04' HOUR TO MINUTE", Dates.CompoundPeriod(Period[Day(1), Hour(2), Minute(3)])),
# With precision
("INTERVAL '6.1001 seconds' SECOND(0)", Dates.CompoundPeriod(Period[Second(6)])),
("INTERVAL '6.1001 seconds' SECOND(2)", Dates.CompoundPeriod(Period[Second(6), Millisecond(100)])),
("'{{{1,2,3},{4,5,6}}}'::int2[]", Array{Union{Int16, Missing}}(reshape(Int16[1 2 3; 4 5 6], 1, 2, 3))),
("'{}'::int2[]", Union{Missing, Int16}[]),
("'{{{1,2,3},{4,5,6}}}'::int4[]", Array{Union{Int32, Missing}}(reshape(Int32[1 2 3; 4 5 6], 1, 2, 3))),
Expand Down