-
Notifications
You must be signed in to change notification settings - Fork 454
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
liuneng1994
committed
Aug 26, 2024
1 parent
6815f08
commit f0848ad
Showing
17 changed files
with
366 additions
and
284 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
/* | ||
* 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 "LocalExecutor.h" | ||
|
||
#include <Common/QueryContext.h> | ||
#include <QueryPipeline/printPipeline.h> | ||
#include <Processors/Executors/PipelineExecutor.h> | ||
#include <Core/Settings.h> | ||
|
||
#include "SerializedPlanParser.h" | ||
|
||
using namespace DB; | ||
namespace local_engine | ||
{ | ||
|
||
LocalExecutor::~LocalExecutor() | ||
{ | ||
if (dump_pipeline) | ||
LOG_INFO(&Poco::Logger::get("LocalExecutor"), "Dump pipeline:\n{}", dumpPipeline()); | ||
|
||
if (spark_buffer) | ||
{ | ||
ch_column_to_spark_row->freeMem(spark_buffer->address, spark_buffer->size); | ||
spark_buffer.reset(); | ||
} | ||
} | ||
|
||
std::unique_ptr<SparkRowInfo> LocalExecutor::writeBlockToSparkRow(const Block & block) const | ||
{ | ||
return ch_column_to_spark_row->convertCHColumnToSparkRow(block); | ||
} | ||
|
||
void LocalExecutor::initPullingPipelineExecutor() | ||
{ | ||
if (!executor) | ||
{ | ||
query_pipeline = QueryPipelineBuilder::getPipeline(std::move(*query_pipeline_builder)); | ||
query_pipeline.setNumThreads(1); | ||
executor = std::make_unique<PullingAsyncPipelineExecutor>(query_pipeline); | ||
} | ||
} | ||
|
||
bool LocalExecutor::hasNext() | ||
{ | ||
initPullingPipelineExecutor(); | ||
size_t columns = currentBlock().columns(); | ||
if (columns == 0 || isConsumed()) | ||
{ | ||
auto empty_block = header.cloneEmpty(); | ||
setCurrentBlock(empty_block); | ||
bool has_next = executor->pull(currentBlock()); | ||
produce(); | ||
return has_next; | ||
} | ||
return true; | ||
} | ||
|
||
bool LocalExecutor::fallbackMode() | ||
{ | ||
if (executor.get() || fallback_mode) | ||
std::cerr << fmt::format("executor {} in fallback mode\n", reinterpret_cast<long>(this)); | ||
else | ||
std::cerr << fmt::format("executor {} not in fallback mode\n", reinterpret_cast<long>(this)); | ||
return executor.get() || fallback_mode; | ||
} | ||
|
||
SparkRowInfoPtr LocalExecutor::next() | ||
{ | ||
checkNextValid(); | ||
SparkRowInfoPtr row_info = writeBlockToSparkRow(currentBlock()); | ||
consume(); | ||
if (spark_buffer) | ||
{ | ||
ch_column_to_spark_row->freeMem(spark_buffer->address, spark_buffer->size); | ||
spark_buffer.reset(); | ||
} | ||
spark_buffer = std::make_unique<SparkBuffer>(); | ||
spark_buffer->address = row_info->getBufferAddress(); | ||
spark_buffer->size = row_info->getTotalBytes(); | ||
std::cerr << "call next\n"; | ||
return row_info; | ||
} | ||
Block * LocalExecutor::nextColumnar() | ||
{ | ||
checkNextValid(); | ||
Block * columnar_batch; | ||
if (currentBlock().columns() > 0) | ||
{ | ||
columnar_batch = ¤tBlock(); | ||
} | ||
else | ||
{ | ||
auto empty_block = header.cloneEmpty(); | ||
setCurrentBlock(empty_block); | ||
columnar_batch = ¤tBlock(); | ||
} | ||
consume(); | ||
return columnar_batch; | ||
} | ||
|
||
void LocalExecutor::cancel() | ||
{ | ||
if (executor) | ||
executor->cancel(); | ||
if (push_executor) | ||
push_executor->cancel(); | ||
} | ||
|
||
void LocalExecutor::setSinks(std::function<void(DB::QueryPipelineBuilder &)> setter) | ||
{ | ||
setter(*query_pipeline_builder); | ||
} | ||
|
||
void LocalExecutor::setExternalPipelineBuilder(DB::QueryPipelineBuilderPtr builder) | ||
{ | ||
external_pipeline_builder = std::move(builder); | ||
} | ||
|
||
void LocalExecutor::execute() | ||
{ | ||
chassert(query_pipeline_builder || external_pipeline_builder); | ||
if (external_pipeline_builder) | ||
push_executor = external_pipeline_builder->execute(); | ||
else | ||
push_executor = query_pipeline_builder->execute(); | ||
push_executor->execute(local_engine::QueryContextManager::instance().currentQueryContext()->getSettingsRef().max_threads, false); | ||
} | ||
|
||
Block LocalExecutor::getHeader() | ||
{ | ||
return header; | ||
} | ||
|
||
LocalExecutor::LocalExecutor(QueryPlanPtr query_plan, QueryPipelineBuilderPtr pipeline_builder, bool dump_pipeline_) | ||
: query_pipeline_builder(std::move(pipeline_builder)) | ||
, header(query_plan->getCurrentDataStream().header.cloneEmpty()) | ||
, dump_pipeline(dump_pipeline_) | ||
, ch_column_to_spark_row(std::make_unique<CHColumnToSparkRow>()) | ||
, current_query_plan(std::move(query_plan)) | ||
{ | ||
if (current_executor) | ||
{ | ||
fallback_mode = true; | ||
} | ||
// only need record last executor | ||
current_executor = this; | ||
} | ||
thread_local LocalExecutor * LocalExecutor::current_executor = nullptr; | ||
std::string LocalExecutor::dumpPipeline() const | ||
{ | ||
const auto & processors = query_pipeline.getProcessors(); | ||
for (auto & processor : processors) | ||
{ | ||
WriteBufferFromOwnString buffer; | ||
auto data_stats = processor->getProcessorDataStats(); | ||
buffer << "("; | ||
buffer << "\nexecution time: " << processor->getElapsedNs() / 1000U << " us."; | ||
buffer << "\ninput wait time: " << processor->getInputWaitElapsedNs() / 1000U << " us."; | ||
buffer << "\noutput wait time: " << processor->getOutputWaitElapsedNs() / 1000U << " us."; | ||
buffer << "\ninput rows: " << data_stats.input_rows; | ||
buffer << "\ninput bytes: " << data_stats.input_bytes; | ||
buffer << "\noutput rows: " << data_stats.output_rows; | ||
buffer << "\noutput bytes: " << data_stats.output_bytes; | ||
buffer << ")"; | ||
processor->setDescription(buffer.str()); | ||
} | ||
WriteBufferFromOwnString out; | ||
DB::printPipeline(processors, out); | ||
return out.str(); | ||
} | ||
} |
Oops, something went wrong.