Add more clear comments, refined code. [#CLICKHOUSE-2957]

This commit is contained in:
Vitaliy Lyudvichenko 2017-05-22 13:20:22 +03:00 committed by alexey-milovidov
parent 571af20a4f
commit 4fd357a311
2 changed files with 24 additions and 16 deletions

View File

@ -55,17 +55,17 @@ Block CastTypeBlockInputStream::readImpl()
if (it == cast_description.end()) if (it == cast_description.end())
{ {
// Leave the same column
res.insert(src_column); res.insert(src_column);
} }
else else
{ {
CastElement & cast_element = it->second; CastElement & cast_element = it->second;
size_t tmp_col = cast_element.tmp_col_offset; size_t tmp_col = cast_element.tmp_col_offset;
ColumnNumbers arguments{tmp_col, tmp_col + 1};
tmp_conversion_block.getByPosition(tmp_col).column = src_column.column;
cast_element.function->execute(tmp_conversion_block, arguments, tmp_col + 2); tmp_conversion_block.getByPosition(tmp_col).column = src_column.column;
cast_element.function->execute(tmp_conversion_block, ColumnNumbers{tmp_col, tmp_col + 1}, tmp_col + 2);
res.insert(tmp_conversion_block.getByPosition(tmp_col + 2)); res.insert(tmp_conversion_block.getByPosition(tmp_col + 2));
} }
} }
@ -93,22 +93,24 @@ void CastTypeBlockInputStream::initialize(const Block & src_block)
/// Force conversion if source and destination types is different. /// Force conversion if source and destination types is different.
if (!ref_column.type->equals(*src_column.type)) if (!ref_column.type->equals(*src_column.type))
{ {
ColumnWithTypeAndName src_columnn_copy = src_column.cloneEmpty(); ColumnWithTypeAndName res_type_name_column(std::make_shared<ColumnConstString>(1, ref_column.type->getName()), std::make_shared<DataTypeString>(), "");
ColumnWithTypeAndName alias_column(std::make_shared<ColumnConstString>(1, ref_column.type->getName()), std::make_shared<DataTypeString>(), ""); ColumnWithTypeAndName res_blank_column(nullptr, ref_column.type->clone(), src_column.name);
ColumnWithTypeAndName result_column(nullptr, ref_column.type->clone(), src_column.name);
DataTypePtr unused_return_type; /// Prepares function to execution
std::vector<ExpressionAction> unused_prerequisites;
ColumnsWithTypeAndName arguments{src_columnn_copy, alias_column};
/// Prepares function to execution. TODO It is not obvious.
auto cast_function = FunctionFactory::instance().get("CAST", context); auto cast_function = FunctionFactory::instance().get("CAST", context);
cast_function->getReturnTypeAndPrerequisites(arguments, unused_return_type, unused_prerequisites); {
DataTypePtr unused_return_type;
std::vector<ExpressionAction> unused_prerequisites;
ColumnsWithTypeAndName arguments{src_column, res_type_name_column};
cast_function->getReturnTypeAndPrerequisites(arguments, unused_return_type, unused_prerequisites);
}
/// Prefill arguments and result column for current CAST
tmp_conversion_block.insert(src_column); tmp_conversion_block.insert(src_column);
tmp_conversion_block.insert(alias_column); tmp_conversion_block.insert(res_type_name_column);
tmp_conversion_block.insert(result_column); tmp_conversion_block.insert(res_blank_column);
/// Index of src_column blank in tmp_conversion_block
size_t tmp_col_offset = cast_description.size() * 3; size_t tmp_col_offset = cast_description.size() * 3;
cast_description.emplace(src_col, CastElement(std::move(cast_function), tmp_col_offset)); cast_description.emplace(src_col, CastElement(std::move(cast_function), tmp_col_offset));
} }

View File

@ -27,20 +27,26 @@ private:
const Context & context; const Context & context;
Block ref_defenition; Block ref_defenition;
/// Initializes cast_description and prepares tmp_conversion_block
void initialize(const Block & src_block); void initialize(const Block & src_block);
bool initialized = false; bool initialized = false;
struct CastElement struct CastElement
{ {
/// Prepared function to do conversion
std::shared_ptr<IFunction> function; std::shared_ptr<IFunction> function;
/// Position of first function argument in tmp_conversion_block
size_t tmp_col_offset; size_t tmp_col_offset;
CastElement(std::shared_ptr<IFunction> && function_, size_t tmp_col_offset_); CastElement(std::shared_ptr<IFunction> && function_, size_t tmp_col_offset_);
}; };
/// Describes required conversions on source block /// Describes required conversions on source block
/// Contains column numbers in source block that should be converted
std::map<size_t, CastElement> cast_description; std::map<size_t, CastElement> cast_description;
/// Auxiliary block, stores arguments and results of required CAST calls
/// Auxiliary block, stores prefilled arguments and result for each CAST function in cast_description
/// 3 columns are allocated for each conversion: [blank of source column, column with res type name, blank of res column]
Block tmp_conversion_block; Block tmp_conversion_block;
}; };