2018-09-13 01:59:51 +00:00
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
|
|
|
#include <Columns/ColumnsNumber.h>
|
2019-06-21 15:31:37 +00:00
|
|
|
#include "FunctionArrayMapped.h"
|
2018-09-13 01:59:51 +00:00
|
|
|
#include <Functions/FunctionFactory.h>
|
2019-12-29 01:13:17 +00:00
|
|
|
|
2018-09-13 01:59:51 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2020-02-25 18:10:48 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int ILLEGAL_COLUMN;
|
|
|
|
}
|
2018-09-13 01:59:51 +00:00
|
|
|
|
|
|
|
/** arrayExists(x1,...,xn -> expression, array1,...,arrayn) - is the expression true for at least one array element.
|
|
|
|
* An overload of the form f(array) is available, which works in the same way as f(x -> x, array).
|
|
|
|
*/
|
|
|
|
struct ArrayExistsImpl
|
|
|
|
{
|
|
|
|
static bool needBoolean() { return true; }
|
|
|
|
static bool needExpression() { return false; }
|
|
|
|
static bool needOneArray() { return false; }
|
|
|
|
|
|
|
|
static DataTypePtr getReturnType(const DataTypePtr & /*expression_return*/, const DataTypePtr & /*array_element*/)
|
|
|
|
{
|
|
|
|
return std::make_shared<DataTypeUInt8>();
|
|
|
|
}
|
|
|
|
|
|
|
|
static ColumnPtr execute(const ColumnArray & array, ColumnPtr mapped)
|
|
|
|
{
|
|
|
|
const ColumnUInt8 * column_filter = typeid_cast<const ColumnUInt8 *>(&*mapped);
|
|
|
|
|
|
|
|
if (!column_filter)
|
|
|
|
{
|
2020-04-22 08:31:10 +00:00
|
|
|
const auto * column_filter_const = checkAndGetColumnConst<ColumnUInt8>(&*mapped);
|
2018-09-13 01:59:51 +00:00
|
|
|
|
|
|
|
if (!column_filter_const)
|
|
|
|
throw Exception("Unexpected type of filter column", ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
|
|
|
|
if (column_filter_const->getValue<UInt8>())
|
|
|
|
{
|
|
|
|
const IColumn::Offsets & offsets = array.getOffsets();
|
|
|
|
auto out_column = ColumnUInt8::create(offsets.size());
|
|
|
|
ColumnUInt8::Container & out_exists = out_column->getData();
|
|
|
|
|
|
|
|
size_t pos = 0;
|
|
|
|
for (size_t i = 0; i < offsets.size(); ++i)
|
|
|
|
{
|
|
|
|
out_exists[i] = offsets[i] - pos > 0;
|
|
|
|
pos = offsets[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
return out_column;
|
|
|
|
}
|
|
|
|
else
|
2018-10-22 08:54:54 +00:00
|
|
|
return DataTypeUInt8().createColumnConst(array.size(), 0u);
|
2018-09-13 01:59:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const IColumn::Filter & filter = column_filter->getData();
|
|
|
|
const IColumn::Offsets & offsets = array.getOffsets();
|
|
|
|
auto out_column = ColumnUInt8::create(offsets.size());
|
|
|
|
ColumnUInt8::Container & out_exists = out_column->getData();
|
|
|
|
|
|
|
|
size_t pos = 0;
|
|
|
|
for (size_t i = 0; i < offsets.size(); ++i)
|
|
|
|
{
|
|
|
|
UInt8 exists = 0;
|
|
|
|
for (; pos < offsets[i]; ++pos)
|
|
|
|
{
|
|
|
|
if (filter[pos])
|
|
|
|
{
|
|
|
|
exists = 1;
|
|
|
|
pos = offsets[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out_exists[i] = exists;
|
|
|
|
}
|
|
|
|
|
|
|
|
return out_column;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct NameArrayExists { static constexpr auto name = "arrayExists"; };
|
|
|
|
using FunctionArrayExists = FunctionArrayMapped<ArrayExistsImpl, NameArrayExists>;
|
|
|
|
|
|
|
|
void registerFunctionArrayExists(FunctionFactory & factory)
|
|
|
|
{
|
|
|
|
factory.registerFunction<FunctionArrayExists>();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|