2018-03-06 20:18:34 +00:00
|
|
|
#include <Storages/ColumnsDescription.h>
|
2020-03-24 17:05:38 +00:00
|
|
|
|
|
|
|
#include <boost/algorithm/string/replace.hpp>
|
2018-11-06 13:26:43 +00:00
|
|
|
#include <Parsers/ASTLiteral.h>
|
2018-11-16 12:01:52 +00:00
|
|
|
#include <Parsers/ExpressionElementParsers.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Parsers/ExpressionListParsers.h>
|
2019-02-27 16:41:51 +00:00
|
|
|
#include <Parsers/ParserCreateQuery.h>
|
2017-07-12 02:40:28 +00:00
|
|
|
#include <Parsers/parseQuery.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Parsers/queryToString.h>
|
2020-03-03 09:05:17 +00:00
|
|
|
#include <Parsers/ASTSubquery.h>
|
|
|
|
#include <Parsers/ASTSelectQuery.h>
|
2020-03-03 12:51:41 +00:00
|
|
|
#include <Parsers/ASTSelectWithUnionQuery.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/WriteBuffer.h>
|
|
|
|
#include <IO/WriteHelpers.h>
|
|
|
|
#include <IO/ReadBuffer.h>
|
|
|
|
#include <IO/ReadHelpers.h>
|
|
|
|
#include <IO/WriteBufferFromString.h>
|
|
|
|
#include <IO/ReadBufferFromString.h>
|
|
|
|
#include <DataTypes/DataTypeFactory.h>
|
2019-03-14 15:20:51 +00:00
|
|
|
#include <DataTypes/NestedUtils.h>
|
|
|
|
#include <DataTypes/DataTypeArray.h>
|
|
|
|
#include <DataTypes/DataTypeTuple.h>
|
2018-03-06 20:18:34 +00:00
|
|
|
#include <Common/Exception.h>
|
2018-12-04 20:03:04 +00:00
|
|
|
#include <Interpreters/Context.h>
|
|
|
|
#include <Storages/IStorage.h>
|
2018-11-06 13:26:43 +00:00
|
|
|
#include <Common/typeid_cast.h>
|
2018-10-11 02:57:48 +00:00
|
|
|
#include <Compression/CompressionFactory.h>
|
2020-03-03 09:05:17 +00:00
|
|
|
#include <Interpreters/ExpressionAnalyzer.h>
|
|
|
|
#include <Interpreters/SyntaxAnalyzer.h>
|
|
|
|
#include <Interpreters/ExpressionActions.h>
|
2015-04-11 03:10:23 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2016-01-11 21:46:36 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2018-03-06 20:18:34 +00:00
|
|
|
extern const int NO_SUCH_COLUMN_IN_TABLE;
|
2019-03-14 15:20:51 +00:00
|
|
|
extern const int ILLEGAL_COLUMN;
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const int CANNOT_PARSE_TEXT;
|
2020-03-03 09:05:17 +00:00
|
|
|
extern const int THERE_IS_NO_DEFAULT_VALUE;
|
2020-03-31 16:18:18 +00:00
|
|
|
extern const int LOGICAL_ERROR;
|
2016-01-11 21:46:36 +00:00
|
|
|
}
|
|
|
|
|
2019-05-21 11:24:32 +00:00
|
|
|
ColumnDescription::ColumnDescription(String name_, DataTypePtr type_, bool is_virtual_)
|
|
|
|
: name(std::move(name_)), type(std::move(type_)), is_virtual(is_virtual_)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-03-14 15:20:51 +00:00
|
|
|
bool ColumnDescription::operator==(const ColumnDescription & other) const
|
|
|
|
{
|
2019-03-15 17:40:16 +00:00
|
|
|
auto codec_str = [](const CompressionCodecPtr & codec_ptr) { return codec_ptr ? codec_ptr->getCodecDesc() : String(); };
|
2019-04-15 09:30:45 +00:00
|
|
|
auto ttl_str = [](const ASTPtr & ttl_ast) { return ttl_ast ? queryToString(ttl_ast) : String{}; };
|
2015-04-11 03:10:23 +00:00
|
|
|
|
2019-03-14 15:20:51 +00:00
|
|
|
return name == other.name
|
|
|
|
&& type->equals(*other.type)
|
|
|
|
&& default_desc == other.default_desc
|
|
|
|
&& comment == other.comment
|
2019-04-15 09:30:45 +00:00
|
|
|
&& codec_str(codec) == codec_str(other.codec)
|
|
|
|
&& ttl_str(ttl) == ttl_str(other.ttl);
|
2019-03-14 15:20:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ColumnDescription::writeText(WriteBuffer & buf) const
|
2018-03-06 20:18:34 +00:00
|
|
|
{
|
2019-03-14 15:20:51 +00:00
|
|
|
writeBackQuotedString(name, buf);
|
|
|
|
writeChar(' ', buf);
|
|
|
|
DB::writeText(type->getName(), buf);
|
|
|
|
|
|
|
|
if (default_desc.expression)
|
|
|
|
{
|
|
|
|
writeChar('\t', buf);
|
|
|
|
DB::writeText(DB::toString(default_desc.kind), buf);
|
|
|
|
writeChar('\t', buf);
|
|
|
|
DB::writeText(queryToString(default_desc.expression), buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!comment.empty())
|
|
|
|
{
|
|
|
|
writeChar('\t', buf);
|
|
|
|
DB::writeText("COMMENT ", buf);
|
|
|
|
DB::writeText(queryToString(ASTLiteral(Field(comment))), buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (codec)
|
|
|
|
{
|
|
|
|
writeChar('\t', buf);
|
|
|
|
DB::writeText("CODEC(", buf);
|
|
|
|
DB::writeText(codec->getCodecDesc(), buf);
|
|
|
|
DB::writeText(")", buf);
|
|
|
|
}
|
|
|
|
|
2019-04-15 09:30:45 +00:00
|
|
|
if (ttl)
|
|
|
|
{
|
|
|
|
writeChar('\t', buf);
|
|
|
|
DB::writeText("TTL ", buf);
|
|
|
|
DB::writeText(queryToString(ttl), buf);
|
|
|
|
}
|
|
|
|
|
2019-03-14 15:20:51 +00:00
|
|
|
writeChar('\n', buf);
|
2018-03-06 20:18:34 +00:00
|
|
|
}
|
|
|
|
|
2019-03-14 15:20:51 +00:00
|
|
|
void ColumnDescription::readText(ReadBuffer & buf)
|
|
|
|
{
|
2019-08-08 20:02:30 +00:00
|
|
|
ParserColumnDeclaration column_parser(/* require type */ true);
|
2019-03-14 15:20:51 +00:00
|
|
|
String column_line;
|
|
|
|
readEscapedStringUntilEOL(column_line, buf);
|
|
|
|
ASTPtr ast = parseQuery(column_parser, column_line, "column parser", 0);
|
2019-03-16 21:46:53 +00:00
|
|
|
if (const auto * col_ast = ast->as<ASTColumnDeclaration>())
|
2019-03-14 15:20:51 +00:00
|
|
|
{
|
|
|
|
name = col_ast->name;
|
|
|
|
type = DataTypeFactory::instance().get(col_ast->type);
|
2018-03-06 20:18:34 +00:00
|
|
|
|
2019-03-14 15:20:51 +00:00
|
|
|
if (col_ast->default_expression)
|
|
|
|
{
|
|
|
|
default_desc.kind = columnDefaultKindFromString(col_ast->default_specifier);
|
|
|
|
default_desc.expression = std::move(col_ast->default_expression);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (col_ast->comment)
|
2019-03-16 21:46:53 +00:00
|
|
|
comment = col_ast->comment->as<ASTLiteral &>().value.get<String>();
|
2019-03-14 15:20:51 +00:00
|
|
|
|
|
|
|
if (col_ast->codec)
|
|
|
|
codec = CompressionCodecFactory::instance().get(col_ast->codec, type);
|
2019-04-15 09:30:45 +00:00
|
|
|
|
|
|
|
if (col_ast->ttl)
|
|
|
|
ttl = col_ast->ttl;
|
2019-03-14 15:20:51 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
throw Exception("Cannot parse column description", ErrorCodes::CANNOT_PARSE_TEXT);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-21 11:24:32 +00:00
|
|
|
ColumnsDescription::ColumnsDescription(NamesAndTypesList ordinary, bool all_virtuals)
|
2018-03-10 17:03:57 +00:00
|
|
|
{
|
2019-03-14 15:20:51 +00:00
|
|
|
for (auto & elem : ordinary)
|
2019-05-21 11:24:32 +00:00
|
|
|
add(ColumnDescription(std::move(elem.name), std::move(elem.type), all_virtuals));
|
2018-03-10 17:03:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-14 15:20:51 +00:00
|
|
|
/// We are trying to find first column from end with name `column_name` or with a name beginning with `column_name` and ".".
|
|
|
|
/// For example "fruits.bananas"
|
|
|
|
/// names are considered the same if they completely match or `name_without_dot` matches the part of the name to the point
|
2019-05-01 21:43:05 +00:00
|
|
|
static auto getNameRange(const ColumnsDescription::Container & columns, const String & name_without_dot)
|
2018-03-06 20:18:34 +00:00
|
|
|
{
|
2019-03-14 15:20:51 +00:00
|
|
|
String name_with_dot = name_without_dot + ".";
|
|
|
|
|
|
|
|
auto begin = columns.begin();
|
|
|
|
for (; begin != columns.end(); ++begin)
|
|
|
|
{
|
|
|
|
if (begin->name == name_without_dot)
|
|
|
|
return std::make_pair(begin, std::next(begin));
|
|
|
|
|
|
|
|
if (startsWith(begin->name, name_with_dot))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (begin == columns.end())
|
|
|
|
return std::make_pair(begin, begin);
|
|
|
|
|
|
|
|
auto end = std::next(begin);
|
|
|
|
for (; end != columns.end(); ++end)
|
|
|
|
{
|
|
|
|
if (!startsWith(end->name, name_with_dot))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::make_pair(begin, end);
|
2018-03-06 20:18:34 +00:00
|
|
|
}
|
|
|
|
|
2019-03-14 15:20:51 +00:00
|
|
|
void ColumnsDescription::add(ColumnDescription column, const String & after_column)
|
|
|
|
{
|
|
|
|
if (has(column.name))
|
|
|
|
throw Exception("Cannot add column " + column.name + ": column with this name already exists",
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
|
|
|
|
auto insert_it = columns.cend();
|
2018-03-06 20:18:34 +00:00
|
|
|
|
2019-03-14 15:20:51 +00:00
|
|
|
if (!after_column.empty())
|
|
|
|
{
|
|
|
|
auto range = getNameRange(columns, after_column);
|
|
|
|
if (range.first == range.second)
|
|
|
|
throw Exception("Wrong column name. Cannot find column " + after_column + " to insert after",
|
|
|
|
ErrorCodes::NO_SUCH_COLUMN_IN_TABLE);
|
|
|
|
|
|
|
|
insert_it = range.second;
|
|
|
|
}
|
|
|
|
|
2019-05-01 21:43:05 +00:00
|
|
|
columns.get<0>().insert(insert_it, std::move(column));
|
2019-03-14 15:20:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ColumnsDescription::remove(const String & column_name)
|
2018-03-06 20:18:34 +00:00
|
|
|
{
|
2019-03-14 15:20:51 +00:00
|
|
|
auto range = getNameRange(columns, column_name);
|
|
|
|
if (range.first == range.second)
|
|
|
|
throw Exception("There is no column " + column_name + " in table.",
|
|
|
|
ErrorCodes::NO_SUCH_COLUMN_IN_TABLE);
|
|
|
|
|
|
|
|
for (auto list_it = range.first; list_it != range.second;)
|
2019-05-01 21:43:05 +00:00
|
|
|
list_it = columns.get<0>().erase(list_it);
|
2018-03-06 20:18:34 +00:00
|
|
|
}
|
|
|
|
|
2020-03-24 17:05:38 +00:00
|
|
|
void ColumnsDescription::rename(const String & column_from, const String & column_to)
|
|
|
|
{
|
2020-03-25 18:44:08 +00:00
|
|
|
auto it = columns.get<1>().find(column_from);
|
|
|
|
if (it == columns.get<1>().end())
|
|
|
|
throw Exception("Cannot find column " + column_from + " in ColumnsDescription", ErrorCodes::LOGICAL_ERROR);
|
2020-03-24 17:05:38 +00:00
|
|
|
|
2020-03-25 18:44:08 +00:00
|
|
|
columns.get<1>().modify_key(it, [&column_to] (String & old_name)
|
2020-03-24 17:05:38 +00:00
|
|
|
{
|
2020-03-25 18:44:08 +00:00
|
|
|
old_name = column_to;
|
|
|
|
});
|
2020-03-24 17:05:38 +00:00
|
|
|
}
|
|
|
|
|
2018-03-06 20:18:34 +00:00
|
|
|
|
2019-03-14 15:20:51 +00:00
|
|
|
void ColumnsDescription::flattenNested()
|
2018-03-06 20:18:34 +00:00
|
|
|
{
|
2019-03-16 15:07:08 +00:00
|
|
|
for (auto it = columns.begin(); it != columns.end();)
|
2019-03-14 15:20:51 +00:00
|
|
|
{
|
2019-03-16 15:07:08 +00:00
|
|
|
const auto * type_arr = typeid_cast<const DataTypeArray *>(it->type.get());
|
|
|
|
if (!type_arr)
|
|
|
|
{
|
|
|
|
++it;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto * type_tuple = typeid_cast<const DataTypeTuple *>(type_arr->getNestedType().get());
|
|
|
|
if (!type_tuple)
|
|
|
|
{
|
|
|
|
++it;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-01-27 20:26:39 +00:00
|
|
|
if (!type_tuple->haveExplicitNames())
|
|
|
|
{
|
|
|
|
++it;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-03-16 15:07:08 +00:00
|
|
|
ColumnDescription column = std::move(*it);
|
2019-05-01 21:43:05 +00:00
|
|
|
it = columns.get<0>().erase(it);
|
2019-03-16 15:07:08 +00:00
|
|
|
|
|
|
|
const DataTypes & elements = type_tuple->getElements();
|
|
|
|
const Strings & names = type_tuple->getElementNames();
|
|
|
|
size_t tuple_size = elements.size();
|
|
|
|
|
|
|
|
for (size_t i = 0; i < tuple_size; ++i)
|
2019-03-14 15:20:51 +00:00
|
|
|
{
|
2019-03-16 15:07:08 +00:00
|
|
|
auto nested_column = column;
|
|
|
|
/// TODO: what to do with default expressions?
|
|
|
|
nested_column.name = Nested::concatenateName(column.name, names[i]);
|
|
|
|
nested_column.type = std::make_shared<DataTypeArray>(elements[i]);
|
|
|
|
|
2019-05-01 21:43:05 +00:00
|
|
|
columns.get<0>().insert(it, std::move(nested_column));
|
2019-03-14 15:20:51 +00:00
|
|
|
}
|
|
|
|
}
|
2018-03-06 20:18:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-14 15:20:51 +00:00
|
|
|
NamesAndTypesList ColumnsDescription::getOrdinary() const
|
2019-02-27 16:41:51 +00:00
|
|
|
{
|
2019-03-14 15:20:51 +00:00
|
|
|
NamesAndTypesList ret;
|
|
|
|
for (const auto & col : columns)
|
2019-05-21 11:24:32 +00:00
|
|
|
if (col.default_desc.kind == ColumnDefaultKind::Default && !col.is_virtual)
|
2019-03-14 15:20:51 +00:00
|
|
|
ret.emplace_back(col.name, col.type);
|
|
|
|
return ret;
|
|
|
|
}
|
2019-02-27 16:41:51 +00:00
|
|
|
|
2019-03-14 15:20:51 +00:00
|
|
|
NamesAndTypesList ColumnsDescription::getMaterialized() const
|
|
|
|
{
|
|
|
|
NamesAndTypesList ret;
|
|
|
|
for (const auto & col : columns)
|
|
|
|
if (col.default_desc.kind == ColumnDefaultKind::Materialized)
|
|
|
|
ret.emplace_back(col.name, col.type);
|
|
|
|
return ret;
|
|
|
|
}
|
2019-02-27 16:41:51 +00:00
|
|
|
|
2019-05-23 11:15:18 +00:00
|
|
|
NamesAndTypesList ColumnsDescription::getAliases() const
|
2019-03-14 15:20:51 +00:00
|
|
|
{
|
|
|
|
NamesAndTypesList ret;
|
|
|
|
for (const auto & col : columns)
|
2019-05-23 11:15:18 +00:00
|
|
|
if (col.default_desc.kind == ColumnDefaultKind::Alias)
|
2019-03-14 15:20:51 +00:00
|
|
|
ret.emplace_back(col.name, col.type);
|
|
|
|
return ret;
|
2019-02-27 16:41:51 +00:00
|
|
|
}
|
|
|
|
|
2019-05-23 11:15:18 +00:00
|
|
|
NamesAndTypesList ColumnsDescription::getVirtuals() const
|
|
|
|
{
|
|
|
|
NamesAndTypesList result;
|
|
|
|
for (const auto & column : columns)
|
|
|
|
if (column.is_virtual)
|
|
|
|
result.emplace_back(column.name, column.type);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-03-14 15:20:51 +00:00
|
|
|
NamesAndTypesList ColumnsDescription::getAll() const
|
2015-04-11 03:10:23 +00:00
|
|
|
{
|
2019-03-14 15:20:51 +00:00
|
|
|
NamesAndTypesList ret;
|
|
|
|
for (const auto & col : columns)
|
|
|
|
ret.emplace_back(col.name, col.type);
|
|
|
|
return ret;
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
|
2019-03-14 15:20:51 +00:00
|
|
|
bool ColumnsDescription::has(const String & column_name) const
|
|
|
|
{
|
2019-05-01 21:43:05 +00:00
|
|
|
return columns.get<1>().find(column_name) != columns.get<1>().end();
|
2019-03-14 15:20:51 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2019-03-14 15:20:51 +00:00
|
|
|
bool ColumnsDescription::hasNested(const String & column_name) const
|
|
|
|
{
|
|
|
|
auto range = getNameRange(columns, column_name);
|
|
|
|
return range.first != range.second && range.first->name.length() > column_name.length();
|
|
|
|
}
|
2018-10-14 15:30:06 +00:00
|
|
|
|
2019-03-14 15:20:51 +00:00
|
|
|
const ColumnDescription & ColumnsDescription::get(const String & column_name) const
|
|
|
|
{
|
2019-05-01 21:43:05 +00:00
|
|
|
auto it = columns.get<1>().find(column_name);
|
|
|
|
if (it == columns.get<1>().end())
|
2019-03-14 15:20:51 +00:00
|
|
|
throw Exception("There is no column " + column_name + " in table.",
|
|
|
|
ErrorCodes::NO_SUCH_COLUMN_IN_TABLE);
|
2018-12-25 10:04:38 +00:00
|
|
|
|
2019-05-01 21:43:05 +00:00
|
|
|
return *it;
|
2019-03-14 15:20:51 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2019-03-14 15:20:51 +00:00
|
|
|
|
|
|
|
NamesAndTypesList ColumnsDescription::getAllPhysical() const
|
|
|
|
{
|
|
|
|
NamesAndTypesList ret;
|
|
|
|
for (const auto & col : columns)
|
2019-05-21 11:24:32 +00:00
|
|
|
if (col.default_desc.kind != ColumnDefaultKind::Alias && !col.is_virtual)
|
2019-03-14 15:20:51 +00:00
|
|
|
ret.emplace_back(col.name, col.type);
|
|
|
|
return ret;
|
2015-04-11 03:10:23 +00:00
|
|
|
}
|
|
|
|
|
2019-03-14 15:20:51 +00:00
|
|
|
Names ColumnsDescription::getNamesOfPhysical() const
|
2018-11-16 12:01:52 +00:00
|
|
|
{
|
2019-03-14 15:20:51 +00:00
|
|
|
Names ret;
|
|
|
|
for (const auto & col : columns)
|
2019-05-21 11:24:32 +00:00
|
|
|
if (col.default_desc.kind != ColumnDefaultKind::Alias && !col.is_virtual)
|
2019-03-14 15:20:51 +00:00
|
|
|
ret.emplace_back(col.name);
|
|
|
|
return ret;
|
|
|
|
}
|
2018-11-16 12:01:52 +00:00
|
|
|
|
2019-03-14 15:20:51 +00:00
|
|
|
NameAndTypePair ColumnsDescription::getPhysical(const String & column_name) const
|
|
|
|
{
|
2019-05-01 21:43:05 +00:00
|
|
|
auto it = columns.get<1>().find(column_name);
|
2019-05-21 11:24:32 +00:00
|
|
|
if (it == columns.get<1>().end() || it->default_desc.kind == ColumnDefaultKind::Alias || it->is_virtual)
|
2019-03-14 15:20:51 +00:00
|
|
|
throw Exception("There is no physical column " + column_name + " in table.", ErrorCodes::NO_SUCH_COLUMN_IN_TABLE);
|
2019-05-01 21:43:05 +00:00
|
|
|
return NameAndTypePair(it->name, it->type);
|
2019-03-14 15:20:51 +00:00
|
|
|
}
|
2018-11-16 12:01:52 +00:00
|
|
|
|
2019-03-14 15:20:51 +00:00
|
|
|
bool ColumnsDescription::hasPhysical(const String & column_name) const
|
|
|
|
{
|
2019-05-01 21:43:05 +00:00
|
|
|
auto it = columns.get<1>().find(column_name);
|
2019-05-21 11:24:32 +00:00
|
|
|
return it != columns.get<1>().end() && it->default_desc.kind != ColumnDefaultKind::Alias && !it->is_virtual;
|
2019-03-14 15:20:51 +00:00
|
|
|
}
|
2018-11-16 12:01:52 +00:00
|
|
|
|
2018-12-25 11:33:54 +00:00
|
|
|
|
2019-03-14 15:20:51 +00:00
|
|
|
ColumnDefaults ColumnsDescription::getDefaults() const
|
|
|
|
{
|
|
|
|
ColumnDefaults ret;
|
|
|
|
for (const auto & column : columns)
|
|
|
|
if (column.default_desc.expression)
|
|
|
|
ret.emplace(column.name, column.default_desc);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ColumnsDescription::hasDefault(const String & column_name) const
|
|
|
|
{
|
2019-05-01 21:43:05 +00:00
|
|
|
auto it = columns.get<1>().find(column_name);
|
|
|
|
return it != columns.get<1>().end() && it->default_desc.expression;
|
2018-11-16 12:01:52 +00:00
|
|
|
}
|
2015-04-11 03:10:23 +00:00
|
|
|
|
2019-03-14 15:20:51 +00:00
|
|
|
std::optional<ColumnDefault> ColumnsDescription::getDefault(const String & column_name) const
|
|
|
|
{
|
2019-05-01 21:43:05 +00:00
|
|
|
auto it = columns.get<1>().find(column_name);
|
|
|
|
if (it != columns.get<1>().end() && it->default_desc.expression)
|
|
|
|
return it->default_desc;
|
2019-03-14 15:20:51 +00:00
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-12-21 12:17:30 +00:00
|
|
|
CompressionCodecPtr ColumnsDescription::getCodecOrDefault(const String & column_name, CompressionCodecPtr default_codec) const
|
2018-10-11 02:57:48 +00:00
|
|
|
{
|
2019-05-01 21:43:05 +00:00
|
|
|
const auto it = columns.get<1>().find(column_name);
|
2018-10-11 02:57:48 +00:00
|
|
|
|
2019-05-01 21:43:05 +00:00
|
|
|
if (it == columns.get<1>().end() || !it->codec)
|
2018-12-21 12:17:30 +00:00
|
|
|
return default_codec;
|
2018-10-11 02:57:48 +00:00
|
|
|
|
2019-05-01 21:43:05 +00:00
|
|
|
return it->codec;
|
2018-10-11 02:57:48 +00:00
|
|
|
}
|
|
|
|
|
2019-01-21 14:00:06 +00:00
|
|
|
CompressionCodecPtr ColumnsDescription::getCodecOrDefault(const String & column_name) const
|
|
|
|
{
|
|
|
|
return getCodecOrDefault(column_name, CompressionCodecFactory::instance().getDefaultCodec());
|
|
|
|
}
|
|
|
|
|
2019-04-15 09:30:45 +00:00
|
|
|
ColumnsDescription::ColumnTTLs ColumnsDescription::getColumnTTLs() const
|
|
|
|
{
|
|
|
|
ColumnTTLs ret;
|
|
|
|
for (const auto & column : columns)
|
|
|
|
if (column.ttl)
|
|
|
|
ret.emplace(column.name, column.ttl);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-03-14 15:20:51 +00:00
|
|
|
|
|
|
|
String ColumnsDescription::toString() const
|
|
|
|
{
|
|
|
|
WriteBufferFromOwnString buf;
|
|
|
|
|
|
|
|
writeCString("columns format version: 1\n", buf);
|
|
|
|
DB::writeText(columns.size(), buf);
|
|
|
|
writeCString(" columns:\n", buf);
|
|
|
|
|
|
|
|
for (const ColumnDescription & column : columns)
|
|
|
|
column.writeText(buf);
|
|
|
|
|
|
|
|
return buf.str();
|
|
|
|
}
|
|
|
|
|
2018-03-06 20:18:34 +00:00
|
|
|
ColumnsDescription ColumnsDescription::parse(const String & str)
|
2015-04-11 03:10:23 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
ReadBufferFromString buf{str};
|
|
|
|
|
|
|
|
assertString("columns format version: 1\n", buf);
|
|
|
|
size_t count{};
|
|
|
|
readText(count, buf);
|
|
|
|
assertString(" columns:\n", buf);
|
|
|
|
|
2018-03-06 20:18:34 +00:00
|
|
|
ColumnsDescription result;
|
2017-04-01 07:20:54 +00:00
|
|
|
for (size_t i = 0; i < count; ++i)
|
|
|
|
{
|
2019-03-14 15:20:51 +00:00
|
|
|
ColumnDescription column;
|
|
|
|
column.readText(buf);
|
2019-02-27 16:41:51 +00:00
|
|
|
buf.ignore(1); /// ignore new line
|
2019-03-14 15:20:51 +00:00
|
|
|
result.add(std::move(column));
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
assertEOF(buf);
|
|
|
|
return result;
|
2015-04-11 03:10:23 +00:00
|
|
|
}
|
|
|
|
|
2020-03-03 09:05:17 +00:00
|
|
|
|
|
|
|
Block validateColumnsDefaultsAndGetSampleBlock(ASTPtr default_expr_list, const NamesAndTypesList & all_columns, const Context & context)
|
|
|
|
{
|
|
|
|
for (const auto & child : default_expr_list->children)
|
2020-03-03 12:51:41 +00:00
|
|
|
if (child->as<ASTSelectQuery>() || child->as<ASTSelectWithUnionQuery>() || child->as<ASTSubquery>())
|
2020-03-03 09:05:17 +00:00
|
|
|
throw Exception("Select query is not allowed in columns DEFAULT expression", ErrorCodes::THERE_IS_NO_DEFAULT_VALUE);
|
|
|
|
|
2020-03-03 10:02:43 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
auto syntax_analyzer_result = SyntaxAnalyzer(context).analyze(default_expr_list, all_columns);
|
|
|
|
const auto actions = ExpressionAnalyzer(default_expr_list, syntax_analyzer_result, context).getActions(true);
|
|
|
|
for (auto & action : actions->getActions())
|
|
|
|
if (action.type == ExpressionAction::Type::JOIN || action.type == ExpressionAction::Type::ARRAY_JOIN)
|
|
|
|
throw Exception("Unsupported default value that requires ARRAY JOIN or JOIN action", ErrorCodes::THERE_IS_NO_DEFAULT_VALUE);
|
2020-03-03 09:05:17 +00:00
|
|
|
|
2020-03-03 10:02:43 +00:00
|
|
|
return actions->getSampleBlock();
|
|
|
|
}
|
|
|
|
catch (Exception & ex)
|
|
|
|
{
|
|
|
|
ex.addMessage("default expression and column type are incompatible.");
|
|
|
|
throw;
|
|
|
|
}
|
2020-03-03 09:05:17 +00:00
|
|
|
}
|
2020-03-03 10:02:43 +00:00
|
|
|
|
2015-04-11 03:10:23 +00:00
|
|
|
}
|