@@ -425,6 +425,11 @@ ResultColumnMapping MultiFileColumnMapper::CreateColumnMappingByMapper(const Col
425425 auto expr =
426426 multi_file_reader.GetVirtualColumnExpression (context, reader_data, local_columns, global_column_id,
427427 virtual_column_type, local_idx, global_column_reference);
428+ if (expr && expr->type == ExpressionType::VALUE_CONSTANT) {
429+ // the column is constant after all - handle it
430+ expressions.push_back (std::move (expr));
431+ continue ;
432+ }
428433 if (!global_column_reference) {
429434 auto is_reference = expr->type == ExpressionType::BOUND_REF;
430435 expressions.push_back (std::move (expr));
@@ -626,6 +631,23 @@ static bool EvaluateFilterAgainstConstant(TableFilter &filter, const Value &cons
626631 }
627632}
628633
634+ Value MultiFileColumnMapper::GetConstantValue (idx_t global_index) {
635+ auto global_column_id = global_column_ids[global_index].GetPrimaryIndex ();
636+ auto &expr = reader_data.expressions [global_index];
637+ if (expr->type == ExpressionType::VALUE_CONSTANT) {
638+ return expr->Cast <BoundConstantExpression>().value ;
639+ }
640+ for (idx_t i = 0 ; i < reader_data.constant_map .size (); i++) {
641+ auto &constant_map_entry = reader_data.constant_map [MultiFileConstantMapIndex (i)];
642+ if (constant_map_entry.column_idx .GetIndex () == global_index) {
643+ return constant_map_entry.value ;
644+ }
645+ }
646+ auto &global_column = global_columns[global_column_id];
647+ throw InternalException (" Column '%s' is not present in the file, but no constant_map entry exists for it!" ,
648+ global_column.name );
649+ }
650+
629651ReaderInitializeType
630652MultiFileColumnMapper::EvaluateConstantFilters (ResultColumnMapping &mapping,
631653 map<idx_t , reference<TableFilter>> &remaining_filters) {
@@ -645,36 +667,7 @@ MultiFileColumnMapper::EvaluateConstantFilters(ResultColumnMapping &mapping,
645667 }
646668
647669 // ! FIXME: this does not check for filters against struct fields that are not present in the file
648- auto global_column_id = global_column_ids[global_index].GetPrimaryIndex ();
649- Value constant_value;
650- auto virtual_it = virtual_columns.find (global_column_id);
651- if (virtual_it != virtual_columns.end ()) {
652- auto &virtual_column = virtual_it->second ;
653- if (virtual_column.name == " filename" ) {
654- constant_value = Value (reader_data.reader ->GetFileName ());
655- } else if (global_column_id == MultiFileReader::COLUMN_IDENTIFIER_FILE_INDEX) {
656- constant_value = Value::UBIGINT (reader_data.reader ->file_list_idx .GetIndex ());
657- } else {
658- throw InternalException (" Unrecognized virtual column found: %s" , virtual_column.name );
659- }
660- } else {
661- bool has_constant = false ;
662- for (idx_t i = 0 ; i < reader_data.constant_map .size (); i++) {
663- auto &constant_map_entry = reader_data.constant_map [MultiFileConstantMapIndex (i)];
664- if (constant_map_entry.column_idx .GetIndex () == global_index) {
665- has_constant = true ;
666- constant_value = constant_map_entry.value ;
667- break ;
668- }
669- }
670- if (!has_constant) {
671- auto &global_column = global_columns[global_column_id];
672- throw InternalException (
673- " Column '%s' is not present in the file, but no constant_map entry exists for it!" ,
674- global_column.name );
675- }
676- }
677-
670+ auto constant_value = GetConstantValue (global_index);
678671 if (!EvaluateFilterAgainstConstant (*global_filter, constant_value)) {
679672 return ReaderInitializeType::SKIP_READING_FILE;
680673 }
0 commit comments