Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement arrayCompact #7328

Merged
merged 10 commits into from
Nov 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
96 changes: 96 additions & 0 deletions dbms/src/Functions/array/arrayCompact.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#include <DataTypes/DataTypesNumber.h>
#include <Columns/ColumnsNumber.h>
#include "FunctionArrayMapped.h"
#include <Functions/FunctionFactory.h>

namespace DB
{
/// arrayCompact(['a', 'a', 'b', 'b', 'a']) = ['a', 'b', 'a'] - compact arrays
namespace ErrorCodes
{
extern const int ILLEGAL_COLUMN;
}

struct ArrayCompactImpl
{
static bool useDefaultImplementationForConstants() { return true; }
static bool needBoolean() { return false; }
static bool needExpression() { return false; }
static bool needOneArray() { return false; }

static DataTypePtr getReturnType(const DataTypePtr & nested_type, const DataTypePtr & /*nested_type*/)
{
return std::make_shared<DataTypeArray>(nested_type);
}

template <typename T>
static bool executeType(const ColumnPtr & mapped, const ColumnArray & array, ColumnPtr & res_ptr)
{
const ColumnVector<T> * column = checkAndGetColumn<ColumnVector<T>>(&*mapped);

if (!column)
return false;

const IColumn::Offsets & offsets = array.getOffsets();
const typename ColumnVector<T>::Container & data = column->getData();
auto column_data = ColumnVector<T>::create(data.size());
typename ColumnVector<T>::Container & res_values = column_data->getData();
auto column_offsets = ColumnArray::ColumnOffsets::create(offsets.size());
IColumn::Offsets & res_offsets = column_offsets->getData();

size_t res_pos = 0;
size_t pos = 0;
for (size_t i = 0; i < offsets.size(); ++i)
{
if (pos < offsets[i])
{
res_values[res_pos] = data[pos];
for (++pos, ++res_pos; pos < offsets[i]; ++pos)
{
if (data[pos] != data[pos - 1])
{
res_values[res_pos++] = data[pos];
}
}
}
res_offsets[i] = res_pos;
}
for (size_t i = 0; i < data.size() - res_pos; ++i)
{
res_values.pop_back();
}
res_ptr = ColumnArray::create(std::move(column_data), std::move(column_offsets));
return true;
}

static ColumnPtr execute(const ColumnArray & array, ColumnPtr mapped)
{
ColumnPtr res;

if (executeType< UInt8 >(mapped, array, res) ||
executeType< UInt16>(mapped, array, res) ||
executeType< UInt32>(mapped, array, res) ||
executeType< UInt64>(mapped, array, res) ||
executeType< Int8 >(mapped, array, res) ||
executeType< Int16 >(mapped, array, res) ||
executeType< Int32 >(mapped, array, res) ||
executeType< Int64 >(mapped, array, res) ||
executeType<Float32>(mapped, array, res) ||
executeType<Float64>(mapped, array, res))
return res;
else
throw Exception("Unexpected column for arrayCompact: " + mapped->getName(), ErrorCodes::ILLEGAL_COLUMN);
}

};

struct NameArrayCompact { static constexpr auto name = "arrayCompact"; };
using FunctionArrayCompact = FunctionArrayMapped<ArrayCompactImpl, NameArrayCompact>;

void registerFunctionArrayCompact(FunctionFactory & factory)
{
factory.registerFunction<FunctionArrayCompact>();
}

}

2 changes: 2 additions & 0 deletions dbms/src/Functions/registerFunctionsHigherOrder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ void registerFunctionArrayFilter(FunctionFactory &);
void registerFunctionArrayCount(FunctionFactory &);
void registerFunctionArrayExists(FunctionFactory &);
void registerFunctionArrayAll(FunctionFactory &);
void registerFunctionArrayCompact(FunctionFactory &);
void registerFunctionArraySum(FunctionFactory &);
void registerFunctionArrayFirst(FunctionFactory &);
void registerFunctionArrayFirstIndex(FunctionFactory &);
Expand All @@ -24,6 +25,7 @@ void registerFunctionsHigherOrder(FunctionFactory & factory)
registerFunctionArrayCount(factory);
registerFunctionArrayExists(factory);
registerFunctionArrayAll(factory);
registerFunctionArrayCompact(factory);
registerFunctionArraySum(factory);
registerFunctionArrayFirst(factory);
registerFunctionArrayFirstIndex(factory);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[0]
[1]
[2]
[1]
[1,2]
[1,2]
[1,2,1]
[2,1]
[1,2,3,4,5]
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
select arrayCompact([0]);
select arrayCompact([1]);
select arrayCompact([2]);
select arrayCompact([1,1]);
select arrayCompact([1,2]);
select arrayCompact([1,1,2]);
select arrayCompact([1,2,1]);
select arrayCompact([2,1,1]);
select arrayCompact([1,2,2,3,3,3,4,4,4,4,5,5,5,5,5]);
19 changes: 18 additions & 1 deletion docs/en/query_language/functions/array_functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -789,5 +789,22 @@ SELECT arrayReverse([1, 2, 3])

Synonym for ["arrayReverse"](#array_functions-arrayreverse)


[Original article](https://clickhouse.yandex/docs/en/query_language/functions/array_functions/) <!--hide-->

## arrayCompact(arr) {#array_functions-arraycompact}

Takes an array, returns an array with consecutive duplicate elements removed.

Example:

```sql
SELECT arrayCompact([1, 2, 2, 3, 2, 3, 3])
```

```text
┌─arrayCompact([1, 2, 2, 3, 2, 3, 3])──┐
│ [1,2,3,2,3] │
└──────────────────────────────────────┘
```

##