Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dbms/programs/server/TCPHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@ bool TCPHandler::receiveData()
{
NamesAndTypesList columns = block.getNamesAndTypesList();
storage = StorageMemory::create(external_table_name,
ColumnsDescription{columns, NamesAndTypesList{}, NamesAndTypesList{}, ColumnDefaults{}});
ColumnsDescription{columns, NamesAndTypesList{}, NamesAndTypesList{}, ColumnDefaults{}, ColumnComments{}});
storage->startup();
query_context.addExternalTable(external_table_name, storage);
}
Expand Down
31 changes: 24 additions & 7 deletions dbms/src/Interpreters/InterpreterCreateQuery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <Poco/FileStream.h>

#include <Common/escapeForFileName.h>
#include <Common/typeid_cast.h>

#include <IO/WriteBufferFromFile.h>
#include <IO/WriteHelpers.h>
Expand Down Expand Up @@ -167,13 +168,15 @@ BlockIO InterpreterCreateQuery::createDatabase(ASTCreateQuery & create)


using ColumnsAndDefaults = std::pair<NamesAndTypesList, ColumnDefaults>;
using ParsedColumns = std::tuple<NamesAndTypesList, ColumnDefaults, ColumnComments>;

/// AST to the list of columns with types. Columns of Nested type are expanded into a list of real columns.
static ColumnsAndDefaults parseColumns(const ASTExpressionList & column_list_ast, const Context & context)
static ParsedColumns parseColumns(const ASTExpressionList & column_list_ast, const Context & context)
{
/// list of table columns in correct order
NamesAndTypesList columns{};
ColumnDefaults defaults{};
ColumnComments comments{};

/// Columns requiring type-deduction or default_expression type-check
std::vector<std::pair<NameAndTypePair *, ASTColumnDeclaration *>> defaulted_columns{};
Expand Down Expand Up @@ -217,6 +220,12 @@ static ColumnsAndDefaults parseColumns(const ASTExpressionList & column_list_ast
else
default_expr_list->children.emplace_back(setAlias(col_decl.default_expression->clone(), col_decl.name));
}

if (col_decl.comment)
{
auto comment_literal = typeid_cast<ASTLiteral &>(*col_decl.comment);
comments.emplace(col_decl.name, comment_literal.value.get<String>());
}
}

/// set missing types and wrap default_expression's in a conversion-function if necessary
Expand Down Expand Up @@ -266,7 +275,7 @@ static ColumnsAndDefaults parseColumns(const ASTExpressionList & column_list_ast
}
}

return {Nested::flatten(columns), defaults};
return {Nested::flatten(columns), defaults, comments};
}


Expand Down Expand Up @@ -334,11 +343,17 @@ ASTPtr InterpreterCreateQuery::formatColumns(const ColumnsDescription & columns)
column_declaration->type = parseQuery(storage_p, pos, end, "data type", 0);
column_declaration->type->owned_string = type_name;

const auto it = columns.defaults.find(column.name);
if (it != std::end(columns.defaults))
const auto defaults_it = columns.defaults.find(column.name);
if (defaults_it != std::end(columns.defaults))
{
column_declaration->default_specifier = toString(defaults_it->second.kind);
column_declaration->default_expression = defaults_it->second.expression->clone();
}

const auto comments_it = columns.comments.find(column.name);
if (comments_it != std::end(columns.comments))
{
column_declaration->default_specifier = toString(it->second.kind);
column_declaration->default_expression = it->second.expression->clone();
column_declaration->comment = std::make_shared<ASTLiteral>(Field(comments_it->second));
}

columns_list->children.push_back(column_declaration_ptr);
Expand All @@ -352,11 +367,13 @@ ColumnsDescription InterpreterCreateQuery::getColumnsDescription(const ASTExpres
{
ColumnsDescription res;

auto && columns_and_defaults = parseColumns(columns, context);
auto && parsed_columns = parseColumns(columns, context);
auto columns_and_defaults = std::make_pair(std::move(std::get<0>(parsed_columns)), std::move(std::get<1>(parsed_columns)));
res.materialized = removeAndReturnColumns(columns_and_defaults, ColumnDefaultKind::Materialized);
res.aliases = removeAndReturnColumns(columns_and_defaults, ColumnDefaultKind::Alias);
res.ordinary = std::move(columns_and_defaults.first);
res.defaults = std::move(columns_and_defaults.second);
res.comments = std::move(std::get<2>(parsed_columns));

if (res.ordinary.size() + res.materialized.size() == 0)
throw Exception{"Cannot CREATE table without physical columns", ErrorCodes::EMPTY_LIST_OF_COLUMNS_PASSED};
Expand Down
2 changes: 1 addition & 1 deletion dbms/src/Interpreters/InterpreterCreateQuery.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class InterpreterCreateQuery : public IInterpreter
internal = internal_;
}

/// Obtain information about columns, their types and default values, for case when columns in CREATE query is specified explicitly.
/// Obtain information about columns, their types, default values and column comments, for case when columns in CREATE query is specified explicitly.
static ColumnsDescription getColumnsDescription(const ASTExpressionList & columns, const Context & context);
/// Check that column types are allowed for usage in table according to settings.
static void checkSupportedTypes(const ColumnsDescription & columns, const Context & context);
Expand Down
23 changes: 19 additions & 4 deletions dbms/src/Interpreters/InterpreterDescribeQuery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ Block InterpreterDescribeQuery::getSampleBlock()
col.name = "default_expression";
block.insert(col);

col.name = "comment_expression";
block.insert(col);

return block;
}

Expand All @@ -55,6 +58,7 @@ BlockInputStreamPtr InterpreterDescribeQuery::executeImpl()

NamesAndTypesList columns;
ColumnDefaults column_defaults;
ColumnComments column_comments;
StoragePtr table;

auto table_expression = typeid_cast<const ASTTableExpression *>(ast.table_expression.get());
Expand Down Expand Up @@ -101,6 +105,7 @@ BlockInputStreamPtr InterpreterDescribeQuery::executeImpl()
auto table_lock = table->lockStructure(false, __PRETTY_FUNCTION__);
columns = table->getColumns().getAll();
column_defaults = table->getColumns().defaults;
column_comments = table->getColumns().comments;
}

Block sample_block = getSampleBlock();
Expand All @@ -111,16 +116,26 @@ BlockInputStreamPtr InterpreterDescribeQuery::executeImpl()
res_columns[0]->insert(column.name);
res_columns[1]->insert(column.type->getName());

const auto it = column_defaults.find(column.name);
if (it == std::end(column_defaults))
const auto defaults_it = column_defaults.find(column.name);
if (defaults_it == std::end(column_defaults))
{
res_columns[2]->insertDefault();
res_columns[3]->insertDefault();
}
else
{
res_columns[2]->insert(toString(it->second.kind));
res_columns[3]->insert(queryToString(it->second.expression));
res_columns[2]->insert(toString(defaults_it->second.kind));
res_columns[3]->insert(queryToString(defaults_it->second.expression));
}

const auto comments_it = column_comments.find(column.name);
if (comments_it == std::end(column_comments))
{
res_columns[4]->insertDefault();
}
else
{
res_columns[4]->insert(comments_it->second);
}
}

Expand Down
7 changes: 7 additions & 0 deletions dbms/src/Parsers/ASTAlterQuery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,13 @@ void ASTAlterCommand::formatImpl(
settings.ostr << (settings.hilite ? hilite_keyword : "") << " WHERE " << (settings.hilite ? hilite_none : "");
predicate->formatImpl(settings, state, frame);
}
else if (type == ASTAlterCommand::COMMENT_COLUMN)
{
settings.ostr << (settings.hilite ? hilite_keyword : "") << indent_str << "COMMENT COLUMN " << (settings.hilite ? hilite_none : "");
column->formatImpl(settings, state, frame);
settings.ostr << " " << (settings.hilite ? hilite_none : "");
comment->formatImpl(settings, state, frame);
}
else
throw Exception("Unexpected type of ALTER", ErrorCodes::UNEXPECTED_AST_STRUCTURE);
}
Expand Down
5 changes: 5 additions & 0 deletions dbms/src/Parsers/ASTAlterQuery.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace DB
* DROP COLUMN col_drop [FROM PARTITION partition],
* MODIFY COLUMN col_name type,
* DROP PARTITION partition,
* COMMENT_COLUMN col_name 'comment',
*/

class ASTAlterCommand : public IAST
Expand All @@ -25,6 +26,7 @@ class ASTAlterCommand : public IAST
DROP_COLUMN,
MODIFY_COLUMN,
MODIFY_PRIMARY_KEY,
COMMENT_COLUMN,

DROP_PARTITION,
ATTACH_PARTITION,
Expand Down Expand Up @@ -66,6 +68,9 @@ class ASTAlterCommand : public IAST
/// A list of expressions of the form `column = expr` for the UPDATE command.
ASTPtr update_assignments;

/// A column comment
ASTPtr comment;

bool detach = false; /// true for DETACH PARTITION

bool part = false; /// true for ATTACH PART
Expand Down
16 changes: 14 additions & 2 deletions dbms/src/Parsers/ASTColumnDeclaration.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace DB
{

/** Name, type, default-specifier, default-expression.
/** Name, type, default-specifier, default-expression, comment-expression.
* The type is optional if default-expression is specified.
*/
class ASTColumnDeclaration : public IAST
Expand All @@ -15,6 +15,7 @@ class ASTColumnDeclaration : public IAST
ASTPtr type;
String default_specifier;
ASTPtr default_expression;
ASTPtr comment;

String getID() const override { return "ColumnDeclaration_" + name; }

Expand All @@ -35,10 +36,15 @@ class ASTColumnDeclaration : public IAST
res->children.push_back(res->default_expression);
}

if (comment)
{
res->comment = comment->clone();
res->children.push_back(res->comment);
}

return res;
}

protected:
void formatImpl(const FormatSettings & settings, FormatState & state, FormatStateStacked frame) const override
{
frame.need_parens = false;
Expand All @@ -56,6 +62,12 @@ class ASTColumnDeclaration : public IAST
settings.ostr << ' ' << (settings.hilite ? hilite_keyword : "") << default_specifier << (settings.hilite ? hilite_none : "") << ' ';
default_expression->formatImpl(settings, state, frame);
}

if (comment)
{
settings.ostr << ' ' << (settings.hilite ? hilite_keyword : "") << "COMMENT" << (settings.hilite ? hilite_none : "") << ' ';
comment->formatImpl(settings, state, frame);
}
}
};

Expand Down
8 changes: 8 additions & 0 deletions dbms/src/Parsers/IParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ class IParser
return true;
}

/* The same, but never move the position and do not write the result to node.
*/
bool check_without_moving(Pos pos, Expected & expected)
{
ASTPtr node;
return parse(pos, node, expected);
}

virtual ~IParser() {}
};

Expand Down
14 changes: 13 additions & 1 deletion dbms/src/Parsers/ParserAlterQuery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ bool ParserAlterCommand::parseImpl(Pos & pos, ASTPtr & node, Expected & expected
ParserKeyword s_clear_column("CLEAR COLUMN");
ParserKeyword s_modify_column("MODIFY COLUMN");
ParserKeyword s_modify_primary_key("MODIFY PRIMARY KEY");
ParserKeyword s_comment_column("COMMENT COLUMN");

ParserKeyword s_attach_partition("ATTACH PARTITION");
ParserKeyword s_detach_partition("DETACH PARTITION");
Expand All @@ -46,6 +47,7 @@ bool ParserAlterCommand::parseImpl(Pos & pos, ASTPtr & node, Expected & expected
ParserCompoundIdentifier parser_name;
ParserStringLiteral parser_string_literal;
ParserCompoundColumnDeclaration parser_col_decl;
ParserCompoundColumnDeclaration parser_modify_col_decl(false);
ParserPartition parser_partition;
ParserExpression parser_exp_elem;
ParserList parser_assignment_list(
Expand Down Expand Up @@ -180,7 +182,7 @@ bool ParserAlterCommand::parseImpl(Pos & pos, ASTPtr & node, Expected & expected
}
else if (s_modify_column.ignore(pos, expected))
{
if (!parser_col_decl.parse(pos, command->col_decl, expected))
if (!parser_modify_col_decl.parse(pos, command->col_decl, expected))
return false;

command->type = ASTAlterCommand::MODIFY_COLUMN;
Expand Down Expand Up @@ -220,6 +222,16 @@ bool ParserAlterCommand::parseImpl(Pos & pos, ASTPtr & node, Expected & expected

command->type = ASTAlterCommand::UPDATE;
}
else if (s_comment_column.ignore(pos, expected))
{
if (!parser_name.parse(pos, command->column, expected))
return false;

if (!parser_string_literal.parse(pos, command->comment, expected))
return false;

command->type = ASTAlterCommand::COMMENT_COLUMN;
}
else
return false;

Expand Down
1 change: 1 addition & 0 deletions dbms/src/Parsers/ParserAlterQuery.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace DB
* [CLEAR COLUMN col_to_clear [IN PARTITION partition],]
* [MODIFY COLUMN col_to_modify type, ...]
* [MODIFY PRIMARY KEY (a, b, c...)]
* [COMMENT COLUMN col_name string]
* [DROP|DETACH|ATTACH PARTITION|PART partition, ...]
* [FETCH PARTITION partition FROM ...]
* [FREEZE PARTITION]
Expand Down
Loading