2013-10-25 14:56:47 +00:00
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <Poco/SharedPtr.h>
|
|
|
|
|
|
|
|
|
|
#include <DB/DataStreams/IBlockOutputStream.h>
|
|
|
|
|
#include <DB/Columns/ColumnConst.h>
|
|
|
|
|
|
2014-09-30 03:08:47 +00:00
|
|
|
|
#include <DB/Storages/ColumnDefault.h>
|
|
|
|
|
#include <DB/Interpreters/Context.h>
|
2014-10-23 13:53:16 +00:00
|
|
|
|
#include <DB/Interpreters/evaluateMissingDefaults.h>
|
2013-10-25 14:56:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Добавляет в блок недостающие столбцы со значениями по-умолчанию.
|
|
|
|
|
* Эти столбцы - материалированные (не константы).
|
|
|
|
|
*/
|
|
|
|
|
class AddingDefaultBlockOutputStream : public IBlockOutputStream
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
AddingDefaultBlockOutputStream(
|
|
|
|
|
BlockOutputStreamPtr output_,
|
2014-09-30 03:08:47 +00:00
|
|
|
|
NamesAndTypesListPtr required_columns_,
|
|
|
|
|
const ColumnDefaults & column_defaults_,
|
2015-04-10 00:43:08 +00:00
|
|
|
|
const Context & context_,
|
|
|
|
|
bool only_explicit_column_defaults_)
|
2014-09-30 03:08:47 +00:00
|
|
|
|
: output(output_), required_columns(required_columns_),
|
2015-04-10 00:43:08 +00:00
|
|
|
|
column_defaults(column_defaults_), context(context_),
|
|
|
|
|
only_explicit_column_defaults(only_explicit_column_defaults_)
|
2013-10-25 14:56:47 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-18 19:49:31 +00:00
|
|
|
|
void write(const Block & block) override
|
2013-10-31 18:38:34 +00:00
|
|
|
|
{
|
2013-10-25 14:56:47 +00:00
|
|
|
|
Block res = block;
|
2015-04-10 00:43:08 +00:00
|
|
|
|
|
|
|
|
|
/// Вычисляет явно указанные (в column_defaults) значения по-умолчанию.
|
2015-09-11 14:26:26 +00:00
|
|
|
|
/** @todo if somehow block does not contain values for implicitly-defaulted columns that are prerequisites
|
|
|
|
|
* for explicitly-defaulted ones, exception will be thrown during evaluating such columns
|
|
|
|
|
* (implicitly-defaulted columns are evaluated on the line after following one. */
|
2014-10-23 13:53:16 +00:00
|
|
|
|
evaluateMissingDefaults(res, *required_columns, column_defaults, context);
|
2015-04-10 00:43:08 +00:00
|
|
|
|
|
|
|
|
|
/// Добавляет не указанные значения по-умолчанию.
|
|
|
|
|
if (!only_explicit_column_defaults)
|
2015-09-11 14:26:26 +00:00
|
|
|
|
/// @todo this line may be moved before `evaluateMissingDefaults` with passing {required_columns - explicitly-defaulted columns}
|
2015-04-10 00:43:08 +00:00
|
|
|
|
res.addDefaults(*required_columns);
|
|
|
|
|
|
2013-10-31 18:38:34 +00:00
|
|
|
|
output->write(res);
|
2013-10-25 14:56:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-11-08 23:52:18 +00:00
|
|
|
|
void flush() override { output->flush(); }
|
2014-08-14 20:27:41 +00:00
|
|
|
|
|
2014-09-18 19:49:31 +00:00
|
|
|
|
void writePrefix() override { output->writePrefix(); }
|
|
|
|
|
void writeSuffix() override { output->writeSuffix(); }
|
|
|
|
|
|
2013-10-25 14:56:47 +00:00
|
|
|
|
private:
|
|
|
|
|
BlockOutputStreamPtr output;
|
|
|
|
|
NamesAndTypesListPtr required_columns;
|
2014-09-30 03:08:47 +00:00
|
|
|
|
const ColumnDefaults & column_defaults;
|
|
|
|
|
Context context;
|
2015-04-10 00:43:08 +00:00
|
|
|
|
bool only_explicit_column_defaults;
|
2013-10-25 14:56:47 +00:00
|
|
|
|
};
|
|
|
|
|
|
2013-10-28 14:15:56 +00:00
|
|
|
|
|
2013-10-25 14:56:47 +00:00
|
|
|
|
}
|