LibPQ.jl
LibPQ.jl copied to clipboard
Add binary parsing for `interval` type
trafficstars
We have a postgres table that contains intervals and are using binary parsing on all our other tables to increase query speed in our internal database support package. We are currently running into the nested error when trying to run queries on the new table due to the interval data failing to parse.
nested task error: Couldn't parse ,? as interval using regex r"^P(?:(?<Year>-?\d+)Y)?(?:(?<Month>-?\d+)M)?(?:(?<Day>-?\d+)D)?(?:T(?:(?<Hour>-?\d+)H)?(?:(?<Minute>-?\d+)M)?(?:(?<Second>(?<whole_seconds>-?\d+)(?:\.(?<frac_seconds>\d{1,9}))?)S)?)?$"
Stacktrace:
[1] error(s::String)
@ Base ./error.jl:33
[2] pqparse(#unused#::Type{Dates.CompoundPeriod}, str::String)
@ LibPQ ~/.julia/packages/LibPQ/IcOLJ/src/parsing.jl:444
[3] parse(#unused#::Type{Dates.CompoundPeriod}, pqv::LibPQ.PQValue{0x000004a2, true})
@ LibPQ ~/.julia/packages/LibPQ/IcOLJ/src/parsing.jl:127
[4] (::LibPQ.var"#parse_type#79"{DataType})(pqv::LibPQ.PQValue{0x000004a2, true})
@ LibPQ ~/.julia/packages/LibPQ/IcOLJ/src/parsing.jl:644
[5] getindex(jl_result::LibPQ.Result{true}, row::Int64, col::Int64)
@ LibPQ ~/.julia/packages/LibPQ/IcOLJ/src/results.jl:536
[6] getproperty(pqrow::LibPQ.Row, name::Symbol)
@ LibPQ ~/.julia/packages/LibPQ/IcOLJ/src/tables.jl:37
[7] getcolumn
@ ~/.julia/packages/Tables/OWzlh/src/Tables.jl:102 [inlined]
[8] getcolumn
@ ~/.julia/packages/Tables/OWzlh/src/Tables.jl:103 [inlined]
[9] eachcolumn
@ ~/.julia/packages/Tables/OWzlh/src/utils.jl:70 [inlined]
[10] writerow(buf::Vector{UInt8}, pos::Base.RefValue{Int64}, len::Int64, io::IOStream, sch::Tables.Schema{(:release_date, :target_start, :target_end, :region, :tag, :target_bounds, :horizon, :temperature, :target), Tuple{Union{Missing, ZonedDateTime}, Union{Missing, ZonedDateTime}, Union{Missing, ZonedDateTime}, Union{Missing, String}, Union{Missing, String}, Union{Missing, Int16}, Union{Missing, Dates.CompoundPeriod}, Union{Missing, Float64}, Union{Missing, Interval{ZonedDateTime, L, R} where {L<:Bound, R<:Bound}}}}, row::LibPQ.Row, cols::Int64, opts::CSV.Options{UInt8, UInt8, Nothing, Tuple{}, typeof(CSV._identity)})
@ CSV ~/.julia/packages/CSV/owrEo/src/write.jl:367
https://github.com/postgres/postgres/blob/e94c1a55dada49772622d2be2d17a2a9973b2661/src/backend/utils/adt/timestamp.c#L980
The AdjustIntervalForTypmod function looks difficult to understand. Mostly I think it is truncating, but I'm not sure what the precision section does and I don't have time to look into it right now. Maybe it is easy or maybe it is not.