ClickHouse/src/Interpreters/inplaceBlockConversions.cpp

175 lines
6.3 KiB
C++
Raw Normal View History

2020-02-17 15:44:13 +00:00
#include "inplaceBlockConversions.h"
#include <Core/Block.h>
2020-01-15 13:00:08 +00:00
#include <Parsers/queryToString.h>
#include <Storages/ColumnDefault.h>
#include <Interpreters/SyntaxAnalyzer.h>
#include <Interpreters/ExpressionAnalyzer.h>
#include <Interpreters/ExpressionActions.h>
#include <Parsers/ASTExpressionList.h>
#include <Parsers/ASTWithAlias.h>
2020-01-15 13:00:08 +00:00
#include <Parsers/ASTIdentifier.h>
2020-02-19 14:58:06 +00:00
#include <Parsers/ASTLiteral.h>
#include <Parsers/ASTFunction.h>
2016-01-13 00:32:59 +00:00
#include <utility>
#include <DataTypes/DataTypesNumber.h>
#include <Interpreters/RequiredSourceColumnsVisitor.h>
2016-01-13 00:32:59 +00:00
namespace DB
{
2020-02-17 12:47:34 +00:00
namespace
{
2020-01-15 13:00:08 +00:00
ASTPtr defaultRequiredExpressions(Block & block, const NamesAndTypesList & required_columns, const ColumnDefaults & column_defaults)
2016-01-13 00:32:59 +00:00
{
ASTPtr default_expr_list = std::make_shared<ASTExpressionList>();
2016-01-13 00:32:59 +00:00
for (const auto & column : required_columns)
{
if (block.has(column.name))
continue;
2016-01-13 00:32:59 +00:00
const auto it = column_defaults.find(column.name);
2016-01-13 00:32:59 +00:00
if (it != column_defaults.end())
2020-02-19 12:52:27 +00:00
{
/// expressions must be cloned to prevent modification by the ExpressionAnalyzer
auto column_default_expr = it->second.expression->clone();
2020-03-03 14:27:43 +00:00
/// Our default may depend on columns with ALIAS as default expr which not present in block
/// we can easily add them from column_defaults struct
RequiredSourceColumnsVisitor::Data columns_context;
RequiredSourceColumnsVisitor(columns_context).visit(column_default_expr);
NameSet required_columns_names = columns_context.requiredColumns();
for (const auto & required_column_name : required_columns_names)
2020-03-03 14:27:43 +00:00
{
/// If we have such default column and it's alias than we should
/// add it into default_expression_list
if (auto rit = column_defaults.find(required_column_name);
rit != column_defaults.end() && rit->second.kind == ColumnDefaultKind::Alias)
2020-03-03 14:27:43 +00:00
{
default_expr_list->children.emplace_back(setAlias(rit->second.expression->clone(), required_column_name));
2020-03-03 14:27:43 +00:00
}
}
auto cast_func = makeASTFunction("CAST", column_default_expr, std::make_shared<ASTLiteral>(column.type->getName()));
2020-02-19 12:52:27 +00:00
default_expr_list->children.emplace_back(setAlias(cast_func, it->first));
}
}
2016-01-13 00:32:59 +00:00
if (default_expr_list->children.empty())
return nullptr;
return default_expr_list;
}
2020-01-15 13:00:08 +00:00
ASTPtr convertRequiredExpressions(Block & block, const NamesAndTypesList & required_columns)
{
2020-01-15 13:00:08 +00:00
ASTPtr conversion_expr_list = std::make_shared<ASTExpressionList>();
for (const auto & required_column : required_columns)
{
if (!block.has(required_column.name))
continue;
auto column_in_block = block.getByName(required_column.name);
if (column_in_block.type->equals(*required_column.type))
continue;
auto cast_func = makeASTFunction(
"CAST", std::make_shared<ASTIdentifier>(required_column.name), std::make_shared<ASTLiteral>(required_column.type->getName()));
conversion_expr_list->children.emplace_back(setAlias(cast_func, required_column.name));
2020-01-15 13:00:08 +00:00
}
return conversion_expr_list;
}
void executeExpressionsOnBlock(
Block & block,
ASTPtr expr_list,
bool save_unneeded_columns,
const NamesAndTypesList & required_columns,
const Context & context)
{
if (!expr_list)
return;
2016-01-13 00:32:59 +00:00
if (!save_unneeded_columns)
{
2020-01-15 13:00:08 +00:00
auto syntax_result = SyntaxAnalyzer(context).analyze(expr_list, block.getNamesAndTypesList());
ExpressionAnalyzer{expr_list, syntax_result, context}.getActions(true)->execute(block);
return;
}
/** ExpressionAnalyzer eliminates "unused" columns, in order to ensure their safety
* we are going to operate on a copy instead of the original block */
Block copy_block{block};
2020-01-15 13:00:08 +00:00
auto syntax_result = SyntaxAnalyzer(context).analyze(expr_list, block.getNamesAndTypesList());
auto expression_analyzer = ExpressionAnalyzer{expr_list, syntax_result, context};
auto required_source_columns = syntax_result->requiredSourceColumns();
auto rows_was = copy_block.rows();
// Delete all not needed columns in DEFAULT expression.
// They can intersect with columns added in PREWHERE
// test 00950_default_prewhere
// CLICKHOUSE-4523
for (const auto & delete_column : copy_block.getNamesAndTypesList())
{
if (std::find(required_source_columns.begin(), required_source_columns.end(), delete_column.name) == required_source_columns.end())
{
copy_block.erase(delete_column.name);
}
}
if (copy_block.columns() == 0)
{
// Add column to indicate block size in execute()
copy_block.insert({DataTypeUInt8().createColumnConst(rows_was, 0u), std::make_shared<DataTypeUInt8>(), "__dummy"});
}
expression_analyzer.getActions(true)->execute(copy_block);
2016-01-13 00:32:59 +00:00
/// move evaluated columns to the original block, materializing them at the same time
size_t pos = 0;
for (auto col = required_columns.begin(); col != required_columns.end(); ++col, ++pos)
{
if (copy_block.has(col->name))
{
auto evaluated_col = copy_block.getByName(col->name);
evaluated_col.column = evaluated_col.column->convertToFullColumnIfConst();
2016-01-13 00:32:59 +00:00
2020-01-15 13:00:08 +00:00
if (block.has(col->name))
block.getByName(col->name) = std::move(evaluated_col);
else
block.insert(pos, std::move(evaluated_col));
}
}
2016-01-13 00:32:59 +00:00
}
}
2020-01-15 13:00:08 +00:00
void performRequiredConversions(Block & block, const NamesAndTypesList & required_columns, const Context & context)
{
ASTPtr conversion_expr_list = convertRequiredExpressions(block, required_columns);
if (conversion_expr_list->children.empty())
return;
executeExpressionsOnBlock(block, conversion_expr_list, true, required_columns, context);
}
void evaluateMissingDefaults(Block & block,
const NamesAndTypesList & required_columns,
const ColumnDefaults & column_defaults,
const Context & context, bool save_unneeded_columns)
{
if (column_defaults.empty())
return;
ASTPtr default_expr_list = defaultRequiredExpressions(block, required_columns, column_defaults);
executeExpressionsOnBlock(block, default_expr_list, save_unneeded_columns, required_columns, context);
}
}