2018-11-27 00:43:58 +00:00
|
|
|
#include <Interpreters/addMissingDefaults.h>
|
|
|
|
|
|
|
|
#include <Common/typeid_cast.h>
|
|
|
|
#include <DataTypes/NestedUtils.h>
|
|
|
|
#include <DataTypes/DataTypeArray.h>
|
|
|
|
#include <Columns/ColumnArray.h>
|
2020-02-17 15:44:13 +00:00
|
|
|
#include <Interpreters/inplaceBlockConversions.h>
|
2018-11-27 00:43:58 +00:00
|
|
|
#include <Core/Block.h>
|
2020-10-02 12:38:50 +00:00
|
|
|
#include <Storages/ColumnsDescription.h>
|
2021-02-04 20:36:50 +00:00
|
|
|
#include <Interpreters/ExpressionActions.h>
|
2021-02-05 11:41:44 +00:00
|
|
|
#include <Functions/IFunctionAdaptors.h>
|
|
|
|
#include <Functions/materialize.h>
|
2018-11-27 00:43:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2021-02-05 11:41:44 +00:00
|
|
|
ActionsDAGPtr addMissingDefaults(
|
|
|
|
const Block & header,
|
2020-10-02 12:38:50 +00:00
|
|
|
const NamesAndTypesList & required_columns,
|
|
|
|
const ColumnsDescription & columns,
|
2021-04-23 09:50:56 +00:00
|
|
|
ContextPtr context,
|
|
|
|
bool null_as_default)
|
2018-11-27 00:43:58 +00:00
|
|
|
{
|
2021-03-03 20:01:07 +00:00
|
|
|
auto actions = std::make_shared<ActionsDAG>(header.getColumnsWithTypeAndName());
|
|
|
|
auto & index = actions->getIndex();
|
|
|
|
|
2018-11-27 00:43:58 +00:00
|
|
|
/// For missing columns of nested structure, you need to create not a column of empty arrays, but a column of arrays of correct lengths.
|
|
|
|
/// First, remember the offset columns for all arrays in the block.
|
2021-03-03 20:01:07 +00:00
|
|
|
std::map<String, ActionsDAG::NodeRawConstPtrs> nested_groups;
|
2018-11-27 00:43:58 +00:00
|
|
|
|
2021-02-05 11:41:44 +00:00
|
|
|
for (size_t i = 0, size = header.columns(); i < size; ++i)
|
2018-11-27 00:43:58 +00:00
|
|
|
{
|
2021-02-05 11:41:44 +00:00
|
|
|
const auto & elem = header.getByPosition(i);
|
2018-11-27 00:43:58 +00:00
|
|
|
|
2021-02-05 11:41:44 +00:00
|
|
|
if (typeid_cast<const ColumnArray *>(&*elem.column))
|
2018-11-27 00:43:58 +00:00
|
|
|
{
|
|
|
|
String offsets_name = Nested::extractTableName(elem.name);
|
|
|
|
|
2021-02-05 11:41:44 +00:00
|
|
|
auto & group = nested_groups[offsets_name];
|
|
|
|
if (group.empty())
|
2021-03-03 20:01:07 +00:00
|
|
|
group.push_back(nullptr);
|
2021-02-05 11:41:44 +00:00
|
|
|
|
2021-03-03 20:01:07 +00:00
|
|
|
group.push_back(actions->getInputs()[i]);
|
2018-11-27 00:43:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-10 12:45:39 +00:00
|
|
|
FunctionOverloadResolverPtr func_builder_replicate = FunctionFactory::instance().get("replicate", context);
|
2018-11-27 00:43:58 +00:00
|
|
|
|
|
|
|
/// We take given columns from input block and missed columns without default value
|
|
|
|
/// (default and materialized will be computed later).
|
|
|
|
for (const auto & column : required_columns)
|
|
|
|
{
|
2021-02-05 11:41:44 +00:00
|
|
|
if (header.has(column.name))
|
2021-02-05 18:21:48 +00:00
|
|
|
continue;
|
2018-11-27 00:43:58 +00:00
|
|
|
|
2020-10-02 12:38:50 +00:00
|
|
|
if (columns.hasDefault(column.name))
|
2018-11-27 00:43:58 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
String offsets_name = Nested::extractTableName(column.name);
|
2022-01-13 09:54:07 +00:00
|
|
|
const auto * array_type = typeid_cast<const DataTypeArray *>(column.type.get());
|
2022-04-18 10:18:43 +00:00
|
|
|
if (array_type && nested_groups.contains(offsets_name))
|
2018-11-27 00:43:58 +00:00
|
|
|
{
|
2022-01-13 09:44:25 +00:00
|
|
|
const auto & nested_type = array_type->getNestedType();
|
2021-02-05 11:41:44 +00:00
|
|
|
ColumnPtr nested_column = nested_type->createColumnConstWithDefaultValue(0);
|
2022-03-02 17:22:12 +00:00
|
|
|
const auto & constant = actions->addColumn({nested_column, nested_type, column.name});
|
2018-11-27 00:43:58 +00:00
|
|
|
|
2021-02-05 11:41:44 +00:00
|
|
|
auto & group = nested_groups[offsets_name];
|
2021-03-03 20:01:07 +00:00
|
|
|
group[0] = &constant;
|
|
|
|
index.push_back(&actions->addFunction(func_builder_replicate, group, constant.result_name));
|
2021-02-05 11:41:44 +00:00
|
|
|
|
2018-11-27 00:43:58 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** It is necessary to turn a constant column into a full column, since in part of blocks (from other parts),
|
|
|
|
* it can be full (or the interpreter may decide that it is constant everywhere).
|
|
|
|
*/
|
2021-02-05 16:35:21 +00:00
|
|
|
auto new_column = column.type->createColumnConstWithDefaultValue(0);
|
2022-03-02 17:22:12 +00:00
|
|
|
const auto * col = &actions->addColumn({new_column, column.type, column.name});
|
2021-03-09 16:54:06 +00:00
|
|
|
index.push_back(&actions->materializeNode(*col));
|
2018-11-27 00:43:58 +00:00
|
|
|
}
|
|
|
|
|
2020-10-02 12:38:50 +00:00
|
|
|
/// Computes explicitly specified values by default and materialized columns.
|
2021-04-23 09:50:56 +00:00
|
|
|
if (auto dag = evaluateMissingDefaults(actions->getResultColumns(), required_columns, columns, context, true, null_as_default))
|
2021-02-05 11:41:44 +00:00
|
|
|
actions = ActionsDAG::merge(std::move(*actions), std::move(*dag));
|
2022-02-14 12:49:07 +00:00
|
|
|
|
|
|
|
/// Removes unused columns and reorders result.
|
|
|
|
actions->removeUnusedActions(required_columns.getNames(), false);
|
2022-02-14 13:53:32 +00:00
|
|
|
actions->addMaterializingOutputActions();
|
2021-02-05 11:41:44 +00:00
|
|
|
|
|
|
|
return actions;
|
2018-11-27 00:43:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|