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>
|
2015-04-11 03:30:54 +00:00
|
|
|
|
#include <DB/Parsers/parseQuery.h>
|
2013-08-25 03:44:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char ** argv)
|
2016-05-28 14:14:18 +00:00
|
|
|
|
try
|
2013-08-25 03:44:11 +00:00
|
|
|
|
{
|
|
|
|
|
using namespace DB;
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
2016-05-28 14:14:18 +00:00
|
|
|
|
const size_t rows = 12345;
|
2013-08-25 03:44:11 +00:00
|
|
|
|
|
2016-05-28 14:14:18 +00:00
|
|
|
|
Context context;
|
2013-08-25 03:44:11 +00:00
|
|
|
|
|
2016-05-28 14:14:18 +00:00
|
|
|
|
/// создаём таблицу с парой столбцов
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
2016-05-28 14:14:18 +00:00
|
|
|
|
NamesAndTypesListPtr names_and_types = std::make_shared<NamesAndTypesList>();
|
|
|
|
|
names_and_types->push_back(NameAndTypePair("d", std::make_shared<DataTypeDate>()));
|
|
|
|
|
names_and_types->push_back(NameAndTypePair("a", std::make_shared<DataTypeArray>(std::make_shared<DataTypeUInt32>())));
|
2013-08-25 03:44:11 +00:00
|
|
|
|
|
2016-05-28 14:14:18 +00:00
|
|
|
|
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();
|
|
|
|
|
const char * max_parsed_pos = begin;
|
|
|
|
|
ParserExpressionList parser(false);
|
|
|
|
|
if (!parser.parse(begin, end, primary_expr, max_parsed_pos, expected))
|
|
|
|
|
throw Poco::Exception("Cannot parse " + primary_expr_str);
|
2013-08-25 03:44:11 +00:00
|
|
|
|
|
2016-05-28 14:14:18 +00:00
|
|
|
|
MergeTreeData::MergingParams params;
|
|
|
|
|
params.mode = MergeTreeData::MergingParams::Ordinary;
|
2016-04-24 21:46:14 +00:00
|
|
|
|
|
2016-05-28 14:14:18 +00:00
|
|
|
|
StoragePtr table = StorageMergeTree::create(
|
|
|
|
|
"./", "default", "test",
|
|
|
|
|
names_and_types, {}, {}, ColumnDefaults{},
|
|
|
|
|
context, primary_expr, "d",
|
|
|
|
|
nullptr, 101, params, {});
|
2013-08-25 03:44:11 +00:00
|
|
|
|
|
2016-05-28 14:14:18 +00:00
|
|
|
|
/// пишем в неё
|
|
|
|
|
{
|
|
|
|
|
Block block;
|
2013-08-25 03:44:11 +00:00
|
|
|
|
|
2016-05-28 14:14:18 +00:00
|
|
|
|
ColumnWithTypeAndName 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
|
|
|
|
|
2016-05-28 14:14:18 +00:00
|
|
|
|
vec1.resize(rows);
|
|
|
|
|
for (size_t i = 0; i < rows; ++i)
|
|
|
|
|
vec1[i] = 10000;
|
2013-08-25 03:44:11 +00:00
|
|
|
|
|
2016-05-28 14:14:18 +00:00
|
|
|
|
block.insert(column1);
|
2013-08-25 03:44:11 +00:00
|
|
|
|
|
2016-05-28 14:14:18 +00:00
|
|
|
|
ColumnWithTypeAndName column2;
|
|
|
|
|
column2.name = "a";
|
|
|
|
|
column2.type = table->getDataTypeByName("a");
|
|
|
|
|
column2.column = column2.type->createColumn();
|
2013-08-25 03:44:11 +00:00
|
|
|
|
|
2016-05-28 14:14:18 +00:00
|
|
|
|
for (size_t i = 0; i < rows; ++i)
|
|
|
|
|
column2.column->insert(Array((rand() % 10) == 0 ? (rand() % 10) : 0, i));
|
2013-08-25 03:44:11 +00:00
|
|
|
|
|
2016-05-28 14:14:18 +00:00
|
|
|
|
block.insert(column2);
|
2013-08-25 03:44:11 +00:00
|
|
|
|
|
2016-05-28 14:14:18 +00:00
|
|
|
|
BlockOutputStreamPtr out = table->write({}, {});
|
|
|
|
|
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();
|
|
|
|
|
const char * max_parsed_pos = begin;
|
|
|
|
|
ParserSelectQuery parser;
|
|
|
|
|
if (!parser.parse(begin, end, select, max_parsed_pos, expected))
|
|
|
|
|
throw Poco::Exception("Cannot parse " + primary_expr_str);
|
|
|
|
|
|
|
|
|
|
BlockInputStreamPtr in = table->read(column_names, select, context, Settings(), stage)[0];
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
2016-05-28 14:14:18 +00:00
|
|
|
|
Block sample;
|
2013-08-25 03:44:11 +00:00
|
|
|
|
{
|
2016-05-28 14:14:18 +00:00
|
|
|
|
ColumnWithTypeAndName col;
|
|
|
|
|
col.type = names_and_types->front().type;
|
|
|
|
|
sample.insert(col);
|
2013-08-25 03:44:11 +00:00
|
|
|
|
}
|
2016-05-28 14:14:18 +00:00
|
|
|
|
{
|
|
|
|
|
ColumnWithTypeAndName col;
|
|
|
|
|
col.type = names_and_types->back().type;
|
|
|
|
|
sample.insert(col);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
WriteBufferFromFileDescriptor out_buf(STDOUT_FILENO);
|
|
|
|
|
|
|
|
|
|
RowOutputStreamPtr output_ = std::make_shared<TabSeparatedRowOutputStream>(out_buf, sample);
|
|
|
|
|
BlockOutputStreamFromRowOutputStream output(output_);
|
|
|
|
|
|
|
|
|
|
copyData(*in, output);
|
2013-08-25 03:44:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2016-05-28 14:14:18 +00:00
|
|
|
|
catch (const DB::Exception & e)
|
|
|
|
|
{
|
|
|
|
|
std::cerr << e.displayText() << ", stack trace: \n\n" << e.getStackTrace().toString() << std::endl;
|
|
|
|
|
throw;
|
|
|
|
|
}
|