mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-11-10 01:25:21 +00:00
Add tupleConcat function
This commit is contained in:
parent
abc48a0b73
commit
5403507950
@ -4,7 +4,7 @@ sidebar_position: 54
|
||||
sidebar_label: Tuple(T1, T2, ...)
|
||||
---
|
||||
|
||||
# Tuple(t1, T2, …)
|
||||
# Tuple(T1, T2, …)
|
||||
|
||||
A tuple of elements, each having an individual [type](../../sql-reference/data-types/index.md#data_types). Tuple must contain at least one element.
|
||||
|
||||
|
@ -183,9 +183,8 @@ arrayConcat(arrays)
|
||||
**Arguments**
|
||||
|
||||
- `arrays` – Arbitrary number of arguments of [Array](../../sql-reference/data-types/array.md) type.
|
||||
**Example**
|
||||
|
||||
<!-- -->
|
||||
**Example**
|
||||
|
||||
``` sql
|
||||
SELECT arrayConcat([1, 2], [3, 4], [5, 6]) AS res
|
||||
|
@ -559,6 +559,29 @@ Result:
|
||||
└────────────────────────────┘
|
||||
```
|
||||
|
||||
## tupleConcat
|
||||
|
||||
Combines tuples passed as arguments.
|
||||
|
||||
``` sql
|
||||
tupleConcat(tuples)
|
||||
```
|
||||
|
||||
**Arguments**
|
||||
|
||||
- `tuples` – Arbitrary number of arguments of [Tuple](../../sql-reference/data-types/tuple.md) type.
|
||||
|
||||
**Example**
|
||||
|
||||
``` sql
|
||||
SELECT tupleConcat((1, 2), (3, 4), (true, false)) AS res
|
||||
```
|
||||
|
||||
``` text
|
||||
┌─res──────────────────┐
|
||||
│ (1,2,3,4,true,false) │
|
||||
└──────────────────────┘
|
||||
```
|
||||
|
||||
## Distance functions
|
||||
|
||||
|
@ -208,6 +208,10 @@ public:
|
||||
{
|
||||
return FunctionFactory::instance().getImpl("mapConcat", context)->build(arguments);
|
||||
}
|
||||
else if (isTuple(arguments.at(0).type))
|
||||
{
|
||||
return FunctionFactory::instance().getImpl("tupleConcat", context)->build(arguments);
|
||||
}
|
||||
else
|
||||
return std::make_unique<FunctionToFunctionBaseAdaptor>(
|
||||
FunctionConcat::create(context), collections::map<DataTypes>(arguments, [](const auto & elem) { return elem.type; }), return_type);
|
||||
|
102
src/Functions/tupleConcat.cpp
Normal file
102
src/Functions/tupleConcat.cpp
Normal file
@ -0,0 +1,102 @@
|
||||
#include <Columns/ColumnTuple.h>
|
||||
#include <DataTypes/DataTypeTuple.h>
|
||||
#include <Functions/FunctionFactory.h>
|
||||
#include <Functions/FunctionHelpers.h>
|
||||
#include <Functions/IFunction.h>
|
||||
|
||||
namespace DB
|
||||
{
|
||||
namespace ErrorCodes
|
||||
{
|
||||
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
||||
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
|
||||
extern const int ILLEGAL_COLUMN;
|
||||
}
|
||||
|
||||
/// tupleConcat(tup1, ...) - concatenate tuples.
|
||||
class FunctionTupleConcat : public IFunction
|
||||
{
|
||||
public:
|
||||
static constexpr auto name = "tupleConcat";
|
||||
static FunctionPtr create(ContextPtr) { return std::make_shared<FunctionTupleConcat>(); }
|
||||
|
||||
String getName() const override { return name; }
|
||||
|
||||
bool isVariadic() const override { return true; }
|
||||
|
||||
size_t getNumberOfArguments() const override { return 0; }
|
||||
|
||||
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return true; }
|
||||
|
||||
bool useDefaultImplementationForConstants() const override { return true; }
|
||||
|
||||
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
||||
{
|
||||
if (arguments.empty())
|
||||
throw Exception(
|
||||
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH,
|
||||
"Function {} requires at least one argument.",
|
||||
getName());
|
||||
|
||||
DataTypes tuple_arg_types;
|
||||
|
||||
for (const auto arg_idx : collections::range(0, arguments.size()))
|
||||
{
|
||||
const auto * arg = arguments[arg_idx].get();
|
||||
if (!isTuple(arg))
|
||||
throw Exception(
|
||||
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
|
||||
"Illegal type {} of argument {} of function {}",
|
||||
arg->getName(),
|
||||
arg_idx + 1,
|
||||
getName());
|
||||
|
||||
const auto * type = checkAndGetDataType<DataTypeTuple>(arg);
|
||||
for (const auto & elem : type->getElements())
|
||||
tuple_arg_types.push_back(elem);
|
||||
}
|
||||
|
||||
return std::make_shared<DataTypeTuple>(tuple_arg_types);
|
||||
}
|
||||
|
||||
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t) const override
|
||||
{
|
||||
const size_t num_arguments = arguments.size();
|
||||
Columns columns;
|
||||
|
||||
for (size_t i = 0; i < num_arguments; i++)
|
||||
{
|
||||
const DataTypeTuple * arg_type = checkAndGetDataType<DataTypeTuple>(arguments[i].type.get());
|
||||
|
||||
if (!arg_type)
|
||||
throw Exception(
|
||||
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
|
||||
"Illegal type {} of argument {} of function {}",
|
||||
arguments[i].type->getName(),
|
||||
i + 1,
|
||||
getName());
|
||||
|
||||
ColumnPtr arg_col = arguments[i].column->convertToFullColumnIfConst();
|
||||
const ColumnTuple * tuple_col = checkAndGetColumn<ColumnTuple>(arg_col.get());
|
||||
|
||||
if (!tuple_col)
|
||||
throw Exception(
|
||||
ErrorCodes::ILLEGAL_COLUMN,
|
||||
"Illegal column {} of argument of function {}",
|
||||
arguments[i].column->getName(),
|
||||
getName());
|
||||
|
||||
for (const auto & inner_col : tuple_col->getColumns())
|
||||
columns.push_back(inner_col);
|
||||
}
|
||||
|
||||
return ColumnTuple::create(columns);
|
||||
}
|
||||
};
|
||||
|
||||
REGISTER_FUNCTION(TupleConcat)
|
||||
{
|
||||
factory.registerFunction<FunctionTupleConcat>();
|
||||
}
|
||||
|
||||
}
|
6
tests/queries/0_stateless/02833_tuple_concat.reference
Normal file
6
tests/queries/0_stateless/02833_tuple_concat.reference
Normal file
@ -0,0 +1,6 @@
|
||||
(1,'y',2,'n')
|
||||
(1,'y',2,'n',3,'n')
|
||||
(1,2,3,'a','b','c','2020-10-08','2020-11-08') 1 2 3 a b c 2020-10-08 2020-11-08
|
||||
(1,2,1,2) 1 2 1 2
|
||||
(1,2,3,4) 1 2 3 4
|
||||
(3,4,1,2) 3 4 1 2
|
23
tests/queries/0_stateless/02833_tuple_concat.sql
Normal file
23
tests/queries/0_stateless/02833_tuple_concat.sql
Normal file
@ -0,0 +1,23 @@
|
||||
SELECT tupleConcat(); -- { serverError NUMBER_OF_ARGUMENTS_DOESNT_MATCH }
|
||||
SELECT tupleConcat((1, 'y'), 1); -- { serverError ILLEGAL_TYPE_OF_ARGUMENT }
|
||||
|
||||
SELECT tupleConcat((1, 'y'), (2, 'n'));
|
||||
SELECT tupleConcat((1, 'y'), (2, 'n'), (3, 'n'));
|
||||
|
||||
WITH (1,2,3) || ('a','b','c') || ('2020-10-08'::Date, '2020-11-08'::Date) AS t
|
||||
SELECT t, t.1, t.2, t.3, t.4, t.5, t.6, t.7, t.8;
|
||||
|
||||
DROP TABLE IF EXISTS t_02833;
|
||||
CREATE TABLE t_02833 (tup Tuple(a UInt64, b UInt64)) ENGINE=Log;
|
||||
INSERT INTO t_02833 VALUES ((1, 2));
|
||||
|
||||
WITH (tup || tup) AS res
|
||||
SELECT res, res.1, res.2, res.3, res.4 FROM t_02833;
|
||||
|
||||
WITH (tup || (3, 4)) AS res
|
||||
SELECT res, res.1, res.2, res.3, res.4 FROM t_02833;
|
||||
|
||||
WITH ((3, 4) || tup) AS res
|
||||
SELECT res, res.1, res.2, res.3, res.4 FROM t_02833;
|
||||
|
||||
DROP TABLE t_02833;
|
Loading…
Reference in New Issue
Block a user