2019-09-03 10:07:31 +00:00
|
|
|
#include <Interpreters/FillingRow.h>
|
2020-06-14 18:42:10 +00:00
|
|
|
#include <Common/FieldVisitorsAccurateComparison.h>
|
|
|
|
|
2019-09-02 14:29:51 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
bool less(const Field & lhs, const Field & rhs, int direction)
|
|
|
|
{
|
|
|
|
if (direction == -1)
|
|
|
|
return applyVisitor(FieldVisitorAccurateLess(), rhs, lhs);
|
|
|
|
|
|
|
|
return applyVisitor(FieldVisitorAccurateLess(), lhs, rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool equals(const Field & lhs, const Field & rhs)
|
|
|
|
{
|
|
|
|
return applyVisitor(FieldVisitorAccurateEquals(), lhs, rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-03-17 05:51:35 +00:00
|
|
|
FillingRow::FillingRow(const SortDescription & sort_description_, const InterpolateDescription & interpolate_description_)
|
|
|
|
: sort{*this}
|
|
|
|
, interpolate{*this}
|
|
|
|
, sort_description(sort_description_)
|
|
|
|
, interpolate_description(interpolate_description_)
|
2019-09-02 14:29:51 +00:00
|
|
|
{
|
2022-03-17 05:51:35 +00:00
|
|
|
row.resize(sort_description.size() + interpolate_description.size());
|
2019-09-02 14:29:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool FillingRow::operator<(const FillingRow & other) const
|
|
|
|
{
|
2022-03-17 05:51:35 +00:00
|
|
|
for (size_t i = 0; i < sort.size(); ++i)
|
2019-09-02 14:29:51 +00:00
|
|
|
{
|
2022-03-17 05:51:35 +00:00
|
|
|
if (sort[i].isNull() || other.sort[i].isNull() || equals(sort[i], other.sort[i]))
|
2019-09-02 14:29:51 +00:00
|
|
|
continue;
|
2022-03-17 05:51:35 +00:00
|
|
|
return less(sort[i], other.sort[i], getDirection(i));
|
2019-09-02 14:29:51 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FillingRow::operator==(const FillingRow & other) const
|
|
|
|
{
|
2022-03-17 05:51:35 +00:00
|
|
|
for (size_t i = 0; i < sort.size(); ++i)
|
|
|
|
if (!equals(sort[i], other.sort[i]))
|
2019-09-02 14:29:51 +00:00
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FillingRow::next(const FillingRow & to_row)
|
|
|
|
{
|
|
|
|
size_t pos = 0;
|
|
|
|
|
2022-03-17 05:51:35 +00:00
|
|
|
for(size_t i = 0; i < to_row.interpolate.size(); ++i) {
|
|
|
|
std::cout << to_row.interpolate[i] <<" : ";
|
|
|
|
interpolate[i] = to_row.interpolate[i];
|
|
|
|
}
|
|
|
|
|
2019-09-02 14:29:51 +00:00
|
|
|
/// Find position we need to increment for generating next row.
|
2022-03-17 05:51:35 +00:00
|
|
|
for (; pos < sort.size(); ++pos)
|
|
|
|
if (!sort[pos].isNull() && !to_row.sort[pos].isNull() && !equals(sort[pos], to_row.sort[pos]))
|
2019-09-02 14:29:51 +00:00
|
|
|
break;
|
|
|
|
|
2022-03-17 05:51:35 +00:00
|
|
|
if (pos == sort.size() || less(to_row.sort[pos], sort[pos], getDirection(pos)))
|
2019-09-02 14:29:51 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
/// If we have any 'fill_to' value at position greater than 'pos',
|
|
|
|
/// we need to generate rows up to 'fill_to' value.
|
2022-03-17 05:51:35 +00:00
|
|
|
for (size_t i = sort.size() - 1; i > pos; --i)
|
2019-09-02 14:29:51 +00:00
|
|
|
{
|
2022-03-17 05:51:35 +00:00
|
|
|
if (getFillDescription(i).fill_to.isNull() || sort[i].isNull())
|
2019-09-02 14:29:51 +00:00
|
|
|
continue;
|
|
|
|
|
2022-03-17 05:51:35 +00:00
|
|
|
auto next_value = sort[i];
|
2021-10-31 19:48:12 +00:00
|
|
|
getFillDescription(i).step_func(next_value);
|
2019-09-02 14:29:51 +00:00
|
|
|
if (less(next_value, getFillDescription(i).fill_to, getDirection(i)))
|
|
|
|
{
|
2022-03-17 05:51:35 +00:00
|
|
|
sort[i] = next_value;
|
2019-09-02 14:29:51 +00:00
|
|
|
initFromDefaults(i + 1);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-17 05:51:35 +00:00
|
|
|
auto next_value = sort[pos];
|
2021-10-31 16:22:20 +00:00
|
|
|
getFillDescription(pos).step_func(next_value);
|
2019-09-02 14:29:51 +00:00
|
|
|
|
2022-03-17 05:51:35 +00:00
|
|
|
if (less(to_row.sort[pos], next_value, getDirection(pos)))
|
2019-09-02 14:29:51 +00:00
|
|
|
return false;
|
|
|
|
|
2022-03-17 05:51:35 +00:00
|
|
|
sort[pos] = next_value;
|
|
|
|
if (equals(sort[pos], to_row.sort[pos]))
|
2019-09-02 14:29:51 +00:00
|
|
|
{
|
|
|
|
bool is_less = false;
|
2022-03-17 05:51:35 +00:00
|
|
|
for (size_t i = pos + 1; i < sort.size(); ++i)
|
2019-09-02 14:29:51 +00:00
|
|
|
{
|
|
|
|
const auto & fill_from = getFillDescription(i).fill_from;
|
|
|
|
if (!fill_from.isNull())
|
2022-03-17 05:51:35 +00:00
|
|
|
sort[i] = fill_from;
|
2019-09-02 14:29:51 +00:00
|
|
|
else
|
2022-03-17 05:51:35 +00:00
|
|
|
sort[i] = to_row.sort[i];
|
|
|
|
is_less |= less(sort[i], to_row.sort[i], getDirection(i));
|
2019-09-02 14:29:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return is_less;
|
|
|
|
}
|
|
|
|
|
|
|
|
initFromDefaults(pos + 1);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FillingRow::initFromDefaults(size_t from_pos)
|
|
|
|
{
|
2022-03-17 05:51:35 +00:00
|
|
|
for (size_t i = from_pos; i < sort.size(); ++i)
|
|
|
|
sort[i] = getFillDescription(i).fill_from;
|
2019-09-02 14:29:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void insertFromFillingRow(MutableColumns & filling_columns, MutableColumns & other_columns, const FillingRow & filling_row)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < filling_columns.size(); ++i)
|
|
|
|
{
|
|
|
|
if (filling_row[i].isNull())
|
|
|
|
filling_columns[i]->insertDefault();
|
|
|
|
else
|
|
|
|
filling_columns[i]->insert(filling_row[i]);
|
|
|
|
}
|
|
|
|
|
2020-03-09 00:08:02 +00:00
|
|
|
for (const auto & other_column : other_columns)
|
|
|
|
other_column->insertDefault();
|
2019-09-02 14:29:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void copyRowFromColumns(MutableColumns & dest, const Columns & source, size_t row_num)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < source.size(); ++i)
|
|
|
|
dest[i]->insertFrom(*source[i], row_num);
|
|
|
|
}
|
|
|
|
|
2019-09-02 17:18:44 +00:00
|
|
|
}
|