2019-04-15 09:30:45 +00:00
|
|
|
#include <DataStreams/TTLBlockInputStream.h>
|
|
|
|
#include <DataTypes/DataTypeDate.h>
|
|
|
|
#include <Interpreters/evaluateMissingDefaults.h>
|
|
|
|
#include <Interpreters/SyntaxAnalyzer.h>
|
|
|
|
#include <Interpreters/ExpressionAnalyzer.h>
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int LOGICAL_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TTLBlockInputStream::TTLBlockInputStream(
|
|
|
|
const BlockInputStreamPtr & input_,
|
|
|
|
const MergeTreeData & storage_,
|
|
|
|
const MergeTreeData::MutableDataPartPtr & data_part_,
|
2019-08-01 11:10:42 +00:00
|
|
|
time_t current_time_,
|
|
|
|
bool force_)
|
2019-04-15 09:30:45 +00:00
|
|
|
: storage(storage_)
|
|
|
|
, data_part(data_part_)
|
|
|
|
, current_time(current_time_)
|
2019-08-01 11:10:42 +00:00
|
|
|
, force(force_)
|
2019-04-15 09:30:45 +00:00
|
|
|
, old_ttl_infos(data_part->ttl_infos)
|
|
|
|
, log(&Logger::get(storage.getLogName() + " (TTLBlockInputStream)"))
|
|
|
|
, date_lut(DateLUT::instance())
|
|
|
|
{
|
|
|
|
children.push_back(input_);
|
2019-07-01 12:50:50 +00:00
|
|
|
header = children.at(0)->getHeader();
|
2019-04-15 09:30:45 +00:00
|
|
|
|
|
|
|
const auto & column_defaults = storage.getColumns().getDefaults();
|
|
|
|
ASTPtr default_expr_list = std::make_shared<ASTExpressionList>();
|
|
|
|
for (const auto & [name, ttl_info] : old_ttl_infos.columns_ttl)
|
|
|
|
{
|
2019-09-03 10:27:02 +00:00
|
|
|
if (force || isTTLExpired(ttl_info.min))
|
2019-04-15 09:30:45 +00:00
|
|
|
{
|
|
|
|
new_ttl_infos.columns_ttl.emplace(name, MergeTreeDataPart::TTLInfo{});
|
|
|
|
empty_columns.emplace(name);
|
|
|
|
|
|
|
|
auto it = column_defaults.find(name);
|
|
|
|
|
|
|
|
if (it != column_defaults.end())
|
2019-07-18 13:44:29 +00:00
|
|
|
{
|
|
|
|
auto expression = it->second.expression->clone();
|
|
|
|
default_expr_list->children.emplace_back(setAlias(expression, it->first));
|
|
|
|
}
|
2019-04-15 09:30:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
new_ttl_infos.columns_ttl.emplace(name, ttl_info);
|
|
|
|
}
|
|
|
|
|
2019-09-03 10:27:02 +00:00
|
|
|
if (!force && !isTTLExpired(old_ttl_infos.table_ttl.min))
|
2019-04-15 09:30:45 +00:00
|
|
|
new_ttl_infos.table_ttl = old_ttl_infos.table_ttl;
|
|
|
|
|
|
|
|
if (!default_expr_list->children.empty())
|
|
|
|
{
|
|
|
|
auto syntax_result = SyntaxAnalyzer(storage.global_context).analyze(
|
|
|
|
default_expr_list, storage.getColumns().getAllPhysical());
|
|
|
|
defaults_expression = ExpressionAnalyzer{default_expr_list, syntax_result, storage.global_context}.getActions(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-01 11:10:42 +00:00
|
|
|
bool TTLBlockInputStream::isTTLExpired(time_t ttl)
|
|
|
|
{
|
|
|
|
return (ttl && (ttl <= current_time));
|
|
|
|
}
|
2019-04-15 09:30:45 +00:00
|
|
|
|
|
|
|
Block TTLBlockInputStream::readImpl()
|
|
|
|
{
|
2019-07-28 10:30:46 +00:00
|
|
|
/// Skip all data if table ttl is expired for part
|
2019-08-26 18:00:13 +00:00
|
|
|
if (storage.hasTableTTL() && isTTLExpired(old_ttl_infos.table_ttl.max))
|
2019-07-28 10:30:46 +00:00
|
|
|
{
|
|
|
|
rows_removed = data_part->rows_count;
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2019-07-31 19:36:03 +00:00
|
|
|
Block block = children.at(0)->read();
|
|
|
|
if (!block)
|
|
|
|
return block;
|
|
|
|
|
2019-08-26 18:00:13 +00:00
|
|
|
if (storage.hasTableTTL() && (force || isTTLExpired(old_ttl_infos.table_ttl.min)))
|
|
|
|
removeRowsWithExpiredTableTTL(block);
|
2019-04-15 09:30:45 +00:00
|
|
|
|
|
|
|
removeValuesWithExpiredColumnTTL(block);
|
|
|
|
|
2019-10-22 07:55:36 +00:00
|
|
|
updateMovesTTL(block);
|
|
|
|
|
2019-04-15 09:30:45 +00:00
|
|
|
return block;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TTLBlockInputStream::readSuffixImpl()
|
|
|
|
{
|
|
|
|
for (const auto & elem : new_ttl_infos.columns_ttl)
|
2019-07-28 10:30:46 +00:00
|
|
|
new_ttl_infos.updatePartMinMaxTTL(elem.second.min, elem.second.max);
|
2019-04-15 09:30:45 +00:00
|
|
|
|
2019-07-28 10:30:46 +00:00
|
|
|
new_ttl_infos.updatePartMinMaxTTL(new_ttl_infos.table_ttl.min, new_ttl_infos.table_ttl.max);
|
2019-04-15 09:30:45 +00:00
|
|
|
|
|
|
|
data_part->ttl_infos = std::move(new_ttl_infos);
|
|
|
|
data_part->empty_columns = std::move(empty_columns);
|
|
|
|
|
|
|
|
if (rows_removed)
|
2019-08-01 11:10:42 +00:00
|
|
|
LOG_INFO(log, "Removed " << rows_removed << " rows with expired TTL from part " << data_part->name);
|
2019-04-15 09:30:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TTLBlockInputStream::removeRowsWithExpiredTableTTL(Block & block)
|
|
|
|
{
|
|
|
|
storage.ttl_table_entry.expression->execute(block);
|
|
|
|
|
2019-08-01 11:10:42 +00:00
|
|
|
const IColumn * ttl_column =
|
|
|
|
block.getByName(storage.ttl_table_entry.result_column).column.get();
|
2019-04-15 09:30:45 +00:00
|
|
|
|
2019-07-01 12:50:50 +00:00
|
|
|
const auto & column_names = header.getNames();
|
2019-04-15 09:30:45 +00:00
|
|
|
MutableColumns result_columns;
|
2019-07-01 12:50:50 +00:00
|
|
|
result_columns.reserve(column_names.size());
|
|
|
|
|
|
|
|
for (auto it = column_names.begin(); it != column_names.end(); ++it)
|
2019-04-15 09:30:45 +00:00
|
|
|
{
|
2019-08-01 11:10:42 +00:00
|
|
|
const IColumn * values_column = block.getByName(*it).column.get();
|
2019-04-15 09:30:45 +00:00
|
|
|
MutableColumnPtr result_column = values_column->cloneEmpty();
|
|
|
|
result_column->reserve(block.rows());
|
|
|
|
|
|
|
|
for (size_t i = 0; i < block.rows(); ++i)
|
|
|
|
{
|
|
|
|
UInt32 cur_ttl = getTimestampByIndex(ttl_column, i);
|
2019-08-01 11:10:42 +00:00
|
|
|
if (!isTTLExpired(cur_ttl))
|
2019-04-15 09:30:45 +00:00
|
|
|
{
|
|
|
|
new_ttl_infos.table_ttl.update(cur_ttl);
|
|
|
|
result_column->insertFrom(*values_column, i);
|
|
|
|
}
|
2019-07-01 12:50:50 +00:00
|
|
|
else if (it == column_names.begin())
|
2019-04-15 09:30:45 +00:00
|
|
|
++rows_removed;
|
|
|
|
}
|
|
|
|
result_columns.emplace_back(std::move(result_column));
|
|
|
|
}
|
|
|
|
|
2019-07-01 12:50:50 +00:00
|
|
|
block = header.cloneWithColumns(std::move(result_columns));
|
2019-04-15 09:30:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TTLBlockInputStream::removeValuesWithExpiredColumnTTL(Block & block)
|
|
|
|
{
|
|
|
|
Block block_with_defaults;
|
|
|
|
if (defaults_expression)
|
|
|
|
{
|
|
|
|
block_with_defaults = block;
|
|
|
|
defaults_expression->execute(block_with_defaults);
|
|
|
|
}
|
|
|
|
|
2019-11-27 18:39:47 +00:00
|
|
|
std::vector<String> columns_to_remove;
|
2019-11-26 08:02:48 +00:00
|
|
|
for (const auto & [name, ttl_entry] : storage.column_ttl_entries_by_name)
|
2019-04-15 09:30:45 +00:00
|
|
|
{
|
|
|
|
const auto & old_ttl_info = old_ttl_infos.columns_ttl[name];
|
|
|
|
auto & new_ttl_info = new_ttl_infos.columns_ttl[name];
|
|
|
|
|
2019-08-01 11:10:42 +00:00
|
|
|
/// Nothing to do
|
|
|
|
if (!force && !isTTLExpired(old_ttl_info.min))
|
2019-04-15 09:30:45 +00:00
|
|
|
continue;
|
|
|
|
|
2019-08-01 11:10:42 +00:00
|
|
|
/// Later drop full column
|
|
|
|
if (isTTLExpired(old_ttl_info.max))
|
2019-04-15 09:30:45 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!block.has(ttl_entry.result_column))
|
2019-11-27 18:39:47 +00:00
|
|
|
{
|
|
|
|
columns_to_remove.push_back(ttl_entry.result_column);
|
2019-04-15 09:30:45 +00:00
|
|
|
ttl_entry.expression->execute(block);
|
2019-11-27 18:39:47 +00:00
|
|
|
}
|
2019-04-15 09:30:45 +00:00
|
|
|
|
|
|
|
ColumnPtr default_column = nullptr;
|
|
|
|
if (block_with_defaults.has(name))
|
|
|
|
default_column = block_with_defaults.getByName(name).column->convertToFullColumnIfConst();
|
|
|
|
|
|
|
|
auto & column_with_type = block.getByName(name);
|
|
|
|
const IColumn * values_column = column_with_type.column.get();
|
|
|
|
MutableColumnPtr result_column = values_column->cloneEmpty();
|
|
|
|
result_column->reserve(block.rows());
|
|
|
|
|
2019-08-01 11:10:42 +00:00
|
|
|
const IColumn * ttl_column = block.getByName(ttl_entry.result_column).column.get();
|
2019-04-15 09:30:45 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < block.rows(); ++i)
|
|
|
|
{
|
|
|
|
UInt32 cur_ttl = getTimestampByIndex(ttl_column, i);
|
2019-08-01 11:10:42 +00:00
|
|
|
if (isTTLExpired(cur_ttl))
|
2019-04-15 09:30:45 +00:00
|
|
|
{
|
|
|
|
if (default_column)
|
|
|
|
result_column->insertFrom(*default_column, i);
|
|
|
|
else
|
|
|
|
result_column->insertDefault();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
new_ttl_info.update(cur_ttl);
|
|
|
|
empty_columns.erase(name);
|
|
|
|
result_column->insertFrom(*values_column, i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
column_with_type.column = std::move(result_column);
|
|
|
|
}
|
|
|
|
|
2019-11-27 18:39:47 +00:00
|
|
|
for (const String & column : columns_to_remove)
|
|
|
|
block.erase(column);
|
2019-04-15 09:30:45 +00:00
|
|
|
}
|
|
|
|
|
2019-10-22 07:55:36 +00:00
|
|
|
void TTLBlockInputStream::updateMovesTTL(Block & block)
|
|
|
|
{
|
2019-11-27 18:39:47 +00:00
|
|
|
std::vector<String> columns_to_remove;
|
2019-11-28 06:44:26 +00:00
|
|
|
for (const auto & ttl_entry : storage.move_ttl_entries)
|
2019-10-22 07:55:36 +00:00
|
|
|
{
|
2019-11-28 06:44:26 +00:00
|
|
|
auto & new_ttl_info = new_ttl_infos.moves_ttl[ttl_entry.result_column];
|
2019-10-22 07:55:36 +00:00
|
|
|
|
|
|
|
if (!block.has(ttl_entry.result_column))
|
2019-11-27 18:39:47 +00:00
|
|
|
{
|
|
|
|
columns_to_remove.push_back(ttl_entry.result_column);
|
2019-10-22 07:55:36 +00:00
|
|
|
ttl_entry.expression->execute(block);
|
2019-11-27 18:39:47 +00:00
|
|
|
}
|
2019-10-22 07:55:36 +00:00
|
|
|
|
|
|
|
const IColumn * ttl_column = block.getByName(ttl_entry.result_column).column.get();
|
|
|
|
|
|
|
|
for (size_t i = 0; i < block.rows(); ++i)
|
|
|
|
{
|
|
|
|
UInt32 cur_ttl = getTimestampByIndex(ttl_column, i);
|
|
|
|
new_ttl_info.update(cur_ttl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-27 18:39:47 +00:00
|
|
|
for (const String & column : columns_to_remove)
|
|
|
|
block.erase(column);
|
2019-10-22 07:55:36 +00:00
|
|
|
}
|
|
|
|
|
2019-04-15 09:30:45 +00:00
|
|
|
UInt32 TTLBlockInputStream::getTimestampByIndex(const IColumn * column, size_t ind)
|
|
|
|
{
|
|
|
|
if (const ColumnUInt16 * column_date = typeid_cast<const ColumnUInt16 *>(column))
|
|
|
|
return date_lut.fromDayNum(DayNum(column_date->getData()[ind]));
|
|
|
|
else if (const ColumnUInt32 * column_date_time = typeid_cast<const ColumnUInt32 *>(column))
|
|
|
|
return column_date_time->getData()[ind];
|
2019-11-27 10:09:07 +00:00
|
|
|
else if (const ColumnConst * column_const = typeid_cast<const ColumnConst *>(column))
|
|
|
|
{
|
|
|
|
if (typeid_cast<const ColumnUInt16 *>(&column_const->getDataColumn()))
|
|
|
|
return date_lut.fromDayNum(DayNum(column_const->getValue<UInt16>()));
|
|
|
|
else if (typeid_cast<const ColumnUInt32 *>(&column_const->getDataColumn()))
|
|
|
|
return column_const->getValue<UInt32>();
|
|
|
|
}
|
|
|
|
|
|
|
|
throw Exception("Unexpected type of result TTL column", ErrorCodes::LOGICAL_ERROR);
|
2019-04-15 09:30:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|