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

108 lines
3.1 KiB
C++
Raw Normal View History

2010-03-18 19:32:14 +00:00
#include <iostream>
#include <IO/WriteBufferFromOStream.h>
#include <Storages/StorageLog.h>
#include <DataStreams/TabSeparatedRowOutputStream.h>
#include <DataStreams/LimitBlockInputStream.h>
#include <DataStreams/BlockOutputStreamFromRowOutputStream.h>
#include <DataStreams/copyData.h>
#include <DataTypes/DataTypesNumber.h>
#include <Columns/ColumnsNumber.h>
#include <Interpreters/Context.h>
#include <Common/typeid_cast.h>
2010-03-18 19:32:14 +00:00
2017-12-02 02:47:12 +00:00
int main(int, char **)
try
2010-03-18 19:32:14 +00:00
{
using namespace DB;
2010-03-18 19:32:14 +00:00
const size_t rows = 10000000;
/// create table with a pair of columns
NamesAndTypesList names_and_types;
names_and_types.emplace_back("a", std::make_shared<DataTypeUInt64>());
names_and_types.emplace_back("b", std::make_shared<DataTypeUInt8>());
2010-03-18 19:32:14 +00:00
StoragePtr table = StorageLog::create("./", "test", names_and_types,
NamesAndTypesList{}, NamesAndTypesList{}, ColumnDefaults{}, DEFAULT_MAX_COMPRESS_BLOCK_SIZE);
table->startup();
2010-03-18 19:32:14 +00:00
/// write into it
{
Block block;
2010-03-18 19:32:14 +00:00
{
ColumnWithTypeAndName column;
column.name = "a";
column.type = table->getDataTypeByName("a");
auto col = column.type->createColumn();
ColumnUInt64::Container & vec = typeid_cast<ColumnUInt64 &>(*col).getData();
vec.resize(rows);
for (size_t i = 0; i < rows; ++i)
vec[i] = i;
column.column = std::move(col);
block.insert(column);
}
{
ColumnWithTypeAndName column;
column.name = "b";
column.type = table->getDataTypeByName("b");
auto col = column.type->createColumn();
ColumnUInt8::Container & vec = typeid_cast<ColumnUInt8 &>(*col).getData();
vec.resize(rows);
for (size_t i = 0; i < rows; ++i)
vec[i] = i * 2;
column.column = std::move(col);
block.insert(column);
}
BlockOutputStreamPtr out = table->write({}, {});
out->write(block);
}
2010-03-18 19:32:14 +00:00
/// read from it
{
Names column_names;
column_names.push_back("a");
column_names.push_back("b");
2012-05-22 18:32:45 +00:00
QueryProcessingStage::Enum stage;
2010-03-18 19:32:14 +00:00
BlockInputStreamPtr in = table->read(column_names, {}, Context::createGlobal(), stage, 8192, 1)[0];
2010-06-04 18:25:25 +00:00
Block sample;
{
ColumnWithTypeAndName col;
col.type = std::make_shared<DataTypeUInt64>();
sample.insert(std::move(col));
}
{
ColumnWithTypeAndName col;
col.type = std::make_shared<DataTypeUInt8>();
sample.insert(std::move(col));
}
WriteBufferFromOStream out_buf(std::cout);
LimitBlockInputStream in_limit(in, 10, 0);
RowOutputStreamPtr output_ = std::make_shared<TabSeparatedRowOutputStream>(out_buf, sample);
BlockOutputStreamFromRowOutputStream output(output_, sample);
copyData(in_limit, output);
}
2010-03-18 19:32:14 +00:00
return 0;
2010-03-18 19:32:14 +00:00
}
catch (const DB::Exception & e)
{
std::cerr << e.what() << ", " << e.displayText() << std::endl;
return 1;
}