forked from apache/incubator-gluten
-
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.
[GLUTEN-7145][CH][PART]refactor for rel parsers (apache#7193)
What changes were proposed in this pull request? (Please fill in changes proposed in this fix) Fixes: apache#7145 Refactor SerializedPlanParser::parseOp. RelParser don't need to call SerializedPlanParser from down to top at the parsing process How was this patch tested? (Please explain how this patch was tested. E.g. unit tests, integration tests, manual tests) unit tests (If this patch involves UI changes, please attach a screenshot; otherwise, remove this)
Showing
35 changed files
with
475 additions
and
514 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
File renamed without changes.
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 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,49 @@ | ||
/* | ||
* 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 <memory> | ||
#include <optional> | ||
#include <Parser/SerializedPlanParser.h> | ||
#include <Processors/QueryPlan/LimitStep.h> | ||
#include "RelParser.h" | ||
namespace local_engine | ||
{ | ||
class FetchRelParser : public RelParser | ||
{ | ||
public: | ||
explicit FetchRelParser(SerializedPlanParser * plan_parser_) : RelParser(plan_parser_) { } | ||
~FetchRelParser() override = default; | ||
|
||
DB::QueryPlanPtr parse(DB::QueryPlanPtr query_plan, const substrait::Rel & rel, std::list<const substrait::Rel *> &) | ||
{ | ||
const auto & limit = rel.fetch(); | ||
auto limit_step = std::make_unique<DB::LimitStep>(query_plan->getCurrentDataStream(), limit.count(), limit.offset()); | ||
limit_step->setStepDescription("LIMIT"); | ||
steps.push_back(limit_step.get()); | ||
query_plan->addStep(std::move(limit_step)); | ||
return query_plan; | ||
} | ||
std::optional<const substrait::Rel *> getSingleInput(const substrait::Rel & rel) override { return &rel.fetch().input(); } | ||
}; | ||
|
||
void registerFetchRelParser(RelParserFactory & factory) | ||
{ | ||
auto builder = [](SerializedPlanParser * plan_parser_) { return std::make_unique<FetchRelParser>(plan_parser_); }; | ||
factory.registerBuilder(substrait::Rel::RelTypeCase::kFetch, builder); | ||
} | ||
|
||
} |
File renamed without changes.
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
File renamed without changes.
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
File renamed without changes.
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
136 changes: 136 additions & 0 deletions
136
cpp-ch/local-engine/Parser/RelParsers/ReadRelParser.cpp
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,136 @@ | ||
/* | ||
* 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 "ReadRelParser.h" | ||
#include <memory> | ||
|
||
namespace DB::ErrorCodes | ||
{ | ||
extern const int LOGICAL_ERROR; | ||
} | ||
|
||
namespace local_engine | ||
{ | ||
DB::QueryPlanPtr ReadRelParser::parse(DB::QueryPlanPtr query_plan, const substrait::Rel & rel, std::list<const substrait::Rel *> &) | ||
{ | ||
if (query_plan) | ||
throw DB::Exception(DB::ErrorCodes::LOGICAL_ERROR, "Source node's input plan should be null"); | ||
const auto & read = rel.read(); | ||
if (read.has_local_files() || (!read.has_extension_table() && !isReadFromMergeTree(read))) | ||
{ | ||
assert(read.has_base_schema()); | ||
DB::QueryPlanStepPtr read_step; | ||
if (isReadRelFromJava(read)) | ||
read_step = parseReadRelWithJavaIter(read); | ||
else | ||
read_step = parseReadRelWithLocalFile(read); | ||
query_plan = std::make_unique<DB::QueryPlan>(); | ||
steps.emplace_back(read_step.get()); | ||
query_plan->addStep(std::move(read_step)); | ||
|
||
if (getContext()->getSettingsRef().max_threads > 1) | ||
{ | ||
auto buffer_step = std::make_unique<BlocksBufferPoolStep>(query_plan->getCurrentDataStream()); | ||
steps.emplace_back(buffer_step.get()); | ||
query_plan->addStep(std::move(buffer_step)); | ||
} | ||
} | ||
else | ||
{ | ||
substrait::ReadRel::ExtensionTable extension_table; | ||
if (read.has_extension_table()) | ||
extension_table = read.extension_table(); | ||
else | ||
{ | ||
extension_table = BinaryToMessage<substrait::ReadRel::ExtensionTable>(split_info); | ||
logDebugMessage(extension_table, "extension_table"); | ||
} | ||
MergeTreeRelParser mergeTreeParser(getPlanParser(), getContext()); | ||
query_plan = mergeTreeParser.parseReadRel(std::make_unique<DB::QueryPlan>(), read, extension_table); | ||
steps = mergeTreeParser.getSteps(); | ||
} | ||
return query_plan; | ||
} | ||
|
||
bool ReadRelParser::isReadRelFromJava(const substrait::ReadRel & rel) | ||
{ | ||
return rel.has_local_files() && rel.local_files().items().size() == 1 | ||
&& rel.local_files().items().at(0).uri_file().starts_with("iterator"); | ||
} | ||
|
||
bool ReadRelParser::isReadFromMergeTree(const substrait::ReadRel & rel) | ||
{ | ||
assert(rel.has_advanced_extension()); | ||
bool is_read_from_merge_tree; | ||
google::protobuf::StringValue optimization; | ||
optimization.ParseFromString(rel.advanced_extension().optimization().value()); | ||
ReadBufferFromString in(optimization.value()); | ||
if (!checkString("isMergeTree=", in)) | ||
return false; | ||
readBoolText(is_read_from_merge_tree, in); | ||
assertChar('\n', in); | ||
return is_read_from_merge_tree; | ||
} | ||
|
||
DB::QueryPlanStepPtr ReadRelParser::parseReadRelWithJavaIter(const substrait::ReadRel & rel) | ||
{ | ||
GET_JNIENV(env) | ||
SCOPE_EXIT({CLEAN_JNIENV}); | ||
auto first_block = SourceFromJavaIter::peekBlock(env, input_iter); | ||
|
||
/// Try to decide header from the first block read from Java iterator. Thus AggregateFunction with parameters has more precise types. | ||
auto header = first_block.has_value() ? first_block->cloneEmpty() : TypeParser::buildBlockFromNamedStruct(rel.base_schema()); | ||
auto source = std::make_shared<SourceFromJavaIter>( | ||
getContext(), std::move(header), input_iter, is_input_iter_materialize, std::move(first_block)); | ||
|
||
QueryPlanStepPtr source_step = std::make_unique<ReadFromPreparedSource>(Pipe(source)); | ||
source_step->setStepDescription("Read From Java Iter"); | ||
return source_step; | ||
} | ||
|
||
QueryPlanStepPtr ReadRelParser::parseReadRelWithLocalFile(const substrait::ReadRel & rel) | ||
{ | ||
auto header = TypeParser::buildBlockFromNamedStruct(rel.base_schema()); | ||
substrait::ReadRel::LocalFiles local_files; | ||
if (rel.has_local_files()) | ||
local_files = rel.local_files(); | ||
else | ||
{ | ||
local_files = BinaryToMessage<substrait::ReadRel::LocalFiles>(getPlanParser()->nextSplitInfo()); | ||
logDebugMessage(local_files, "local_files"); | ||
} | ||
auto source = std::make_shared<SubstraitFileSource>(getContext(), header, local_files); | ||
auto source_pipe = Pipe(source); | ||
auto source_step = std::make_unique<SubstraitFileSourceStep>(getContext(), std::move(source_pipe), "substrait local files"); | ||
source_step->setStepDescription("read local files"); | ||
if (rel.has_filter()) | ||
{ | ||
DB::ActionsDAG actions_dag{blockToNameAndTypeList(header)}; | ||
const DB::ActionsDAG::Node * filter_node = parseExpression(actions_dag, rel.filter()); | ||
actions_dag.addOrReplaceInOutputs(*filter_node); | ||
assert(filter_node == &(actions_dag.findInOutputs(filter_node->result_name))); | ||
source_step->addFilter(std::move(actions_dag), filter_node->result_name); | ||
} | ||
return source_step; | ||
} | ||
|
||
void registerReadRelParser(RelParserFactory & factory) | ||
{ | ||
auto builder = [](SerializedPlanParser * plan_parser_) { return std::make_unique<ReadRelParser>(plan_parser_); }; | ||
factory.registerBuilder(substrait::Rel::RelTypeCase::kRead, builder); | ||
} | ||
} |
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,71 @@ | ||
/* | ||
* 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. | ||
*/ | ||
#pragma once | ||
#include <Core/Block.h> | ||
#include <Core/Settings.h> | ||
#include <Interpreters/Context.h> | ||
#include <Operator/BlocksBufferPoolTransform.h> | ||
#include <Parser/RelParsers/MergeTreeRelParser.h> | ||
#include <Parser/SerializedPlanParser.h> | ||
#include <Parser/SubstraitParserUtils.h> | ||
#include <Parser/TypeParser.h> | ||
#include <Processors/QueryPlan/ReadFromPreparedSource.h> | ||
#include <Storages/SourceFromJavaIter.h> | ||
#include <Storages/SubstraitSource/SubstraitFileSource.h> | ||
#include <Storages/SubstraitSource/SubstraitFileSourceStep.h> | ||
#include <google/protobuf/wrappers.pb.h> | ||
#include <Common/BlockTypeUtils.h> | ||
#include <Common/JNIUtils.h> | ||
|
||
namespace local_engine | ||
{ | ||
class ReadRelParser : public RelParser | ||
{ | ||
public: | ||
explicit ReadRelParser(SerializedPlanParser * plan_parser_) : RelParser(plan_parser_) { } | ||
~ReadRelParser() override = default; | ||
|
||
DB::QueryPlanPtr | ||
parse(std::vector<DB::QueryPlanPtr> &, const substrait::Rel & rel, std::list<const substrait::Rel *> & rel_stack_) override | ||
{ | ||
DB::QueryPlanPtr query_plan; | ||
return parse(std::move(query_plan), rel, rel_stack_); | ||
} | ||
|
||
DB::QueryPlanPtr parse(DB::QueryPlanPtr query_plan, const substrait::Rel & rel, std::list<const substrait::Rel *> &) override; | ||
// This is source node, there is no input | ||
std::optional<const substrait::Rel *> getSingleInput(const substrait::Rel & rel) override { return {}; } | ||
|
||
bool isReadRelFromJava(const substrait::ReadRel & rel); | ||
bool isReadFromMergeTree(const substrait::ReadRel & rel); | ||
|
||
void setInputIter(jobject input_iter_, bool is_materialze) | ||
{ | ||
input_iter = input_iter_; | ||
is_input_iter_materialize = is_materialze; | ||
} | ||
|
||
void setSplitInfo(String split_info_) { split_info = split_info_; } | ||
|
||
private: | ||
jobject input_iter; | ||
bool is_input_iter_materialize; | ||
String split_info; | ||
DB::QueryPlanStepPtr parseReadRelWithJavaIter(const substrait::ReadRel & rel); | ||
QueryPlanStepPtr parseReadRelWithLocalFile(const substrait::ReadRel & rel); | ||
}; | ||
} |
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 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
File renamed without changes.
File renamed without changes.
Large diffs are not rendered by default.
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
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