ClickHouse/src/Functions/map.cpp

141 lines
4.1 KiB
C++
Raw Normal View History

2020-11-02 06:46:32 +00:00
#include <Functions/IFunctionImpl.h>
#include <Functions/FunctionFactory.h>
#include <DataTypes/DataTypeMap.h>
#include <DataTypes/DataTypeTuple.h>
2020-12-03 03:52:41 +00:00
#include <DataTypes/DataTypesNumber.h>
2020-11-02 06:46:32 +00:00
#include <Columns/ColumnMap.h>
#include <Columns/ColumnArray.h>
#include <DataTypes/getLeastSupertype.h>
#include <Interpreters/castColumn.h>
#include <memory>
namespace DB
{
namespace ErrorCodes
{
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
}
namespace
{
2020-11-02 09:23:02 +00:00
// map(x, y, ...) is a function that allows you to make key-value pair
2020-11-02 06:46:32 +00:00
class FunctionMap : public IFunction
{
public:
static constexpr auto name = "map";
static FunctionPtr create(const Context &)
{
return std::make_shared<FunctionMap>();
}
String getName() const override
{
return name;
}
bool isVariadic() const override
{
return true;
}
size_t getNumberOfArguments() const override
{
return 0;
}
bool isInjective(const ColumnsWithTypeAndName &) const override
{
return true;
}
bool useDefaultImplementationForNulls() const override { return false; }
bool useDefaultImplementationForConstants() const override { return true; }
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
{
2020-12-15 14:34:37 +00:00
if (arguments.size() % 2 != 0)
throw Exception("Function " + getName() + " even number of arguments.", ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
2020-11-02 06:46:32 +00:00
DataTypes keys, values;
for (size_t i = 0; i < arguments.size(); i += 2)
{
keys.emplace_back(arguments[i]);
values.emplace_back(arguments[i + 1]);
}
DataTypes tmp;
tmp.emplace_back(getLeastSupertype(keys));
tmp.emplace_back(getLeastSupertype(values));
return std::make_shared<DataTypeMap>(tmp);
}
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type, size_t input_rows_count) const override
2020-11-02 06:46:32 +00:00
{
size_t num_elements = arguments.size();
if (num_elements == 0)
return result_type->createColumnConstWithDefaultValue(input_rows_count);
2020-12-03 03:52:41 +00:00
const auto & result_type_map = static_cast<const DataTypeMap &>(*result_type);
const DataTypePtr & key_type = result_type_map.getKeyType();
const DataTypePtr & value_type = result_type_map.getValueType();
2020-11-02 06:46:32 +00:00
Columns columns_holder(num_elements);
ColumnRawPtrs column_ptrs(num_elements);
for (size_t i = 0; i < num_elements; ++i)
{
const auto & arg = arguments[i];
2020-12-03 03:52:41 +00:00
const auto to_type = i % 2 == 0 ? key_type : value_type;
2020-11-02 06:46:32 +00:00
2020-12-03 03:52:41 +00:00
ColumnPtr preprocessed_column = castColumn(arg, to_type);
2020-11-02 06:46:32 +00:00
preprocessed_column = preprocessed_column->convertToFullColumnIfConst();
columns_holder[i] = std::move(preprocessed_column);
column_ptrs[i] = columns_holder[i].get();
}
/// Create and fill the result map.
2020-12-03 03:52:41 +00:00
MutableColumnPtr keys_data = key_type->createColumn();
MutableColumnPtr values_data = value_type->createColumn();
MutableColumnPtr offsets = DataTypeNumber<IColumn::Offset>().createColumn();
2020-11-02 06:46:32 +00:00
2020-12-03 03:52:41 +00:00
size_t total_elements = input_rows_count * num_elements / 2;
keys_data->reserve(total_elements);
values_data->reserve(total_elements);
offsets->reserve(input_rows_count);
2020-11-02 06:46:32 +00:00
IColumn::Offset current_offset = 0;
for (size_t i = 0; i < input_rows_count; ++i)
{
for (size_t j = 0; j < num_elements; j += 2)
{
2020-12-03 03:52:41 +00:00
keys_data->insertFrom(*column_ptrs[j], i);
values_data->insertFrom(*column_ptrs[j + 1], i);
2020-11-02 06:46:32 +00:00
}
current_offset += num_elements / 2;
2020-12-03 03:52:41 +00:00
offsets->insert(current_offset);
2020-11-02 06:46:32 +00:00
}
2020-12-03 03:52:41 +00:00
auto nested_column = ColumnArray::create(
ColumnTuple::create(Columns{std::move(keys_data), std::move(values_data)}),
std::move(offsets));
2020-11-02 06:46:32 +00:00
2020-12-03 03:52:41 +00:00
return ColumnMap::create(nested_column);
2020-11-02 06:46:32 +00:00
}
};
}
void registerFunctionsMap(FunctionFactory & factory)
{
factory.registerFunction<FunctionMap>();
}
}