Skip to content

Commit 7b6939d

Browse files
Shanyue Wanfacebook-github-bot
authored andcommitted
refactor: Allow SqlQueryRunner to be extended (#767)
Summary: Create a virtual class to let SqlRunner to be extended. Differential Revision: D90933335
1 parent 27e4602 commit 7b6939d

File tree

4 files changed

+61
-35
lines changed

4 files changed

+61
-35
lines changed

axiom/cli/AxiomSql.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ int main(int argc, char** argv) {
128128

129129
facebook::axiom::Connectors connectors{FLAGS_data_path, FLAGS_data_format};
130130

131-
axiom::sql::SqlQueryRunner runner;
131+
axiom::sql::AxiomSqlQueryRunner runner;
132132
runner.initialize([&](auto& history) {
133133
return std::make_pair(connectors.initialize(history), std::nullopt);
134134
});

axiom/cli/SqlQueryRunner.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ using namespace facebook::axiom;
3535

3636
namespace axiom::sql {
3737

38-
void SqlQueryRunner::initialize(
38+
void AxiomSqlQueryRunner::initialize(
3939
const std::function<std::pair<std::string, std::optional<std::string>>(
4040
optimizer::VeloxHistory& history)>& initializeConnectors) {
4141
static folly::once_flag kInitialized;
@@ -87,7 +87,7 @@ std::vector<velox::RowVectorPtr> fetchResults(runner::LocalRunner& runner) {
8787

8888
} // namespace
8989

90-
connector::TablePtr SqlQueryRunner::createTable(
90+
connector::TablePtr AxiomSqlQueryRunner::createTable(
9191
const presto::CreateTableAsSelectStatement& statement) {
9292
auto metadata =
9393
connector::ConnectorMetadata::metadata(statement.connectorId());
@@ -103,7 +103,7 @@ connector::TablePtr SqlQueryRunner::createTable(
103103
session, statement.tableName(), statement.tableSchema(), options);
104104
}
105105

106-
std::string SqlQueryRunner::dropTable(
106+
std::string AxiomSqlQueryRunner::dropTable(
107107
const presto::DropTableStatement& statement) {
108108
auto metadata =
109109
connector::ConnectorMetadata::metadata(statement.connectorId());
@@ -121,7 +121,7 @@ std::string SqlQueryRunner::dropTable(
121121
}
122122
}
123123

124-
SqlQueryRunner::SqlResult SqlQueryRunner::run(
124+
AxiomSqlQueryRunner::SqlResult AxiomSqlQueryRunner::run(
125125
std::string_view sql,
126126
const RunOptions& options) {
127127
auto statements = parseMultiple(sql, options);
@@ -133,13 +133,13 @@ SqlQueryRunner::SqlResult SqlQueryRunner::run(
133133
return run(*statements[0], options);
134134
}
135135

136-
std::vector<presto::SqlStatementPtr> SqlQueryRunner::parseMultiple(
136+
std::vector<presto::SqlStatementPtr> AxiomSqlQueryRunner::parseMultiple(
137137
std::string_view sql,
138138
const RunOptions& options) {
139139
return prestoParser_->parseMultiple(sql, /*enableTracing=*/options.debugMode);
140140
}
141141

142-
SqlQueryRunner::SqlResult SqlQueryRunner::run(
142+
AxiomSqlQueryRunner::SqlResult AxiomSqlQueryRunner::run(
143143
const presto::SqlStatement& sqlStatement,
144144
const RunOptions& options) {
145145
if (sqlStatement.isExplain()) {
@@ -198,7 +198,7 @@ SqlQueryRunner::SqlResult SqlQueryRunner::run(
198198
return {.results = runSql(logicalPlan, options)};
199199
}
200200

201-
std::shared_ptr<velox::core::QueryCtx> SqlQueryRunner::newQuery(
201+
std::shared_ptr<velox::core::QueryCtx> AxiomSqlQueryRunner::newQuery(
202202
const RunOptions& options) {
203203
++queryCounter_;
204204

@@ -216,7 +216,7 @@ std::shared_ptr<velox::core::QueryCtx> SqlQueryRunner::newQuery(
216216
fmt::format("query_{}", queryCounter_));
217217
}
218218

219-
std::string SqlQueryRunner::runExplain(
219+
std::string AxiomSqlQueryRunner::runExplain(
220220
const logical_plan::LogicalPlanNodePtr& logicalPlan,
221221
presto::ExplainStatement::Type type,
222222
const RunOptions& options) {
@@ -273,7 +273,7 @@ std::string printPlanWithStats(
273273
}
274274
} // namespace
275275

276-
std::string SqlQueryRunner::runExplainAnalyze(
276+
std::string AxiomSqlQueryRunner::runExplainAnalyze(
277277
const logical_plan::LogicalPlanNodePtr& logicalPlan,
278278
const RunOptions& options) {
279279
auto queryCtx = newQuery(options);
@@ -293,7 +293,7 @@ std::string SqlQueryRunner::runExplainAnalyze(
293293
return out.str();
294294
}
295295

296-
optimizer::PlanAndStats SqlQueryRunner::optimize(
296+
optimizer::PlanAndStats AxiomSqlQueryRunner::optimize(
297297
const logical_plan::LogicalPlanNodePtr& logicalPlan,
298298
const std::shared_ptr<velox::core::QueryCtx>& queryCtx,
299299
const RunOptions& options,
@@ -339,7 +339,7 @@ optimizer::PlanAndStats SqlQueryRunner::optimize(
339339
return optimization.toVeloxPlan(best->op);
340340
}
341341

342-
std::shared_ptr<runner::LocalRunner> SqlQueryRunner::makeLocalRunner(
342+
std::shared_ptr<runner::LocalRunner> AxiomSqlQueryRunner::makeLocalRunner(
343343
optimizer::PlanAndStats& planAndStats,
344344
const std::shared_ptr<velox::core::QueryCtx>& queryCtx,
345345
const RunOptions& options) {
@@ -357,7 +357,7 @@ std::shared_ptr<runner::LocalRunner> SqlQueryRunner::makeLocalRunner(
357357
optimizerPool_);
358358
}
359359

360-
std::vector<velox::RowVectorPtr> SqlQueryRunner::runSql(
360+
std::vector<velox::RowVectorPtr> AxiomSqlQueryRunner::runSql(
361361
const logical_plan::LogicalPlanNodePtr& logicalPlan,
362362
const RunOptions& options) {
363363
auto queryCtx = newQuery(options);

axiom/cli/SqlQueryRunner.h

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,7 @@ namespace axiom::sql {
2626

2727
class SqlQueryRunner {
2828
public:
29-
/// @param initializeConnectors Lambda to call to initialize connectors and
30-
/// return a pair of default {connector ID, schema}. Takes a reference to the
31-
/// history to allow for loading from persistent storage.
32-
void initialize(
33-
const std::function<std::pair<std::string, std::optional<std::string>>(
34-
facebook::axiom::optimizer::VeloxHistory& history)>&
35-
initializeConnectors);
36-
37-
/// Results of running a query. SELECT queries return a vector of results.
38-
/// Other queries return a message. SELECT query that returns no rows returns
39-
/// std::nullopt message and empty vector of results.
40-
struct SqlResult {
41-
std::optional<std::string> message;
42-
std::vector<facebook::velox::RowVectorPtr> results;
43-
};
29+
virtual ~SqlQueryRunner() = default;
4430

4531
struct RunOptions {
4632
int32_t numWorkers{4};
@@ -52,30 +38,70 @@ class SqlQueryRunner {
5238
bool debugMode{false};
5339
};
5440

41+
/// Results of running a query. SELECT queries return a vector of results.
42+
/// Other queries return a message. SELECT query that returns no rows returns
43+
/// std::nullopt message and empty vector of results.
44+
struct SqlResult {
45+
std::optional<std::string> message;
46+
std::vector<facebook::velox::RowVectorPtr> results;
47+
};
48+
49+
/// Runs a single SQL statement and returns the result.
50+
virtual SqlResult run(std::string_view sql, const RunOptions& options) = 0;
51+
52+
/// Runs a single parsed SQL statement and returns the result.
53+
virtual SqlResult run(
54+
const presto::SqlStatement& statement,
55+
const RunOptions& options) = 0;
56+
57+
/// Parses SQL text containing one or more semicolon-separated statements.
58+
/// @param sql SQL text to parse.
59+
/// @return Vector of parsed statements.
60+
virtual std::vector<presto::SqlStatementPtr> parseMultiple(
61+
std::string_view sql,
62+
const RunOptions& options) = 0;
63+
64+
virtual std::unordered_map<std::string, std::string>& sessionConfig() = 0;
65+
66+
virtual void saveHistory(const std::string& path) = 0;
67+
68+
virtual void clearHistory() = 0;
69+
};
70+
71+
class AxiomSqlQueryRunner : public SqlQueryRunner {
72+
public:
73+
/// @param initializeConnectors Lambda to call to initialize connectors and
74+
/// return a pair of default {connector ID, schema}. Takes a reference to the
75+
/// history to allow for loading from persistent storage.
76+
void initialize(
77+
const std::function<std::pair<std::string, std::optional<std::string>>(
78+
facebook::axiom::optimizer::VeloxHistory& history)>&
79+
initializeConnectors);
80+
5581
/// Runs a single SQL statement and returns the result.
56-
SqlResult run(std::string_view sql, const RunOptions& options);
82+
SqlResult run(std::string_view sql, const RunOptions& options) override;
5783

5884
/// Runs a single parsed SQL statement and returns the result.
5985
SqlResult run(
6086
const presto::SqlStatement& statement,
61-
const RunOptions& options);
87+
const RunOptions& options) override;
6288

6389
/// Parses SQL text containing one or more semicolon-separated statements.
6490
/// @param sql SQL text to parse.
6591
/// @return Vector of parsed statements.
6692
std::vector<presto::SqlStatementPtr> parseMultiple(
6793
std::string_view sql,
68-
const RunOptions& options);
94+
const RunOptions& options) override;
6995

70-
std::unordered_map<std::string, std::string>& sessionConfig() {
96+
std::unordered_map<std::string, std::string>& sessionConfig() override {
7197
return config_;
7298
}
7399

74-
void saveHistory(const std::string& path) {
100+
void saveHistory(const std::string& path) override {
75101
history_->saveToFile(path);
76102
}
77103

78-
void clearHistory() {
104+
void clearHistory() override {
79105
history_ = std::make_unique<facebook::axiom::optimizer::VeloxHistory>();
80106
}
81107

axiom/cli/tests/SqlQueryRunnerTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class SqlQueryRunnerTest : public ::testing::Test, public test::VectorTestBase {
3838
}
3939

4040
std::unique_ptr<SqlQueryRunner> makeRunner() {
41-
auto runner = std::make_unique<SqlQueryRunner>();
41+
auto runner = std::make_unique<AxiomSqlQueryRunner>();
4242

4343
runner->initialize([&](auto&) {
4444
static int32_t kCounter = 0;

0 commit comments

Comments
 (0)