diff --git a/src/array_parsing.jl b/src/array_parsing.jl index eabde12..b7bae05 100644 --- a/src/array_parsing.jl +++ b/src/array_parsing.jl @@ -175,6 +175,64 @@ function parse_array_inner(code::Base.CodeUnits{UInt8, String}, pos::Ref{Int}, i return coerce_array(values, inner_type) end +# Whether the literal is one `parse_flat_array` gives up on, told without +# reading any element: a nested array (the server writes `{{`), a backslash +# escape, or the bytes `NULL`. Memchr scans keep this cheap. A quoted "NULL" or +# a word containing it also counts; the general parser reads those the same way. +function needs_general_parser(code::Base.CodeUnits{UInt8, String}, open::Int) + open < length(code) && code[open + 1] == BRACE_OPEN && return true + findfirst(==(BACKSLASH), code) !== nothing && return true + i = findfirst(==(UInt8('N')), code) + while i !== nothing + i + 3 <= length(code) && code[i + 1] == UInt8('U') && code[i + 2] == UInt8('L') && code[i + 3] == UInt8('L') && return true + i = findnext(==(UInt8('N')), code, i + 1) + end + return false +end + +# The common case, a one-dimensional array without NULLs or escapes, read +# straight into a `Vector{T}`. `parse_array_inner` copies each token through a +# growable buffer and boxes each value into a `Vector{Any}` first. Elements are +# split and parsed exactly as there. Returns `nothing` when +# `needs_general_parser` says so, so such a literal is parsed once, or at a +# later nested array or a value that is not a `T`; the caller then parses the +# whole literal with `parse_array_inner`. +function parse_flat_array(str::String, open::Int, inner_type::Type{T}) where {T} + code = codeunits(str) + needs_general_parser(code, open) && return nothing + n = length(code) + dest = T[] + i = open + 1 + while i <= n + while i <= n && is_ws(code[i]) + i += 1 + end + i > n && break + c = code[i] + c == BRACE_CLOSE && break + c == BRACE_OPEN && return nothing + quoted = c == QUOTE + quoted && (i += 1) + j = i + while j <= n + b = code[j] + (quoted ? b == QUOTE : (b == COMMA || b == BRACE_CLOSE)) && break + j += 1 + end + token = GC.@preserve str unsafe_string(pointer(str, i), j - i) + # a quoted element ends past its closing quote + i = quoted && j <= n ? j + 1 : j + value = parse_scalar(token, inner_type, quoted) + value isa T || return nothing + push!(dest, value) + while i <= n && is_ws(code[i]) + i += 1 + end + i <= n && code[i] == COMMA && (i += 1) + end + return dest +end + const EQUALS = UInt8('=') # PostgreSQL prefixes an array literal with explicit dimensions whenever a @@ -217,6 +275,8 @@ function parse_array(str::String, inner_type::Type{T}) where {T} end c = code[pos[]] if c == BRACE_OPEN + flat = parse_flat_array(str, pos[], inner_type) + flat === nothing || return flat return parse_array_inner(code, pos, inner_type) end value = parse_scalar(str, inner_type, true) diff --git a/test/runtests.jl b/test/runtests.jl index ed88479..be6d184 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -492,6 +492,13 @@ function random_array_string(rng::AbstractRNG) return String(rand(rng, alphabet, rand(rng, 0:12))) end +# allocations of a call after a warm-up call; measured inside a function so +# `@allocations` does not force-compile the enclosing testset +function warm_allocations(f, s::String, ::Type{T}) where {T} + f(s, T) + return @allocations f(s, T) +end + include("timestamps.jl") include("decimals.jl") include("result_decoding.jl") @@ -963,6 +970,28 @@ include("trim_compile_tests.jl") @test Postgres.API.parse_array_by_oid("{/var/log/x[1].txt,b}", 25, registry) == ["/var/log/x[1].txt", "b"] @test Postgres.API.ArrayParsing.parse_array("{1,2}", Int64) isa Vector{Int64} + # one-dimensional arrays without NULLs or escapes are read straight into + # a typed vector; any other literal gives what the general parser gives + let parse = Postgres.API.ArrayParsing.parse_array + @test parse("{ 1 , \"2\",3 }", Int32) == Int32[1, 2, 3] + @test parse("{12.00,-7}", Int64) == [12, -7] + @test parse("{}", Float64) == Float64[] + @test parse("{NaN,-Infinity,1.5}", Float64) isa Vector{Float64} + @test isequal(parse("{1,NULL}", Int32), Union{Missing, Int32}[1, missing]) + @test parse("{\"NULL\",NULL}", String) isa Vector{Union{Missing, String}} + @test parse("{\"a\\\"b\",c d,\"\"}", String) == ["a\"b", "c d", ""] + @test parse("{{1,2},{3,4}}", Int32) == [Int32[1, 2], Int32[3, 4]] + @test parse("{1,{2}}", Int32) == Any[Int32(1), Int32[2]] + @test parse("{2024-01-02,2024-02-29}", Date) == [Date(2024, 1, 2), Date(2024, 2, 29)] + @test_throws ArgumentError parse("{1,x}", Int32) + @test parse("{aNULLb,\"NULL\"}", String)::Vector{String} == ["aNULLb", "NULL"] + # a literal with a NULL, an escape or a nested array costs no more than + # the general parser alone: the fast path turns it away before reading + general(s, ::Type{T}) where {T} = Postgres.API.ArrayParsing.parse_array_inner(codeunits(s), Ref(1), T) + for (s, T) in (("{1,2,3,4,NULL}", Int32), ("{abc,\"x\\\\y\"}", String), ("{{1,2},{3,4}}", Int32)) + @test warm_allocations(parse, s, T) <= warm_allocations(general, s, T) + end + end @test Postgres.API.parse_value(1005, "{1,-2}", registry) == Int16[1, -2] @test Postgres.API.parse_value(1021, "{1.5,NaN}", registry) isa Vector{Float32} @test Postgres.API.parse_value(1000, "{t,f}", registry) == [true, false]