mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-11-10 01:25:21 +00:00
dbms: development [#CONV-2944].
This commit is contained in:
parent
e07d47617f
commit
d9a68ccf2a
22
dbms/include/DB/Functions/FunctionsLibrary.h
Normal file
22
dbms/include/DB/Functions/FunctionsLibrary.h
Normal file
@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
|
||||
#include <Poco/SharedPtr.h>
|
||||
|
||||
#include <DB/Functions/IFunction.h>
|
||||
|
||||
|
||||
namespace DB
|
||||
{
|
||||
|
||||
/// имя функции -> функция
|
||||
typedef std::map<String, FunctionPtr> Functions;
|
||||
|
||||
|
||||
/// Библиотека обычных функций.
|
||||
namespace FunctionsLibrary
|
||||
{
|
||||
/// Получить все функции.
|
||||
SharedPtr<Functions> get();
|
||||
};
|
||||
|
||||
}
|
@ -8,7 +8,7 @@
|
||||
|
||||
#include <DB/Core/NamesAndTypes.h>
|
||||
#include <DB/Storages/IStorage.h>
|
||||
#include <DB/Functions/IFunction.h>
|
||||
#include <DB/Functions/FunctionsLibrary.h>
|
||||
#include <DB/AggregateFunctions/AggregateFunctionFactory.h>
|
||||
#include <DB/DataTypes/DataTypeFactory.h>
|
||||
#include <DB/Storages/StorageFactory.h>
|
||||
@ -20,9 +20,6 @@ namespace DB
|
||||
|
||||
using Poco::SharedPtr;
|
||||
|
||||
/// имя функции -> функция
|
||||
typedef std::map<String, FunctionPtr> Functions;
|
||||
|
||||
/// имя таблицы -> таблица
|
||||
typedef std::map<String, StoragePtr> Tables;
|
||||
|
||||
@ -44,9 +41,30 @@ struct Context
|
||||
NamesAndTypesList columns; /// Столбцы текущей обрабатываемой таблицы.
|
||||
Settings settings; /// Настройки выполнения запроса.
|
||||
|
||||
SharedPtr<Poco::FastMutex> mutex; /// Для доступа и модификации разделяемых объектов.
|
||||
mutable SharedPtr<Poco::FastMutex> mutex; /// Для доступа и модификации разделяемых объектов.
|
||||
|
||||
Context() : databases(new Databases), functions(new Functions), mutex(new Poco::FastMutex) {}
|
||||
|
||||
/** В сервере есть глобальный контекст.
|
||||
* При соединении, он копируется в контекст сессии.
|
||||
* Для каждого запроса, контекст сессии копируется в контекст запроса.
|
||||
* Блокировка нужна, так как запрос может модифицировать глобальный контекст (SET GLOBAL ...).
|
||||
*/
|
||||
Context(const Context & rhs)
|
||||
{
|
||||
Poco::ScopedLock<Poco::FastMutex> lock(*rhs.mutex);
|
||||
|
||||
path = rhs.path;
|
||||
databases = rhs.databases;
|
||||
current_database = rhs.current_database;
|
||||
functions = rhs.functions;
|
||||
aggregate_function_factory = rhs.aggregate_function_factory;
|
||||
data_type_factory = rhs.data_type_factory;
|
||||
storage_factory = rhs.storage_factory;
|
||||
columns = rhs.columns;
|
||||
settings = rhs.settings;
|
||||
mutex = rhs.mutex;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
@ -17,7 +17,7 @@
|
||||
|
||||
#include <DB/DataTypes/DataTypesNumberFixed.h>
|
||||
|
||||
#include <DB/Functions/FunctionsArithmetic.h>
|
||||
#include <DB/Functions/FunctionsLibrary.h>
|
||||
|
||||
#include <DB/Parsers/ParserSelectQuery.h>
|
||||
|
||||
@ -49,9 +49,7 @@ int main(int argc, char ** argv)
|
||||
|
||||
DB::Context context;
|
||||
context.columns.push_back(DB::NameAndTypePair("number", new DB::DataTypeUInt64));
|
||||
(*context.functions)["plus"] = new DB::FunctionPlus;
|
||||
(*context.functions)["multiply"] = new DB::FunctionMultiply;
|
||||
(*context.functions)["divide"] = new DB::FunctionDivideFloating;
|
||||
context.functions = DB::FunctionsLibrary::get();
|
||||
|
||||
Poco::SharedPtr<DB::Expression> expression = new DB::Expression(ast, context);
|
||||
|
||||
|
@ -19,10 +19,12 @@
|
||||
#include <DB/DataStreams/copyData.h>
|
||||
|
||||
#include <DB/DataTypes/DataTypesNumberFixed.h>
|
||||
#include <DB/DataTypes/DataTypeString.h>
|
||||
#include <DB/DataTypes/DataTypeFixedString.h>
|
||||
#include <DB/DataTypes/DataTypeDate.h>
|
||||
#include <DB/DataTypes/DataTypeDateTime.h>
|
||||
|
||||
//#include <DB/Functions/FunctionsArithmetic.h>
|
||||
#include <DB/Functions/FunctionsComparison.h>
|
||||
//#include <DB/Functions/FunctionsLogical.h>
|
||||
#include <DB/Functions/FunctionsLibrary.h>
|
||||
|
||||
#include <DB/Parsers/ParserSelectQuery.h>
|
||||
#include <DB/Parsers/formatAST.h>
|
||||
@ -100,25 +102,8 @@ int main(int argc, char ** argv)
|
||||
|
||||
DB::Context context;
|
||||
|
||||
/* (*context.functions)["plus"] = new DB::FunctionPlus;
|
||||
(*context.functions)["minus"] = new DB::FunctionMinus;
|
||||
(*context.functions)["multiply"] = new DB::FunctionMultiply;
|
||||
(*context.functions)["divide"] = new DB::FunctionDivideFloating;
|
||||
(*context.functions)["intDiv"] = new DB::FunctionDivideIntegral;
|
||||
(*context.functions)["modulo"] = new DB::FunctionModulo;
|
||||
*/
|
||||
(*context.functions)["equals"] = new DB::FunctionEquals;
|
||||
/* (*context.functions)["notEquals"] = new DB::FunctionNotEquals;
|
||||
(*context.functions)["less"] = new DB::FunctionLess;
|
||||
(*context.functions)["greater"] = new DB::FunctionGreater;
|
||||
(*context.functions)["lessOrEquals"] = new DB::FunctionLessOrEquals;
|
||||
(*context.functions)["greaterOrEquals"] = new DB::FunctionGreaterOrEquals;
|
||||
context.functions = DB::FunctionsLibrary::get();
|
||||
|
||||
(*context.functions)["and"] = new DB::FunctionAnd;
|
||||
(*context.functions)["or"] = new DB::FunctionOr;
|
||||
(*context.functions)["xor"] = new DB::FunctionXor;
|
||||
(*context.functions)["not"] = new DB::FunctionNot;
|
||||
*/
|
||||
context.columns = *names_and_types_list;
|
||||
|
||||
DB::ParserSelectQuery parser;
|
||||
|
97
dbms/src/Functions/FunctionsLibrary.cpp
Normal file
97
dbms/src/Functions/FunctionsLibrary.cpp
Normal file
@ -0,0 +1,97 @@
|
||||
#include <boost/assign/list_inserter.hpp>
|
||||
|
||||
#include <DB/Functions/FunctionsArithmetic.h>
|
||||
#include <DB/Functions/FunctionsComparison.h>
|
||||
#include <DB/Functions/FunctionsLogical.h>
|
||||
#include <DB/Functions/FunctionsString.h>
|
||||
#include <DB/Functions/FunctionsConversion.h>
|
||||
#include <DB/Functions/FunctionsDateTime.h>
|
||||
#include <DB/Functions/FunctionsStringSearch.h>
|
||||
#include <DB/Functions/FunctionsMiscellaneous.h>
|
||||
|
||||
#include <DB/Functions/FunctionsLibrary.h>
|
||||
|
||||
|
||||
namespace DB
|
||||
{
|
||||
|
||||
namespace FunctionsLibrary
|
||||
{
|
||||
SharedPtr<Functions> get()
|
||||
{
|
||||
Functions * res = new Functions;
|
||||
|
||||
boost::assign::insert(*res)
|
||||
("plus", new FunctionPlus)
|
||||
("minus", new FunctionMinus)
|
||||
("multiply", new FunctionMultiply)
|
||||
("divide", new FunctionDivideFloating)
|
||||
("intDiv", new FunctionDivideIntegral)
|
||||
("modulo", new FunctionModulo)
|
||||
("negate", new FunctionNegate)
|
||||
|
||||
("equals", new FunctionEquals)
|
||||
("notEquals", new FunctionNotEquals)
|
||||
("less", new FunctionLess)
|
||||
("greater", new FunctionGreater)
|
||||
("lessOrEquals", new FunctionLessOrEquals)
|
||||
("greaterOrEquals", new FunctionGreaterOrEquals)
|
||||
|
||||
("and", new FunctionAnd)
|
||||
("or", new FunctionOr)
|
||||
("xor", new FunctionXor)
|
||||
("not", new FunctionNot)
|
||||
|
||||
("length", new FunctionLength)
|
||||
("lengthUTF8", new FunctionLengthUTF8)
|
||||
("lower", new FunctionLower)
|
||||
("upper", new FunctionUpper)
|
||||
("lowerUTF8", new FunctionLowerUTF8)
|
||||
("upperUTF8", new FunctionUpperUTF8)
|
||||
("reverse", new FunctionReverse)
|
||||
("reverseUTF8", new FunctionReverseUTF8)
|
||||
("concat", new FunctionConcat)
|
||||
("substring", new FunctionSubstring)
|
||||
("substringUTF8", new FunctionSubstringUTF8)
|
||||
|
||||
("toUInt8", new FunctionToUInt8)
|
||||
("toUInt16", new FunctionToUInt16)
|
||||
("toUInt32", new FunctionToUInt32)
|
||||
("toUInt64", new FunctionToUInt64)
|
||||
("toInt8", new FunctionToInt8)
|
||||
("toInt16", new FunctionToInt16)
|
||||
("toInt32", new FunctionToInt32)
|
||||
("toInt64", new FunctionToInt64)
|
||||
("toFloat32", new FunctionToFloat32)
|
||||
("toFloat64", new FunctionToFloat64)
|
||||
("toVarUInt", new FunctionToVarUInt)
|
||||
("toVarInt", new FunctionToVarInt)
|
||||
("toDate", new FunctionToDate)
|
||||
("toDateTime", new FunctionToDateTime)
|
||||
("toString", new FunctionToString)
|
||||
|
||||
("toYear", new FunctionToYear)
|
||||
("toMonth", new FunctionToMonth)
|
||||
("toDayOfMonth", new FunctionToDayOfMonth)
|
||||
("toDayOfWeek", new FunctionToDayOfWeek)
|
||||
("toHour", new FunctionToHour)
|
||||
("toMinute", new FunctionToMinute)
|
||||
("toSecond", new FunctionToSecond)
|
||||
("toMonday", new FunctionToMonday)
|
||||
("toStartOfMonth", new FunctionToStartOfMonth)
|
||||
("toTime", new FunctionToTime)
|
||||
|
||||
("position", new FunctionPosition)
|
||||
("positionUTF8", new FunctionPositionUTF8)
|
||||
("match", new FunctionMatch)
|
||||
("like", new FunctionLike)
|
||||
("notLike", new FunctionNotLike)
|
||||
|
||||
("visibleWidth", new FunctionVisibleWidth)
|
||||
;
|
||||
|
||||
return res;
|
||||
}
|
||||
};
|
||||
|
||||
}
|
@ -34,7 +34,7 @@ static void executeCreateQuery(const String & query, Context & context, const St
|
||||
ASTCreateQuery & ast_create_query = dynamic_cast<ASTCreateQuery &>(*ast);
|
||||
ast_create_query.attach = true;
|
||||
ast_create_query.database = database;
|
||||
|
||||
|
||||
InterpreterCreateQuery interpreter(ast, context);
|
||||
interpreter.execute();
|
||||
}
|
||||
|
@ -7,9 +7,7 @@
|
||||
|
||||
#include <DB/DataTypes/DataTypesNumberFixed.h>
|
||||
|
||||
#include <DB/Functions/FunctionsArithmetic.h>
|
||||
#include <DB/Functions/FunctionsComparison.h>
|
||||
#include <DB/Functions/FunctionsLogical.h>
|
||||
#include <DB/Functions/FunctionsLibrary.h>
|
||||
|
||||
#include <DB/Parsers/ASTSelectQuery.h>
|
||||
#include <DB/Parsers/ParserSelectQuery.h>
|
||||
@ -110,25 +108,8 @@ int main(int argc, char ** argv)
|
||||
context.columns.push_back(DB::NameAndTypePair("s1", new DB::DataTypeString));
|
||||
context.columns.push_back(DB::NameAndTypePair("s2", new DB::DataTypeString));
|
||||
|
||||
(*context.functions)["plus"] = new DB::FunctionPlus;
|
||||
(*context.functions)["minus"] = new DB::FunctionMinus;
|
||||
(*context.functions)["multiply"] = new DB::FunctionMultiply;
|
||||
(*context.functions)["divide"] = new DB::FunctionDivideFloating;
|
||||
(*context.functions)["intDiv"] = new DB::FunctionDivideIntegral;
|
||||
(*context.functions)["modulo"] = new DB::FunctionModulo;
|
||||
context.functions = DB::FunctionsLibrary::get();
|
||||
|
||||
(*context.functions)["equals"] = new DB::FunctionEquals;
|
||||
(*context.functions)["notEquals"] = new DB::FunctionNotEquals;
|
||||
(*context.functions)["less"] = new DB::FunctionLess;
|
||||
(*context.functions)["greater"] = new DB::FunctionGreater;
|
||||
(*context.functions)["lessOrEquals"] = new DB::FunctionLessOrEquals;
|
||||
(*context.functions)["greaterOrEquals"] = new DB::FunctionGreaterOrEquals;
|
||||
|
||||
(*context.functions)["and"] = new DB::FunctionAnd;
|
||||
(*context.functions)["or"] = new DB::FunctionOr;
|
||||
(*context.functions)["xor"] = new DB::FunctionXor;
|
||||
(*context.functions)["not"] = new DB::FunctionNot;
|
||||
|
||||
DB::Expression expression(ast, context);
|
||||
|
||||
dump(*ast);
|
||||
|
@ -15,17 +15,7 @@
|
||||
#include <DB/Storages/StorageSystemOne.h>
|
||||
#include <DB/Storages/StorageFactory.h>
|
||||
|
||||
#include <DB/DataTypes/DataTypesNumberFixed.h>
|
||||
#include <DB/DataTypes/DataTypeFactory.h>
|
||||
|
||||
#include <DB/Functions/FunctionsArithmetic.h>
|
||||
#include <DB/Functions/FunctionsComparison.h>
|
||||
#include <DB/Functions/FunctionsLogical.h>
|
||||
#include <DB/Functions/FunctionsString.h>
|
||||
#include <DB/Functions/FunctionsConversion.h>
|
||||
#include <DB/Functions/FunctionsDateTime.h>
|
||||
#include <DB/Functions/FunctionsStringSearch.h>
|
||||
#include <DB/Functions/FunctionsMiscellaneous.h>
|
||||
#include <DB/Functions/FunctionsLibrary.h>
|
||||
|
||||
#include <DB/Interpreters/loadMetadata.h>
|
||||
#include <DB/Interpreters/executeQuery.h>
|
||||
@ -41,140 +31,9 @@ int main(int argc, char ** argv)
|
||||
/// Заранее инициализируем DateLUT, чтобы первая инициализация потом не влияла на измеряемую скорость выполнения.
|
||||
Yandex::DateLUTSingleton::instance();
|
||||
|
||||
DB::NamesAndTypesListPtr names_and_types_list = new DB::NamesAndTypesList;
|
||||
|
||||
boost::assign::push_back(*names_and_types_list)
|
||||
("WatchID", new DB::DataTypeUInt64)
|
||||
("JavaEnable", new DB::DataTypeUInt8)
|
||||
("Title", new DB::DataTypeString)
|
||||
("GoodEvent", new DB::DataTypeUInt32)
|
||||
("EventTime", new DB::DataTypeDateTime)
|
||||
("CounterID", new DB::DataTypeUInt32)
|
||||
("ClientIP", new DB::DataTypeUInt32)
|
||||
("RegionID", new DB::DataTypeUInt32)
|
||||
("UniqID", new DB::DataTypeUInt64)
|
||||
("CounterClass", new DB::DataTypeUInt8)
|
||||
("OS", new DB::DataTypeUInt8)
|
||||
("UserAgent", new DB::DataTypeUInt8)
|
||||
("URL", new DB::DataTypeString)
|
||||
("Referer", new DB::DataTypeString)
|
||||
("Refresh", new DB::DataTypeUInt8)
|
||||
("ResolutionWidth", new DB::DataTypeUInt16)
|
||||
("ResolutionHeight", new DB::DataTypeUInt16)
|
||||
("ResolutionDepth", new DB::DataTypeUInt8)
|
||||
("FlashMajor", new DB::DataTypeUInt8)
|
||||
("FlashMinor", new DB::DataTypeUInt8)
|
||||
("FlashMinor2", new DB::DataTypeString)
|
||||
("NetMajor", new DB::DataTypeUInt8)
|
||||
("NetMinor", new DB::DataTypeUInt8)
|
||||
("UserAgentMajor", new DB::DataTypeUInt16)
|
||||
("UserAgentMinor", new DB::DataTypeFixedString(2))
|
||||
("CookieEnable", new DB::DataTypeUInt8)
|
||||
("JavascriptEnable", new DB::DataTypeUInt8)
|
||||
("IsMobile", new DB::DataTypeUInt8)
|
||||
("MobilePhone", new DB::DataTypeUInt8)
|
||||
("MobilePhoneModel", new DB::DataTypeString)
|
||||
("Params", new DB::DataTypeString)
|
||||
("IPNetworkID", new DB::DataTypeUInt32)
|
||||
("TraficSourceID", new DB::DataTypeInt8)
|
||||
("SearchEngineID", new DB::DataTypeUInt16)
|
||||
("SearchPhrase", new DB::DataTypeString)
|
||||
("AdvEngineID", new DB::DataTypeUInt8)
|
||||
("IsArtifical", new DB::DataTypeUInt8)
|
||||
("WindowClientWidth", new DB::DataTypeUInt16)
|
||||
("WindowClientHeight", new DB::DataTypeUInt16)
|
||||
("ClientTimeZone", new DB::DataTypeInt16)
|
||||
("ClientEventTime", new DB::DataTypeDateTime)
|
||||
("SilverlightVersion1", new DB::DataTypeUInt8)
|
||||
("SilverlightVersion2", new DB::DataTypeUInt8)
|
||||
("SilverlightVersion3", new DB::DataTypeUInt32)
|
||||
("SilverlightVersion4", new DB::DataTypeUInt16)
|
||||
("PageCharset", new DB::DataTypeString)
|
||||
("CodeVersion", new DB::DataTypeUInt32)
|
||||
("IsLink", new DB::DataTypeUInt8)
|
||||
("IsDownload", new DB::DataTypeUInt8)
|
||||
("IsNotBounce", new DB::DataTypeUInt8)
|
||||
("FUniqID", new DB::DataTypeUInt64)
|
||||
("OriginalURL", new DB::DataTypeString)
|
||||
("HID", new DB::DataTypeUInt32)
|
||||
("IsOldCounter", new DB::DataTypeUInt8)
|
||||
("IsEvent", new DB::DataTypeUInt8)
|
||||
("IsParameter", new DB::DataTypeUInt8)
|
||||
("DontCountHits", new DB::DataTypeUInt8)
|
||||
("WithHash", new DB::DataTypeUInt8)
|
||||
;
|
||||
|
||||
DB::Context context;
|
||||
|
||||
boost::assign::insert(*context.functions)
|
||||
("plus", new DB::FunctionPlus)
|
||||
("minus", new DB::FunctionMinus)
|
||||
("multiply", new DB::FunctionMultiply)
|
||||
("divide", new DB::FunctionDivideFloating)
|
||||
("intDiv", new DB::FunctionDivideIntegral)
|
||||
("modulo", new DB::FunctionModulo)
|
||||
("negate", new DB::FunctionNegate)
|
||||
|
||||
("equals", new DB::FunctionEquals)
|
||||
("notEquals", new DB::FunctionNotEquals)
|
||||
("less", new DB::FunctionLess)
|
||||
("greater", new DB::FunctionGreater)
|
||||
("lessOrEquals", new DB::FunctionLessOrEquals)
|
||||
("greaterOrEquals", new DB::FunctionGreaterOrEquals)
|
||||
|
||||
("and", new DB::FunctionAnd)
|
||||
("or", new DB::FunctionOr)
|
||||
("xor", new DB::FunctionXor)
|
||||
("not", new DB::FunctionNot)
|
||||
|
||||
("length", new DB::FunctionLength)
|
||||
("lengthUTF8", new DB::FunctionLengthUTF8)
|
||||
("lower", new DB::FunctionLower)
|
||||
("upper", new DB::FunctionUpper)
|
||||
("lowerUTF8", new DB::FunctionLowerUTF8)
|
||||
("upperUTF8", new DB::FunctionUpperUTF8)
|
||||
("reverse", new DB::FunctionReverse)
|
||||
("reverseUTF8", new DB::FunctionReverseUTF8)
|
||||
("concat", new DB::FunctionConcat)
|
||||
("substring", new DB::FunctionSubstring)
|
||||
("substringUTF8", new DB::FunctionSubstringUTF8)
|
||||
|
||||
("toUInt8", new DB::FunctionToUInt8)
|
||||
("toUInt16", new DB::FunctionToUInt16)
|
||||
("toUInt32", new DB::FunctionToUInt32)
|
||||
("toUInt64", new DB::FunctionToUInt64)
|
||||
("toInt8", new DB::FunctionToInt8)
|
||||
("toInt16", new DB::FunctionToInt16)
|
||||
("toInt32", new DB::FunctionToInt32)
|
||||
("toInt64", new DB::FunctionToInt64)
|
||||
("toFloat32", new DB::FunctionToFloat32)
|
||||
("toFloat64", new DB::FunctionToFloat64)
|
||||
("toVarUInt", new DB::FunctionToVarUInt)
|
||||
("toVarInt", new DB::FunctionToVarInt)
|
||||
("toDate", new DB::FunctionToDate)
|
||||
("toDateTime", new DB::FunctionToDateTime)
|
||||
("toString", new DB::FunctionToString)
|
||||
|
||||
("toYear", new DB::FunctionToYear)
|
||||
("toMonth", new DB::FunctionToMonth)
|
||||
("toDayOfMonth", new DB::FunctionToDayOfMonth)
|
||||
("toDayOfWeek", new DB::FunctionToDayOfWeek)
|
||||
("toHour", new DB::FunctionToHour)
|
||||
("toMinute", new DB::FunctionToMinute)
|
||||
("toSecond", new DB::FunctionToSecond)
|
||||
("toMonday", new DB::FunctionToMonday)
|
||||
("toStartOfMonth", new DB::FunctionToStartOfMonth)
|
||||
("toTime", new DB::FunctionToTime)
|
||||
|
||||
("position", new DB::FunctionPosition)
|
||||
("positionUTF8", new DB::FunctionPositionUTF8)
|
||||
("match", new DB::FunctionMatch)
|
||||
("like", new DB::FunctionLike)
|
||||
("notLike", new DB::FunctionNotLike)
|
||||
|
||||
("visibleWidth", new DB::FunctionVisibleWidth)
|
||||
;
|
||||
|
||||
context.functions = DB::FunctionsLibrary::get();
|
||||
context.path = "./";
|
||||
|
||||
context.aggregate_function_factory = new DB::AggregateFunctionFactory;
|
||||
@ -183,9 +42,6 @@ int main(int argc, char ** argv)
|
||||
|
||||
DB::loadMetadata(context);
|
||||
|
||||
(*context.databases)["default"]["hits"] = new DB::StorageLog("./data/default/", "hits", names_and_types_list);
|
||||
(*context.databases)["default"]["hits2"] = new DB::StorageLog("./data/default/", "hits2", names_and_types_list);
|
||||
(*context.databases)["default"]["hits3"] = new DB::StorageLog("./data/default/", "hits3", names_and_types_list);
|
||||
(*context.databases)["system"]["one"] = new DB::StorageSystemOne("one");
|
||||
(*context.databases)["system"]["numbers"] = new DB::StorageSystemNumbers("numbers");
|
||||
context.current_database = "default";
|
||||
|
87
dbms/src/Server/Handler.cpp
Normal file
87
dbms/src/Server/Handler.cpp
Normal file
@ -0,0 +1,87 @@
|
||||
#include <Poco/URI.h>
|
||||
|
||||
#include <DB/Core/ErrorCodes.h>
|
||||
|
||||
#include <DB/IO/ReadBufferFromIStream.h>
|
||||
#include <DB/IO/WriteBufferFromOStream.h>
|
||||
#include <DB/IO/WriteBufferFromString.h>
|
||||
#include <DB/IO/WriteHelpers.h>
|
||||
|
||||
#include <DB/Interpreters/executeQuery.h>
|
||||
|
||||
#include "Handler.h"
|
||||
|
||||
|
||||
namespace DB
|
||||
{
|
||||
|
||||
|
||||
struct HTMLForm : public Poco::Net::HTMLForm
|
||||
{
|
||||
HTMLForm(Poco::Net::HTTPRequest & request)
|
||||
{
|
||||
Poco::URI uri(request.getURI());
|
||||
std::istringstream istr(uri.getRawQuery());
|
||||
readUrl(istr);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
void HTTPRequestHandler::processQuery(Poco::Net::NameValueCollection & params, std::ostream & ostr, std::istream & istr)
|
||||
{
|
||||
BlockInputStreamPtr query_plan;
|
||||
ReadBufferFromIStream in(istr);
|
||||
WriteBufferFromOStream out(ostr);
|
||||
Context context = server.global_context;
|
||||
|
||||
executeQuery(in, out, context, query_plan);
|
||||
|
||||
if (query_plan)
|
||||
{
|
||||
std::stringstream log_str;
|
||||
log_str << "Query plan:\n";
|
||||
query_plan->dumpTree(log_str);
|
||||
LOG_DEBUG(log, log_str.str());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void HTTPRequestHandler::handleRequest(Poco::Net::HTTPServerRequest & request, Poco::Net::HTTPServerResponse & response)
|
||||
{
|
||||
std::ostream & ostr = response.send();
|
||||
try
|
||||
{
|
||||
LOG_TRACE(log, "Request URI: " << request.getURI());
|
||||
|
||||
HTMLForm params(request);
|
||||
std::istream & istr = request.stream();
|
||||
processQuery(params, ostr, istr);
|
||||
|
||||
LOG_INFO(log, "Done processing query");
|
||||
}
|
||||
catch (Poco::Exception & e)
|
||||
{
|
||||
std::stringstream s;
|
||||
s << "Code: " << ErrorCodes::POCO_EXCEPTION << ", e.code() = " << e.code()
|
||||
<< ", e.message() = " << e.message() << ", e.what() = " << e.what();
|
||||
ostr << s.str() << std::endl;
|
||||
LOG_ERROR(log, s.str());
|
||||
}
|
||||
catch (std::exception & e)
|
||||
{
|
||||
std::stringstream s;
|
||||
s << "Code: " << ErrorCodes::STD_EXCEPTION << ". " << e.what();
|
||||
ostr << s.str() << std::endl;
|
||||
LOG_ERROR(log, s.str());
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
std::stringstream s;
|
||||
s << "Code: " << ErrorCodes::UNKNOWN_EXCEPTION << ". Unknown exception.";
|
||||
ostr << s.str() << std::endl;
|
||||
LOG_ERROR(log, s.str());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
31
dbms/src/Server/Handler.h
Normal file
31
dbms/src/Server/Handler.h
Normal file
@ -0,0 +1,31 @@
|
||||
#pragma once
|
||||
|
||||
#include <Poco/Net/HTMLForm.h>
|
||||
|
||||
#include "Server.h"
|
||||
|
||||
|
||||
namespace DB
|
||||
{
|
||||
|
||||
class HTTPRequestHandler : public Poco::Net::HTTPRequestHandler
|
||||
{
|
||||
public:
|
||||
HTTPRequestHandler(Server & server_)
|
||||
: server(server_)
|
||||
, log(&Logger::get("HTTPRequestHandler"))
|
||||
{
|
||||
LOG_TRACE(log, "In constructor.");
|
||||
}
|
||||
|
||||
void handleRequest(Poco::Net::HTTPServerRequest & request, Poco::Net::HTTPServerResponse & response);
|
||||
|
||||
private:
|
||||
Server & server;
|
||||
|
||||
Logger * log;
|
||||
|
||||
void processQuery(Poco::Net::NameValueCollection & params, std::ostream & ostr, std::istream & istr);
|
||||
};
|
||||
|
||||
}
|
87
dbms/src/Server/Server.cpp
Normal file
87
dbms/src/Server/Server.cpp
Normal file
@ -0,0 +1,87 @@
|
||||
#include <Poco/Net/HTTPServerRequest.h>
|
||||
|
||||
#include <Yandex/ApplicationServerExt.h>
|
||||
|
||||
#include <DB/Functions/FunctionsLibrary.h>
|
||||
#include <DB/Interpreters/loadMetadata.h>
|
||||
#include <DB/Storages/StorageSystemNumbers.h>
|
||||
#include <DB/Storages/StorageSystemOne.h>
|
||||
|
||||
#include "Server.h"
|
||||
#include "Handler.h"
|
||||
|
||||
|
||||
namespace DB
|
||||
{
|
||||
|
||||
/// Отвечает "Ok.\n", если получен любой GET запрос. Используется для проверки живости.
|
||||
class PingRequestHandler : public Poco::Net::HTTPRequestHandler
|
||||
{
|
||||
public:
|
||||
PingRequestHandler()
|
||||
{
|
||||
LOG_TRACE((&Logger::get("PingRequestHandler")), "Ping request.");
|
||||
}
|
||||
|
||||
void handleRequest(Poco::Net::HTTPServerRequest & request, Poco::Net::HTTPServerResponse & response)
|
||||
{
|
||||
response.send() << "Ok." << std::endl;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Poco::Net::HTTPRequestHandler * HTTPRequestHandlerFactory::createRequestHandler(
|
||||
const Poco::Net::HTTPServerRequest & request)
|
||||
{
|
||||
LOG_TRACE(log, "HTTP Request. "
|
||||
<< "Method: " << request.getMethod()
|
||||
<< ", Address: " << request.clientAddress().toString()
|
||||
<< ", User-Agent: " << (request.has("User-Agent") ? request.get("User-Agent") : "none"));
|
||||
|
||||
if (request.getURI().find('?') != std::string::npos)
|
||||
return new HTTPRequestHandler(server);
|
||||
else if (request.getMethod() == Poco::Net::HTTPRequest::HTTP_GET)
|
||||
return new PingRequestHandler();
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int Server::main(const std::vector<std::string> & args)
|
||||
{
|
||||
/// Заранее инициализируем DateLUT, чтобы первая инициализация потом не влияла на измеряемую скорость выполнения.
|
||||
Yandex::DateLUTSingleton::instance();
|
||||
|
||||
global_context.path = config.getString("path");
|
||||
global_context.functions = FunctionsLibrary::get();
|
||||
global_context.aggregate_function_factory = new AggregateFunctionFactory;
|
||||
global_context.data_type_factory = new DataTypeFactory;
|
||||
global_context.storage_factory = new StorageFactory;
|
||||
|
||||
loadMetadata(global_context);
|
||||
|
||||
(*global_context.databases)["system"]["one"] = new StorageSystemOne("one");
|
||||
(*global_context.databases)["system"]["numbers"] = new StorageSystemNumbers("numbers");
|
||||
|
||||
global_context.current_database = config.getString("default_database", "default");
|
||||
|
||||
Poco::Net::ServerSocket socket(Poco::Net::SocketAddress("[::]:" + config.getString("http_port")));
|
||||
|
||||
Poco::ThreadPool server_pool(2, config.getInt("max_threads", 128));
|
||||
|
||||
Poco::Net::HTTPServer server(
|
||||
new HTTPRequestHandlerFactory(*this),
|
||||
server_pool,
|
||||
socket,
|
||||
new Poco::Net::HTTPServerParams);
|
||||
|
||||
server.start();
|
||||
waitForTerminationRequest();
|
||||
server.stop();
|
||||
return Application::EXIT_OK;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
YANDEX_APP_SERVER_MAIN(DB::Server);
|
78
dbms/src/Server/Server.h
Normal file
78
dbms/src/Server/Server.h
Normal file
@ -0,0 +1,78 @@
|
||||
#pragma once
|
||||
|
||||
#include <Poco/Util/LayeredConfiguration.h>
|
||||
|
||||
#include <Poco/Net/HTTPServer.h>
|
||||
#include <Poco/Net/HTTPRequestHandlerFactory.h>
|
||||
#include <Poco/Net/HTTPRequestHandler.h>
|
||||
#include <Poco/Net/HTTPRequest.h>
|
||||
#include <Poco/Net/HTTPServerParams.h>
|
||||
#include <Poco/Net/HTTPServerRequest.h>
|
||||
#include <Poco/Net/HTTPServerResponse.h>
|
||||
|
||||
#include <Yandex/logger_useful.h>
|
||||
#include <Yandex/daemon.h>
|
||||
|
||||
#include <DB/Interpreters/Context.h>
|
||||
|
||||
/** Сервер предоставляет два интерфейса:
|
||||
* 1. HTTP - простой интерфейс для доступа из любых приложений.
|
||||
* 2. TCP - интерфейс для доступа из родной библиотеки, родного клиента, и для межсерверного взаимодействия.
|
||||
* Более эффективен, так как
|
||||
* - данные передаются по столбцам;
|
||||
* - данные передаются со сжатием;
|
||||
* - возможно выполнение нескольких запросов одновременно;
|
||||
* Позволяет тонко управлять настройками и получать более подробную информацию в ответах.
|
||||
*/
|
||||
|
||||
|
||||
namespace DB
|
||||
{
|
||||
|
||||
|
||||
class Server;
|
||||
|
||||
class HTTPRequestHandlerFactory : public Poco::Net::HTTPRequestHandlerFactory
|
||||
{
|
||||
private:
|
||||
Server & server;
|
||||
Logger * log;
|
||||
|
||||
public:
|
||||
HTTPRequestHandlerFactory(Server & server_) : server(server_), log(&Logger::get("RequestHandlerFactory")) {}
|
||||
Poco::Net::HTTPRequestHandler * createRequestHandler(const Poco::Net::HTTPServerRequest & request);
|
||||
};
|
||||
|
||||
|
||||
class Server : public Daemon
|
||||
{
|
||||
private:
|
||||
Logger * log;
|
||||
|
||||
public:
|
||||
Poco::Util::LayeredConfiguration & config;
|
||||
|
||||
/// Глобальные настройки севрера
|
||||
Context global_context;
|
||||
|
||||
|
||||
Server() : log(&Logger::get("Server")), config(Application::instance().config()) {}
|
||||
|
||||
protected:
|
||||
void initialize(Application& self)
|
||||
{
|
||||
Daemon::initialize(self);
|
||||
logger().information("starting up");
|
||||
}
|
||||
|
||||
void uninitialize()
|
||||
{
|
||||
logger().information("shutting down");
|
||||
Daemon::uninitialize();
|
||||
}
|
||||
|
||||
int main(const std::vector<std::string>& args);
|
||||
};
|
||||
|
||||
|
||||
}
|
61
dbms/src/Server/metadata/default/hits.sql
Normal file
61
dbms/src/Server/metadata/default/hits.sql
Normal file
@ -0,0 +1,61 @@
|
||||
ATTACH TABLE hits
|
||||
(
|
||||
WatchID UInt64,
|
||||
JavaEnable UInt8,
|
||||
Title String,
|
||||
GoodEvent UInt32,
|
||||
EventTime DateTime,
|
||||
CounterID UInt32,
|
||||
ClientIP UInt32,
|
||||
RegionID UInt32,
|
||||
UniqID UInt64,
|
||||
CounterClass UInt8,
|
||||
OS UInt8,
|
||||
UserAgent UInt8,
|
||||
URL String,
|
||||
Referer String,
|
||||
Refresh UInt8,
|
||||
ResolutionWidth UInt16,
|
||||
ResolutionHeight UInt16,
|
||||
ResolutionDepth UInt8,
|
||||
FlashMajor UInt8,
|
||||
FlashMinor UInt8,
|
||||
FlashMinor2 String,
|
||||
NetMajor UInt8,
|
||||
NetMinor UInt8,
|
||||
UserAgentMajor UInt16,
|
||||
UserAgentMinor FixedString(2),
|
||||
CookieEnable UInt8,
|
||||
JavascriptEnable UInt8,
|
||||
IsMobile UInt8,
|
||||
MobilePhone UInt8,
|
||||
MobilePhoneModel String,
|
||||
Params String,
|
||||
IPNetworkID UInt32,
|
||||
TraficSourceID Int8,
|
||||
SearchEngineID UInt16,
|
||||
SearchPhrase String,
|
||||
AdvEngineID UInt8,
|
||||
IsArtifical UInt8,
|
||||
WindowClientWidth UInt16,
|
||||
WindowClientHeight UInt16,
|
||||
ClientTimeZone Int16,
|
||||
ClientEventTime DateTime,
|
||||
SilverlightVersion1 UInt8,
|
||||
SilverlightVersion2 UInt8,
|
||||
SilverlightVersion3 UInt32,
|
||||
SilverlightVersion4 UInt16,
|
||||
PageCharset String,
|
||||
CodeVersion UInt32,
|
||||
IsLink UInt8,
|
||||
IsDownload UInt8,
|
||||
IsNotBounce UInt8,
|
||||
FUniqID UInt64,
|
||||
OriginalURL String,
|
||||
HID UInt32,
|
||||
IsOldCounter UInt8,
|
||||
IsEvent UInt8,
|
||||
IsParameter UInt8,
|
||||
DontCountHits UInt8,
|
||||
WithHash UInt8
|
||||
) ENGINE = Log
|
Loading…
Reference in New Issue
Block a user