mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-11-18 05:32:52 +00:00
97f2a2213e
* Move some code outside dbms/src folder * Fix paths
91 lines
2.6 KiB
C++
91 lines
2.6 KiB
C++
#include <DataTypes/DataTypesNumber.h>
|
|
#include <Columns/ColumnsNumber.h>
|
|
#include "FunctionArrayMapped.h"
|
|
#include <Functions/FunctionFactory.h>
|
|
|
|
|
|
namespace DB
|
|
{
|
|
namespace ErrorCodes
|
|
{
|
|
extern const int ILLEGAL_COLUMN;
|
|
}
|
|
|
|
struct ArrayFirstIndexImpl
|
|
{
|
|
static bool needBoolean() { return false; }
|
|
static bool needExpression() { return true; }
|
|
static bool needOneArray() { return false; }
|
|
|
|
static DataTypePtr getReturnType(const DataTypePtr & /*expression_return*/, const DataTypePtr & /*array_element*/)
|
|
{
|
|
return std::make_shared<DataTypeUInt32>();
|
|
}
|
|
|
|
static ColumnPtr execute(const ColumnArray & array, ColumnPtr mapped)
|
|
{
|
|
auto column_filter = typeid_cast<const ColumnUInt8 *>(&*mapped);
|
|
|
|
if (!column_filter)
|
|
{
|
|
auto column_filter_const = checkAndGetColumnConst<ColumnUInt8>(&*mapped);
|
|
|
|
if (!column_filter_const)
|
|
throw Exception("Unexpected type of filter column", ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
if (column_filter_const->getValue<UInt8>())
|
|
{
|
|
const auto & offsets = array.getOffsets();
|
|
auto out_column = ColumnUInt32::create(offsets.size());
|
|
auto & out_index = out_column->getData();
|
|
|
|
size_t pos{};
|
|
for (size_t i = 0; i < offsets.size(); ++i)
|
|
{
|
|
out_index[i] = offsets[i] - pos > 0;
|
|
pos = offsets[i];
|
|
}
|
|
|
|
return out_column;
|
|
}
|
|
else
|
|
return DataTypeUInt32().createColumnConst(array.size(), 0u);
|
|
}
|
|
|
|
const auto & filter = column_filter->getData();
|
|
const auto & offsets = array.getOffsets();
|
|
auto out_column = ColumnUInt32::create(offsets.size());
|
|
auto & out_index = out_column->getData();
|
|
|
|
size_t pos{};
|
|
for (size_t i = 0; i < offsets.size(); ++i)
|
|
{
|
|
UInt32 index{};
|
|
for (size_t idx{1}; pos < offsets[i]; ++pos, ++idx)
|
|
{
|
|
if (filter[pos])
|
|
{
|
|
index = idx;
|
|
pos = offsets[i];
|
|
break;
|
|
}
|
|
}
|
|
|
|
out_index[i] = index;
|
|
}
|
|
|
|
return out_column;
|
|
}
|
|
};
|
|
|
|
struct NameArrayFirstIndex { static constexpr auto name = "arrayFirstIndex"; };
|
|
using FunctionArrayFirstIndex = FunctionArrayMapped<ArrayFirstIndexImpl, NameArrayFirstIndex>;
|
|
|
|
void registerFunctionArrayFirstIndex(FunctionFactory & factory)
|
|
{
|
|
factory.registerFunction<FunctionArrayFirstIndex>();
|
|
}
|
|
|
|
}
|
|
|