From f91c64bd286bcc96f6340da29f4c49b6edc83307 Mon Sep 17 00:00:00 2001 From: Dmitry Rubashkin Date: Wed, 31 Jul 2019 17:06:22 +0300 Subject: [PATCH] parser changed, test modified, comments added --- dbms/src/Parsers/ParserCreateQuery.h | 2 +- dbms/src/Storages/StorageValues.cpp | 4 ++- dbms/src/Storages/StorageValues.h | 4 ++- .../TableFunctions/TableFunctionValues.cpp | 35 ++++++++++++------- .../0_stateless/00975_values_list.reference | 1 + .../queries/0_stateless/00975_values_list.sql | 2 ++ 6 files changed, 32 insertions(+), 16 deletions(-) diff --git a/dbms/src/Parsers/ParserCreateQuery.h b/dbms/src/Parsers/ParserCreateQuery.h index bd3c8f671f0..98109ae9893 100644 --- a/dbms/src/Parsers/ParserCreateQuery.h +++ b/dbms/src/Parsers/ParserCreateQuery.h @@ -263,7 +263,7 @@ protected: class ParserColumnsOrIndicesDeclarationList : public IParserBase { - protected: +protected: const char * getName() const override { return "columns or indices declaration list"; } bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected) override; }; diff --git a/dbms/src/Storages/StorageValues.cpp b/dbms/src/Storages/StorageValues.cpp index bda44569cc6..c8b470307dc 100644 --- a/dbms/src/Storages/StorageValues.cpp +++ b/dbms/src/Storages/StorageValues.cpp @@ -5,7 +5,9 @@ namespace DB { - +/* One block storage used for values table function + * It's structure is similar to IStorageSystemOneBlock + */ StorageValues::StorageValues(const std::string & database_name_, const std::string & table_name_, const Block & res_block_) : database_name(database_name_), table_name(table_name_), res_block(res_block_) { diff --git a/dbms/src/Storages/StorageValues.h b/dbms/src/Storages/StorageValues.h index 640a198d97b..8ff52ed9df5 100644 --- a/dbms/src/Storages/StorageValues.h +++ b/dbms/src/Storages/StorageValues.h @@ -6,7 +6,9 @@ namespace DB { - +/* values(structure, values...) - creates a temporary storage filling columns with values + * values is case-insensitive table function + */ class StorageValues : public ext::shared_ptr_helper, public IStorage { public: diff --git a/dbms/src/TableFunctions/TableFunctionValues.cpp b/dbms/src/TableFunctions/TableFunctionValues.cpp index bf2e8679962..ebfd9fa5cba 100644 --- a/dbms/src/TableFunctions/TableFunctionValues.cpp +++ b/dbms/src/TableFunctions/TableFunctionValues.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include @@ -15,8 +16,7 @@ #include #include - -#include +#include namespace DB @@ -25,6 +25,7 @@ namespace DB namespace ErrorCodes { extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH; + extern const int SYNTAX_ERROR; } static void parseAndInsertValues(MutableColumns & res_columns, const ASTs & args, const Block & sample_block, const Context & context) @@ -74,21 +75,30 @@ StoragePtr TableFunctionValues::executeImpl(const ASTPtr & ast_function, const C /// Parsing first argument as table structure std::string structure = args[0]->as().value.safeGet(); - std::vector structure_values; - boost::split(structure_values, structure, boost::algorithm::is_any_of(" ,"), boost::algorithm::token_compress_on); + Expected expected; - if (structure_values.size() % 2 != 0) - throw Exception("Odd number of elements in section structure: must be a list of name type pairs", ErrorCodes::LOGICAL_ERROR); + Tokens tokens(structure.c_str(), structure.c_str() + structure.size()); + TokenIterator token_iterator(tokens); + + ParserColumnDeclarationList parser; + ASTPtr columns_list_raw; + + if (!parser.parse(token_iterator, columns_list_raw, expected)) + throw Exception("Cannot parse columns declaration list.", ErrorCodes::SYNTAX_ERROR); + + auto * columns_list = dynamic_cast(columns_list_raw.get()); + if (!columns_list) + throw Exception("Could not cast AST to ASTExpressionList", ErrorCodes::LOGICAL_ERROR); + + ColumnsDescription columns_desc = InterpreterCreateQuery::getColumnsDescription(*columns_list, context); Block sample_block; - const DataTypeFactory & data_type_factory = DataTypeFactory::instance(); - - for (size_t i = 0, size = structure_values.size(); i < size; i += 2) + for (const auto & [name, type]: columns_desc.getAllPhysical()) { ColumnWithTypeAndName column; - column.name = structure_values[i]; - column.type = data_type_factory.get(structure_values[i + 1]); - column.column = column.type->createColumn(); + column.name = name; + column.type = type; + column.column = type->createColumn(); sample_block.insert(std::move(column)); } @@ -101,7 +111,6 @@ StoragePtr TableFunctionValues::executeImpl(const ASTPtr & ast_function, const C auto res = StorageValues::create(getDatabaseName(), table_name, res_block); res->startup(); return res; - } void registerTableFunctionValues(TableFunctionFactory & factory) diff --git a/dbms/tests/queries/0_stateless/00975_values_list.reference b/dbms/tests/queries/0_stateless/00975_values_list.reference index 0f719e42424..eee9e0a0ca5 100644 --- a/dbms/tests/queries/0_stateless/00975_values_list.reference +++ b/dbms/tests/queries/0_stateless/00975_values_list.reference @@ -10,3 +10,4 @@ cadabra abracadabra 23 23 23 24 24 24 +1.6660 a b diff --git a/dbms/tests/queries/0_stateless/00975_values_list.sql b/dbms/tests/queries/0_stateless/00975_values_list.sql index a71dd50860d..ad30cec21e9 100644 --- a/dbms/tests/queries/0_stateless/00975_values_list.sql +++ b/dbms/tests/queries/0_stateless/00975_values_list.sql @@ -9,4 +9,6 @@ SELECT subtractYears(date, 1), subtractYears(date_time, 1) FROM VALUES('date Dat SELECT * FROM VALUES('s String', ('abra'), ('cadabra'), ('abracadabra')); SELECT * FROM VALUES('n UInt64, s String, ss String', (1 + 22, '23', toString(23)), (toUInt64('24'), '24', concat('2', '4'))); + +SELECT * FROM VALUES('a Decimal(4, 4), b String, c String', (divide(toDecimal32(5, 3), 3), 'a', 'b')); DROP TABLE values_list;