2021-05-17 07:30:42 +00:00
|
|
|
#include <Functions/IFunction.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";
|
|
|
|
|
2021-06-01 12:20:52 +00:00
|
|
|
static FunctionPtr create(ContextPtr)
|
2018-09-10 00:58:04 +00:00
|
|
|
{
|
|
|
|
return std::make_shared<FunctionTuple>();
|
|
|
|
}
|
|
|
|
|
|
|
|
String getName() const override
|
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isVariadic() const override
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t getNumberOfArguments() const override
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-10-09 07:41:28 +00:00
|
|
|
bool isInjective(const ColumnsWithTypeAndName &) const override
|
2018-09-10 00:58:04 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-06-22 16:21:23 +00:00
|
|
|
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; }
|
2021-04-29 14:48:26 +00:00
|
|
|
|
2018-09-10 00:58:04 +00:00
|
|
|
bool useDefaultImplementationForNulls() const override { return false; }
|
2022-05-19 10:13:44 +00:00
|
|
|
/// tuple(..., Nothing, ...) -> Tuple(..., Nothing, ...)
|
2022-05-12 15:15:31 +00:00
|
|
|
bool useDefaultImplementationForNothing() const override { return false; }
|
2018-09-10 00:58:04 +00:00
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
2022-07-12 02:01:41 +00:00
|
|
|
bool useDefaultImplementationForLowCardinalityColumns() const override { return false; }
|
2018-09-10 00:58:04 +00:00
|
|
|
|
2022-03-28 18:44:53 +00:00
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & 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);
|
|
|
|
|
2022-03-28 18:44:53 +00:00
|
|
|
return std::make_shared<DataTypeTuple>(arguments);
|
2018-09-10 00:58:04 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 13:24:45 +00:00
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, 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,
|
2018-12-21 16:00:07 +00:00
|
|
|
* convert all to non-constant columns,
|
|
|
|
* because many places in code expect all non-constant columns in non-constant tuple.
|
|
|
|
*/
|
2020-10-19 15:27:41 +00:00
|
|
|
tuple_columns[i] = arguments[i].column->convertToFullColumnIfConst();
|
2018-09-10 00:58:04 +00:00
|
|
|
}
|
2020-10-19 15:27:41 +00:00
|
|
|
return 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>();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|