174969 {
174970 vector<CSVSchema> schemas;
174971 const auto option_og = options;
174972
174973 const auto file_paths = multi_file_list.GetAllFiles();
174974
174975
174976
174977 const idx_t required_number_of_lines = options.sniff_size * options.sample_size_chunks;
174978
174979 idx_t total_number_of_rows = 0;
174980 idx_t current_file = 0;
174981 options.file_path = file_paths[current_file].path;
174982
174983 buffer_manager = make_shared_ptr<CSVBufferManager>(context, options, options.file_path, false);
174984 idx_t only_header_or_empty_files = 0;
174985
174986 {
174988 auto sniffer_result = sniffer.SniffCSV();
174989 idx_t rows_read = sniffer.LinesSniffed() -
174990 (options.dialect_options.skip_rows.GetValue() + options.dialect_options.header.GetValue());
174991
174992 schemas.emplace_back(sniffer_result.names, sniffer_result.return_types, file_paths[0].path, rows_read,
174993 buffer_manager->GetBuffer(0)->actual_size == 0);
174994 total_number_of_rows += sniffer.LinesSniffed();
174995 current_file++;
174996 if (sniffer.EmptyOrOnlyHeader()) {
174997 only_header_or_empty_files++;
174998 }
174999 }
175000
175001
175002 idx_t max_files_to_sniff =
static_cast<idx_t>(options.files_to_sniff == -1)
175003 ? NumericLimits<idx_t>::Maximum()
175004 :
static_cast<idx_t>(options.files_to_sniff);
175005 idx_t files_to_sniff = file_paths.size() > max_files_to_sniff ? max_files_to_sniff : file_paths.size();
175006 while (total_number_of_rows < required_number_of_lines && current_file < files_to_sniff) {
175007 auto option_copy = option_og;
175008 option_copy.file_path = file_paths[current_file].path;
175009 auto file_buffer_manager =
175010 make_shared_ptr<CSVBufferManager>(context, option_copy, option_copy.file_path, false);
175011
175012
175014 auto sniffer_result = sniffer.SniffCSV();
175015 idx_t rows_read = sniffer.LinesSniffed() - (option_copy.dialect_options.skip_rows.GetValue() +
175016 option_copy.dialect_options.header.GetValue());
175017 if (file_buffer_manager->GetBuffer(0)->actual_size == 0) {
175018 schemas.emplace_back(true);
175019 } else {
175020 schemas.emplace_back(sniffer_result.names, sniffer_result.return_types, option_copy.file_path, rows_read);
175021 }
175022 total_number_of_rows += sniffer.LinesSniffed();
175023 if (sniffer.EmptyOrOnlyHeader()) {
175024 only_header_or_empty_files++;
175025 }
175026 current_file++;
175027 }
175028
175029
175030 CSVSchema best_schema;
175031 for (auto &schema : schemas) {
175032 if (best_schema.Empty()) {
175033
175034 best_schema = schema;
175035 } else if (best_schema.GetRowsRead() == 0) {
175036
175037 best_schema = schema;
175038 } else if (schema.GetRowsRead() != 0) {
175039
175040 best_schema.MergeSchemas(schema, options.null_padding);
175041 }
175042 }
175043
175044
175045 best_schema.ReplaceNullWithVarchar();
175046
175047 if (names.empty()) {
175048 names = best_schema.GetNames();
175049 return_types = best_schema.GetTypes();
175050 }
175051 if (only_header_or_empty_files == current_file && !options.columns_set) {
175052 for (
idx_t i = 0; i < return_types.size(); i++) {
175053 if (!options.sql_types_per_column.empty()) {
175054 if (options.sql_types_per_column.find(names[i]) != options.sql_types_per_column.end()) {
175055 continue;
175056 }
175057 } else if (i < options.sql_type_list.size()) {
175058 continue;
175059 }
175060
175061 return_types[i] = LogicalType::VARCHAR;
175062 }
175063 }
175064 return best_schema;
175065}
static CSVStateMachineCache & Get(ClientContext &context)
Definition duckdb.cpp:174537
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237