@@ -64,6 +64,12 @@ inline table_data::table_data(
6464 }
6565 }
6666
67+ // Build reverse mapping: TupleDesc index → logical index (-1 for dropped)
68+ tupdesc_to_logical_.resize (tuple_descriptor_->natts , -1 );
69+ for (int32_t logical = 0 ; logical < static_cast <int32_t >(active_column_indices_.size ()); ++logical) {
70+ tupdesc_to_logical_[active_column_indices_[logical]] = logical;
71+ }
72+
6773 const auto num_active = active_column_indices_.size ();
6874 requested_columns_.resize (num_active, false );
6975 base_typeids_.resize (num_active);
@@ -272,6 +278,15 @@ inline int32_t table_data::get_tupdesc_index(AttrNumber attr_num) const noexcept
272278 return active_column_indices_[attr_num];
273279}
274280
281+ inline int32_t table_data::logical_index_for_attnum (int32_t attnum) const noexcept
282+ {
283+ const auto tupdesc_idx = attnum - 1 ;
284+ if (tupdesc_idx < 0 || tupdesc_idx >= static_cast <int32_t >(tupdesc_to_logical_.size ())) {
285+ return -1 ;
286+ }
287+ return tupdesc_to_logical_[tupdesc_idx];
288+ }
289+
275290inline bool table_data::is_column_indexed (AttrNumber attr_num) const noexcept
276291{
277292 return pg::pg_index::get_oid (table_name_, get_atttypename (attr_num)) != InvalidOid;
@@ -318,13 +333,14 @@ inline void table_data::add_insert_slots(int32_t nslots, TupleTableSlot** slots)
318333 for (int32_t i = 0 ; i < num_columns (); ++i) {
319334 auto & column_values = insert_rows_[get_atttypename (i)];
320335 const auto dt = get_column_view (i)->dtype ();
336+ const auto slot_pos = get_tupdesc_index (i);
321337 for (int32_t k = 0 ; k < nslots; ++k) {
322338 auto slot = slots[k];
323339 nd::array val;
324- if (slot->tts_isnull [i ]) {
340+ if (slot->tts_isnull [slot_pos ]) {
325341 val = (nd::dtype_is_numeric (dt) ? nd::adapt (0 ) : nd::none (dt, 0 ));
326342 } else {
327- val = pg::utils::datum_to_nd (slot->tts_values [i ], get_base_atttypid (i), get_atttypmod (i));
343+ val = pg::utils::datum_to_nd (slot->tts_values [slot_pos ], get_base_atttypid (i), get_atttypmod (i));
328344 }
329345 column_values.push_back (std::move (val));
330346 }
0 commit comments