Try to remove some code.

This commit is contained in:
Nikolai Kochetov 2020-01-23 18:53:58 +03:00
parent 25ea3d83e2
commit 7d4d063f38
3 changed files with 11 additions and 44 deletions

View File

@ -1,11 +1,11 @@
#include <DataStreams/ConvertingBlockInputStream.h>
#include <Interpreters/addMissingDefaults.h>
#include <Interpreters/castColumn.h>
#include <Columns/ColumnConst.h>
#include <Common/assert_cast.h>
#include <Common/quoteString.h>
#include <Parsers/IAST.h>
namespace DB
{
@ -35,9 +35,8 @@ ConvertingBlockInputStream::ConvertingBlockInputStream(
const Context & context_,
const BlockInputStreamPtr & input,
const Block & result_header,
MatchColumnsMode mode,
const ColumnDefaults & column_defaults_)
: context(context_), header(result_header), column_defaults(column_defaults_), conversion(header.columns())
MatchColumnsMode mode)
: context(context_), header(result_header), conversion(header.columns())
{
children.emplace_back(input);
@ -66,18 +65,8 @@ ConvertingBlockInputStream::ConvertingBlockInputStream(
throw Exception("Cannot find column " + backQuote(res_elem.name) + " in source stream",
ErrorCodes::THERE_IS_NO_COLUMN);
break;
case MatchColumnsMode::NameOrDefault:
if (input_header.has(res_elem.name))
conversion[result_col_num] = input_header.getPositionByName(res_elem.name);
else
conversion[result_col_num] = USE_DEFAULT;
break;
}
if (conversion[result_col_num] == USE_DEFAULT)
continue;
const auto & src_elem = input_header.getByPosition(conversion[result_col_num]);
/// Check constants.
@ -104,7 +93,6 @@ ConvertingBlockInputStream::ConvertingBlockInputStream(
Block ConvertingBlockInputStream::readImpl()
{
Block src = children.back()->read();
std::set<size_t> default_columns;
if (!src)
return src;
@ -112,15 +100,8 @@ Block ConvertingBlockInputStream::readImpl()
Block res = header.cloneEmpty();
for (size_t res_pos = 0, size = conversion.size(); res_pos < size; ++res_pos)
{
auto & res_elem = res.getByPosition(res_pos);
if (conversion[res_pos] == USE_DEFAULT)
{
default_columns.insert(res_pos);
continue;
}
const auto & src_elem = src.getByPosition(conversion[res_pos]);
auto & res_elem = res.getByPosition(res_pos);
ColumnPtr converted = castColumnWithDiagnostic(src_elem, res_elem, context);
@ -129,16 +110,7 @@ Block ConvertingBlockInputStream::readImpl()
res_elem.column = std::move(converted);
}
// replace missing columns with default value or expression if any
if (!default_columns.empty())
{
res.erase(default_columns);
res = addMissingDefaults(res, header.getNamesAndTypesList(), column_defaults, context);
}
return res;
}
}

View File

@ -2,7 +2,6 @@
#include <unordered_map>
#include <DataStreams/IBlockInputStream.h>
#include <Storages/ColumnDefault.h>
namespace DB
@ -29,17 +28,14 @@ public:
/// Require same number of columns in source and result. Match columns by corresponding positions, regardless to names.
Position,
/// Find columns in source by their names. Allow excessive columns in source.
Name,
/// Find columns in source by their names if present else use the default. Allow excessive columns in source.
NameOrDefault
Name
};
ConvertingBlockInputStream(
const Context & context,
const BlockInputStreamPtr & input,
const Block & result_header,
MatchColumnsMode mode,
const ColumnDefaults & column_defaults = {});
MatchColumnsMode mode);
String getName() const override { return "Converting"; }
Block getHeader() const override { return header; }
@ -49,12 +45,9 @@ private:
const Context & context;
Block header;
/// Only used in NameOrDefault mode
const ColumnDefaults column_defaults;
/// How to construct result block. Position in source block, where to get each column.
using Conversion = std::vector<size_t>;
const size_t USE_DEFAULT = static_cast<size_t>(-1);
Conversion conversion;
};

View File

@ -13,6 +13,7 @@
#include <Storages/MergeTree/ReplicatedMergeTreeBlockOutputStream.h>
#include <Storages/StorageValues.h>
#include <Storages/LiveView/StorageLiveView.h>
#include "AddingMissedBlockInputStream.h"
namespace DB
{
@ -233,13 +234,14 @@ void PushingToViewsBlockOutputStream::process(const Block & block, size_t view_n
auto view_table = context.getTable(view.table_id);
in = std::make_shared<ConvertingBlockInputStream>(context, in, view.out->getHeader(), ConvertingBlockInputStream::MatchColumnsMode::Name);
/// Add defaults to inner table in case it was altered after creation.
if (auto * materialized_view = dynamic_cast<const StorageMaterializedView *>(view_table.get()))
{
StoragePtr inner_table = materialized_view->getTargetTable();
in = std::make_shared<ConvertingBlockInputStream>(context, in, view.out->getHeader(), ConvertingBlockInputStream::MatchColumnsMode::NameOrDefault, inner_table->getColumns().getDefaults());
in = std::make_shared<AddingMissedBlockInputStream>(in, view.out->getHeader(), inner_table->getColumns().getDefaults(), context);
}
else
in = std::make_shared<ConvertingBlockInputStream>(context, in, view.out->getHeader(), ConvertingBlockInputStream::MatchColumnsMode::Name);
}
else
in = std::make_shared<OneBlockInputStream>(block);