3631 auto& catalog = session->getCatalog();
3633 bool populate_table =
false;
3636 populate_table =
true;
3640 populate_table =
true;
3644 auto get_target_column_descriptors = [
this, &catalog](
const TableDescriptor* td) {
3645 std::vector<const ColumnDescriptor*> target_column_descriptors;
3647 auto list = catalog.getAllColumnMetadataForTable(td->
tableId,
false,
false,
false);
3648 target_column_descriptors = {std::begin(list), std::end(list)};
3652 if (cd ==
nullptr) {
3653 throw std::runtime_error(
"Column " + *c +
" does not exist.");
3655 target_column_descriptors.push_back(cd);
3659 return target_column_descriptors;
3664 if (validate_table) {
3667 throw std::runtime_error(
"Table " +
table_name_ +
" does not exist.");
3670 throw std::runtime_error(
"Insert to views is not supported yet.");
3676 throw std::runtime_error(
"User has no insert privileges on " +
table_name_ +
".");
3681 LocalQueryConnector local_connector;
3683 auto source_column_descriptors = local_connector.getColumnDescriptors(
result,
false);
3685 std::vector<const ColumnDescriptor*> target_column_descriptors =
3686 get_target_column_descriptors(td);
3688 if (source_column_descriptors.size() != target_column_descriptors.size()) {
3689 throw std::runtime_error(
"The number of source and target columns does not match.");
3692 for (
int i = 0; i < source_column_descriptors.size(); i++) {
3694 &(*std::next(source_column_descriptors.begin(), i));
3698 auto type_cannot_be_cast = [](
const auto& col_type) {
3699 return (col_type.is_time() || col_type.is_geometry() || col_type.is_array() ||
3700 col_type.is_boolean());
3703 if (type_cannot_be_cast(source_cd->
columnType) ||
3704 type_cannot_be_cast(target_cd->
columnType)) {
3705 throw std::runtime_error(
"Source '" + source_cd->
columnName +
" " +
3707 "' and target '" + target_cd->
columnName +
" " +
3709 "' column types do not match.");
3714 throw std::runtime_error(
"Source '" + source_cd->
columnName +
" " +
3716 "' and target '" + target_cd->
columnName +
" " +
3718 "' array column element types do not match.");
3723 throw std::runtime_error(
"Source '" + source_cd->
columnName +
" " +
3725 "' and target '" + target_cd->
columnName +
" " +
3727 "' column types do not match.");
3741 throw std::runtime_error(
"Source '" + source_cd->
columnName +
" " +
3743 "' and target '" + target_cd->
columnName +
" " +
3745 "' decimal columns scales do not match.");
3751 throw std::runtime_error(
"Source '" + source_cd->
columnName +
" " +
3753 "' and target '" + target_cd->
columnName +
" " +
3755 "' column types do not match.");
3759 throw std::runtime_error(
"Source '" + source_cd->
columnName +
" " +
3761 "' and target '" + target_cd->
columnName +
" " +
3763 "' columns string encodings do not match.");
3770 throw std::runtime_error(
"Source '" + source_cd->
columnName +
" " +
3772 "' and target '" + target_cd->
columnName +
" " +
3774 "' timestamp column precisions do not match.");
3783 throw std::runtime_error(
"Source '" + source_cd->
columnName +
" " +
3785 "' and target '" + target_cd->
columnName +
" " +
3787 "' column encoding sizes do not match.");
3792 if (!populate_table) {
3796 int64_t total_row_count = 0;
3797 int64_t total_source_query_time_ms = 0;
3798 int64_t total_target_value_translate_time_ms = 0;
3799 int64_t total_data_load_time_ms = 0;
3802 auto target_column_descriptors = get_target_column_descriptors(td);
3803 auto outer_frag_count =
3806 size_t outer_frag_end = outer_frag_count == 0 ? 1 : outer_frag_count;
3807 auto query_session = session ? session->get_session_id() :
"";
3809 std::string work_type_str = for_CTAS ?
"CTAS" :
"ITAS";
3811 for (
size_t outer_frag_idx = 0; outer_frag_idx < outer_frag_end; outer_frag_idx++) {
3812 std::vector<size_t> allowed_outer_fragment_indices;
3814 if (outer_frag_count) {
3815 allowed_outer_fragment_indices.push_back(outer_frag_idx);
3819 std::vector<AggregatedResult> query_results =
3822 allowed_outer_fragment_indices,
3824 total_source_query_time_ms +=
timer_stop(query_clock_begin);
3827 auto query_str =
"INSERT_DATA for " + work_type_str;
3832 executor->enrollQuerySession(query_session,
3836 QuerySessionStatus::QueryStatus::RUNNING_IMPORTER);
3843 executor->clearQuerySessionStatus(query_session,
start_time);
3847 for (
auto&
res : query_results) {
3849 throw std::runtime_error(
3850 "Query execution has been interrupted while performing " + work_type_str);
3852 auto& result_rows =
res.rs;
3854 const auto num_rows = result_rows->rowCount();
3856 if (0 == num_rows) {
3860 total_row_count += num_rows;
3865 const size_t rows_per_block =
3866 std::max(std::min(num_rows / leaf_count,
size_t(64 * 1024)),
size_t(1));
3868 std::vector<std::unique_ptr<TargetValueConverter>> value_converters;
3872 const int num_worker_threads = std::thread::hardware_concurrency();
3874 std::vector<size_t> thread_start_idx(num_worker_threads),
3875 thread_end_idx(num_worker_threads);
3876 bool can_go_parallel = !result_rows->isTruncated() && rows_per_block > 20000;
3878 std::atomic<size_t> crt_row_idx{0};
3880 auto do_work = [&result_rows, &value_converters, &crt_row_idx](
3882 const size_t block_end,
3883 const size_t num_cols,
3885 bool& stop_convert) {
3886 const auto result_row = result_rows->getRowAtNoTranslations(idx);
3887 if (!result_row.empty()) {
3888 size_t target_row = crt_row_idx.fetch_add(1);
3889 if (target_row >= block_end) {
3890 stop_convert =
true;
3893 for (
unsigned int col = 0; col < num_cols; col++) {
3894 const auto& mapd_variant = result_row[col];
3895 value_converters[col]->convertToColumnarFormat(target_row, &mapd_variant);
3900 auto convert_function = [&thread_start_idx,
3906 &do_work](
const int thread_id,
const size_t block_end) {
3907 const int num_cols = value_converters.size();
3908 const size_t start = thread_start_idx[
thread_id];
3909 const size_t end = thread_end_idx[
thread_id];
3911 bool stop_convert =
false;
3913 size_t local_idx = 0;
3914 for (idx = start; idx < end; ++idx, ++local_idx) {
3915 if (
UNLIKELY((local_idx & 0xFFFF) == 0 &&
3917 throw std::runtime_error(
3918 "Query execution has been interrupted while performing " +
3921 do_work(idx, block_end, num_cols, thread_id, stop_convert);
3927 for (idx = start; idx < end; ++idx) {
3928 do_work(idx, block_end, num_cols, thread_id, stop_convert);
3937 auto single_threaded_value_converter =
3938 [&crt_row_idx, &value_converters, &result_rows](
const size_t idx,
3939 const size_t block_end,
3940 const size_t num_cols,
3941 bool& stop_convert) {
3942 size_t target_row = crt_row_idx.fetch_add(1);
3943 if (target_row >= block_end) {
3944 stop_convert =
true;
3947 const auto result_row = result_rows->getNextRow(
false,
false);
3948 CHECK(!result_row.empty());
3949 for (
unsigned int col = 0; col < num_cols; col++) {
3950 const auto& mapd_variant = result_row[col];
3951 value_converters[col]->convertToColumnarFormat(target_row, &mapd_variant);
3955 auto single_threaded_convert_function = [&value_converters,
3961 &single_threaded_value_converter](
3963 const size_t block_end) {
3964 const int num_cols = value_converters.size();
3965 const size_t start = thread_start_idx[
thread_id];
3966 const size_t end = thread_end_idx[
thread_id];
3968 bool stop_convert =
false;
3970 size_t local_idx = 0;
3971 for (idx = start; idx < end; ++idx, ++local_idx) {
3972 if (
UNLIKELY((local_idx & 0xFFFF) == 0 &&
3974 throw std::runtime_error(
3975 "Query execution has been interrupted while performing " +
3978 single_threaded_value_converter(idx, block_end, num_cols, stop_convert);
3984 for (idx = start; idx < end; ++idx) {
3985 single_threaded_value_converter(idx, end, num_cols, stop_convert);
3994 if (can_go_parallel) {
3995 const size_t entry_count = result_rows->entryCount();
3999 stride = (entry_count + num_worker_threads - 1) / num_worker_threads;
4000 i < num_worker_threads && start_entry < entry_count;
4001 ++i, start_entry += stride) {
4002 const auto end_entry = std::min(start_entry + stride, entry_count);
4003 thread_start_idx[i] = start_entry;
4004 thread_end_idx[i] = end_entry;
4007 thread_start_idx[0] = 0;
4008 thread_end_idx[0] = result_rows->entryCount();
4013 for (
size_t block_start = 0; block_start < num_rows;
4014 block_start += rows_per_block) {
4015 const auto num_rows_this_itr = block_start + rows_per_block < num_rows
4017 : num_rows - block_start;
4019 value_converters.clear();
4021 for (
const auto targetDescriptor : target_column_descriptors) {
4022 auto sourceDataMetaInfo =
res.targets_meta[colNum++];
4028 targetDescriptor->columnType,
4029 !targetDescriptor->columnType.get_notnull(),
4030 result_rows->getRowSetMemOwner()->getLiteralStringDictProxy(),
4032 sourceDataMetaInfo.get_type_info().is_dict_encoded_string()
4033 ? executor->getStringDictionaryProxy(
4034 sourceDataMetaInfo.get_type_info().getStringDictKey(),
4035 result_rows->getRowSetMemOwner(),
4038 IS_GEO_POLY(targetDescriptor->columnType.get_type()) &&
4040 ? &render_group_analyzer_map
4042 auto converter = factory.
create(param);
4043 value_converters.push_back(std::move(converter));
4047 if (can_go_parallel) {
4048 std::vector<std::future<void>> worker_threads;
4049 for (
int i = 0; i < num_worker_threads; ++i) {
4050 worker_threads.push_back(
4054 for (
auto& child : worker_threads) {
4057 for (
auto& child : worker_threads) {
4062 single_threaded_convert_function(0, num_rows_this_itr);
4066 auto finalizer_func =
4067 [](std::unique_ptr<TargetValueConverter>::pointer targetValueConverter) {
4068 targetValueConverter->finalizeDataBlocksForInsertData();
4071 std::vector<std::future<void>> worker_threads;
4072 for (
auto& converterPtr : value_converters) {
4073 worker_threads.push_back(
4077 for (
auto& child : worker_threads) {
4080 for (
auto& child : worker_threads) {
4085 insert_data.
databaseId = catalog.getCurrentDB().dbId;
4088 insert_data.
numRows = num_rows_this_itr;
4090 for (
int col_idx = 0; col_idx < target_column_descriptors.size(); col_idx++) {
4093 throw std::runtime_error(
4094 "Query execution has been interrupted while performing " +
4097 value_converters[col_idx]->addDataBlocksToInsertData(insert_data);
4099 total_target_value_translate_time_ms +=
timer_stop(translate_clock_begin);
4102 auto data_memory_holder =
4104 insertDataLoader.insertData(*session, insert_data);
4105 total_data_load_time_ms +=
timer_stop(data_load_clock_begin);
4112 }
catch (std::exception& e) {
4113 LOG(
ERROR) <<
"An error occurred during ITAS rollback attempt. Table id: "
4114 << td->
tableId <<
", Error: " << e.what();
4119 int64_t total_time_ms = total_source_query_time_ms +
4120 total_target_value_translate_time_ms + total_data_load_time_ms;
4122 VLOG(1) <<
"CTAS/ITAS " << total_row_count <<
" rows loaded in " << total_time_ms
4123 <<
"ms (outer_frag_count=" << outer_frag_count
4124 <<
", query_time=" << total_source_query_time_ms
4125 <<
"ms, translation_time=" << total_target_value_translate_time_ms
4126 <<
"ms, data_load_time=" << total_data_load_time_ms
4129 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_
bool g_enable_assign_render_groups
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.
std::map< int, import_export::RenderGroupAnalyzer > RenderGroupAnalyzerMap
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)
static const ExecutorId UNITARY_EXECUTOR_ID