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

123 lines
3.3 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#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;
try
{
const size_t rows = 12345;
Context context;
/// создаём таблицу с парой столбцов
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 = "";
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))
throw Poco::Exception("Cannot parse " + primary_expr_str);
StoragePtr table = StorageMergeTree::create("./", "default", "test", names_and_types, context, primary_expr, "d", nullptr, 101);
/// пишем в неё
{
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();
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);
SharedPtr<IBlockOutputStream> out = table->write(nullptr);
out->write(block);
}
/// читаем из неё
{
Names column_names;
column_names.push_back("d");
column_names.push_back("a");
QueryProcessingStage::Enum stage;
ASTPtr select;
Expected expected = "";
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))
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);
RowOutputStreamPtr output_ = new TabSeparatedRowOutputStream(out_buf, sample);
BlockOutputStreamFromRowOutputStream output(output_);
copyData(*in, output);
}
}
catch (const Exception & e)
{
std::cerr << e.displayText() << ", stack trace: \n\n" << e.getStackTrace().toString() << std::endl;
return 1;
}
return 0;
}