ClickHouse/src/Functions/tuple.cpp

100 lines
2.6 KiB
C++
Raw Normal View History

#include <Functions/IFunctionImpl.h>
2018-09-10 00:58:04 +00:00
#include <Functions/FunctionFactory.h>
#include <DataTypes/DataTypeTuple.h>
#include <Columns/ColumnTuple.h>
#include <memory>
namespace DB
{
namespace ErrorCodes
{
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
}
2020-09-07 18:00:37 +00:00
namespace
{
2018-09-10 00:58:04 +00:00
/** tuple(x, y, ...) is a function that allows you to group several columns
* tupleElement(tuple, n) is a function that allows you to retrieve a column from tuple.
*/
class FunctionTuple : public IFunction
{
public:
static constexpr auto name = "tuple";
static FunctionPtr create(const Context &)
{
return std::make_shared<FunctionTuple>();
}
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
2018-09-10 00:58:04 +00:00
{
return true;
}
bool useDefaultImplementationForNulls() const override { return false; }
bool useDefaultImplementationForConstants() const override { return true; }
2020-10-21 18:17:27 +00:00
DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override
2018-09-10 00:58:04 +00:00
{
2020-03-09 03:38:43 +00:00
if (arguments.empty())
2018-09-10 00:58:04 +00:00
throw Exception("Function " + getName() + " requires at least one argument.", ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
2020-10-21 18:17:27 +00:00
DataTypes types;
Strings names;
for (const auto & argument : arguments)
{
types.emplace_back(argument.type);
names.emplace_back(argument.name);
}
/// Create named tuple if possible.
if (DataTypeTuple::canBeCreatedWithNames(names))
return std::make_shared<DataTypeTuple>(types, names);
return std::make_shared<DataTypeTuple>(types);
2018-09-10 00:58:04 +00:00
}
2020-10-14 14:04:50 +00:00
void executeImpl(ColumnsWithTypeAndName & columns, const ColumnNumbers & arguments, size_t result, size_t /*input_rows_count*/) const override
2018-09-10 00:58:04 +00:00
{
size_t tuple_size = arguments.size();
Columns tuple_columns(tuple_size);
for (size_t i = 0; i < tuple_size; ++i)
{
/** If tuple is mixed of constant and not constant columns,
* convert all to non-constant columns,
* because many places in code expect all non-constant columns in non-constant tuple.
*/
2020-10-14 14:04:50 +00:00
tuple_columns[i] = columns[arguments[i]].column->convertToFullColumnIfConst();
2018-09-10 00:58:04 +00:00
}
2020-10-14 14:04:50 +00:00
columns[result].column = ColumnTuple::create(tuple_columns);
2018-09-10 00:58:04 +00:00
}
};
2020-09-07 18:00:37 +00:00
}
2018-09-10 00:58:04 +00:00
void registerFunctionTuple(FunctionFactory & factory)
{
factory.registerFunction<FunctionTuple>();
}
}