diff --git a/src/Analyzer/Passes/QueryAnalysisPass.cpp b/src/Analyzer/Passes/QueryAnalysisPass.cpp index e8dbbc1be15..12d9051ac57 100644 --- a/src/Analyzer/Passes/QueryAnalysisPass.cpp +++ b/src/Analyzer/Passes/QueryAnalysisPass.cpp @@ -6247,7 +6247,7 @@ void QueryAnalyzer::resolveTableFunction(QueryTreeNodePtr & table_function_node, .getTable(insertion_table, scope_context) ->getInMemoryMetadataPtr() ->getColumns(); - const auto & insert_column_names = insert_columns.getInsertable().getNames(); + const auto & insert_column_names = scope_context->hasInsertionTableColumnNames() ? *scope_context->getInsertionTableColumnNames() : insert_columns.getInsertable().getNames(); DB::ColumnsDescription structure_hint; bool use_columns_from_insert_query = true; diff --git a/src/Interpreters/Context.cpp b/src/Interpreters/Context.cpp index fa408b36f55..8f5ad774478 100644 --- a/src/Interpreters/Context.cpp +++ b/src/Interpreters/Context.cpp @@ -1592,7 +1592,7 @@ StoragePtr Context::executeTableFunction(const ASTPtr & table_expression, const ->getInMemoryMetadataPtr() ->getColumns(); - const auto & insert_column_names = hasInsertionTableColumnNames() ? getInsertionTableColumnNames() : insert_columns.getInsertable().getNames(); + const auto & insert_column_names = hasInsertionTableColumnNames() ? *getInsertionTableColumnNames() : insert_columns.getInsertable().getNames(); DB::ColumnsDescription structure_hint; bool use_columns_from_insert_query = true; diff --git a/src/Interpreters/Context.h b/src/Interpreters/Context.h index 19cbf5be54b..446b6619fca 100644 --- a/src/Interpreters/Context.h +++ b/src/Interpreters/Context.h @@ -723,11 +723,7 @@ public: bool hasInsertionTableColumnNames() const { return insertion_table_info.column_names.has_value(); } void setInsertionTable(StorageID db_and_table, const std::optional & column_names = std::nullopt) { insertion_table_info = {std::move(db_and_table), column_names}; } const StorageID & getInsertionTable() const { return insertion_table_info.table; } - const Names & getInsertionTableColumnNames() const - { - assert(insertion_table_info.column_names.has_value()); - return *insertion_table_info.column_names; - } + const std::optional & getInsertionTableColumnNames() const{ return insertion_table_info.column_names; } void setDistributed(bool is_distributed_) { is_distributed = is_distributed_; } bool isDistributed() const { return is_distributed; } diff --git a/src/Interpreters/InterpreterInsertQuery.cpp b/src/Interpreters/InterpreterInsertQuery.cpp index dedbd56d564..4096204ba40 100644 --- a/src/Interpreters/InterpreterInsertQuery.cpp +++ b/src/Interpreters/InterpreterInsertQuery.cpp @@ -474,7 +474,7 @@ BlockIO InterpreterInsertQuery::execute() auto new_context = Context::createCopy(context); new_context->setSettings(new_settings); - new_context->setInsertionTable(getContext()->getInsertionTable()); + new_context->setInsertionTable(getContext()->getInsertionTable(), getContext()->getInsertionTableColumnNames()); auto select_query_options = SelectQueryOptions(QueryProcessingStage::Complete, 1);