ClickHouse/dbms/src/Storages/tests/merge_tree.cpp

123 lines
3.3 KiB
C++
Raw Normal View History

2013-08-25 03:44:11 +00:00
#include <iostream>
#include <DB/Storages/StorageMergeTree.h>
#include <DB/DataTypes/DataTypeDate.h>
#include <DB/DataTypes/DataTypeArray.h>
#include <DB/DataTypes/DataTypesNumberFixed.h>
#include <DB/DataStreams/TabSeparatedRowOutputStream.h>
#include <DB/DataStreams/BlockOutputStreamFromRowOutputStream.h>
#include <DB/DataStreams/copyData.h>
#include <DB/Parsers/ExpressionListParsers.h>
#include <DB/Parsers/ParserSelectQuery.h>
using Poco::SharedPtr;
int main(int argc, char ** argv)
{
using namespace DB;
2013-08-25 03:44:11 +00:00
try
{
const size_t rows = 12345;
Context context;
/// создаём таблицу с парой столбцов
2013-08-25 03:44:11 +00:00
NamesAndTypesListPtr names_and_types = new NamesAndTypesList;
names_and_types->push_back(NameAndTypePair("d", new DataTypeDate));
names_and_types->push_back(NameAndTypePair("a", new DataTypeArray(new DataTypeUInt32)));
ASTPtr primary_expr;
Expected expected = "";
2013-08-25 03:44:11 +00:00
String primary_expr_str = "d";
const char * begin = primary_expr_str.data();
const char * end = begin + primary_expr_str.size();
ParserExpressionList parser;
if (!parser.parse(begin, end, primary_expr, expected))
2013-08-25 03:44:11 +00:00
throw Poco::Exception("Cannot parse " + primary_expr_str);
2014-05-08 18:34:43 +00:00
StoragePtr table = StorageMergeTree::create("./", "default", "test", names_and_types, context, primary_expr, "d", nullptr, 101);
2013-08-25 03:44:11 +00:00
/// пишем в неё
{
Block block;
ColumnWithNameAndType column1;
column1.name = "d";
column1.type = table->getDataTypeByName("d");
column1.column = column1.type->createColumn();
ColumnUInt16::Container_t & vec1 = typeid_cast<ColumnUInt16 &>(*column1.column).getData();
2013-08-25 03:44:11 +00:00
vec1.resize(rows);
for (size_t i = 0; i < rows; ++i)
vec1[i] = 10000;
block.insert(column1);
ColumnWithNameAndType column2;
column2.name = "a";
column2.type = table->getDataTypeByName("a");
column2.column = column2.type->createColumn();
for (size_t i = 0; i < rows; ++i)
column2.column->insert(Array((rand() % 10) == 0 ? (rand() % 10) : 0, i));
block.insert(column2);
2014-04-08 07:31:51 +00:00
SharedPtr<IBlockOutputStream> out = table->write(nullptr);
2013-08-25 03:44:11 +00:00
out->write(block);
}
2013-08-25 03:44:11 +00:00
/// читаем из неё
{
Names column_names;
column_names.push_back("d");
column_names.push_back("a");
QueryProcessingStage::Enum stage;
ASTPtr select;
Expected expected = "";
2013-08-25 03:44:11 +00:00
String select_str = "SELECT * FROM test";
const char * begin = select_str.data();
const char * end = begin + select_str.size();
ParserSelectQuery parser;
if (!parser.parse(begin, end, select, expected))
2013-08-25 03:44:11 +00:00
throw Poco::Exception("Cannot parse " + primary_expr_str);
SharedPtr<IBlockInputStream> in = table->read(column_names, select, Settings(), stage)[0];
Block sample;
{
ColumnWithNameAndType col;
col.type = names_and_types->front().second;
sample.insert(col);
}
{
ColumnWithNameAndType col;
col.type = names_and_types->back().second;
sample.insert(col);
}
WriteBufferFromFileDescriptor out_buf(STDOUT_FILENO);
2013-08-25 03:44:11 +00:00
RowOutputStreamPtr output_ = new TabSeparatedRowOutputStream(out_buf, sample);
BlockOutputStreamFromRowOutputStream output(output_);
2013-08-25 03:44:11 +00:00
copyData(*in, output);
}
}
catch (const Exception & e)
{
std::cerr << e.displayText() << ", stack trace: \n\n" << e.getStackTrace().toString() << std::endl;
return 1;
}
return 0;
}