Skip to content

Commit 6548cd0

Browse files
committed
Merge branch 'tickets/DM-50657'
2 parents 8fafe5f + 593cd96 commit 6548cd0

File tree

3 files changed

+46
-14
lines changed

3 files changed

+46
-14
lines changed

src/ccontrol/UserQueryFactory.cc

+1
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@ UserQuery::Ptr UserQueryFactory::newUserQuery(std::string const& aQuery, std::st
325325
userQueryId, rowsTable, resultDb, countSpelling, _userQuerySharedResources->qMetaCzarId,
326326
async);
327327
uq->qMetaRegister(resultLocation, msgTableName);
328+
uq->saveResultQuery();
328329
return uq;
329330
}
330331

src/ccontrol/UserQuerySelectCountStar.cc

+34-11
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
#include "UserQuerySelectCountStar.h"
2323

24+
// Third-party headers
25+
#include "boost/algorithm/string/replace.hpp"
2426
#include "boost/lexical_cast.hpp"
2527

2628
// LSST headers
@@ -30,6 +32,7 @@
3032
#include "cconfig/CzarConfig.h"
3133
#include "ccontrol/UserQueryError.h"
3234
#include "ccontrol/UserQueryType.h"
35+
#include "global/LogContext.h"
3336
#include "qdisp/MessageStore.h"
3437
#include "qmeta/QInfo.h"
3538
#include "qmeta/QMetaSelect.h"
@@ -40,10 +43,6 @@
4043

4144
namespace {
4245

43-
std::string g_nextResultTableId(std::string const& userQueryId) {
44-
return "qserv_result_countstar_" + userQueryId;
45-
}
46-
4746
LOG_LOGGER _log = LOG_GET("lsst.qserv.ccontrol.UserQuerySelectCountStar");
4847

4948
} // end namespace
@@ -63,7 +62,6 @@ UserQuerySelectCountStar::UserQuerySelectCountStar(std::string query,
6362
: _qMetaSelect(qMetaSelect),
6463
_queryMetadata(queryMetadata),
6564
_messageStore(std::make_shared<qdisp::MessageStore>()),
66-
_resultTableName(::g_nextResultTableId(userQueryId)),
6765
_userQueryId(userQueryId),
6866
_rowsTable(rowsTable),
6967
_resultDb(resultDb),
@@ -124,7 +122,7 @@ void UserQuerySelectCountStar::submit() {
124122
}
125123

126124
// Create a result table, with one column (row_count) and one row (the total number of rows):
127-
std::string createTable = "CREATE TABLE " + _resultTableName + "(row_count BIGINT UNSIGNED)";
125+
std::string createTable = "CREATE TABLE " + _resultTable + "(row_count BIGINT UNSIGNED)";
128126
LOGS(_log, LOG_LVL_DEBUG, "creating result table: " << createTable);
129127
auto const czarConfig = cconfig::CzarConfig::instance();
130128
auto const resultDbConn = sql::SqlConnectionFactory::make(czarConfig->getMySqlResultConfig());
@@ -137,7 +135,7 @@ void UserQuerySelectCountStar::submit() {
137135
}
138136

139137
// Insert the total row count into the result table:
140-
std::string insertRow = "INSERT INTO " + _resultTableName + " VALUES (";
138+
std::string insertRow = "INSERT INTO " + _resultTable + " VALUES (";
141139
try {
142140
insertRow += lexical_cast<std::string>(row_count);
143141
} catch (bad_lexical_cast const& exc) {
@@ -171,13 +169,38 @@ void UserQuerySelectCountStar::qMetaRegister(std::string const& resultLocation,
171169
std::string const& msgTableName) {
172170
qmeta::QInfo::QType qType = _async ? qmeta::QInfo::ASYNC : qmeta::QInfo::SYNC;
173171
std::string user = "anonymous"; // we do not have access to that info yet
174-
std::string qTemplate = "template";
175-
std::string qMerge = "merge";
172+
std::string qTemplate;
173+
std::string qMerge;
174+
if (_resultLoc.empty()) {
175+
// Special token #QID# is replaced with query ID later.
176+
_resultLoc = "table:result_#QID#";
177+
}
176178
int chunkCount = 0;
177-
qmeta::QInfo qInfo(qType, _qMetaCzarId, user, _query, qTemplate, qMerge, getResultLocation(),
178-
msgTableName, getResultQuery(), chunkCount);
179+
qmeta::QInfo qInfo(qType, _qMetaCzarId, user, _query, qTemplate, qMerge, _resultLoc, msgTableName,
180+
getResultQuery(), chunkCount);
181+
// register query, save its ID
179182
qmeta::QMeta::TableNames tableNames;
180183
_qMetaQueryId = _queryMetadata->registerQuery(qInfo, tableNames);
184+
_queryIdStr = QueryIdHelper::makeIdStr(_qMetaQueryId);
185+
// Add logging context with query ID
186+
QSERV_LOGCONTEXT_QUERY(_qMetaQueryId);
187+
188+
// update #QID# with actual query ID
189+
boost::replace_all(_resultLoc, "#QID#", std::to_string(_qMetaQueryId));
190+
191+
// guess query result location
192+
if (_resultLoc.compare(0, 6, "table:") == 0) {
193+
_resultTable = _resultLoc.substr(6);
194+
} else {
195+
// we only support results going to tables for now, abort for anything else
196+
std::string const msg = "Unexpected result location '" + _resultLoc + "'";
197+
_messageStore->addMessage(-1, "SYSTEM", 1146, msg, MessageSeverity::MSG_ERROR);
198+
throw UserQueryError(getQueryIdString());
199+
}
200+
}
201+
202+
void UserQuerySelectCountStar::saveResultQuery() {
203+
_queryMetadata->saveResultQuery(_qMetaQueryId, getResultQuery());
181204
}
182205

183206
QueryState UserQuerySelectCountStar::join() {

src/ccontrol/UserQuerySelectCountStar.h

+11-3
Original file line numberDiff line numberDiff line change
@@ -84,19 +84,21 @@ class UserQuerySelectCountStar : public UserQuery {
8484
/// in locations other than MySQL tables. We'll switch to getResultLocation()
8585
/// at that point.
8686
/// @return Name of the result table for this query, can be empty
87-
std::string getResultTableName() const { return _resultTableName; }
87+
std::string getResultTableName() const { return _resultTable; }
8888

8989
/// Result location could be something like "table:table_name" or
9090
/// "file:/path/to/file.csv".
9191
/// @return Result location for this query, can be empty
92-
std::string getResultLocation() const override { return "table:" + _resultTableName; }
92+
std::string getResultLocation() const override { return _resultLoc; }
9393

9494
/// @return True if query is async query
9595
bool isAsync() const override { return _async; }
9696

9797
/// @return get the SELECT statement to be executed by proxy
9898
std::string getResultQuery() const override;
9999

100+
std::string getQueryIdString() const override { return _queryIdStr; }
101+
100102
/// @return this query's QueryId.
101103
QueryId getQueryId() const override { return _qMetaQueryId; }
102104

@@ -107,20 +109,26 @@ class UserQuerySelectCountStar : public UserQuery {
107109
*/
108110
void qMetaRegister(std::string const& resultLocation, std::string const& msgTableName);
109111

112+
/// save the result query in the query metadata
113+
void saveResultQuery();
114+
110115
private:
111116
void _qMetaUpdateStatus(qmeta::QInfo::QStatus qStatus);
112117

113118
std::shared_ptr<qmeta::QMetaSelect> _qMetaSelect;
114119
std::shared_ptr<qmeta::QMeta> const& _queryMetadata;
115120
std::shared_ptr<qdisp::MessageStore> _messageStore;
116-
std::string _resultTableName;
121+
std::string _resultTable;
122+
std::string _resultLoc; ///< Result location
117123
std::string _userQueryId;
118124
std::string _rowsTable;
119125
std::string _resultDb;
120126
std::string _countSpelling; // keeps track of how "COUNT" is spelled, for the result query.
121127
std::string _query; // The original query text (without SUBMIT if async)
122128
qmeta::CzarId _qMetaCzarId;
123129
QueryId _qMetaQueryId;
130+
/// QueryId in a standard string form, initially set to unknown.
131+
std::string _queryIdStr{QueryIdHelper::makeIdStr(0, true)};
124132
bool _async;
125133
QueryState _qState{UNKNOWN};
126134
};

0 commit comments

Comments
 (0)