2018-02-22 23:02:35 +00:00
|
|
|
#include <DataStreams/ConvertingBlockInputStream.h>
|
|
|
|
#include <Interpreters/castColumn.h>
|
|
|
|
#include <Columns/ColumnConst.h>
|
2019-08-21 02:28:04 +00:00
|
|
|
#include <Common/assert_cast.h>
|
2019-10-08 18:42:22 +00:00
|
|
|
#include <Common/quoteString.h>
|
2018-02-22 23:02:35 +00:00
|
|
|
#include <Parsers/IAST.h>
|
|
|
|
|
2020-01-23 15:53:58 +00:00
|
|
|
|
2018-02-22 23:02:35 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int THERE_IS_NO_COLUMN;
|
|
|
|
extern const int BLOCKS_HAVE_DIFFERENT_STRUCTURE;
|
2018-02-23 01:00:47 +00:00
|
|
|
extern const int NUMBER_OF_COLUMNS_DOESNT_MATCH;
|
2018-02-22 23:02:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-20 16:10:58 +00:00
|
|
|
static ColumnPtr castColumnWithDiagnostic(const ColumnWithTypeAndName & src_elem, const ColumnWithTypeAndName & res_elem, const Context & context)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return castColumn(src_elem, res_elem.type, context);
|
|
|
|
}
|
|
|
|
catch (Exception & e)
|
|
|
|
{
|
|
|
|
e.addMessage("while converting source column " + backQuoteIfNeed(src_elem.name) + " to destination column " + backQuoteIfNeed(res_elem.name));
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-02-22 23:02:35 +00:00
|
|
|
ConvertingBlockInputStream::ConvertingBlockInputStream(
|
|
|
|
const Context & context_,
|
|
|
|
const BlockInputStreamPtr & input,
|
2018-02-23 01:00:47 +00:00
|
|
|
const Block & result_header,
|
2020-01-23 15:53:58 +00:00
|
|
|
MatchColumnsMode mode)
|
|
|
|
: context(context_), header(result_header), conversion(header.columns())
|
2018-02-22 23:02:35 +00:00
|
|
|
{
|
|
|
|
children.emplace_back(input);
|
|
|
|
|
|
|
|
Block input_header = input->getHeader();
|
2018-02-23 01:00:47 +00:00
|
|
|
|
2018-02-22 23:02:35 +00:00
|
|
|
size_t num_input_columns = input_header.columns();
|
2018-02-23 01:00:47 +00:00
|
|
|
size_t num_result_columns = result_header.columns();
|
|
|
|
|
|
|
|
if (mode == MatchColumnsMode::Position && num_input_columns != num_result_columns)
|
|
|
|
throw Exception("Number of columns doesn't match", ErrorCodes::NUMBER_OF_COLUMNS_DOESNT_MATCH);
|
2018-02-22 23:02:35 +00:00
|
|
|
|
2018-02-23 01:00:47 +00:00
|
|
|
for (size_t result_col_num = 0; result_col_num < num_result_columns; ++result_col_num)
|
2018-02-22 23:02:35 +00:00
|
|
|
{
|
|
|
|
const auto & res_elem = result_header.getByPosition(result_col_num);
|
|
|
|
|
2018-02-23 01:00:47 +00:00
|
|
|
switch (mode)
|
|
|
|
{
|
|
|
|
case MatchColumnsMode::Position:
|
|
|
|
conversion[result_col_num] = result_col_num;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MatchColumnsMode::Name:
|
|
|
|
if (input_header.has(res_elem.name))
|
|
|
|
conversion[result_col_num] = input_header.getPositionByName(res_elem.name);
|
|
|
|
else
|
2019-05-27 21:01:24 +00:00
|
|
|
throw Exception("Cannot find column " + backQuote(res_elem.name) + " in source stream",
|
2018-02-23 01:00:47 +00:00
|
|
|
ErrorCodes::THERE_IS_NO_COLUMN);
|
|
|
|
break;
|
|
|
|
}
|
2018-02-22 23:02:35 +00:00
|
|
|
|
|
|
|
const auto & src_elem = input_header.getByPosition(conversion[result_col_num]);
|
|
|
|
|
|
|
|
/// Check constants.
|
|
|
|
|
2019-06-27 19:28:52 +00:00
|
|
|
if (isColumnConst(*res_elem.column))
|
2018-02-22 23:02:35 +00:00
|
|
|
{
|
2019-06-27 19:28:52 +00:00
|
|
|
if (!isColumnConst(*src_elem.column))
|
2018-02-22 23:02:35 +00:00
|
|
|
throw Exception("Cannot convert column " + backQuoteIfNeed(res_elem.name)
|
|
|
|
+ " because it is non constant in source stream but must be constant in result",
|
|
|
|
ErrorCodes::BLOCKS_HAVE_DIFFERENT_STRUCTURE);
|
2019-08-21 02:28:04 +00:00
|
|
|
else if (assert_cast<const ColumnConst &>(*src_elem.column).getField() != assert_cast<const ColumnConst &>(*res_elem.column).getField())
|
2018-02-22 23:02:35 +00:00
|
|
|
throw Exception("Cannot convert column " + backQuoteIfNeed(res_elem.name)
|
|
|
|
+ " because it is constant but values of constants are different in source and result",
|
|
|
|
ErrorCodes::BLOCKS_HAVE_DIFFERENT_STRUCTURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Check conversion by dry run CAST function.
|
|
|
|
|
2018-08-20 16:10:58 +00:00
|
|
|
castColumnWithDiagnostic(src_elem, res_elem, context);
|
2018-02-22 23:02:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Block ConvertingBlockInputStream::readImpl()
|
|
|
|
{
|
|
|
|
Block src = children.back()->read();
|
|
|
|
|
|
|
|
if (!src)
|
|
|
|
return src;
|
|
|
|
|
|
|
|
Block res = header.cloneEmpty();
|
2020-02-21 19:17:09 +00:00
|
|
|
/// This is important because header.cloneEmpty() doesn't copy info about aggregation bucket.
|
|
|
|
/// Otherwise information in buckets may be lost (and aggregation will return wrong result).
|
2020-02-21 16:57:22 +00:00
|
|
|
res.info = src.info;
|
|
|
|
|
2018-02-22 23:02:35 +00:00
|
|
|
for (size_t res_pos = 0, size = conversion.size(); res_pos < size; ++res_pos)
|
|
|
|
{
|
2020-01-22 21:26:08 +00:00
|
|
|
const auto & src_elem = src.getByPosition(conversion[res_pos]);
|
2020-01-23 15:53:58 +00:00
|
|
|
auto & res_elem = res.getByPosition(res_pos);
|
2020-01-22 21:26:08 +00:00
|
|
|
|
2018-08-20 16:10:58 +00:00
|
|
|
ColumnPtr converted = castColumnWithDiagnostic(src_elem, res_elem, context);
|
2018-02-22 23:02:35 +00:00
|
|
|
|
2019-06-27 19:28:52 +00:00
|
|
|
if (isColumnConst(*src_elem.column) && !isColumnConst(*res_elem.column))
|
2018-02-22 23:02:35 +00:00
|
|
|
converted = converted->convertToFullColumnIfConst();
|
|
|
|
|
|
|
|
res_elem.column = std::move(converted);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|