2014-10-13 14:35:40 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <DB/Columns/ColumnConst.h>
|
|
|
|
#include <DB/DataStreams/IBlockOutputStream.h>
|
2016-10-19 15:00:56 +00:00
|
|
|
#include <DB/DataTypes/DataTypeNullable.h>
|
|
|
|
#include <DB/DataTypes/DataTypesNumberFixed.h>
|
2015-10-05 00:33:43 +00:00
|
|
|
#include <ext/range.hpp>
|
2014-10-13 14:35:40 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
/** Преобразует столбцы-константы в полноценные столбцы ("материализует" их).
|
|
|
|
*/
|
|
|
|
class MaterializingBlockOutputStream : public IBlockOutputStream
|
|
|
|
{
|
|
|
|
public:
|
2015-01-27 00:52:03 +00:00
|
|
|
MaterializingBlockOutputStream(const BlockOutputStreamPtr & output)
|
|
|
|
: output{output} {}
|
2016-02-25 02:37:31 +00:00
|
|
|
|
|
|
|
void write(const Block & block) override
|
|
|
|
{
|
2016-10-19 15:00:56 +00:00
|
|
|
output->write(materialize(block));
|
2016-02-25 02:37:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void flush() override { output->flush(); }
|
|
|
|
void writePrefix() override { output->writePrefix(); }
|
|
|
|
void writeSuffix() override { output->writeSuffix(); }
|
|
|
|
void setRowsBeforeLimit(size_t rows_before_limit) override { output->setRowsBeforeLimit(rows_before_limit); }
|
|
|
|
void setTotals(const Block & totals) override { output->setTotals(materialize(totals)); }
|
|
|
|
void setExtremes(const Block & extremes) override { output->setExtremes(materialize(extremes)); }
|
2016-08-17 04:38:19 +00:00
|
|
|
void onProgress(const Progress & progress) override { output->onProgress(progress); }
|
2016-02-25 02:37:31 +00:00
|
|
|
String getContentType() const override { return output->getContentType(); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
BlockOutputStreamPtr output;
|
|
|
|
|
|
|
|
static Block materialize(const Block & original_block)
|
2014-10-13 14:35:40 +00:00
|
|
|
{
|
|
|
|
/// copy block to get rid of const
|
|
|
|
auto block = original_block;
|
|
|
|
|
|
|
|
for (const auto i : ext::range(0, block.columns()))
|
|
|
|
{
|
2017-01-02 20:12:12 +00:00
|
|
|
auto & element = block.safeGetByPosition(i);
|
2016-10-19 15:00:56 +00:00
|
|
|
auto & src = element.column;
|
2015-10-28 21:21:18 +00:00
|
|
|
ColumnPtr converted = src->convertToFullColumnIfConst();
|
|
|
|
if (converted)
|
2016-10-19 15:00:56 +00:00
|
|
|
{
|
2015-10-28 21:21:18 +00:00
|
|
|
src = converted;
|
2016-10-19 15:00:56 +00:00
|
|
|
auto & type = element.type;
|
|
|
|
if (type->isNull())
|
|
|
|
{
|
|
|
|
/// A ColumnNull that is converted to a full column
|
|
|
|
/// has the type DataTypeNullable(DataTypeUInt8).
|
|
|
|
type = std::make_shared<DataTypeNullable>(std::make_shared<DataTypeUInt8>());
|
|
|
|
}
|
|
|
|
}
|
2014-10-13 14:35:40 +00:00
|
|
|
}
|
|
|
|
|
2016-02-25 02:37:31 +00:00
|
|
|
return block;
|
2014-10-13 14:35:40 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|