@@ -67,6 +67,20 @@ size_t computeTotalStringsLength(std::shared_ptr<arrow::ChunkedArray> arr,
6767 return total_bytes;
6868}
6969
70+ /* *
71+ * Get column ID by its 0-based index (position) in the table.
72+ */
73+ int columnId (size_t col_idx) {
74+ return static_cast <int >(col_idx + 1000 );
75+ }
76+
77+ /* *
78+ * Translate column ID to 0-based index (position) in the table.
79+ */
80+ size_t columnIndex (int col_id) {
81+ return static_cast <size_t >(col_id - 1000 );
82+ }
83+
7084} // anonymous namespace
7185
7286void ArrowStorage::fetchBuffer (const ChunkKey& key,
@@ -79,7 +93,7 @@ void ArrowStorage::fetchBuffer(const ChunkKey& key,
7993 mapd_shared_lock<mapd_shared_mutex> table_lock (table.mutex );
8094 data_lock.unlock ();
8195
82- size_t col_idx = static_cast < size_t > (key[CHUNK_KEY_COLUMN_IDX] - 1 );
96+ size_t col_idx = columnIndex (key[CHUNK_KEY_COLUMN_IDX]);
8397 size_t frag_idx = static_cast <size_t >(key[CHUNK_KEY_FRAGMENT_IDX] - 1 );
8498 CHECK_LT (frag_idx, table.fragments .size ());
8599 CHECK_LT (col_idx, table.col_data .size ());
@@ -134,7 +148,7 @@ std::unique_ptr<AbstractDataToken> ArrowStorage::getZeroCopyBufferMemory(
134148 ->type ;
135149
136150 if (!col_type->isVarLen ()) {
137- size_t col_idx = static_cast < size_t > (key[CHUNK_KEY_COLUMN_IDX] - 1 );
151+ size_t col_idx = columnIndex (key[CHUNK_KEY_COLUMN_IDX]);
138152 size_t frag_idx = static_cast <size_t >(key[CHUNK_KEY_FRAGMENT_IDX] - 1 );
139153 CHECK_EQ (key.size (), (size_t )4 );
140154 size_t elem_size = col_type->size ();
@@ -293,7 +307,7 @@ TableFragmentsInfo ArrowStorage::getTableMetadata(int db_id, int table_id) const
293307 frag_info.deviceIds .push_back (0 ); // Data_Namespace::CPU_LEVEL
294308 frag_info.deviceIds .push_back (0 ); // Data_Namespace::GPU_LEVEL
295309 for (size_t col_idx = 0 ; col_idx < frag.metadata .size (); ++col_idx) {
296- frag_info.setChunkMetadata (static_cast < int > (col_idx + 1 ), frag.metadata [col_idx]);
310+ frag_info.setChunkMetadata (columnId (col_idx), frag.metadata [col_idx]);
297311 }
298312 }
299313 return res;
@@ -331,14 +345,14 @@ TableInfoPtr ArrowStorage::createTable(const std::string& table_name,
331345 TableInfoPtr res;
332346 int table_id;
333347 mapd_unique_lock<mapd_shared_mutex> data_lock (data_mutex_);
348+ size_t next_col_idx = 0 ;
334349 {
335350 mapd_unique_lock<mapd_shared_mutex> dict_lock (dict_mutex_);
336351 mapd_unique_lock<mapd_shared_mutex> schema_lock (schema_mutex_);
337352 table_id = next_table_id_++;
338353 checkNewTableParams (table_name, columns, options);
339354 res = addTableInfo (
340355 db_id_, table_id, table_name, false , Data_Namespace::MemoryLevel::CPU_LEVEL, 0 );
341- int next_col_id = 1 ;
342356 std::unordered_map<int , int > dict_ids;
343357 for (auto & col : columns) {
344358 auto type = col.type ;
@@ -382,10 +396,10 @@ TableInfoPtr ArrowStorage::createTable(const std::string& table_name,
382396 type = elem_type;
383397 }
384398 }
385- auto col_info =
386- addColumnInfo ( db_id_, table_id, next_col_id++ , col.name , type, false );
399+ auto col_info = addColumnInfo (
400+ db_id_, table_id, columnId (next_col_idx++) , col.name , type, false );
387401 }
388- addRowidColumn (db_id_, table_id);
402+ addRowidColumn (db_id_, table_id, columnId (next_col_idx++) );
389403 }
390404
391405 std::vector<std::shared_ptr<arrow::Field>> fields;
@@ -479,7 +493,7 @@ void ArrowStorage::appendArrowTable(std::shared_ptr<arrow::Table> at, int table_
479493 threading::parallel_for (
480494 threading::blocked_range (0 , (int )at->columns ().size ()), [&](auto range) {
481495 for (auto col_idx = range.begin (); col_idx != range.end (); col_idx++) {
482- auto col_info = getColumnInfo (db_id_, table_id, col_idx + 1 );
496+ auto col_info = getColumnInfo (db_id_, table_id, columnId ( col_idx) );
483497 auto col_type = col_info->type ;
484498 auto col_arr = at->column (col_idx);
485499
@@ -605,7 +619,7 @@ void ArrowStorage::appendArrowTable(std::shared_ptr<arrow::Table> at, int table_
605619 auto & first_frag = fragments.front ();
606620 last_frag.row_count += first_frag.row_count ;
607621 for (size_t col_idx = 0 ; col_idx < last_frag.metadata .size (); ++col_idx) {
608- auto col_type = getColumnInfo (db_id_, table_id, col_idx + 1 )->type ;
622+ auto col_type = getColumnInfo (db_id_, table_id, columnId ( col_idx) )->type ;
609623 size_t num_elems = last_frag.metadata [col_idx]->numElements () +
610624 first_frag.metadata [col_idx]->numElements ();
611625 size_t num_bytes = last_frag.metadata [col_idx]->numBytes () +
0 commit comments