mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-09-23 18:20:50 +00:00
Fixed build after merge [#CLICKHOUSE-3].
This commit is contained in:
parent
43daa720a4
commit
320b1e635b
@ -16,12 +16,14 @@
|
|||||||
#include <Common/HashTable/HashMap.h>
|
#include <Common/HashTable/HashMap.h>
|
||||||
#include <DataTypes/EnrichedDataTypePtr.h>
|
#include <DataTypes/EnrichedDataTypePtr.h>
|
||||||
|
|
||||||
|
|
||||||
namespace DB
|
namespace DB
|
||||||
{
|
{
|
||||||
|
|
||||||
namespace ErrorCodes
|
namespace ErrorCodes
|
||||||
{
|
{
|
||||||
extern const int BAD_ARGUMENTS;
|
extern const int BAD_ARGUMENTS;
|
||||||
|
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
|
||||||
}
|
}
|
||||||
|
|
||||||
enum ClusterOperation
|
enum ClusterOperation
|
||||||
@ -47,154 +49,147 @@ template<typename CentroidsType>
|
|||||||
class Centroids
|
class Centroids
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Centroids() {}
|
Centroids() {}
|
||||||
|
|
||||||
Centroids(const Centroids & c): centroids(c.centroids) {}
|
Centroids(const Centroids & c): centroids(c.centroids) {}
|
||||||
|
|
||||||
virtual ~Centroids() {}
|
virtual ~Centroids() {}
|
||||||
|
|
||||||
bool fill (const IColumn* centroids_array_untyped)
|
bool fill (const IColumn* centroids_array_untyped)
|
||||||
{
|
{
|
||||||
const ColumnArray * centroids_array = typeid_cast<const ColumnArray *>(centroids_array_untyped);
|
const ColumnArray * centroids_array = typeid_cast<const ColumnArray *>(centroids_array_untyped);
|
||||||
|
|
||||||
if (centroids_array)
|
if (centroids_array)
|
||||||
{
|
{
|
||||||
if (centroids_array->empty())
|
if (centroids_array->empty())
|
||||||
throw Exception{"Centroids array must be not empty", ErrorCodes::ILLEGAL_COLUMN};
|
throw Exception{"Centroids array must be not empty", ErrorCodes::ILLEGAL_COLUMN};
|
||||||
|
|
||||||
for (size_t k = 0; k < centroids_array->size(); k++)
|
for (size_t k = 0; k < centroids_array->size(); k++)
|
||||||
{
|
{
|
||||||
const Field& tmp_field = (*centroids_array)[k];
|
const Field& tmp_field = (*centroids_array)[k];
|
||||||
CentroidsType value;
|
CentroidsType value;
|
||||||
if (!tmp_field.tryGet(value))
|
if (!tmp_field.tryGet(value))
|
||||||
return false;
|
return false;
|
||||||
centroids.push_back(Float64(value));
|
centroids.push_back(Float64(value));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
const ColumnConst<Array> * const_centroids_array = typeid_cast<const ColumnConst<Array> *>(centroids_array_untyped);
|
const ColumnConst<Array> * const_centroids_array = typeid_cast<const ColumnConst<Array> *>(centroids_array_untyped);
|
||||||
|
|
||||||
if (!const_centroids_array)
|
if (!const_centroids_array)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (const_centroids_array->getData().empty())
|
if (const_centroids_array->getData().empty())
|
||||||
throw Exception{"Centroids array must be not empty", ErrorCodes::ILLEGAL_COLUMN};
|
throw Exception{"Centroids array must be not empty", ErrorCodes::ILLEGAL_COLUMN};
|
||||||
|
|
||||||
for (size_t k = 0; k < const_centroids_array->getData().size(); ++k)
|
for (size_t k = 0; k < const_centroids_array->getData().size(); ++k)
|
||||||
{
|
{
|
||||||
const Field& tmp_field = (const_centroids_array->getData())[k];
|
const Field& tmp_field = (const_centroids_array->getData())[k];
|
||||||
CentroidsType value;
|
CentroidsType value;
|
||||||
if (!tmp_field.tryGet(value))
|
if (!tmp_field.tryGet(value))
|
||||||
return false;
|
return false;
|
||||||
centroids.push_back(Float64(value));
|
centroids.push_back(Float64(value));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template <typename InputType>
|
template <typename InputType>
|
||||||
bool findCluster(
|
bool findCluster(
|
||||||
const IColumn* in_untyped,
|
const IColumn* in_untyped,
|
||||||
IColumn* out_untyped,
|
IColumn* out_untyped,
|
||||||
ClusterOperation operation)
|
ClusterOperation operation)
|
||||||
{
|
{
|
||||||
if (operation == ClusterOperation::FindClusterIndex)
|
if (operation == ClusterOperation::FindClusterIndex)
|
||||||
return findClusterTyped<InputType, UInt64>(in_untyped, out_untyped, operation);
|
return findClusterTyped<InputType, UInt64>(in_untyped, out_untyped, operation);
|
||||||
else if (operation == ClusterOperation::FindCentroidValue)
|
else if (operation == ClusterOperation::FindCentroidValue)
|
||||||
return findClusterTyped<InputType, CentroidsType>(in_untyped, out_untyped, operation);
|
return findClusterTyped<InputType, CentroidsType>(in_untyped, out_untyped, operation);
|
||||||
|
|
||||||
throw Exception{"Unexpected error in findCluster* function", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT};
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
throw Exception{"Unexpected error in findCluster* function", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT};
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::vector<Float64> centroids;
|
std::vector<Float64> centroids;
|
||||||
|
|
||||||
// Centroids array has the same size as number of clusters. We expect it
|
// Centroids array has the same size as number of clusters. We expect it
|
||||||
// to be small, maybe 10s or 100s in most real life situation, so we
|
// to be small, maybe 10s or 100s in most real life situation, so we
|
||||||
// choose the naive implementation
|
// choose the naive implementation
|
||||||
size_t find_centroid(CentroidsType x)
|
size_t find_centroid(CentroidsType x)
|
||||||
{
|
{
|
||||||
Float64 y = Float64(x);
|
Float64 y = Float64(x);
|
||||||
|
|
||||||
// Centroids array has to have at least one element, and if it has only one element,
|
// Centroids array has to have at least one element, and if it has only one element,
|
||||||
// it is also the result of this Function.
|
// it is also the result of this Function.
|
||||||
Float64 distance = abs(centroids[0]-y);
|
Float64 distance = abs(centroids[0]-y);
|
||||||
size_t index = 0;
|
size_t index = 0;
|
||||||
|
|
||||||
// Check if we have more clusters and if we have, whether some is closer to src[i]
|
// Check if we have more clusters and if we have, whether some is closer to src[i]
|
||||||
for (size_t j = 1; j < centroids.size(); ++j)
|
for (size_t j = 1; j < centroids.size(); ++j)
|
||||||
{
|
{
|
||||||
Float64 next_distance = abs(centroids[j]-y);
|
Float64 next_distance = abs(centroids[j]-y);
|
||||||
|
|
||||||
if (next_distance < distance)
|
if (next_distance < distance)
|
||||||
{
|
{
|
||||||
distance = next_distance;
|
distance = next_distance;
|
||||||
index = j;
|
index = j;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Index of the closest cluster, or 0 in case of just one cluster
|
|
||||||
return index;
|
|
||||||
|
|
||||||
|
// Index of the closest cluster, or 0 in case of just one cluster
|
||||||
|
return index;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename InputType, typename OutputType>
|
template <typename InputType, typename OutputType>
|
||||||
bool findClusterTyped(
|
bool findClusterTyped(
|
||||||
const IColumn* in_untyped,
|
const IColumn* in_untyped,
|
||||||
IColumn* out_untyped,
|
IColumn* out_untyped,
|
||||||
ClusterOperation operation)
|
ClusterOperation operation)
|
||||||
{
|
{
|
||||||
ColumnVector<OutputType> * out = typeid_cast<ColumnVector<OutputType> *>(out_untyped);
|
ColumnVector<OutputType> * out = typeid_cast<ColumnVector<OutputType> *>(out_untyped);
|
||||||
|
|
||||||
if (!out)
|
if (!out)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
PaddedPODArray<OutputType> & dst = out->getData();
|
PaddedPODArray<OutputType> & dst = out->getData();
|
||||||
|
|
||||||
|
|
||||||
const auto in_vector = typeid_cast<const ColumnVector<InputType> *>(in_untyped);
|
const auto in_vector = typeid_cast<const ColumnVector<InputType> *>(in_untyped);
|
||||||
if (in_vector)
|
if (in_vector)
|
||||||
{
|
{
|
||||||
const PaddedPODArray<InputType> & src = in_vector->getData();
|
const PaddedPODArray<InputType> & src = in_vector->getData();
|
||||||
|
|
||||||
if (operation == ClusterOperation::FindClusterIndex)
|
if (operation == ClusterOperation::FindClusterIndex)
|
||||||
for (size_t i = 0; i < src.size(); ++i)
|
for (size_t i = 0; i < src.size(); ++i)
|
||||||
// Note that array indexes start with 1 in Clickhouse
|
// Note that array indexes start with 1 in Clickhouse
|
||||||
dst.push_back(UInt64(find_centroid(CentroidsType(src[i]))+1));
|
dst.push_back(UInt64(find_centroid(CentroidsType(src[i]))+1));
|
||||||
else if (operation == ClusterOperation::FindCentroidValue)
|
else if (operation == ClusterOperation::FindCentroidValue)
|
||||||
for (size_t i = 0; i < src.size(); ++i)
|
for (size_t i = 0; i < src.size(); ++i)
|
||||||
dst.push_back(centroids[find_centroid(CentroidsType(src[i]))]);
|
dst.push_back(centroids[find_centroid(CentroidsType(src[i]))]);
|
||||||
else
|
else
|
||||||
throw Exception{"Unexpected error in findCluster* function", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT};
|
throw Exception{"Unexpected error in findCluster* function", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT};
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
const auto in_const = typeid_cast<const ColumnConst<InputType> *>(in_untyped);
|
||||||
|
|
||||||
}
|
if (!in_const)
|
||||||
else
|
return false;
|
||||||
{
|
|
||||||
const auto in_const = typeid_cast<const ColumnConst<InputType> *>(in_untyped);
|
|
||||||
|
|
||||||
if (!in_const)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
if (operation == ClusterOperation::FindClusterIndex)
|
|
||||||
// Note that array indexes start with 1 in Clickhouse
|
|
||||||
dst.push_back(UInt64(find_centroid(CentroidsType(in_const->getData()))+1));
|
|
||||||
else if (operation == ClusterOperation::FindCentroidValue)
|
|
||||||
dst.push_back(centroids[find_centroid(CentroidsType(in_const->getData()))]);
|
|
||||||
else
|
|
||||||
throw Exception{"Unexpected error in findCluster* function", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT};
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if (operation == ClusterOperation::FindClusterIndex)
|
||||||
|
// Note that array indexes start with 1 in Clickhouse
|
||||||
|
dst.push_back(UInt64(find_centroid(CentroidsType(in_const->getData()))+1));
|
||||||
|
else if (operation == ClusterOperation::FindCentroidValue)
|
||||||
|
dst.push_back(centroids[find_centroid(CentroidsType(in_const->getData()))]);
|
||||||
|
else
|
||||||
|
throw Exception{"Unexpected error in findCluster* function", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT};
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@ -211,7 +206,7 @@ private:
|
|||||||
class FunctionFindClusterIndex : public IFunction
|
class FunctionFindClusterIndex : public IFunction
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static constexpr auto name = "findClusterIndex";
|
static constexpr auto name = "findClusterIndex";
|
||||||
static FunctionPtr create(const Context &) { return std::make_shared<FunctionFindClusterIndex>(); }
|
static FunctionPtr create(const Context &) { return std::make_shared<FunctionFindClusterIndex>(); }
|
||||||
|
|
||||||
String getName() const override
|
String getName() const override
|
||||||
@ -245,7 +240,7 @@ public:
|
|||||||
throw Exception{"Second argument of function " + getName()
|
throw Exception{"Second argument of function " + getName()
|
||||||
+ ", must be array.", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT};
|
+ ", must be array.", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT};
|
||||||
|
|
||||||
return std::make_shared<DataTypeUInt64>();
|
return std::make_shared<DataTypeUInt64>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -257,16 +252,16 @@ public:
|
|||||||
auto out_untyped = column_result.get();
|
auto out_untyped = column_result.get();
|
||||||
|
|
||||||
if ( !executeByCentroidsType<UInt8>(in_untyped, out_untyped, centroids_array_untyped)
|
if ( !executeByCentroidsType<UInt8>(in_untyped, out_untyped, centroids_array_untyped)
|
||||||
&& !executeByCentroidsType<UInt16>(in_untyped, out_untyped, centroids_array_untyped)
|
&& !executeByCentroidsType<UInt16>(in_untyped, out_untyped, centroids_array_untyped)
|
||||||
&& !executeByCentroidsType<UInt32>(in_untyped, out_untyped, centroids_array_untyped)
|
&& !executeByCentroidsType<UInt32>(in_untyped, out_untyped, centroids_array_untyped)
|
||||||
&& !executeByCentroidsType<UInt64>(in_untyped, out_untyped, centroids_array_untyped)
|
&& !executeByCentroidsType<UInt64>(in_untyped, out_untyped, centroids_array_untyped)
|
||||||
&& !executeByCentroidsType<Int8>(in_untyped, out_untyped, centroids_array_untyped)
|
&& !executeByCentroidsType<Int8>(in_untyped, out_untyped, centroids_array_untyped)
|
||||||
&& !executeByCentroidsType<Int16>(in_untyped, out_untyped, centroids_array_untyped)
|
&& !executeByCentroidsType<Int16>(in_untyped, out_untyped, centroids_array_untyped)
|
||||||
&& !executeByCentroidsType<Int32>(in_untyped, out_untyped, centroids_array_untyped)
|
&& !executeByCentroidsType<Int32>(in_untyped, out_untyped, centroids_array_untyped)
|
||||||
&& !executeByCentroidsType<Int64>(in_untyped, out_untyped, centroids_array_untyped)
|
&& !executeByCentroidsType<Int64>(in_untyped, out_untyped, centroids_array_untyped)
|
||||||
&& !executeByCentroidsType<Float32>(in_untyped, out_untyped, centroids_array_untyped)
|
&& !executeByCentroidsType<Float32>(in_untyped, out_untyped, centroids_array_untyped)
|
||||||
&& !executeByCentroidsType<Float64>(in_untyped, out_untyped, centroids_array_untyped)
|
&& !executeByCentroidsType<Float64>(in_untyped, out_untyped, centroids_array_untyped)
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
throw Exception{
|
throw Exception{
|
||||||
"Function " + getName() + " expects centroids_array of a numeric type",
|
"Function " + getName() + " expects centroids_array of a numeric type",
|
||||||
@ -278,32 +273,32 @@ public:
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual ClusterOperation getOperation()
|
virtual ClusterOperation getOperation()
|
||||||
{
|
{
|
||||||
return ClusterOperation::FindClusterIndex;
|
return ClusterOperation::FindClusterIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename CentroidsType>
|
template <typename CentroidsType>
|
||||||
bool executeByCentroidsType(
|
bool executeByCentroidsType(
|
||||||
const IColumn* in_untyped,
|
const IColumn* in_untyped,
|
||||||
IColumn* out_untyped,
|
IColumn* out_untyped,
|
||||||
const IColumn* centroids_array_untyped)
|
const IColumn* centroids_array_untyped)
|
||||||
{
|
{
|
||||||
Centroids<typename NearestFieldType<CentroidsType>::Type> centroids;
|
Centroids<typename NearestFieldType<CentroidsType>::Type> centroids;
|
||||||
|
|
||||||
if (!centroids.fill(centroids_array_untyped))
|
if (!centroids.fill(centroids_array_untyped))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if ( !centroids.template findCluster<UInt8>(in_untyped, out_untyped, getOperation())
|
if ( !centroids.template findCluster<UInt8>(in_untyped, out_untyped, getOperation())
|
||||||
&& !centroids.template findCluster<UInt16>(in_untyped, out_untyped, getOperation())
|
&& !centroids.template findCluster<UInt16>(in_untyped, out_untyped, getOperation())
|
||||||
&& !centroids.template findCluster<UInt32>(in_untyped, out_untyped, getOperation())
|
&& !centroids.template findCluster<UInt32>(in_untyped, out_untyped, getOperation())
|
||||||
&& !centroids.template findCluster<UInt64>(in_untyped, out_untyped, getOperation())
|
&& !centroids.template findCluster<UInt64>(in_untyped, out_untyped, getOperation())
|
||||||
&& !centroids.template findCluster<Int8>(in_untyped, out_untyped, getOperation())
|
&& !centroids.template findCluster<Int8>(in_untyped, out_untyped, getOperation())
|
||||||
&& !centroids.template findCluster<Int16>(in_untyped, out_untyped, getOperation())
|
&& !centroids.template findCluster<Int16>(in_untyped, out_untyped, getOperation())
|
||||||
&& !centroids.template findCluster<Int32>(in_untyped, out_untyped, getOperation())
|
&& !centroids.template findCluster<Int32>(in_untyped, out_untyped, getOperation())
|
||||||
&& !centroids.template findCluster<Int64>(in_untyped, out_untyped, getOperation())
|
&& !centroids.template findCluster<Int64>(in_untyped, out_untyped, getOperation())
|
||||||
&& !centroids.template findCluster<Float32>(in_untyped, out_untyped, getOperation())
|
&& !centroids.template findCluster<Float32>(in_untyped, out_untyped, getOperation())
|
||||||
&& !centroids.template findCluster<Float64>(in_untyped, out_untyped, getOperation())
|
&& !centroids.template findCluster<Float64>(in_untyped, out_untyped, getOperation())
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
throw Exception{
|
throw Exception{
|
||||||
"Illegal column " + in_untyped->getName() + " of first argument of function " + getName(),
|
"Illegal column " + in_untyped->getName() + " of first argument of function " + getName(),
|
||||||
@ -315,18 +310,19 @@ protected:
|
|||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
class FunctionFindClusterValue : public FunctionFindClusterIndex
|
class FunctionFindClusterValue : public FunctionFindClusterIndex
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static constexpr auto name = "findClusterValue";
|
static constexpr auto name = "findClusterValue";
|
||||||
static FunctionPtr create(const Context &) { return std::make_shared<FunctionFindClusterValue>(); }
|
static FunctionPtr create(const Context &) { return std::make_shared<FunctionFindClusterValue>(); }
|
||||||
|
|
||||||
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
||||||
{
|
{
|
||||||
FunctionFindClusterIndex::getReturnTypeImpl(arguments);
|
FunctionFindClusterIndex::getReturnTypeImpl(arguments);
|
||||||
const DataTypeArray * type_arr_from = typeid_cast<const DataTypeArray *>(arguments[1].get());
|
const DataTypeArray * type_arr_from = typeid_cast<const DataTypeArray *>(arguments[1].get());
|
||||||
return type_arr_from->getNestedType();
|
return type_arr_from->getNestedType();
|
||||||
}
|
}
|
||||||
|
|
||||||
String getName() const override
|
String getName() const override
|
||||||
{
|
{
|
||||||
@ -335,9 +331,9 @@ public:
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
ClusterOperation getOperation() override
|
ClusterOperation getOperation() override
|
||||||
{
|
{
|
||||||
return ClusterOperation::FindCentroidValue;
|
return ClusterOperation::FindCentroidValue;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user