Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interpreter: Hand write tipb::Executor. #4632

Merged
merged 18 commits into from
Apr 15, 2022
3 changes: 0 additions & 3 deletions dbms/src/Debug/astToExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@
#include <Poco/StringTokenizer.h>
#include <common/logger_useful.h>

#include <cstddef>
#include <memory>

namespace DB
{
namespace
Expand Down
82 changes: 82 additions & 0 deletions dbms/src/TestUtils/InterpreterTestUtils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Copyright 2022 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 <TestUtils/InterpreterTestUtils.h>

namespace DB::tests
{
namespace
{
String toTreeString(const tipb::Executor & root_executor, size_t level = 0);
ywqzzy marked this conversation as resolved.
Show resolved Hide resolved

// serialize tipb::DAGRequest, print the executor name in a Tree format.
String toTreeString(std::shared_ptr<tipb::DAGRequest> dag_request)
{
assert((dag_request->executors_size() > 0) != dag_request->has_root_executor());
if (dag_request->has_root_executor())
{
return toTreeString(dag_request->root_executor());
}
else
{
FmtBuffer buffer;
String prefix;
traverseExecutors(dag_request.get(), [&buffer, &prefix](const tipb::Executor & executor) {
assert(executor.has_executor_id());
buffer.fmtAppend("{}{}\n", prefix, executor.executor_id());
prefix.append(" ");
return true;
});
return buffer.toString();
}
}

String toTreeString(const tipb::Executor & root_executor, size_t level)
{
FmtBuffer buffer;

auto append_str = [&buffer, &level](const tipb::Executor & executor) {
assert(executor.has_executor_id());
buffer.append(String(level, ' '));
buffer.append(executor.executor_id()).append("\n");
};

traverseExecutorTree(root_executor, [&](const tipb::Executor & executor) {
if (executor.has_join())
{
append_str(executor);
++level;
for (const auto & child : executor.join().children())
buffer.append(toTreeString(child, level));
return false;
}
else
{
append_str(executor);
++level;
return true;
}
});

return buffer.toString();
}
} // namespace

void dagRequestEqual(String & expected_string, const std::shared_ptr<tipb::DAGRequest> & actual)
{
String actual_string = toTreeString(actual);
ASSERT_EQ(Poco::trimInPlace(expected_string), Poco::trimInPlace(actual_string));
}

} // namespace DB::tests
73 changes: 73 additions & 0 deletions dbms/src/TestUtils/InterpreterTestUtils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright 2022 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 <AggregateFunctions/registerAggregateFunctions.h>
#include <Flash/Coprocessor/DAGContext.h>
#include <Flash/Statistics/traverseExecutors.h>
#include <Functions/registerFunctions.h>
#include <TestUtils/FunctionTestUtils.h>
#include <TestUtils/TiFlashTestBasic.h>
#include <TestUtils/TiFlashTestEnv.h>
#include <TestUtils/mockExecutor.h>
namespace DB::tests
{
void dagRequestEqual(String & expected_string, const std::shared_ptr<tipb::DAGRequest> & actual);
class MockExecutorTest : public ::testing::Test
{
protected:
void SetUp() override
{
initializeContext();
}

public:
MockExecutorTest()
: context(TiFlashTestEnv::getContext())
{}

static void SetUpTestCase()
{
try
{
DB::registerFunctions();
}
catch (DB::Exception &)
{
// Maybe another test has already registered, ignore exception here.
}
}

virtual void initializeContext()
{
dag_context_ptr = std::make_unique<DAGContext>(1024);
context.setDAGContext(dag_context_ptr.get());
mock_dag_request_context = MockDAGRequestContext();
}

DAGContext & getDAGContext()
{
assert(dag_context_ptr != nullptr);
return *dag_context_ptr;
}

protected:
Context context;
MockDAGRequestContext mock_dag_request_context;
std::unique_ptr<DAGContext> dag_context_ptr;
};

#define ASSERT_DAGREQUEST_EQAUL(str, request) dagRequestEqual(str, request);
} // namespace DB::tests
Loading