Reworking fix for missing columns.

This commit is contained in:
Vitaliy Zakaznikov 2020-01-22 16:15:42 +01:00
parent 78ab3127ce
commit 54c8234379
4 changed files with 14 additions and 28 deletions

View File

@ -5,6 +5,7 @@
#include <Common/quoteString.h> #include <Common/quoteString.h>
#include <Parsers/IAST.h> #include <Parsers/IAST.h>
namespace DB namespace DB
{ {
@ -64,18 +65,8 @@ ConvertingBlockInputStream::ConvertingBlockInputStream(
throw Exception("Cannot find column " + backQuote(res_elem.name) + " in source stream", throw Exception("Cannot find column " + backQuote(res_elem.name) + " in source stream",
ErrorCodes::THERE_IS_NO_COLUMN); ErrorCodes::THERE_IS_NO_COLUMN);
break; 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]); const auto & src_elem = input_header.getByPosition(conversion[result_col_num]);
/// Check constants. /// Check constants.
@ -109,17 +100,8 @@ Block ConvertingBlockInputStream::readImpl()
Block res = header.cloneEmpty(); Block res = header.cloneEmpty();
for (size_t res_pos = 0, size = conversion.size(); res_pos < size; ++res_pos) 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)
{
// Create a column with default values
auto column_with_defaults = res_elem.type->createColumn()->cloneResized(src.rows());
res_elem.column = std::move(column_with_defaults);
continue;
}
const auto & src_elem = src.getByPosition(conversion[res_pos]); const auto & src_elem = src.getByPosition(conversion[res_pos]);
auto & res_elem = res.getByPosition(res_pos);
ColumnPtr converted = castColumnWithDiagnostic(src_elem, res_elem, context); ColumnPtr converted = castColumnWithDiagnostic(src_elem, res_elem, context);
@ -132,4 +114,3 @@ Block ConvertingBlockInputStream::readImpl()
} }
} }

View File

@ -28,9 +28,7 @@ public:
/// Require same number of columns in source and result. Match columns by corresponding positions, regardless to names. /// Require same number of columns in source and result. Match columns by corresponding positions, regardless to names.
Position, Position,
/// Find columns in source by their names. Allow excessive columns in source. /// Find columns in source by their names. Allow excessive columns in source.
Name, Name
/// Find columns in source by their names if present else use the default. Allow excessive columns in source.
NameOrDefault
}; };
ConvertingBlockInputStream( ConvertingBlockInputStream(
@ -50,7 +48,6 @@ private:
/// How to construct result block. Position in source block, where to get each column. /// How to construct result block. Position in source block, where to get each column.
using Conversion = std::vector<size_t>; using Conversion = std::vector<size_t>;
const size_t USE_DEFAULT = static_cast<size_t>(-1);
Conversion conversion; Conversion conversion;
}; };

View File

@ -231,8 +231,16 @@ void PushingToViewsBlockOutputStream::process(const Block & block, size_t view_n
/// and two-level aggregation is triggered). /// and two-level aggregation is triggered).
in = std::make_shared<SquashingBlockInputStream>( in = std::make_shared<SquashingBlockInputStream>(
in, context.getSettingsRef().min_insert_block_size_rows, context.getSettingsRef().min_insert_block_size_bytes); in, context.getSettingsRef().min_insert_block_size_rows, context.getSettingsRef().min_insert_block_size_bytes);
in = std::make_shared<AddingMissedBlockInputStream>(
in, view.out->getHeader(), storage->getColumns().getDefaults(), local_context); auto view_table = context.getTable(view.table_id);
if (auto * materialized_view = dynamic_cast<const StorageMaterializedView *>(view_table.get()))
{
StoragePtr inner_table = materialized_view->getTargetTable();
in = std::make_shared<AddingMissedBlockInputStream>(
in, view.out->getHeader(), inner_table->getColumns().getDefaults(), context);
}
in = std::make_shared<ConvertingBlockInputStream>(context, in, view.out->getHeader(), ConvertingBlockInputStream::MatchColumnsMode::Name); in = std::make_shared<ConvertingBlockInputStream>(context, in, view.out->getHeader(), ConvertingBlockInputStream::MatchColumnsMode::Name);
} }
else else

View File

@ -2,7 +2,7 @@
1 1
2 2
3 3
1 0 1 2
1 2 1 2
2 3 2 3
3 4 3 4