You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by qu...@apache.org on 2024/11/26 05:32:43 UTC

(arrow-julia) branch jq-threadsafe-dictencodings updated (129ce5c -> 9683db1)

This is an automated email from the ASF dual-hosted git repository.

quinnj pushed a change to branch jq-threadsafe-dictencodings
in repository https://gitbox.apache.org/repos/asf/arrow-julia.git


 discard 129ce5c  Make DictEncoding reading threadsafe
 discard 51ad08a  Bump version to 2.7.3 (#524)
     new 9683db1  Make DictEncoding reading threadsafe

This update added new revisions after undoing existing revisions.
That is to say, some revisions that were in the old version of the
branch are not in the new version.  This situation occurs
when a user --force pushes a change and generates a repository
containing something like this:

 * -- * -- B -- O -- O -- O   (129ce5c)
            \
             N -- N -- N   refs/heads/jq-threadsafe-dictencodings (9683db1)

You should already have received notification emails for all of the O
revisions, and so the following emails describe only the N revisions
from the common base, B.

Any revisions marked "omit" are not gone; other references still
refer to them.  Any revisions marked "discard" are gone forever.

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:


(arrow-julia) 01/01: Make DictEncoding reading threadsafe

Posted by qu...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

quinnj pushed a commit to branch jq-threadsafe-dictencodings
in repository https://gitbox.apache.org/repos/asf/arrow-julia.git

commit 9683db12fd90c97ef74315ea9ea5af5603793126
Author: Jacob Quinn <qu...@gmail.com>
AuthorDate: Mon Nov 25 22:31:55 2024 -0700

    Make DictEncoding reading threadsafe
---
 src/table.jl | 176 +++++++++++++++++++++++++++++++----------------------------
 1 file changed, 92 insertions(+), 84 deletions(-)

diff --git a/src/table.jl b/src/table.jl
index 6dfb9b4..811f2c8 100644
--- a/src/table.jl
+++ b/src/table.jl
@@ -70,7 +70,7 @@ mutable struct Stream
     names::Vector{Symbol}
     types::Vector{Type}
     schema::Union{Nothing,Meta.Schema}
-    dictencodings::Dict{Int64,DictEncoding} # dictionary id => DictEncoding
+    dictencodings::Lockable{Dict{Int64,DictEncoding}} # dictionary id => DictEncoding
     dictencoded::Dict{Int64,Meta.Field} # dictionary id => field
     convert::Bool
     compression::Ref{Union{Symbol,Nothing}}
@@ -82,7 +82,7 @@ function Stream(inputs::Vector{ArrowBlob}; convert::Bool=true)
     names = Symbol[]
     types = Type[]
     schema = nothing
-    dictencodings = Dict{Int64,DictEncoding}()
+    dictencodings = Lockable(Dict{Int64,DictEncoding}())
     dictencoded = Dict{Int64,Meta.Field}()
     compression = Ref{Union{Symbol,Nothing}}(nothing)
     Stream(
@@ -210,8 +210,26 @@ function Base.iterate(x::Stream, (pos, id)=(1, 0))
             if recordbatch.compression !== nothing
                 compression = recordbatch.compression
             end
-            if haskey(x.dictencodings, id) && header.isDelta
-                # delta
+            @lock x.dictencodings begin
+                dictencodings = x.dictencodings[]
+                if haskey(dictencodings, id) && header.isDelta
+                    # delta
+                    field = x.dictencoded[id]
+                    values, _, _ = build(
+                        field,
+                        field.type,
+                        batch,
+                        recordbatch,
+                        x.dictencodings,
+                        Int64(1),
+                        Int64(1),
+                        x.convert,
+                    )
+                    dictencoding = dictencodings[id]
+                    append!(dictencoding.data, values)
+                    continue
+                end
+                # new dictencoding or replace
                 field = x.dictencoded[id]
                 values, _, _ = build(
                     field,
@@ -223,32 +241,17 @@ function Base.iterate(x::Stream, (pos, id)=(1, 0))
                     Int64(1),
                     x.convert,
                 )
-                dictencoding = x.dictencodings[id]
-                append!(dictencoding.data, values)
-                continue
-            end
-            # new dictencoding or replace
-            field = x.dictencoded[id]
-            values, _, _ = build(
-                field,
-                field.type,
-                batch,
-                recordbatch,
-                x.dictencodings,
-                Int64(1),
-                Int64(1),
-                x.convert,
-            )
-            A = ChainedVector([values])
-            S =
-                field.dictionary.indexType === nothing ? Int32 :
-                juliaeltype(field, field.dictionary.indexType, false)
-            x.dictencodings[id] = DictEncoding{eltype(A),S,typeof(A)}(
-                id,
-                A,
-                field.dictionary.isOrdered,
-                values.metadata,
-            )
+                A = ChainedVector([values])
+                S =
+                    field.dictionary.indexType === nothing ? Int32 :
+                    juliaeltype(field, field.dictionary.indexType, false)
+                dictencodings[id] = DictEncoding{eltype(A),S,typeof(A)}(
+                    id,
+                    A,
+                    field.dictionary.isOrdered,
+                    values.metadata,
+                )
+            end # lock
             @debug "parsed dictionary batch message: id=$id, data=$values\n"
         elseif header isa Meta.RecordBatch
             @debug "parsing record batch message: compression = $(header.compression)"
@@ -415,7 +418,7 @@ Table(inputs::Vector; kw...) =
 function Table(blobs::Vector{ArrowBlob}; convert::Bool=true)
     t = Table()
     sch = nothing
-    dictencodings = Dict{Int64,DictEncoding}() # dictionary id => DictEncoding
+    dictencodingslockable = Lockable(Dict{Int64,DictEncoding}()) # dictionary id => DictEncoding
     dictencoded = Dict{Int64,Meta.Field}() # dictionary id => field
     sync = OrderedSynchronizer()
     tsks = Channel{Any}(Inf)
@@ -465,65 +468,68 @@ function Table(blobs::Vector{ArrowBlob}; convert::Bool=true)
             elseif header isa Meta.DictionaryBatch
                 id = header.id
                 recordbatch = header.data
-                @debug "parsing dictionary batch message: id = $id, compression = $(recordbatch.compression)"
-                if haskey(dictencodings, id) && header.isDelta
-                    # delta
+                @info "parsing dictionary batch message: id = $id, compression = $(recordbatch.compression)"
+                @lock dictencodingslockable begin
+                    dictencodings = dictencodingslockable[]
+                    if haskey(dictencodings, id) && header.isDelta
+                        # delta
+                        field = dictencoded[id]
+                        values, _, _ = build(
+                            field,
+                            field.type,
+                            batch,
+                            recordbatch,
+                            dictencodings,
+                            Int64(1),
+                            Int64(1),
+                            convert,
+                        )
+                        dictencoding = dictencodings[id]
+                        if typeof(dictencoding.data) <: ChainedVector
+                            append!(dictencoding.data, values)
+                        else
+                            A = ChainedVector([dictencoding.data, values])
+                            S =
+                                field.dictionary.indexType === nothing ? Int32 :
+                                juliaeltype(field, field.dictionary.indexType, false)
+                            dictencodings[id] = DictEncoding{eltype(A),S,typeof(A)}(
+                                id,
+                                A,
+                                field.dictionary.isOrdered,
+                                values.metadata,
+                            )
+                        end
+                        continue
+                    end
+                    # new dictencoding or replace
                     field = dictencoded[id]
                     values, _, _ = build(
                         field,
                         field.type,
                         batch,
                         recordbatch,
-                        dictencodings,
+                        dictencodingslockable,
                         Int64(1),
                         Int64(1),
                         convert,
                     )
-                    dictencoding = dictencodings[id]
-                    if typeof(dictencoding.data) <: ChainedVector
-                        append!(dictencoding.data, values)
-                    else
-                        A = ChainedVector([dictencoding.data, values])
-                        S =
-                            field.dictionary.indexType === nothing ? Int32 :
-                            juliaeltype(field, field.dictionary.indexType, false)
-                        dictencodings[id] = DictEncoding{eltype(A),S,typeof(A)}(
-                            id,
-                            A,
-                            field.dictionary.isOrdered,
-                            values.metadata,
-                        )
-                    end
-                    continue
-                end
-                # new dictencoding or replace
-                field = dictencoded[id]
-                values, _, _ = build(
-                    field,
-                    field.type,
-                    batch,
-                    recordbatch,
-                    dictencodings,
-                    Int64(1),
-                    Int64(1),
-                    convert,
-                )
-                A = values
-                S =
-                    field.dictionary.indexType === nothing ? Int32 :
-                    juliaeltype(field, field.dictionary.indexType, false)
-                dictencodings[id] = DictEncoding{eltype(A),S,typeof(A)}(
-                    id,
-                    A,
-                    field.dictionary.isOrdered,
-                    values.metadata,
-                )
+                    A = values
+                    S =
+                        field.dictionary.indexType === nothing ? Int32 :
+                        juliaeltype(field, field.dictionary.indexType, false)
+                    dictencodings[id] = DictEncoding{eltype(A),S,typeof(A)}(
+                        id,
+                        A,
+                        field.dictionary.isOrdered,
+                        values.metadata,
+                    )
+                end # lock
                 @debug "parsed dictionary batch message: id=$id, data=$values\n"
             elseif header isa Meta.RecordBatch
                 anyrecordbatches = true
                 @debug "parsing record batch message: compression = $(header.compression)"
                 @wkspawn begin
-                    cols = collect(VectorIterator(sch, $batch, dictencodings, convert))
+                    cols = collect(VectorIterator(sch, $batch, dictencodingslockable, convert))
                     put!(() -> put!(tsks, cols), sync, $(rbi))
                 end
                 rbi += 1
@@ -610,7 +616,7 @@ end
 struct VectorIterator
     schema::Meta.Schema
     batch::Batch # batch.msg.header MUST BE RecordBatch
-    dictencodings::Dict{Int64,DictEncoding}
+    dictencodings::Lockable{Dict{Int64,DictEncoding}}
     convert::Bool
 end
 
@@ -654,14 +660,16 @@ function build(field::Meta.Field, batch, rb, de, nodeidx, bufferidx, convert)
         buffer = rb.buffers[bufferidx]
         S = d.indexType === nothing ? Int32 : juliaeltype(field, d.indexType, false)
         bytes, indices = reinterp(S, batch, buffer, rb.compression)
-        encoding = de[d.id]
-        A = DictEncoded(
-            bytes,
-            validity,
-            indices,
-            encoding,
-            buildmetadata(field.custom_metadata),
-        )
+        @lock de begin
+            encoding = de[][d.id]
+            A = DictEncoded(
+                bytes,
+                validity,
+                indices,
+                encoding,
+                buildmetadata(field.custom_metadata),
+            )
+        end
         nodeidx += 1
         bufferidx += 1
     else