diff --git a/dbms/src/Storages/tests/merge_tree.cpp b/dbms/src/Storages/tests/merge_tree.cpp new file mode 100644 index 00000000000..41c1ae9b524 --- /dev/null +++ b/dbms/src/Storages/tests/merge_tree.cpp @@ -0,0 +1,122 @@ +#include + +#include + +#include +#include +#include + +#include +#include +#include + +#include +#include + +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; + String exprected; + 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, exprected)) + throw Poco::Exception("Cannot parse " + primary_expr_str); + + StoragePtr table = StorageMergeTree::create("./", "test", names_and_types, context, primary_expr, "d", NULL, 101); + + /// пишем в неё + { + Block block; + + ColumnWithNameAndType column1; + column1.name = "d"; + column1.type = table->getDataTypeByName("d"); + column1.column = column1.type->createColumn(); + ColumnUInt16::Container_t & vec1 = dynamic_cast(*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 out = table->write(NULL); + out->write(block); + } + + /// читаем из неё + { + Names column_names; + column_names.push_back("d"); + column_names.push_back("a"); + + QueryProcessingStage::Enum stage; + + ASTPtr select; + String exprected; + 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, exprected)) + throw Poco::Exception("Cannot parse " + primary_expr_str); + + SharedPtr 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; +}