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>
|
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-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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static bool extractPathImpl(const IAST & elem, String & res)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
const ASTFunction * function = typeid_cast<const ASTFunction *>(&elem);
|
|
|
|
if (!function)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (function->name == "and")
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < function->arguments->children.size(); ++i)
|
|
|
|
if (extractPathImpl(*function->arguments->children[i], res))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (function->name == "equals")
|
|
|
|
{
|
|
|
|
const ASTExpressionList & args = typeid_cast<const ASTExpressionList &>(*function->arguments);
|
|
|
|
const IAST * value;
|
|
|
|
|
|
|
|
if (args.children.size() != 2)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const ASTIdentifier * ident;
|
|
|
|
if ((ident = typeid_cast<const ASTIdentifier *>(&*args.children.at(0))))
|
|
|
|
value = &*args.children.at(1);
|
|
|
|
else if ((ident = typeid_cast<const ASTIdentifier *>(&*args.children.at(1))))
|
|
|
|
value = &*args.children.at(0);
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (ident->name != "path")
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const ASTLiteral * literal = typeid_cast<const ASTLiteral *>(value);
|
|
|
|
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
|
|
|
*/
|
|
|
|
static String extractPath(const ASTPtr & query)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
const ASTSelectQuery & select = typeid_cast<const ASTSelectQuery &>(*query);
|
|
|
|
if (!select.where_expression)
|
|
|
|
return "";
|
2014-10-06 03:49:56 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
String res;
|
|
|
|
return extractPathImpl(*select.where_expression, res) ? 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
|
|
|
{
|
2017-07-15 03:48:36 +00:00
|
|
|
String path = extractPath(query_info.query);
|
2017-04-01 07:20:54 +00:00
|
|
|
if (path.empty())
|
|
|
|
throw Exception("SELECT from system.zookeeper table must contain condition like path = 'path' in WHERE clause.");
|
|
|
|
|
|
|
|
zkutil::ZooKeeperPtr zookeeper = context.getZooKeeper();
|
|
|
|
|
|
|
|
/// In all cases except the root, path must not end with a slash.
|
|
|
|
String path_corrected = path;
|
|
|
|
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();
|
2018-08-25 01:58:14 +00:00
|
|
|
if (res.error == Coordination::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);
|
2017-12-16 00:49:03 +00:00
|
|
|
res_columns[col_num++]->insert(Int64(stat.czxid));
|
|
|
|
res_columns[col_num++]->insert(Int64(stat.mzxid));
|
|
|
|
res_columns[col_num++]->insert(UInt64(stat.ctime / 1000));
|
|
|
|
res_columns[col_num++]->insert(UInt64(stat.mtime / 1000));
|
|
|
|
res_columns[col_num++]->insert(Int64(stat.version));
|
|
|
|
res_columns[col_num++]->insert(Int64(stat.cversion));
|
|
|
|
res_columns[col_num++]->insert(Int64(stat.aversion));
|
|
|
|
res_columns[col_num++]->insert(Int64(stat.ephemeralOwner));
|
|
|
|
res_columns[col_num++]->insert(Int64(stat.dataLength));
|
|
|
|
res_columns[col_num++]->insert(Int64(stat.numChildren));
|
|
|
|
res_columns[col_num++]->insert(Int64(stat.pzxid));
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|