366292 {
366293 if (function.plan) {
366294
366295 return function.plan(*this, stmt);
366296 }
366297
366298 auto ©_info = *stmt.info;
366299
366300 auto node_copy = copy_info.select_statement->Copy();
366301 auto select_node = Bind(*node_copy);
366302
366303 if (!function.copy_to_bind) {
366304 throw NotImplementedException("COPY TO is not supported for FORMAT \"%s\"", stmt.info->format);
366305 }
366306
366307 bool use_tmp_file = true;
366308 CopyOverwriteMode overwrite_mode = CopyOverwriteMode::COPY_ERROR_ON_CONFLICT;
366309 FilenamePattern filename_pattern;
366310 bool user_set_use_tmp_file = false;
366311 bool per_thread_output = false;
366312 optional_idx file_size_bytes;
366313 vector<idx_t> partition_cols;
366314 bool seen_overwrite_mode = false;
366315 bool seen_filepattern = false;
366316 bool write_partition_columns = false;
366317 bool write_empty_file = true;
366318 bool hive_file_pattern = true;
366319 PreserveOrderType preserve_order = PreserveOrderType::AUTOMATIC;
366320 CopyFunctionReturnType return_type = CopyFunctionReturnType::CHANGED_ROWS;
366321
366322 CopyFunctionBindInput bind_input(*stmt.info, function.function_info);
366323
366324 bind_input.file_extension = function.extension;
366325
366326 auto original_options = stmt.info->options;
366327 stmt.info->options.clear();
366328 for (auto &option : original_options) {
366330 if (loption == "use_tmp_file") {
366331 use_tmp_file = GetBooleanArg(
context, option.second);
366332 user_set_use_tmp_file = true;
366333 } else if (loption == "overwrite_or_ignore" || loption == "overwrite" || loption == "append") {
366334 if (seen_overwrite_mode) {
366335 throw BinderException("Can only set one of OVERWRITE_OR_IGNORE, OVERWRITE or APPEND");
366336 }
366337 seen_overwrite_mode = true;
366338
366339 auto boolean = GetBooleanArg(
context, option.second);
366340 if (boolean) {
366341 if (loption == "overwrite_or_ignore") {
366342 overwrite_mode = CopyOverwriteMode::COPY_OVERWRITE_OR_IGNORE;
366343 } else if (loption == "overwrite") {
366344 overwrite_mode = CopyOverwriteMode::COPY_OVERWRITE;
366345 } else if (loption == "append") {
366346 if (!seen_filepattern) {
366347 filename_pattern.SetFilenamePattern("{uuid}");
366348 }
366349 overwrite_mode = CopyOverwriteMode::COPY_APPEND;
366350 }
366351 }
366352 } else if (loption == "filename_pattern") {
366353 if (option.second.empty()) {
366354 throw IOException("FILENAME_PATTERN cannot be empty");
366355 }
366356 filename_pattern.SetFilenamePattern(
366357 option.second[0].CastAs(
context, LogicalType::VARCHAR).GetValue<
string>());
366358 seen_filepattern = true;
366359 } else if (loption == "file_extension") {
366360 if (option.second.empty()) {
366361 throw IOException("FILE_EXTENSION cannot be empty");
366362 }
366363 bind_input.file_extension = option.second[0].CastAs(
context, LogicalType::VARCHAR).GetValue<
string>();
366364 } else if (loption == "per_thread_output") {
366365 per_thread_output = GetBooleanArg(
context, option.second);
366366 } else if (loption == "file_size_bytes") {
366367 if (option.second.empty()) {
366368 throw BinderException("FILE_SIZE_BYTES cannot be empty");
366369 }
366370 if (!function.rotate_files) {
366371 throw NotImplementedException("FILE_SIZE_BYTES not implemented for FORMAT \"%s\"", stmt.info->format);
366372 }
366373 if (option.second[0].GetTypeMutable().id() == LogicalTypeId::VARCHAR) {
366374 file_size_bytes = DBConfig::ParseMemoryLimit(option.second[0].ToString());
366375 } else {
366376 file_size_bytes = option.second[0].GetValue<
uint64_t>();
366377 }
366378 } else if (loption == "partition_by") {
366379 auto converted = ConvertVectorToValue(std::move(option.second));
366380 partition_cols = ParseColumnsOrdered(converted, select_node.names, loption);
366381 } else if (loption == "return_files") {
366382 if (GetBooleanArg(
context, option.second)) {
366383 return_type = CopyFunctionReturnType::CHANGED_ROWS_AND_FILE_LIST;
366384 }
366385 } else if (loption == "preserve_order") {
366386 if (GetBooleanArg(
context, option.second)) {
366387 preserve_order = PreserveOrderType::PRESERVE_ORDER;
366388 } else {
366389 preserve_order = PreserveOrderType::DONT_PRESERVE_ORDER;
366390 }
366391 } else if (loption == "return_stats") {
366392 if (GetBooleanArg(
context, option.second)) {
366393 return_type = CopyFunctionReturnType::WRITTEN_FILE_STATISTICS;
366394 }
366395 } else if (loption == "write_partition_columns") {
366396 write_partition_columns = GetBooleanArg(
context, option.second);
366397 } else if (loption == "write_empty_file") {
366398 write_empty_file = GetBooleanArg(
context, option.second);
366399 } else if (loption == "hive_file_pattern") {
366400 hive_file_pattern = GetBooleanArg(
context, option.second);
366401 } else {
366402 stmt.info->options[option.first] = option.second;
366403 }
366404 }
366405 if (overwrite_mode == CopyOverwriteMode::COPY_APPEND && !filename_pattern.HasUUID()) {
366406 throw BinderException("APPEND mode requires a {uuid} label in filename_pattern");
366407 }
366408 if (user_set_use_tmp_file && per_thread_output) {
366409 throw NotImplementedException("Can't combine USE_TMP_FILE and PER_THREAD_OUTPUT for COPY");
366410 }
366411 if (user_set_use_tmp_file && file_size_bytes.IsValid()) {
366412 throw NotImplementedException("Can't combine USE_TMP_FILE and FILE_SIZE_BYTES for COPY");
366413 }
366414 if (user_set_use_tmp_file && !partition_cols.empty()) {
366415 throw NotImplementedException("Can't combine USE_TMP_FILE and PARTITION_BY for COPY");
366416 }
366417 if (per_thread_output && !partition_cols.empty()) {
366418 throw NotImplementedException("Can't combine PER_THREAD_OUTPUT and PARTITION_BY for COPY");
366419 }
366420 if (file_size_bytes.IsValid() && !partition_cols.empty()) {
366421 throw NotImplementedException("Can't combine FILE_SIZE_BYTES and PARTITION_BY for COPY");
366422 }
366423 if (!write_partition_columns) {
366424 if (partition_cols.size() == select_node.names.size()) {
366425 throw NotImplementedException("No column to write as all columns are specified as partition columns. "
366426 "WRITE_PARTITION_COLUMNS option can be used to write partition columns.");
366427 }
366428 }
366430 if (is_remote_file) {
366431 use_tmp_file = false;
366432 } else {
366433 auto &fs = FileSystem::GetFileSystem(
context);
366434 bool is_file_and_exists = fs.FileExists(stmt.info->file_path);
366435 bool is_stdout = stmt.info->file_path == "/dev/stdout";
366436 if (!user_set_use_tmp_file) {
366437 use_tmp_file = is_file_and_exists && !per_thread_output && partition_cols.empty() && !is_stdout;
366438 }
366439 }
366440
366441
366442 if (function.copy_to_select) {
366443 auto bindings = select_node.plan->GetColumnBindings();
366444
366445 CopyToSelectInput input = {
context, stmt.info->options, {}, copy_to_type};
366446 input.select_list.reserve(bindings.size());
366447
366448
366449 for (
idx_t i = 0; i < bindings.size(); i++) {
366450 auto &binding = bindings[i];
366451 auto &name = select_node.names[i];
366452 auto &type = select_node.types[i];
366453 input.select_list.push_back(make_uniq<BoundColumnRefExpression>(name, type, binding));
366454 }
366455
366456 auto new_select_list = function.copy_to_select(input);
366457 if (!new_select_list.empty()) {
366458
366459 auto projection = make_uniq<LogicalProjection>(
GenerateTableIndex(), std::move(new_select_list));
366460 projection->children.push_back(std::move(select_node.plan));
366461 projection->ResolveOperatorTypes();
366462
366463
366464 select_node.names.clear();
366465 select_node.types.clear();
366466 for (auto &expr : projection->expressions) {
366467 select_node.names.push_back(expr->GetName());
366468 select_node.types.push_back(expr->return_type);
366469 }
366470 select_node.plan = std::move(projection);
366471 }
366472 }
366473
366474 auto unique_column_names = select_node.names;
366476 auto file_path = stmt.info->file_path;
366477
366478 auto names_to_write =
366479 LogicalCopyToFile::GetNamesWithoutPartitions(unique_column_names, partition_cols, write_partition_columns);
366480 auto types_to_write =
366481 LogicalCopyToFile::GetTypesWithoutPartitions(select_node.types, partition_cols, write_partition_columns);
366482 auto function_data = function.copy_to_bind(
context, bind_input, names_to_write, types_to_write);
366483
366484 const auto rotate = function.rotate_files && function.rotate_files(*function_data, file_size_bytes);
366485 if (rotate) {
366486 if (!function.rotate_next_file) {
366487 throw InternalException("rotate_next_file not implemented for \"%s\"", function.extension);
366488 }
366489 if (user_set_use_tmp_file) {
366490 throw NotImplementedException(
366491 "Can't combine USE_TMP_FILE and file rotation (e.g., ROW_GROUPS_PER_FILE) for COPY");
366492 }
366493 if (!partition_cols.empty()) {
366494 throw NotImplementedException(
366495 "Can't combine file rotation (e.g., ROW_GROUPS_PER_FILE) and PARTITION_BY for COPY");
366496 }
366497 }
366498 if (!write_empty_file) {
366499 if (per_thread_output) {
366500 throw NotImplementedException("Can't combine WRITE_EMPTY_FILE false with PER_THREAD_OUTPUT");
366501 }
366502 if (!partition_cols.empty()) {
366503 throw NotImplementedException("Can't combine WRITE_EMPTY_FILE false with PARTITION_BY");
366504 }
366505 }
366506 if (return_type == CopyFunctionReturnType::WRITTEN_FILE_STATISTICS && !function.copy_to_get_written_statistics) {
366507 throw NotImplementedException("RETURN_STATS is not supported for the \"%s\" copy format", stmt.info->format);
366508 }
366509
366510
366511 auto copy = make_uniq<LogicalCopyToFile>(function, std::move(function_data), std::move(stmt.info));
366512 copy->file_path = file_path;
366513 copy->use_tmp_file = use_tmp_file;
366514 copy->overwrite_mode = overwrite_mode;
366515 copy->filename_pattern = filename_pattern;
366516 copy->file_extension = bind_input.file_extension;
366517 copy->per_thread_output = per_thread_output;
366518 if (file_size_bytes.IsValid()) {
366519 copy->file_size_bytes = file_size_bytes;
366520 }
366522 copy->partition_output = !partition_cols.empty();
366523 copy->write_partition_columns = write_partition_columns;
366524 copy->partition_columns = std::move(partition_cols);
366525 copy->write_empty_file = write_empty_file;
366526 copy->return_type = return_type;
366527 copy->preserve_order = preserve_order;
366528 copy->hive_file_pattern = hive_file_pattern;
366529
366530 copy->names = unique_column_names;
366531 copy->expected_types = select_node.types;
366532
366533 copy->AddChild(std::move(select_node.plan));
366534
366535 auto &properties = GetStatementProperties();
366536 switch (copy->return_type) {
366537 case CopyFunctionReturnType::CHANGED_ROWS:
366538 properties.return_type = StatementReturnType::CHANGED_ROWS;
366539 break;
366540 case CopyFunctionReturnType::CHANGED_ROWS_AND_FILE_LIST:
366541 case CopyFunctionReturnType::WRITTEN_FILE_STATISTICS:
366542 properties.return_type = StatementReturnType::QUERY_RESULT;
366543 break;
366544 default:
366545 throw NotImplementedException("Unknown CopyFunctionReturnType");
366546 }
366547
366548 BoundStatement result;
366549 result.names = GetCopyFunctionReturnNames(copy->return_type);
366551 result.plan = std::move(copy);
366552
366553 return result;
366554}
static DUCKDB_API bool IsRemoteFile(const string &path)
Whether or not a file is remote or local, based only on file path.
Definition duckdb.cpp:74413
void rotate(InputArray src, OutputArray dst, int rotateCode)
vector< LogicalType > GetCopyFunctionReturnLogicalTypes(CopyFunctionReturnType return_type)
Definition duckdb.cpp:220611