@@ -365,6 +365,14 @@ unique_ptr<Expression> ConstructMapExpression(ClientContext &context, idx_t loca
365
365
std::move (bind_data));
366
366
}
367
367
368
+ bool VirtualColumnIsConstant (column_t column_id) {
369
+ if (column_id == COLUMN_IDENTIFIER_EMPTY ||
370
+ COLUMN_IDENTIFIER_EMPTY == MultiFileReader::COLUMN_IDENTIFIER_FILENAME) {
371
+ return true ;
372
+ }
373
+ return false ;
374
+ }
375
+
368
376
ResultColumnMapping MultiFileColumnMapper::CreateColumnMappingByMapper (const ColumnMapper &mapper) {
369
377
auto &reader = *reader_data.reader ;
370
378
auto &local_columns = reader.GetColumns ();
@@ -397,28 +405,36 @@ ResultColumnMapping MultiFileColumnMapper::CreateColumnMappingByMapper(const Col
397
405
auto &global_id = global_column_ids[i];
398
406
auto global_column_id = global_id.GetPrimaryIndex ();
399
407
408
+ auto local_idx = MultiFileLocalIndex (reader.column_ids .size ());
400
409
if (IsVirtualColumn (global_column_id)) {
401
- // virtual column - these are emitted for every file
410
+ // virtual column - look it up in the virtual column entry map
402
411
auto virtual_entry = virtual_columns.find (global_column_id);
403
412
if (virtual_entry == virtual_columns.end ()) {
404
413
throw InternalException (" Virtual column id %d not found in virtual columns map" , global_column_id);
405
414
}
406
- expressions.push_back (make_uniq<BoundConstantExpression>(Value (virtual_entry->second .type )));
407
- continue ;
408
- }
409
-
410
- auto local_idx = MultiFileLocalIndex (reader.column_ids .size ());
411
- if (global_column_id >= global_columns.size ()) {
412
- if (bind_data.file_row_number_idx == global_column_id) {
413
- // FIXME: this needs a more extensible solution
414
- auto new_column_id = MultiFileLocalColumnId (mapper.MapCount ());
415
- reader.column_ids .push_back (new_column_id);
416
- reader.column_indexes .emplace_back (mapper.MapCount ());
417
- // ! FIXME: what to do here???
418
- expressions.push_back (make_uniq<BoundReferenceExpression>(LogicalType::BIGINT, local_idx));
419
- } else {
420
- throw InternalException (" Unexpected generated column" );
415
+ auto &virtual_column_type = virtual_entry->second .type ;
416
+ // check if this column is constant for the file
417
+ if (VirtualColumnIsConstant (global_column_id)) {
418
+ // the column is constant (e.g. filename or empty) - no need to project from the file
419
+ expressions.push_back (make_uniq<BoundConstantExpression>(Value (virtual_column_type)));
420
+ continue ;
421
421
}
422
+ // the column is not constant for the file - need to push into the projection list
423
+ auto expr = make_uniq<BoundReferenceExpression>(virtual_column_type, local_idx.GetIndex ());
424
+ reader_data.expressions .push_back (std::move (expr));
425
+
426
+ MultiFileLocalColumnId local_id (reader.columns .size ());
427
+ ColumnIndex local_index (local_id.GetId ());
428
+
429
+ // add the virtual column to the reader
430
+ reader.columns .emplace_back (virtual_entry->second .name , virtual_column_type);
431
+ reader.AddVirtualColumn (global_column_id);
432
+
433
+ // set it as being projected in this spot
434
+ MultiFileColumnMap index_mapping (local_idx, virtual_column_type, virtual_column_type);
435
+ result.global_to_local .insert (make_pair (global_idx.GetIndex (), std::move (index_mapping)));
436
+ reader.column_ids .push_back (local_id);
437
+ reader.column_indexes .push_back (std::move (local_index));
422
438
continue ;
423
439
}
424
440
@@ -433,8 +449,7 @@ ResultColumnMapping MultiFileColumnMapper::CreateColumnMappingByMapper(const Col
433
449
ColumnIndex local_index (local_id.GetId ());
434
450
auto &local_type = local_columns[local_id.GetId ()].type ;
435
451
auto &global_type = global_column.type ;
436
- auto local_idx = reader.column_ids .size ();
437
- auto expr = make_uniq<BoundReferenceExpression>(global_type, local_idx);
452
+ auto expr = make_uniq<BoundReferenceExpression>(global_type, local_idx.GetIndex ());
438
453
if (global_type != local_type) {
439
454
reader.cast_map [local_id.GetId ()] = global_type;
440
455
} else {
0 commit comments