How to define empty IndexedTables?

question

#1

I am unable to define empty IndexedTables, e.g.

using IndexedTables, IndexedTables.Table
t = Table(Columns(a=Int64[],b=String[]),Int64[])
t[1,"a"] = 1
t[1,"b"] = 2
t[1,"c"] = t[1,"a"] + t[1,"b"]
BoundsError: attempt to access 0-element Array{Int64,1} at index [0]

Is there any workaround possible ?

EDIT: I did repost this question on StackOverflow


#2

It seems it has to do with the fact that inserts are not put straightaway on the data, but on a buffer:

tok = Table(Columns(a=Int64[1,1],b=String["a","b"]),Int64[1,2])
tbad = Table(Columns(a=Int64[],b=String[]),Int64[])
tbad[1,"a"] = 1
tbad[1,"b"] = 2
dump(tok)
IndexedTables.IndexedTable{Int64,Tuple{Int64,String},IndexedTables.Columns{NamedTuples._NT_a_b{Int64,String},NamedTuples._NT_a_b{Array{Int64,1},Array{String,1}}},Array{Int64,1}}
  index: IndexedTables.Columns{NamedTuples._NT_a_b{Int64,String},NamedTuples._NT_a_b{Array{Int64,1},Array{String,1}}}
    columns: NamedTuples._NT_a_b{Array{Int64,1},Array{String,1}}
      a: Array{Int64}((2,)) [1, 1]
      b: Array{String}((2,))
        1: String "a"
        2: String "b"
  data: Array{Int64}((2,)) [1, 2]
  index_buffer: IndexedTables.Columns{NamedTuples._NT_a_b{Int64,String},NamedTuples._NT_a_b{Array{Int64,1},Array{String,1}}}
    columns: NamedTuples._NT_a_b{Array{Int64,1},Array{String,1}}
      a: Array{Int64}((0,)) Int64[]
      b: Array{String}((0,))
  data_buffer: Array{Int64}((0,)) Int64[]

dump(tbad)
IndexedTables.IndexedTable{Int64,Tuple{Int64,String},IndexedTables.Columns{NamedTuples._NT_a_b{Int64,String},NamedTuples._NT_a_b{Array{Int64,1},Array{String,1}}},Array{Int64,1}}
  index: IndexedTables.Columns{NamedTuples._NT_a_b{Int64,String},NamedTuples._NT_a_b{Array{Int64,1},Array{String,1}}}
    columns: NamedTuples._NT_a_b{Array{Int64,1},Array{String,1}}
      a: Array{Int64}((0,)) Int64[]
      b: Array{String}((0,))
  data: Array{Int64}((0,)) Int64[]
  index_buffer: IndexedTables.Columns{NamedTuples._NT_a_b{Int64,String},NamedTuples._NT_a_b{Array{Int64,1},Array{String,1}}}
    columns: NamedTuples._NT_a_b{Array{Int64,1},Array{String,1}}
      a: Array{Int64}((2,)) [1, 1]
      b: Array{String}((2,))
        1: String "a"
        2: String "b"
  data_buffer: Array{Int64}((2,)) [1, 2]

ok, it makes sense, after the various assignments I just have to flush!(t) to make them in order all in one go… the problem is that with an empty IndexedTable and after I do tbad[1,"a"] = 1, tbad[1,"b"] = 2 flush!(tbad) still returns an BoundsError: attempt to access 0-element Array{Int64,1} at index [0] error.

EDIT: This issue has a solution (an edit to the IndexedTables._merge! function) in the parallel S.O. question.