2017-04-01 09:19:00 +00:00
|
|
|
#include <DataTypes/DataTypeString.h>
|
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
|
|
|
#include <DataTypes/DataTypeDateTime.h>
|
|
|
|
#include <Storages/System/StorageSystemZooKeeper.h>
|
|
|
|
#include <Parsers/ASTSelectQuery.h>
|
|
|
|
#include <Parsers/ASTIdentifier.h>
|
|
|
|
#include <Parsers/ASTLiteral.h>
|
|
|
|
#include <Parsers/ASTExpressionList.h>
|
|
|
|
#include <Parsers/ASTFunction.h>
|
|
|
|
#include <Interpreters/Context.h>
|
2020-09-21 21:09:50 +00:00
|
|
|
#include <Interpreters/evaluateConstantExpression.h>
|
2017-06-19 20:06:35 +00:00
|
|
|
#include <Common/ZooKeeper/ZooKeeper.h>
|
2017-07-13 20:58:19 +00:00
|
|
|
#include <Common/typeid_cast.h>
|
2014-10-06 03:49:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2018-11-22 21:19:58 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int BAD_ARGUMENTS;
|
|
|
|
}
|
|
|
|
|
2014-10-06 03:49:56 +00:00
|
|
|
|
2018-07-24 18:40:53 +00:00
|
|
|
NamesAndTypesList StorageSystemZooKeeper::getNamesAndTypes()
|
|
|
|
{
|
2018-07-24 14:28:56 +00:00
|
|
|
return {
|
2017-06-06 18:36:13 +00:00
|
|
|
{ "name", std::make_shared<DataTypeString>() },
|
|
|
|
{ "value", std::make_shared<DataTypeString>() },
|
|
|
|
{ "czxid", std::make_shared<DataTypeInt64>() },
|
|
|
|
{ "mzxid", std::make_shared<DataTypeInt64>() },
|
|
|
|
{ "ctime", std::make_shared<DataTypeDateTime>() },
|
|
|
|
{ "mtime", std::make_shared<DataTypeDateTime>() },
|
|
|
|
{ "version", std::make_shared<DataTypeInt32>() },
|
|
|
|
{ "cversion", std::make_shared<DataTypeInt32>() },
|
|
|
|
{ "aversion", std::make_shared<DataTypeInt32>() },
|
|
|
|
{ "ephemeralOwner", std::make_shared<DataTypeInt64>() },
|
|
|
|
{ "dataLength", std::make_shared<DataTypeInt32>() },
|
|
|
|
{ "numChildren", std::make_shared<DataTypeInt32>() },
|
|
|
|
{ "pzxid", std::make_shared<DataTypeInt64>() },
|
|
|
|
{ "path", std::make_shared<DataTypeString>() },
|
2018-07-24 14:28:56 +00:00
|
|
|
};
|
2014-10-06 03:49:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-09-21 21:09:50 +00:00
|
|
|
static bool extractPathImpl(const IAST & elem, String & res, const Context & context)
|
2014-10-06 03:49:56 +00:00
|
|
|
{
|
2019-03-11 13:22:51 +00:00
|
|
|
const auto * function = elem.as<ASTFunction>();
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!function)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (function->name == "and")
|
|
|
|
{
|
2020-03-09 03:14:24 +00:00
|
|
|
for (const auto & child : function->arguments->children)
|
2020-09-21 21:09:50 +00:00
|
|
|
if (extractPathImpl(*child, res, context))
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (function->name == "equals")
|
|
|
|
{
|
2019-03-15 17:09:14 +00:00
|
|
|
const auto & args = function->arguments->as<ASTExpressionList &>();
|
2020-09-21 21:09:50 +00:00
|
|
|
ASTPtr value;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2019-03-15 17:09:14 +00:00
|
|
|
if (args.children.size() != 2)
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
const ASTIdentifier * ident;
|
2019-03-15 17:09:14 +00:00
|
|
|
if ((ident = args.children.at(0)->as<ASTIdentifier>()))
|
2020-09-21 21:09:50 +00:00
|
|
|
value = args.children.at(1);
|
2019-03-15 17:09:14 +00:00
|
|
|
else if ((ident = args.children.at(1)->as<ASTIdentifier>()))
|
2020-09-21 21:09:50 +00:00
|
|
|
value = args.children.at(0);
|
2017-04-01 07:20:54 +00:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (ident->name != "path")
|
|
|
|
return false;
|
|
|
|
|
2020-09-21 21:09:50 +00:00
|
|
|
auto evaluated = evaluateConstantExpressionAsLiteral(value, context);
|
|
|
|
const auto * literal = evaluated->as<ASTLiteral>();
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!literal)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (literal->value.getType() != Field::Types::String)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
res = literal->value.safeGet<String>();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2014-10-06 03:49:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-12 19:18:07 +00:00
|
|
|
/** Retrieve from the query a condition of the form `path = 'path'`, from conjunctions in the WHERE clause.
|
2014-10-06 03:49:56 +00:00
|
|
|
*/
|
2020-09-21 21:09:50 +00:00
|
|
|
static String extractPath(const ASTPtr & query, const Context & context)
|
2014-10-06 03:49:56 +00:00
|
|
|
{
|
2019-03-15 17:09:14 +00:00
|
|
|
const auto & select = query->as<ASTSelectQuery &>();
|
2019-04-09 14:22:35 +00:00
|
|
|
if (!select.where())
|
2017-04-01 07:20:54 +00:00
|
|
|
return "";
|
2014-10-06 03:49:56 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
String res;
|
2020-09-21 21:09:50 +00:00
|
|
|
return extractPathImpl(*select.where(), res, context) ? res : "";
|
2014-10-06 03:49:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-24 14:28:56 +00:00
|
|
|
void StorageSystemZooKeeper::fillData(MutableColumns & res_columns, const Context & context, const SelectQueryInfo & query_info) const
|
2014-10-06 03:49:56 +00:00
|
|
|
{
|
2020-09-21 21:09:50 +00:00
|
|
|
String path = extractPath(query_info.query, context);
|
2017-04-01 07:20:54 +00:00
|
|
|
if (path.empty())
|
2018-11-22 21:19:58 +00:00
|
|
|
throw Exception("SELECT from system.zookeeper table must contain condition like path = 'path' in WHERE clause.", ErrorCodes::BAD_ARGUMENTS);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
zkutil::ZooKeeperPtr zookeeper = context.getZooKeeper();
|
|
|
|
|
2020-06-11 17:51:14 +00:00
|
|
|
String path_corrected;
|
|
|
|
/// path should starts with '/', otherwise ZBADARGUMENTS will be thrown in
|
|
|
|
/// ZooKeeper::sendThread and the session will fail.
|
|
|
|
if (path[0] != '/')
|
|
|
|
path_corrected = '/';
|
|
|
|
path_corrected += path;
|
2017-04-01 07:20:54 +00:00
|
|
|
/// In all cases except the root, path must not end with a slash.
|
|
|
|
if (path_corrected != "/" && path_corrected.back() == '/')
|
|
|
|
path_corrected.resize(path_corrected.size() - 1);
|
|
|
|
|
|
|
|
zkutil::Strings nodes = zookeeper->getChildren(path_corrected);
|
|
|
|
|
|
|
|
String path_part = path_corrected;
|
|
|
|
if (path_part == "/")
|
|
|
|
path_part.clear();
|
|
|
|
|
2018-08-25 01:58:14 +00:00
|
|
|
std::vector<std::future<Coordination::GetResponse>> futures;
|
2017-04-01 07:20:54 +00:00
|
|
|
futures.reserve(nodes.size());
|
|
|
|
for (const String & node : nodes)
|
|
|
|
futures.push_back(zookeeper->asyncTryGet(path_part + '/' + node));
|
|
|
|
|
|
|
|
for (size_t i = 0, size = nodes.size(); i < size; ++i)
|
|
|
|
{
|
|
|
|
auto res = futures[i].get();
|
2020-06-12 15:09:12 +00:00
|
|
|
if (res.error == Coordination::Error::ZNONODE)
|
2017-04-01 07:20:54 +00:00
|
|
|
continue; /// Node was deleted meanwhile.
|
|
|
|
|
2018-08-25 01:58:14 +00:00
|
|
|
const Coordination::Stat & stat = res.stat;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-12-16 00:49:03 +00:00
|
|
|
size_t col_num = 0;
|
|
|
|
res_columns[col_num++]->insert(nodes[i]);
|
2018-03-24 20:28:43 +00:00
|
|
|
res_columns[col_num++]->insert(res.data);
|
2018-10-22 08:54:54 +00:00
|
|
|
res_columns[col_num++]->insert(stat.czxid);
|
|
|
|
res_columns[col_num++]->insert(stat.mzxid);
|
2017-12-16 00:49:03 +00:00
|
|
|
res_columns[col_num++]->insert(UInt64(stat.ctime / 1000));
|
|
|
|
res_columns[col_num++]->insert(UInt64(stat.mtime / 1000));
|
2018-10-22 08:54:54 +00:00
|
|
|
res_columns[col_num++]->insert(stat.version);
|
|
|
|
res_columns[col_num++]->insert(stat.cversion);
|
|
|
|
res_columns[col_num++]->insert(stat.aversion);
|
|
|
|
res_columns[col_num++]->insert(stat.ephemeralOwner);
|
|
|
|
res_columns[col_num++]->insert(stat.dataLength);
|
|
|
|
res_columns[col_num++]->insert(stat.numChildren);
|
|
|
|
res_columns[col_num++]->insert(stat.pzxid);
|
2017-12-16 00:49:03 +00:00
|
|
|
res_columns[col_num++]->insert(path); /// This is the original path. In order to process the request, condition in WHERE should be triggered.
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2014-10-06 03:49:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|