ClickHouse
66 строк · 2.2 Кб
1#include <Processors/TTL/ITTLAlgorithm.h>
2#include <Columns/ColumnVector.h>
3#include <Columns/ColumnConst.h>
4
5namespace DB
6{
7
8namespace ErrorCodes
9{
10extern const int LOGICAL_ERROR;
11}
12
13ITTLAlgorithm::ITTLAlgorithm(
14const TTLExpressions & ttl_expressions_, const TTLDescription & description_, const TTLInfo & old_ttl_info_, time_t current_time_, bool force_)
15: ttl_expressions(ttl_expressions_)
16, description(description_)
17, old_ttl_info(old_ttl_info_)
18, current_time(current_time_)
19, force(force_)
20, date_lut(DateLUT::instance())
21{
22}
23
24bool ITTLAlgorithm::isTTLExpired(time_t ttl) const
25{
26return (ttl && (ttl <= current_time));
27}
28
29ColumnPtr ITTLAlgorithm::executeExpressionAndGetColumn(
30const ExpressionActionsPtr & expression, const Block & block, const String & result_column)
31{
32if (!expression)
33return nullptr;
34
35if (block.has(result_column))
36return block.getByName(result_column).column;
37
38Block block_copy;
39for (const auto & column_name : expression->getRequiredColumns())
40block_copy.insert(block.getByName(column_name));
41
42/// Keep number of rows for const expression.
43size_t num_rows = block.rows();
44expression->execute(block_copy, num_rows);
45
46return block_copy.getByName(result_column).column;
47}
48
49UInt32 ITTLAlgorithm::getTimestampByIndex(const IColumn * column, size_t index) const
50{
51if (const ColumnUInt16 * column_date = typeid_cast<const ColumnUInt16 *>(column))
52return static_cast<UInt32>(date_lut.fromDayNum(DayNum(column_date->getData()[index])));
53else if (const ColumnUInt32 * column_date_time = typeid_cast<const ColumnUInt32 *>(column))
54return column_date_time->getData()[index];
55else if (const ColumnConst * column_const = typeid_cast<const ColumnConst *>(column))
56{
57if (typeid_cast<const ColumnUInt16 *>(&column_const->getDataColumn()))
58return static_cast<UInt32>(date_lut.fromDayNum(DayNum(column_const->getValue<UInt16>())));
59else if (typeid_cast<const ColumnUInt32 *>(&column_const->getDataColumn()))
60return column_const->getValue<UInt32>();
61}
62
63throw Exception(ErrorCodes::LOGICAL_ERROR, "Unexpected type of result TTL column");
64}
65
66}
67