Skip to content

Commit

Permalink
add parsing time to compilation time (#4675)
Browse files Browse the repository at this point in the history
  • Loading branch information
ray6080 authored Jan 3, 2025
1 parent 5ec9fee commit 34397ed
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
5 changes: 4 additions & 1 deletion src/include/parser/statement.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ namespace parser {
class Statement {
public:
explicit Statement(common::StatementType statementType)
: statementType{statementType}, internal{false} {}
: parsingTime{0}, statementType{statementType}, internal{false} {}

virtual ~Statement() = default;

common::StatementType getStatementType() const { return statementType; }
void setToInternal() { internal = true; }
bool isInternal() const { return internal; }
void setParsingTime(double time) { parsingTime = time; }
double getParsingTime() const { return parsingTime; }

bool requireTransaction() const {
switch (statementType) {
Expand All @@ -40,6 +42,7 @@ class Statement {
}

private:
double parsingTime;
common::StatementType statementType;
// By setting the statement to internal, we still execute the statement, but will not return the
// executio result as part of the query result returned to users.
Expand Down
22 changes: 15 additions & 7 deletions src/main/client_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -392,18 +392,25 @@ std::vector<std::shared_ptr<Statement>> ClientContext::parseQuery(std::string_vi
throw ConnectionException("Query is empty.");
}
std::vector<std::shared_ptr<Statement>> statements;
const auto parsedStatements = Parser::parseQuery(query);
auto parserTimer = TimeMetric(true /*enable*/);
parserTimer.start();
auto parsedStatements = Parser::parseQuery(query);
parserTimer.stop();
const auto avgParsingTime = parserTimer.getElapsedTimeMS() / parsedStatements.size() / 1.0;
StandaloneCallRewriter standaloneCallAnalyzer{this};
for (auto i = 0u; i < parsedStatements.size(); i++) {
auto rewriteQuery = standaloneCallAnalyzer.getRewriteQuery(*parsedStatements[i]);
if (!rewriteQuery.empty()) {
parserTimer.start();
auto rewrittenStatements = Parser::parseQuery(rewriteQuery);
parserTimer.stop();
for (auto& statement : rewrittenStatements) {
statement->setToInternal();
statements.push_back(statement);
}
}
statements.push_back(parsedStatements[i]);
parsedStatements[i]->setParsingTime(avgParsingTime + parserTimer.getElapsedTimeMS());
statements.push_back(std::move(parsedStatements[i]));
}
return statements;
}
Expand All @@ -419,7 +426,7 @@ void ClientContext::validateTransaction(const PreparedStatement& preparedStateme
}
if (preparedStatement.parsedStatement->requireTransaction() &&
transactionContext->hasActiveTransaction()) {
// KU_ASSERT(!transactionContext->isAutoTransaction());
KU_ASSERT(!transactionContext->isAutoTransaction());
transactionContext->validateManualTransaction(preparedStatement.readOnly);
}
}
Expand All @@ -428,8 +435,8 @@ std::unique_ptr<PreparedStatement> ClientContext::prepareNoLock(
std::shared_ptr<Statement> parsedStatement, bool shouldCommitNewTransaction,
std::optional<std::unordered_map<std::string, std::shared_ptr<Value>>> inputParams) {
auto preparedStatement = std::make_unique<PreparedStatement>();
auto compilingTimer = TimeMetric(true /* enable */);
compilingTimer.start();
auto prepareTimer = TimeMetric(true /* enable */);
prepareTimer.start();
try {
preparedStatement->preparedSummary.statementType = parsedStatement->getStatementType();
preparedStatement->readOnly = StatementReadWriteAnalyzer().isReadOnly(*parsedStatement);
Expand Down Expand Up @@ -462,8 +469,9 @@ std::unique_ptr<PreparedStatement> ClientContext::prepareNoLock(
preparedStatement->success = false;
preparedStatement->errMsg = exception.what();
}
compilingTimer.stop();
preparedStatement->preparedSummary.compilingTime = compilingTimer.getElapsedTimeMS();
prepareTimer.stop();
preparedStatement->preparedSummary.compilingTime =
preparedStatement->parsedStatement->getParsingTime() + prepareTimer.getElapsedTimeMS();
return preparedStatement;
}

Expand Down
3 changes: 1 addition & 2 deletions src/planner/planner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,7 @@ std::unique_ptr<LogicalPlan> Planner::getBestPlan(const BoundStatement& statemen
default:
KU_UNREACHABLE;
}
// Avoid sharing operator across plans.
return plan->deepCopy();
return plan;
}

std::vector<std::unique_ptr<LogicalPlan>> Planner::getAllPlans(const BoundStatement& statement) {
Expand Down

0 comments on commit 34397ed

Please sign in to comment.