forked from pingcap/tiflash
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is an automated cherry-pick of pingcap#6745
Signed-off-by: ti-chi-bot <[email protected]>
- Loading branch information
1 parent
5b6910f
commit 0ff4a26
Showing
8 changed files
with
254 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright 2023 PingCAP, Ltd. | ||
// | ||
// Licensed 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 <DataStreams/LimitTransformAction.h> | ||
#include <common/likely.h> | ||
|
||
namespace DB | ||
{ | ||
namespace | ||
{ | ||
// Removes all rows outside specified range of Block. | ||
void cut(Block & block, size_t rows [[maybe_unused]], size_t limit, size_t pos) | ||
{ | ||
assert(rows + limit > pos); | ||
size_t pop_back_cnt = pos - limit; | ||
for (auto & col : block) | ||
{ | ||
auto mutate_col = (*std::move(col.column)).mutate(); | ||
mutate_col->popBack(pop_back_cnt); | ||
col.column = std::move(mutate_col); | ||
} | ||
} | ||
} // namespace | ||
|
||
bool GlobalLimitTransformAction::transform(Block & block) | ||
{ | ||
if (unlikely(!block)) | ||
return true; | ||
|
||
/// pos - how many lines were read, including the last read block | ||
if (pos >= limit) | ||
return false; | ||
|
||
auto rows = block.rows(); | ||
size_t prev_pos = pos.fetch_add(rows); | ||
if (prev_pos >= limit) | ||
return false; | ||
|
||
size_t cur_pos = prev_pos + rows; | ||
if (cur_pos > limit) | ||
cut(block, rows, limit, cur_pos); | ||
// for pos <= limit, give away the whole block | ||
return true; | ||
} | ||
} // namespace DB |
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,46 @@ | ||
// Copyright 2023 PingCAP, Ltd. | ||
// | ||
// Licensed 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. | ||
#pragma once | ||
|
||
#include <Core/Block.h> | ||
|
||
#include <atomic> | ||
#include <memory> | ||
|
||
namespace DB | ||
{ | ||
struct GlobalLimitTransformAction | ||
{ | ||
public: | ||
GlobalLimitTransformAction( | ||
const Block & header_, | ||
size_t limit_) | ||
: header(header_) | ||
, limit(limit_) | ||
{ | ||
} | ||
|
||
bool transform(Block & block); | ||
|
||
Block getHeader() const { return header; } | ||
size_t getLimit() const { return limit; } | ||
|
||
private: | ||
const Block header; | ||
const size_t limit; | ||
std::atomic_size_t pos{0}; | ||
}; | ||
|
||
using GlobalLimitPtr = std::shared_ptr<GlobalLimitTransformAction>; | ||
} // namespace DB |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Copyright 2023 PingCAP, Ltd. | ||
// | ||
// Licensed 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 <Common/Logger.h> | ||
#include <DataStreams/LimitBlockInputStream.h> | ||
#include <DataStreams/LimitTransformAction.h> | ||
#include <Flash/Coprocessor/DAGContext.h> | ||
#include <Flash/Coprocessor/DAGPipeline.h> | ||
#include <Flash/Coprocessor/InterpreterUtils.h> | ||
#include <Flash/Pipeline/Exec/PipelineExecBuilder.h> | ||
#include <Flash/Planner/Plans/PhysicalLimit.h> | ||
#include <Interpreters/Context.h> | ||
#include <Operators/LimitTransformOp.h> | ||
|
||
namespace DB | ||
{ | ||
PhysicalPlanNodePtr PhysicalLimit::build( | ||
const String & executor_id, | ||
const LoggerPtr & log, | ||
const tipb::Limit & limit, | ||
const PhysicalPlanNodePtr & child) | ||
{ | ||
assert(child); | ||
auto physical_limit = std::make_shared<PhysicalLimit>( | ||
executor_id, | ||
child->getSchema(), | ||
log->identifier(), | ||
child, | ||
limit.limit()); | ||
return physical_limit; | ||
} | ||
|
||
void PhysicalLimit::buildBlockInputStreamImpl(DAGPipeline & pipeline, Context & context, size_t max_streams) | ||
{ | ||
child->buildBlockInputStream(pipeline, context, max_streams); | ||
|
||
pipeline.transform([&](auto & stream) { stream = std::make_shared<LimitBlockInputStream>(stream, limit, /*offset*/ 0, log->identifier()); }); | ||
if (pipeline.hasMoreThanOneStream()) | ||
{ | ||
executeUnion(pipeline, max_streams, log, false, "for partial limit"); | ||
pipeline.transform([&](auto & stream) { stream = std::make_shared<LimitBlockInputStream>(stream, limit, /*offset*/ 0, log->identifier()); }); | ||
} | ||
} | ||
|
||
void PhysicalLimit::buildPipelineExec(PipelineExecGroupBuilder & group_builder, Context & /*context*/, size_t /*concurrency*/) | ||
{ | ||
auto input_header = group_builder.getCurrentHeader(); | ||
auto global_limit = std::make_shared<GlobalLimitTransformAction>(input_header, limit); | ||
group_builder.transform([&](auto & builder) { | ||
builder.appendTransformOp(std::make_unique<LimitTransformOp>(group_builder.exec_status, global_limit, log->identifier())); | ||
}); | ||
} | ||
|
||
void PhysicalLimit::finalize(const Names & parent_require) | ||
{ | ||
child->finalize(parent_require); | ||
} | ||
|
||
const Block & PhysicalLimit::getSampleBlock() const | ||
{ | ||
return child->getSampleBlock(); | ||
} | ||
} // namespace DB |
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