Github user zuyu commented on a diff in the pull request: https://github.com/apache/incubator-quickstep/pull/291#discussion_r136171013 --- Diff: relational_operators/TableExportOperator.cpp --- @@ -0,0 +1,329 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + **/ + +#include "relational_operators/TableExportOperator.hpp" + +#include <cstdio> +#include <exception> +#include <string> +#include <utility> + +#include "catalog/CatalogAttribute.hpp" +#include "query_execution/QueryContext.hpp" +#include "query_execution/WorkOrderProtosContainer.hpp" +#include "query_execution/WorkOrdersContainer.hpp" +#include "relational_operators/WorkOrder.pb.h" +#include "storage/StorageBlockInfo.hpp" +#include "storage/ValueAccessor.hpp" +#include "threading/SpinMutex.hpp" +#include "types/TypedValue.hpp" +#include "types/containers/Tuple.hpp" +#include "utility/BulkIOConfiguration.hpp" +#include "utility/StringUtil.hpp" + +#include "glog/logging.h" + +#include "tmb/id_typedefs.h" + +namespace quickstep { + +bool TableExportOperator::getAllWorkOrders( + WorkOrdersContainer *container, + QueryContext *query_context, + StorageManager *storage_manager, + const tmb::client_id scheduler_client_id, + tmb::MessageBus *bus) { + const auto add_work_order = [&](const block_id input_block_id, + const bool is_first_work_order) -> void { + std::unique_ptr<std::string> output_buffer = std::make_unique<std::string>(); + container->addNormalWorkOrder( + new TableExportToStringWorkOrder(query_id_, + input_relation_, + input_block_id, + options_->getFormat(), + is_first_work_order && options_->hasHeader(), + options_->getDelimiter(), + options_->escapeStrings(), + options_->getQuoteCharacter(), + options_->getNullString(), + output_buffer.get(), + op_index_, + scheduler_client_id, + storage_manager, + bus), + op_index_); + + SpinMutexLock lock(output_buffers_mutex_); + output_buffers_.emplace(input_block_id, output_buffer.release()); + }; + + if (input_relation_is_stored_) { + if (!started_) { + for (std::size_t i = 0; i < input_relation_block_ids_.size(); ++i) { + add_work_order(input_relation_block_ids_[i], i == 0); + } + num_workorders_generated_ = input_relation_block_ids_.size(); + started_ = true; + } + return true; + } else { + while (num_workorders_generated_ < input_relation_block_ids_.size()) { + add_work_order(input_relation_block_ids_[num_workorders_generated_], + num_workorders_generated_ == 0); + ++num_workorders_generated_; + } + return done_feeding_input_relation_; + } +} + +bool TableExportOperator::getAllWorkOrderProtos( + WorkOrderProtosContainer *container) { + // TODO(quickstep-team): Implement TextExportOperator for the distributed case. + LOG(FATAL) << "TableExportOperator::getAllWorkOrderProtos() is not supported"; +} + +void TableExportOperator::receiveFeedbackMessage( + const WorkOrder::FeedbackMessage &msg) { + DCHECK(TableExportOperator::kBlockOutputMessage == msg.type()); + DCHECK(msg.payload_size() == sizeof(block_id)); + + if (file_ == nullptr) { + const std::string lo_file_name = ToLower(file_name_); + if (lo_file_name == "$stdout") { + file_ = stdout; + } else if (lo_file_name == "$stderr") { + file_ = stderr; + } else { + file_ = std::fopen(file_name_.substr(1).c_str(), "wb"); + // TODO(quickstep-team): Decent handling of exceptions at query runtime. + if (file_ == nullptr) { + throw std::runtime_error("Can not open file " + file_name_ + " for writing"); + } + } + } + + // Mark block done. + const block_id done_block_id = *static_cast<const block_id*>(msg.payload()); + { + SpinMutexLock lock(output_buffers_mutex_); + DCHECK(output_buffers_.find(done_block_id) != output_buffers_.end()); + output_buffers_.at(done_block_id).done = true; + } + + // FIXME(jianqiao): Use work orders to perform the "write to file" operation + // instead of doing it here inside this thread -- as it may stall the scheduler. + while (num_blocks_written_ < num_workorders_generated_) { + // Write block exported strings to file in the same order as the blocks are + // in \p input_relation_block_ids_. + block_id next_block_id; + { + SpinMutexLock lock(block_ids_mutex_); + next_block_id = input_relation_block_ids_[num_blocks_written_]; + } + std::unique_ptr<std::string> output_buffer; + { + SpinMutexLock lock(output_buffers_mutex_); + auto it = output_buffers_.find(next_block_id); + if (it == output_buffers_.end() || !it->second.done) { + break; + } + output_buffer = std::move(it->second.buffer); + output_buffers_.erase(it); + } + std::fwrite(output_buffer->c_str(), 1, output_buffer->length(), file_); + ++num_blocks_written_; + } +} + +void TableExportOperator::updateCatalogOnCompletion() { + if (file_ != nullptr && file_ != stdout && file_ != stderr) { + std::fclose(file_); + } + file_ = nullptr; +} + +void TableExportToStringWorkOrder::execute() { + BlockReference block( + storage_manager_->getBlock(input_block_id_, input_relation_)); + std::unique_ptr<ValueAccessor> accessor( + block->getTupleStorageSubBlock().createValueAccessor()); + + switch (format_) { + case BulkIOFormat::kCSV: + writeToString<&TableExportToStringWorkOrder::quoteCSVField>( + accessor.get(), output_buffer_); + break; + case BulkIOFormat::kText: + writeToString<&TableExportToStringWorkOrder::escapeTextField>( + accessor.get(), output_buffer_); + break; + default: + LOG(FATAL) << "Unsupported export format in TableExportWorkOrder::execute()"; + } + + // Send completion message to operator. + FeedbackMessage msg(TableExportOperator::kBlockOutputMessage, + getQueryID(), + operator_index_, + new block_id(input_block_id_), + sizeof(input_block_id_)); + SendFeedbackMessage( + bus_, ClientIDMap::Instance()->getValue(), scheduler_client_id_, msg); +} + +inline std::string TableExportToStringWorkOrder::quoteCSVField(std::string &&field) const { + bool need_quote = false; + for (const char c : field) { + if (c == field_terminator_ || c == quote_character_ || c == '\n') { --- End diff -- Use `QUICKSTEP_EQUALS_ANY_CONSTANT(c, field_terminator_, quote_character_, '\n')` instead.
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---