-
Notifications
You must be signed in to change notification settings - Fork 86
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
Prepare Statement Improvement #3140
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -190,11 +190,28 @@ std::string ClientContext::getEnvVariable(const std::string& name) { | |
} | ||
|
||
std::unique_ptr<PreparedStatement> ClientContext::prepare(std::string_view query) { | ||
auto preparedStatement = std::unique_ptr<PreparedStatement>(); | ||
if (query.empty()) { | ||
return preparedStatementWithError("Connection Exception: Query is empty."); | ||
} | ||
std::unique_lock<std::mutex> lck{mtx}; | ||
auto parsedStatements = std::vector<std::shared_ptr<Statement>>(); | ||
try { | ||
parsedStatements = Parser::parseQuery(query); | ||
} catch (std::exception& exception) { return preparedStatementWithError(exception.what()); } | ||
if (parsedStatements.size() > 1) { | ||
return preparedStatementWithError( | ||
"Connection Exception: We do not support prepare multiple statements."); | ||
} | ||
return prepareNoLock(parsedStatements[0]); | ||
} | ||
|
||
std::unique_ptr<PreparedStatement> ClientContext::prepareTest(std::string_view query) { | ||
auto preparedStatement = std::unique_ptr<PreparedStatement>(); | ||
std::unique_lock<std::mutex> lck{mtx}; | ||
auto parsedStatements = std::vector<std::unique_ptr<Statement>>(); | ||
auto parsedStatements = std::vector<std::shared_ptr<Statement>>(); | ||
try { | ||
parsedStatements = parseQuery(query); | ||
parsedStatements = Parser::parseQuery(query); | ||
} catch (std::exception& exception) { return preparedStatementWithError(exception.what()); } | ||
if (parsedStatements.size() > 1) { | ||
return preparedStatementWithError( | ||
|
@@ -203,7 +220,8 @@ std::unique_ptr<PreparedStatement> ClientContext::prepare(std::string_view query | |
if (parsedStatements.empty()) { | ||
return preparedStatementWithError("Connection Exception: Query is empty."); | ||
} | ||
return prepareNoLock(parsedStatements[0].get()); | ||
return prepareNoLock( | ||
parsedStatements[0], false /* enumerate all plans */, "", false /*requireNewTx*/); | ||
} | ||
|
||
std::unique_ptr<QueryResult> ClientContext::query(std::string_view queryStatement) { | ||
|
@@ -213,20 +231,20 @@ std::unique_ptr<QueryResult> ClientContext::query(std::string_view queryStatemen | |
std::unique_ptr<QueryResult> ClientContext::query( | ||
std::string_view query, std::string_view encodedJoin, bool enumerateAllPlans) { | ||
lock_t lck{mtx}; | ||
// parsing | ||
auto parsedStatements = std::vector<std::unique_ptr<Statement>>(); | ||
try { | ||
parsedStatements = parseQuery(query); | ||
} catch (std::exception& exception) { return queryResultWithError(exception.what()); } | ||
if (parsedStatements.empty()) { | ||
if (query.empty()) { | ||
return queryResultWithError("Connection Exception: Query is empty."); | ||
} | ||
auto parsedStatements = std::vector<std::shared_ptr<Statement>>(); | ||
try { | ||
parsedStatements = Parser::parseQuery(query); | ||
} catch (std::exception& exception) { return queryResultWithError(exception.what()); } | ||
std::unique_ptr<QueryResult> queryResult; | ||
QueryResult* lastResult = nullptr; | ||
for (auto& statement : parsedStatements) { | ||
auto preparedStatement = prepareNoLock( | ||
statement.get(), enumerateAllPlans /* enumerate all plans */, encodedJoin); | ||
auto currentQueryResult = executeAndAutoCommitIfNecessaryNoLock(preparedStatement.get()); | ||
auto preparedStatement = prepareNoLock(statement, | ||
enumerateAllPlans /* enumerate all plans */, encodedJoin, false /*requireNewTx*/); | ||
auto currentQueryResult = executeAndAutoCommitIfNecessaryNoLock( | ||
preparedStatement.get(), 0u, false /*requiredNexTx*/); | ||
if (!lastResult) { | ||
// first result of the query | ||
queryResult = std::move(currentQueryResult); | ||
|
@@ -256,7 +274,9 @@ std::unique_ptr<PreparedStatement> ClientContext::preparedStatementWithError( | |
} | ||
|
||
std::unique_ptr<PreparedStatement> ClientContext::prepareNoLock( | ||
Statement* parsedStatement, bool enumerateAllPlans, std::string_view encodedJoin) { | ||
std::shared_ptr<Statement> parsedStatement, bool enumerateAllPlans, | ||
std::string_view encodedJoin, bool requireNewTx, | ||
std::optional<std::unordered_map<std::string, std::shared_ptr<common::Value>>> inputParams) { | ||
auto preparedStatement = std::make_unique<PreparedStatement>(); | ||
auto compilingTimer = TimeMetric(true /* enable */); | ||
compilingTimer.start(); | ||
|
@@ -267,18 +287,8 @@ std::unique_ptr<PreparedStatement> ClientContext::prepareNoLock( | |
if (database->systemConfig.readOnly && !preparedStatement->isReadOnly()) { | ||
throw ConnectionException("Cannot execute write operations in a read-only database!"); | ||
} | ||
} catch (std::exception& exception) { | ||
preparedStatement->success = false; | ||
preparedStatement->errMsg = exception.what(); | ||
compilingTimer.stop(); | ||
preparedStatement->preparedSummary.compilingTime = compilingTimer.getElapsedTimeMS(); | ||
return preparedStatement; | ||
} | ||
std::unique_ptr<ExecutionContext> executionContext; | ||
std::unique_ptr<LogicalPlan> logicalPlan; | ||
try { | ||
// parsing | ||
if (parsedStatement->getStatementType() != StatementType::TRANSACTION) { | ||
preparedStatement->parsedStatement = parsedStatement; | ||
if (parsedStatement->requireTx()) { | ||
if (transactionContext->isAutoTransaction()) { | ||
transactionContext->beginAutoTransaction(preparedStatement->readOnly); | ||
} else { | ||
|
@@ -292,6 +302,9 @@ std::unique_ptr<PreparedStatement> ClientContext::prepareNoLock( | |
} | ||
// binding | ||
auto binder = Binder(this); | ||
if (inputParams) { | ||
binder.setInputParameters(*inputParams); | ||
} | ||
auto boundStatement = binder.bind(*parsedStatement); | ||
preparedStatement->parameterMap = binder.getParameterMap(); | ||
preparedStatement->statementResult = | ||
|
@@ -323,6 +336,9 @@ std::unique_ptr<PreparedStatement> ClientContext::prepareNoLock( | |
} else { | ||
preparedStatement->logicalPlans = std::move(plans); | ||
} | ||
if (transactionContext->isAutoTransaction() && requireNewTx) { | ||
this->transactionContext->commit(); | ||
} | ||
} catch (std::exception& exception) { | ||
preparedStatement->success = false; | ||
preparedStatement->errMsg = exception.what(); | ||
|
@@ -333,8 +349,8 @@ std::unique_ptr<PreparedStatement> ClientContext::prepareNoLock( | |
return preparedStatement; | ||
} | ||
|
||
std::vector<std::unique_ptr<Statement>> ClientContext::parseQuery(std::string_view query) { | ||
std::vector<std::unique_ptr<Statement>> statements; | ||
std::vector<std::shared_ptr<Statement>> ClientContext::parseQuery(std::string_view query) { | ||
std::vector<std::shared_ptr<Statement>> statements; | ||
if (query.empty()) { | ||
return statements; | ||
} | ||
|
@@ -356,7 +372,11 @@ std::unique_ptr<QueryResult> ClientContext::executeWithParams(PreparedStatement* | |
std::string errMsg = exception.what(); | ||
return queryResultWithError(errMsg); | ||
} | ||
return executeAndAutoCommitIfNecessaryNoLock(preparedStatement); | ||
// rebind | ||
KU_ASSERT(preparedStatement->parsedStatement != nullptr); | ||
auto rebindPreparedStatement = prepareNoLock( | ||
preparedStatement->parsedStatement, false, "", false, preparedStatement->parameterMap); | ||
return executeAndAutoCommitIfNecessaryNoLock(rebindPreparedStatement.get(), 0u, false); | ||
} | ||
|
||
void ClientContext::bindParametersNoLock(PreparedStatement* preparedStatement, | ||
|
@@ -367,14 +387,6 @@ void ClientContext::bindParametersNoLock(PreparedStatement* preparedStatement, | |
throw Exception("Parameter " + name + " not found."); | ||
} | ||
auto expectParam = parameterMap.at(name); | ||
if (value->getDataType()->getLogicalTypeID() == LogicalTypeID::ANY) { | ||
value->setDataType(*expectParam->getDataType()); | ||
} | ||
if (*expectParam->getDataType() != *value->getDataType()) { | ||
throw Exception("Parameter " + name + " has data type " + | ||
value->getDataType()->toString() + " but expects " + | ||
expectParam->getDataType()->toString() + "."); | ||
} | ||
// The much more natural `parameterMap.at(name) = std::move(v)` fails. | ||
// The reason is that other parts of the code rely on the existing Value object to be | ||
// modified in-place, not replaced in this map. | ||
|
@@ -383,12 +395,11 @@ void ClientContext::bindParametersNoLock(PreparedStatement* preparedStatement, | |
} | ||
|
||
std::unique_ptr<QueryResult> ClientContext::executeAndAutoCommitIfNecessaryNoLock( | ||
PreparedStatement* preparedStatement, uint32_t planIdx) { | ||
PreparedStatement* preparedStatement, uint32_t planIdx, bool requiredNexTx) { | ||
if (!preparedStatement->isSuccess()) { | ||
return queryResultWithError(preparedStatement->errMsg); | ||
} | ||
if (preparedStatement->preparedSummary.statementType != common::StatementType::TRANSACTION && | ||
this->getTx() == nullptr) { | ||
if (preparedStatement->parsedStatement->requireTx() && requiredNexTx && getTx() == nullptr) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can u branch out from this branch and try commit after prepare and we open a new transaction during execute. In this design, we should no longer need to pass |
||
this->transactionContext->beginAutoTransaction(preparedStatement->isReadOnly()); | ||
if (!preparedStatement->readOnly) { | ||
database->catalog->initCatalogContentForWriteTrxIfNecessary(); | ||
|
@@ -470,18 +481,18 @@ void ClientContext::runQuery(std::string query) { | |
if (transactionContext->hasActiveTransaction()) { | ||
transactionContext->commit(); | ||
} | ||
auto parsedStatements = std::vector<std::unique_ptr<Statement>>(); | ||
auto parsedStatements = std::vector<std::shared_ptr<Statement>>(); | ||
try { | ||
parsedStatements = parseQuery(query); | ||
parsedStatements = Parser::parseQuery(query); | ||
} catch (std::exception& exception) { throw ConnectionException(exception.what()); } | ||
if (parsedStatements.empty()) { | ||
throw ConnectionException("Connection Exception: Query is empty."); | ||
} | ||
try { | ||
for (auto& statement : parsedStatements) { | ||
auto preparedStatement = prepareNoLock(statement.get()); | ||
auto preparedStatement = prepareNoLock(statement, false, "", false); | ||
auto currentQueryResult = | ||
executeAndAutoCommitIfNecessaryNoLock(preparedStatement.get()); | ||
executeAndAutoCommitIfNecessaryNoLock(preparedStatement.get(), 0u, false); | ||
if (!currentQueryResult->isSuccess()) { | ||
throw ConnectionException(currentQueryResult->errMsg); | ||
} | ||
|
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldn't we have a similar check when rollback?