From 3e854df5a54afc7cdc51b2ffd612af7ab1a56405 Mon Sep 17 00:00:00 2001 From: CurtizJ Date: Fri, 7 Sep 2018 17:18:15 +0300 Subject: [PATCH] remove code duplicates --- .../DataStreams/RollupBlockInputStream.cpp | 25 ++++--------------- .../TotalsHavingBlockInputStream.cpp | 24 +++--------------- dbms/src/DataStreams/finalizeBlock.cpp | 24 ++++++++++++++++++ dbms/src/DataStreams/finalizeBlock.h | 9 +++++++ 4 files changed, 42 insertions(+), 40 deletions(-) create mode 100644 dbms/src/DataStreams/finalizeBlock.cpp create mode 100644 dbms/src/DataStreams/finalizeBlock.h diff --git a/dbms/src/DataStreams/RollupBlockInputStream.cpp b/dbms/src/DataStreams/RollupBlockInputStream.cpp index 389dbf5bec5..00766b1a390 100644 --- a/dbms/src/DataStreams/RollupBlockInputStream.cpp +++ b/dbms/src/DataStreams/RollupBlockInputStream.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -6,23 +7,7 @@ namespace DB { - -static void finalize(Block & block) -{ - for (size_t i = 0; i < block.columns(); ++i) - { - ColumnWithTypeAndName & current = block.getByPosition(i); - const DataTypeAggregateFunction * unfinalized_type = typeid_cast(current.type.get()); - - if (unfinalized_type) - { - current.type = unfinalized_type->getReturnType(); - if (current.column) - current.column = typeid_cast(*current.column).convertToValues(); - } - } -} - + RollupBlockInputStream::RollupBlockInputStream( const BlockInputStreamPtr & input_, const Aggregator::Params & params_) : aggregator(params_), keys(params_.keys) @@ -36,7 +21,7 @@ RollupBlockInputStream::RollupBlockInputStream( Block RollupBlockInputStream::getHeader() const { Block res = children.at(0)->getHeader(); - finalize(res); + finalizeBlock(res); return res; } @@ -58,7 +43,7 @@ Block RollupBlockInputStream::readImpl() rollup_block = aggregator.mergeBlocks(rollup_blocks, false); Block finalized = rollup_block; - finalize(finalized); + finalizeBlock(finalized); return finalized; } @@ -66,7 +51,7 @@ Block RollupBlockInputStream::readImpl() current_key = keys.size() - 1; rollup_block = block; - finalize(block); + finalizeBlock(block); return block; } diff --git a/dbms/src/DataStreams/TotalsHavingBlockInputStream.cpp b/dbms/src/DataStreams/TotalsHavingBlockInputStream.cpp index e964065822c..103d880f1d3 100644 --- a/dbms/src/DataStreams/TotalsHavingBlockInputStream.cpp +++ b/dbms/src/DataStreams/TotalsHavingBlockInputStream.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -53,23 +54,6 @@ TotalsHavingBlockInputStream::TotalsHavingBlockInputStream( } -static void finalize(Block & block) -{ - for (size_t i = 0; i < block.columns(); ++i) - { - ColumnWithTypeAndName & current = block.getByPosition(i); - const DataTypeAggregateFunction * unfinalized_type = typeid_cast(current.type.get()); - - if (unfinalized_type) - { - current.type = unfinalized_type->getReturnType(); - if (current.column) - current.column = typeid_cast(*current.column).convertToValues(); - } - } -} - - Block TotalsHavingBlockInputStream::getTotals() { if (!totals) @@ -87,7 +71,7 @@ Block TotalsHavingBlockInputStream::getTotals() } totals = children.at(0)->getHeader().cloneWithColumns(std::move(current_totals)); - finalize(totals); + finalizeBlock(totals); } if (totals && expression) @@ -101,7 +85,7 @@ Block TotalsHavingBlockInputStream::getHeader() const { Block res = children.at(0)->getHeader(); if (final) - finalize(res); + finalizeBlock(res); if (expression) expression->execute(res); return res; @@ -129,7 +113,7 @@ Block TotalsHavingBlockInputStream::readImpl() finalized = block; if (final) - finalize(finalized); + finalizeBlock(finalized); total_keys += finalized.rows(); diff --git a/dbms/src/DataStreams/finalizeBlock.cpp b/dbms/src/DataStreams/finalizeBlock.cpp new file mode 100644 index 00000000000..11b9a297227 --- /dev/null +++ b/dbms/src/DataStreams/finalizeBlock.cpp @@ -0,0 +1,24 @@ +#include +#include +#include +#include + + +namespace DB +{ + void finalizeBlock(Block & block) + { + for (size_t i = 0; i < block.columns(); ++i) + { + ColumnWithTypeAndName & current = block.getByPosition(i); + const DataTypeAggregateFunction * unfinalized_type = typeid_cast(current.type.get()); + + if (unfinalized_type) + { + current.type = unfinalized_type->getReturnType(); + if (current.column) + current.column = typeid_cast(*current.column).convertToValues(); + } + } + } +} \ No newline at end of file diff --git a/dbms/src/DataStreams/finalizeBlock.h b/dbms/src/DataStreams/finalizeBlock.h new file mode 100644 index 00000000000..db0ef6968b2 --- /dev/null +++ b/dbms/src/DataStreams/finalizeBlock.h @@ -0,0 +1,9 @@ +#pragma once + +#include + +namespace DB +{ + /// Converts aggregate function columns with non-finalized states to final values + void finalizeBlock(Block & block); +} \ No newline at end of file