2017-04-01 09:19:00 +00:00
|
|
|
#include <DataStreams/AddingDefaultBlockOutputStream.h>
|
2017-03-11 00:27:59 +00:00
|
|
|
|
2017-12-07 12:09:55 +00:00
|
|
|
#include <Common/typeid_cast.h>
|
2017-12-25 18:58:39 +00:00
|
|
|
#include <DataTypes/NestedUtils.h>
|
2017-12-07 12:09:55 +00:00
|
|
|
#include <DataTypes/DataTypeArray.h>
|
|
|
|
#include <Columns/ColumnArray.h>
|
2018-07-10 13:43:01 +00:00
|
|
|
#include <Interpreters/evaluateMissingDefaults.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Core/Block.h>
|
2017-03-11 00:27:59 +00:00
|
|
|
|
2017-12-07 12:09:55 +00:00
|
|
|
|
2017-03-11 00:27:59 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
2017-12-07 12:09:55 +00:00
|
|
|
|
2018-02-19 00:45:32 +00:00
|
|
|
void AddingDefaultBlockOutputStream::write(const Block & block)
|
2017-03-11 00:27:59 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
Block res = block;
|
2017-03-11 00:27:59 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Adds not specified default values.
|
2018-03-10 00:11:39 +00:00
|
|
|
size_t rows = res.rows();
|
2017-12-07 12:09:55 +00:00
|
|
|
|
2018-03-10 00:11:39 +00:00
|
|
|
/// For missing columns of nested structure, you need to create not a column of empty arrays, but a column of arrays of correct lengths.
|
|
|
|
/// First, remember the offset columns for all arrays in the block.
|
|
|
|
std::map<String, ColumnPtr> offset_columns;
|
2017-12-07 12:09:55 +00:00
|
|
|
|
2018-03-10 00:11:39 +00:00
|
|
|
for (size_t i = 0, size = res.columns(); i < size; ++i)
|
|
|
|
{
|
|
|
|
const auto & elem = res.getByPosition(i);
|
2017-12-07 12:09:55 +00:00
|
|
|
|
2018-03-10 00:11:39 +00:00
|
|
|
if (const ColumnArray * array = typeid_cast<const ColumnArray *>(&*elem.column))
|
|
|
|
{
|
|
|
|
String offsets_name = Nested::extractTableName(elem.name);
|
|
|
|
auto & offsets_column = offset_columns[offsets_name];
|
2017-12-07 12:09:55 +00:00
|
|
|
|
2018-03-10 00:11:39 +00:00
|
|
|
/// If for some reason there are different offset columns for one nested structure, then we take nonempty.
|
|
|
|
if (!offsets_column || offsets_column->empty())
|
|
|
|
offsets_column = array->getOffsetsPtr();
|
2017-12-07 12:09:55 +00:00
|
|
|
}
|
2018-03-10 00:11:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto & requested_column : required_columns)
|
|
|
|
{
|
|
|
|
if (res.has(requested_column.name) || column_defaults.count(requested_column.name))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
ColumnWithTypeAndName column_to_add;
|
|
|
|
column_to_add.name = requested_column.name;
|
|
|
|
column_to_add.type = requested_column.type;
|
|
|
|
|
|
|
|
String offsets_name = Nested::extractTableName(column_to_add.name);
|
|
|
|
if (offset_columns.count(offsets_name))
|
|
|
|
{
|
|
|
|
ColumnPtr offsets_column = offset_columns[offsets_name];
|
|
|
|
DataTypePtr nested_type = typeid_cast<const DataTypeArray &>(*column_to_add.type).getNestedType();
|
|
|
|
UInt64 nested_rows = rows ? get<UInt64>((*offsets_column)[rows - 1]) : 0;
|
2017-12-07 12:09:55 +00:00
|
|
|
|
2018-03-10 00:11:39 +00:00
|
|
|
ColumnPtr nested_column = nested_type->createColumnConstWithDefaultValue(nested_rows)->convertToFullColumnIfConst();
|
|
|
|
column_to_add.column = ColumnArray::create(nested_column, offsets_column);
|
|
|
|
}
|
|
|
|
else
|
2017-12-07 12:09:55 +00:00
|
|
|
{
|
2018-03-10 00:11:39 +00:00
|
|
|
/** It is necessary to turn a constant column into a full column, since in part of blocks (from other parts),
|
|
|
|
* it can be full (or the interpreter may decide that it is constant everywhere).
|
|
|
|
*/
|
|
|
|
column_to_add.column = column_to_add.type->createColumnConstWithDefaultValue(rows)->convertToFullColumnIfConst();
|
2017-12-07 12:09:55 +00:00
|
|
|
}
|
2018-03-10 00:11:39 +00:00
|
|
|
|
|
|
|
res.insert(std::move(column_to_add));
|
2017-12-07 12:09:55 +00:00
|
|
|
}
|
2017-03-11 00:27:59 +00:00
|
|
|
|
2018-02-03 06:43:35 +00:00
|
|
|
/// Computes explicitly specified values (in column_defaults) by default.
|
|
|
|
evaluateMissingDefaults(res, required_columns, column_defaults, context);
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
output->write(res);
|
2017-03-11 00:27:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AddingDefaultBlockOutputStream::flush()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
output->flush();
|
2017-03-11 00:27:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AddingDefaultBlockOutputStream::writePrefix()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
output->writePrefix();
|
2017-03-11 00:27:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AddingDefaultBlockOutputStream::writeSuffix()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
output->writeSuffix();
|
2017-03-11 00:27:59 +00:00
|
|
|
}
|
2017-12-07 12:09:55 +00:00
|
|
|
|
2017-03-11 00:27:59 +00:00
|
|
|
}
|