4105 auto& catalog = session->getCatalog();
4107 bool populate_table =
false;
4110 populate_table =
true;
4114 populate_table =
true;
4118 auto get_target_column_descriptors = [
this, &catalog](
const TableDescriptor* td) {
4119 std::vector<const ColumnDescriptor*> target_column_descriptors;
4121 auto list = catalog.getAllColumnMetadataForTable(td->
tableId,
false,
false,
false);
4122 target_column_descriptors = {std::begin(list), std::end(list)};
4126 if (cd ==
nullptr) {
4127 throw std::runtime_error(
"Column " + *c +
" does not exist.");
4129 target_column_descriptors.push_back(cd);
4133 return target_column_descriptors;
4138 if (validate_table) {
4141 throw std::runtime_error(
"Table " +
table_name_ +
" does not exist.");
4144 throw std::runtime_error(
"Insert to views is not supported yet.");
4150 throw std::runtime_error(
"User has no insert privileges on " +
table_name_ +
".");
4155 LocalQueryConnector local_connector;
4157 auto source_column_descriptors = local_connector.getColumnDescriptors(
result,
false);
4159 std::vector<const ColumnDescriptor*> target_column_descriptors =
4160 get_target_column_descriptors(td);
4162 if (source_column_descriptors.size() != target_column_descriptors.size()) {
4163 throw std::runtime_error(
"The number of source and target columns does not match.");
4166 for (
int i = 0; i < source_column_descriptors.size(); i++) {
4168 &(*std::next(source_column_descriptors.begin(), i));
4172 auto type_cannot_be_cast = [](
const auto& col_type) {
4173 return (col_type.is_time() || col_type.is_geometry() || col_type.is_array() ||
4174 col_type.is_boolean());
4177 if (type_cannot_be_cast(source_cd->
columnType) ||
4178 type_cannot_be_cast(target_cd->
columnType)) {
4179 throw std::runtime_error(
"Source '" + source_cd->
columnName +
" " +
4181 "' and target '" + target_cd->
columnName +
" " +
4183 "' column types do not match.");
4188 throw std::runtime_error(
"Source '" + source_cd->
columnName +
" " +
4190 "' and target '" + target_cd->
columnName +
" " +
4192 "' array column element types do not match.");
4197 throw std::runtime_error(
"Source '" + source_cd->
columnName +
" " +
4199 "' and target '" + target_cd->
columnName +
" " +
4201 "' column types do not match.");
4215 throw std::runtime_error(
"Source '" + source_cd->
columnName +
" " +
4217 "' and target '" + target_cd->
columnName +
" " +
4219 "' decimal columns scales do not match.");
4225 throw std::runtime_error(
"Source '" + source_cd->
columnName +
" " +
4227 "' and target '" + target_cd->
columnName +
" " +
4229 "' column types do not match.");
4233 throw std::runtime_error(
"Source '" + source_cd->
columnName +
" " +
4235 "' and target '" + target_cd->
columnName +
" " +
4237 "' columns string encodings do not match.");
4244 throw std::runtime_error(
"Source '" + source_cd->
columnName +
" " +
4246 "' and target '" + target_cd->
columnName +
" " +
4248 "' timestamp column precisions do not match.");
4257 throw std::runtime_error(
"Source '" + source_cd->
columnName +
" " +
4259 "' and target '" + target_cd->
columnName +
" " +
4261 "' column encoding sizes do not match.");
4266 if (!populate_table) {
4270 int64_t total_row_count = 0;
4271 int64_t total_source_query_time_ms = 0;
4272 int64_t total_target_value_translate_time_ms = 0;
4273 int64_t total_data_load_time_ms = 0;
4276 auto target_column_descriptors = get_target_column_descriptors(td);
4277 auto outer_frag_count =
4280 size_t outer_frag_end = outer_frag_count == 0 ? 1 : outer_frag_count;
4281 auto query_session = session ? session->get_session_id() :
"";
4283 std::string work_type_str = for_CTAS ?
"CTAS" :
"ITAS";
4285 for (
size_t outer_frag_idx = 0; outer_frag_idx < outer_frag_end; outer_frag_idx++) {
4286 std::vector<size_t> allowed_outer_fragment_indices;
4288 if (outer_frag_count) {
4289 allowed_outer_fragment_indices.push_back(outer_frag_idx);
4293 std::vector<AggregatedResult> query_results =
4296 allowed_outer_fragment_indices,
4298 total_source_query_time_ms +=
timer_stop(query_clock_begin);
4301 auto query_str =
"INSERT_DATA for " + work_type_str;
4306 executor->enrollQuerySession(query_session,
4310 QuerySessionStatus::QueryStatus::RUNNING_IMPORTER);
4317 executor->clearQuerySessionStatus(query_session,
start_time);
4321 for (
auto&
res : query_results) {
4323 throw std::runtime_error(
4324 "Query execution has been interrupted while performing " + work_type_str);
4326 auto& result_rows =
res.rs;
4328 const auto num_rows = result_rows->rowCount();
4330 if (0 == num_rows) {
4334 total_row_count += num_rows;
4339 const size_t rows_per_block =
4340 std::max(std::min(num_rows / leaf_count,
size_t(64 * 1024)),
size_t(1));
4342 std::vector<std::unique_ptr<TargetValueConverter>> value_converters;
4346 const int num_worker_threads = std::thread::hardware_concurrency();
4348 std::vector<size_t> thread_start_idx(num_worker_threads),
4349 thread_end_idx(num_worker_threads);
4350 bool can_go_parallel = !result_rows->isTruncated() && rows_per_block > 20000;
4352 std::atomic<size_t> crt_row_idx{0};
4354 auto do_work = [&result_rows, &value_converters, &crt_row_idx](
4356 const size_t block_end,
4357 const size_t num_cols,
4359 bool& stop_convert) {
4360 const auto result_row = result_rows->getRowAtNoTranslations(idx);
4361 if (!result_row.empty()) {
4362 size_t target_row = crt_row_idx.fetch_add(1);
4363 if (target_row >= block_end) {
4364 stop_convert =
true;
4367 for (
unsigned int col = 0; col < num_cols; col++) {
4368 const auto& mapd_variant = result_row[col];
4369 value_converters[col]->convertToColumnarFormat(target_row, &mapd_variant);
4374 auto convert_function = [&thread_start_idx,
4380 &do_work](
const int thread_id,
const size_t block_end) {
4381 const int num_cols = value_converters.size();
4382 const size_t start = thread_start_idx[
thread_id];
4383 const size_t end = thread_end_idx[
thread_id];
4385 bool stop_convert =
false;
4387 size_t local_idx = 0;
4388 for (idx = start; idx < end; ++idx, ++local_idx) {
4389 if (
UNLIKELY((local_idx & 0xFFFF) == 0 &&
4391 throw std::runtime_error(
4392 "Query execution has been interrupted while performing " +
4395 do_work(idx, block_end, num_cols, thread_id, stop_convert);
4401 for (idx = start; idx < end; ++idx) {
4402 do_work(idx, block_end, num_cols, thread_id, stop_convert);
4411 auto single_threaded_value_converter =
4412 [&crt_row_idx, &value_converters, &result_rows](
const size_t idx,
4413 const size_t block_end,
4414 const size_t num_cols,
4415 bool& stop_convert) {
4416 size_t target_row = crt_row_idx.fetch_add(1);
4417 if (target_row >= block_end) {
4418 stop_convert =
true;
4421 const auto result_row = result_rows->getNextRow(
false,
false);
4422 CHECK(!result_row.empty());
4423 for (
unsigned int col = 0; col < num_cols; col++) {
4424 const auto& mapd_variant = result_row[col];
4425 value_converters[col]->convertToColumnarFormat(target_row, &mapd_variant);
4429 auto single_threaded_convert_function = [&value_converters,
4435 &single_threaded_value_converter](
4437 const size_t block_end) {
4438 const int num_cols = value_converters.size();
4439 const size_t start = thread_start_idx[
thread_id];
4440 const size_t end = thread_end_idx[
thread_id];
4442 bool stop_convert =
false;
4444 size_t local_idx = 0;
4445 for (idx = start; idx < end; ++idx, ++local_idx) {
4446 if (
UNLIKELY((local_idx & 0xFFFF) == 0 &&
4448 throw std::runtime_error(
4449 "Query execution has been interrupted while performing " +
4452 single_threaded_value_converter(idx, block_end, num_cols, stop_convert);
4458 for (idx = start; idx < end; ++idx) {
4459 single_threaded_value_converter(idx, end, num_cols, stop_convert);
4468 if (can_go_parallel) {
4469 const size_t entry_count = result_rows->entryCount();
4473 stride = (entry_count + num_worker_threads - 1) / num_worker_threads;
4474 i < num_worker_threads && start_entry < entry_count;
4475 ++i, start_entry += stride) {
4476 const auto end_entry = std::min(start_entry + stride, entry_count);
4477 thread_start_idx[i] = start_entry;
4478 thread_end_idx[i] = end_entry;
4481 thread_start_idx[0] = 0;
4482 thread_end_idx[0] = result_rows->entryCount();
4485 for (
size_t block_start = 0; block_start < num_rows;
4486 block_start += rows_per_block) {
4487 const auto num_rows_this_itr = block_start + rows_per_block < num_rows
4489 : num_rows - block_start;
4491 value_converters.clear();
4493 for (
const auto targetDescriptor : target_column_descriptors) {
4494 auto sourceDataMetaInfo =
res.targets_meta[colNum++];
4500 targetDescriptor->columnType,
4501 !targetDescriptor->columnType.get_notnull(),
4502 result_rows->getRowSetMemOwner()->getLiteralStringDictProxy(),
4504 sourceDataMetaInfo.get_type_info().is_dict_encoded_string()
4505 ? executor->getStringDictionaryProxy(
4506 sourceDataMetaInfo.get_type_info().getStringDictKey(),
4507 result_rows->getRowSetMemOwner(),
4510 auto converter = factory.
create(param);
4511 value_converters.push_back(std::move(converter));
4515 if (can_go_parallel) {
4516 std::vector<std::future<void>> worker_threads;
4517 for (
int i = 0; i < num_worker_threads; ++i) {
4518 worker_threads.push_back(
4522 for (
auto& child : worker_threads) {
4525 for (
auto& child : worker_threads) {
4530 single_threaded_convert_function(0, num_rows_this_itr);
4534 auto finalizer_func =
4535 [](std::unique_ptr<TargetValueConverter>::pointer targetValueConverter) {
4536 targetValueConverter->finalizeDataBlocksForInsertData();
4539 std::vector<std::future<void>> worker_threads;
4540 for (
auto& converterPtr : value_converters) {
4541 worker_threads.push_back(
4545 for (
auto& child : worker_threads) {
4548 for (
auto& child : worker_threads) {
4553 insert_data.
databaseId = catalog.getCurrentDB().dbId;
4556 insert_data.
numRows = num_rows_this_itr;
4558 for (
int col_idx = 0; col_idx < target_column_descriptors.size(); col_idx++) {
4561 throw std::runtime_error(
4562 "Query execution has been interrupted while performing " +
4565 value_converters[col_idx]->addDataBlocksToInsertData(insert_data);
4567 total_target_value_translate_time_ms +=
timer_stop(translate_clock_begin);
4570 auto data_memory_holder =
4572 insertDataLoader.insertData(*session, insert_data);
4573 total_data_load_time_ms +=
timer_stop(data_load_clock_begin);
4580 }
catch (std::exception& e) {
4581 LOG(
ERROR) <<
"An error occurred during ITAS rollback attempt. Table id: "
4582 << td->
tableId <<
", Error: " << e.what();
4587 int64_t total_time_ms = total_source_query_time_ms +
4588 total_target_value_translate_time_ms + total_data_load_time_ms;
4590 VLOG(1) <<
"CTAS/ITAS " << total_row_count <<
" rows loaded in " << total_time_ms
4591 <<
"ms (outer_frag_count=" << outer_frag_count
4592 <<
", query_time=" << total_source_query_time_ms
4593 <<
"ms, translation_time=" << total_target_value_translate_time_ms
4594 <<
"ms, data_load_time=" << total_data_load_time_ms
4597 if (!is_temporary) {
void validate_non_foreign_table_write(const TableDescriptor *table_descriptor)
HOST DEVICE SQLTypes get_subtype() const
HOST DEVICE int get_size() const
bool is_timestamp() const
HOST DEVICE int get_scale() const
static const AccessPrivileges INSERT_INTO_TABLE
std::vector< std::unique_ptr< TypedImportBuffer > > fill_missing_columns(const Catalog_Namespace::Catalog *cat, Fragmenter_Namespace::InsertData &insert_data)
TypeR::rep timer_stop(Type clock_begin)
bool g_enable_non_kernel_time_query_interrupt
HOST DEVICE SQLTypes get_type() const
bool g_enable_string_functions
int tableId
identifies the database into which the data is being inserted
static std::shared_ptr< Executor > getExecutor(const ExecutorId id, const std::string &debug_dir="", const std::string &debug_file="", const SystemParameters &system_parameters=SystemParameters())
size_t numRows
a vector of column ids for the row(s) being inserted
future< Result > async(Fn &&fn, Args &&...args)
std::unique_ptr< QueryConnector > leafs_connector_
specifies the content in-memory of a row in the column metadata table
bool check_session_interrupted(const QuerySessionId &query_session, Executor *executor)
HOST DEVICE EncodingType get_compression() const
bool table_is_temporary(const TableDescriptor *const td)
HOST DEVICE int get_dimension() const
std::string select_query_
std::string get_type_name() const
std::vector< std::unique_ptr< std::string > > column_list_
The data to be inserted using the fragment manager.
static constexpr ExecutorId UNITARY_EXECUTOR_ID
SQLTypeInfo get_elem_type() const
std::shared_ptr< Catalog_Namespace::SessionInfo const > getConstSessionInfo() const
const std::string getQuerySubmittedTime() const
std::unique_ptr< TargetValueConverter > create(ConverterCreateParameter param)