forked from ClickHouse/ClickHouse
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request ClickHouse#54947 from amosbird/minmax-combinator
Introduce -ArgMin/-ArgMax combinators.
- Loading branch information
Showing
43 changed files
with
282 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
src/AggregateFunctions/Combinators/AggregateFunctionArgMinMax.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
#include "AggregateFunctionArgMinMax.h" | ||
#include "AggregateFunctionCombinatorFactory.h" | ||
|
||
#include <AggregateFunctions/AggregateFunctionMinMaxAny.h> | ||
#include <DataTypes/DataTypeDate.h> | ||
#include <DataTypes/DataTypeDateTime.h> | ||
#include <DataTypes/DataTypeString.h> | ||
|
||
namespace DB | ||
{ | ||
|
||
namespace ErrorCodes | ||
{ | ||
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH; | ||
} | ||
|
||
namespace | ||
{ | ||
template <template <typename> class Data> | ||
class AggregateFunctionCombinatorArgMinMax final : public IAggregateFunctionCombinator | ||
{ | ||
public: | ||
String getName() const override { return Data<SingleValueDataGeneric<>>::name(); } | ||
|
||
DataTypes transformArguments(const DataTypes & arguments) const override | ||
{ | ||
if (arguments.empty()) | ||
throw Exception( | ||
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH, | ||
"Incorrect number of arguments for aggregate function with {} suffix", | ||
getName()); | ||
|
||
return DataTypes(arguments.begin(), arguments.end() - 1); | ||
} | ||
|
||
AggregateFunctionPtr transformAggregateFunction( | ||
const AggregateFunctionPtr & nested_function, | ||
const AggregateFunctionProperties &, | ||
const DataTypes & arguments, | ||
const Array & params) const override | ||
{ | ||
const DataTypePtr & argument_type = arguments.back(); | ||
WhichDataType which(argument_type); | ||
#define DISPATCH(TYPE) \ | ||
if (which.idx == TypeIndex::TYPE) \ | ||
return std::make_shared<AggregateFunctionArgMinMax<Data<SingleValueDataFixed<TYPE>>>>(nested_function, arguments, params); /// NOLINT | ||
FOR_NUMERIC_TYPES(DISPATCH) | ||
#undef DISPATCH | ||
|
||
if (which.idx == TypeIndex::Date) | ||
return std::make_shared<AggregateFunctionArgMinMax<Data<SingleValueDataFixed<DataTypeDate::FieldType>>>>( | ||
nested_function, arguments, params); | ||
if (which.idx == TypeIndex::DateTime) | ||
return std::make_shared<AggregateFunctionArgMinMax<Data<SingleValueDataFixed<DataTypeDateTime::FieldType>>>>( | ||
nested_function, arguments, params); | ||
if (which.idx == TypeIndex::DateTime64) | ||
return std::make_shared<AggregateFunctionArgMinMax<Data<SingleValueDataFixed<DateTime64>>>>(nested_function, arguments, params); | ||
if (which.idx == TypeIndex::Decimal32) | ||
return std::make_shared<AggregateFunctionArgMinMax<Data<SingleValueDataFixed<Decimal32>>>>(nested_function, arguments, params); | ||
if (which.idx == TypeIndex::Decimal64) | ||
return std::make_shared<AggregateFunctionArgMinMax<Data<SingleValueDataFixed<Decimal64>>>>(nested_function, arguments, params); | ||
if (which.idx == TypeIndex::Decimal128) | ||
return std::make_shared<AggregateFunctionArgMinMax<Data<SingleValueDataFixed<Decimal128>>>>(nested_function, arguments, params); | ||
if (which.idx == TypeIndex::Decimal256) | ||
return std::make_shared<AggregateFunctionArgMinMax<Data<SingleValueDataFixed<Decimal256>>>>(nested_function, arguments, params); | ||
if (which.idx == TypeIndex::String) | ||
return std::make_shared<AggregateFunctionArgMinMax<Data<SingleValueDataString>>>(nested_function, arguments, params); | ||
|
||
return std::make_shared<AggregateFunctionArgMinMax<Data<SingleValueDataGeneric<>>>>(nested_function, arguments, params); | ||
} | ||
}; | ||
|
||
template <typename Data> | ||
struct AggregateFunctionArgMinDataCapitalized : AggregateFunctionMinData<Data> | ||
{ | ||
static const char * name() { return "ArgMin"; } | ||
}; | ||
|
||
template <typename Data> | ||
struct AggregateFunctionArgMaxDataCapitalized : AggregateFunctionMaxData<Data> | ||
{ | ||
static const char * name() { return "ArgMax"; } | ||
}; | ||
|
||
} | ||
|
||
void registerAggregateFunctionCombinatorMinMax(AggregateFunctionCombinatorFactory & factory) | ||
{ | ||
factory.registerCombinator(std::make_shared<AggregateFunctionCombinatorArgMinMax<AggregateFunctionArgMinDataCapitalized>>()); | ||
factory.registerCombinator(std::make_shared<AggregateFunctionCombinatorArgMinMax<AggregateFunctionArgMaxDataCapitalized>>()); | ||
} | ||
|
||
} |
111 changes: 111 additions & 0 deletions
111
src/AggregateFunctions/Combinators/AggregateFunctionArgMinMax.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
#pragma once | ||
|
||
#include <AggregateFunctions/IAggregateFunction.h> | ||
|
||
namespace DB | ||
{ | ||
|
||
template <typename Key> | ||
class AggregateFunctionArgMinMax final : public IAggregateFunctionHelper<AggregateFunctionArgMinMax<Key>> | ||
{ | ||
private: | ||
AggregateFunctionPtr nested_function; | ||
SerializationPtr serialization; | ||
size_t key_col; | ||
size_t key_offset; | ||
|
||
Key & key(AggregateDataPtr __restrict place) const { return *reinterpret_cast<Key *>(place + key_offset); } | ||
const Key & key(ConstAggregateDataPtr __restrict place) const { return *reinterpret_cast<const Key *>(place + key_offset); } | ||
|
||
public: | ||
AggregateFunctionArgMinMax(AggregateFunctionPtr nested_function_, const DataTypes & arguments, const Array & params) | ||
: IAggregateFunctionHelper<AggregateFunctionArgMinMax<Key>>{arguments, params, nested_function_->getResultType()} | ||
, nested_function{nested_function_} | ||
, serialization(arguments.back()->getDefaultSerialization()) | ||
, key_col{arguments.size() - 1} | ||
, key_offset{(nested_function->sizeOfData() + alignof(Key) - 1) / alignof(Key) * alignof(Key)} | ||
{ | ||
} | ||
|
||
String getName() const override { return nested_function->getName() + Key::name(); } | ||
|
||
bool isState() const override { return nested_function->isState(); } | ||
|
||
bool isVersioned() const override { return nested_function->isVersioned(); } | ||
|
||
size_t getVersionFromRevision(size_t revision) const override { return nested_function->getVersionFromRevision(revision); } | ||
|
||
size_t getDefaultVersion() const override { return nested_function->getDefaultVersion(); } | ||
|
||
bool allocatesMemoryInArena() const override { return nested_function->allocatesMemoryInArena() || Key::allocatesMemoryInArena(); } | ||
|
||
bool hasTrivialDestructor() const override { return nested_function->hasTrivialDestructor(); } | ||
|
||
size_t sizeOfData() const override { return key_offset + sizeof(Key); } | ||
|
||
size_t alignOfData() const override { return nested_function->alignOfData(); } | ||
|
||
void create(AggregateDataPtr __restrict place) const override | ||
{ | ||
nested_function->create(place); | ||
new (place + key_offset) Key; | ||
} | ||
|
||
void destroy(AggregateDataPtr __restrict place) const noexcept override { nested_function->destroy(place); } | ||
|
||
void destroyUpToState(AggregateDataPtr __restrict place) const noexcept override { nested_function->destroyUpToState(place); } | ||
|
||
void add(AggregateDataPtr __restrict place, const IColumn ** columns, size_t row_num, Arena * arena) const override | ||
{ | ||
if (key(place).changeIfBetter(*columns[key_col], row_num, arena)) | ||
{ | ||
nested_function->destroy(place); | ||
nested_function->create(place); | ||
nested_function->add(place, columns, row_num, arena); | ||
} | ||
else if (key(place).isEqualTo(*columns[key_col], row_num)) | ||
{ | ||
nested_function->add(place, columns, row_num, arena); | ||
} | ||
} | ||
|
||
void merge(AggregateDataPtr __restrict place, ConstAggregateDataPtr rhs, Arena * arena) const override | ||
{ | ||
if (key(place).changeIfBetter(key(rhs), arena)) | ||
{ | ||
nested_function->destroy(place); | ||
nested_function->create(place); | ||
nested_function->merge(place, rhs, arena); | ||
} | ||
else if (key(place).isEqualTo(key(rhs))) | ||
{ | ||
nested_function->merge(place, rhs, arena); | ||
} | ||
} | ||
|
||
void serialize(ConstAggregateDataPtr __restrict place, WriteBuffer & buf, std::optional<size_t> version) const override | ||
{ | ||
nested_function->serialize(place, buf, version); | ||
key(place).write(buf, *serialization); | ||
} | ||
|
||
void deserialize(AggregateDataPtr __restrict place, ReadBuffer & buf, std::optional<size_t> version, Arena * arena) const override | ||
{ | ||
nested_function->deserialize(place, buf, version, arena); | ||
key(place).read(buf, *serialization, arena); | ||
} | ||
|
||
void insertResultInto(AggregateDataPtr __restrict place, IColumn & to, Arena * arena) const override | ||
{ | ||
nested_function->insertResultInto(place, to, arena); | ||
} | ||
|
||
void insertMergeResultInto(AggregateDataPtr __restrict place, IColumn & to, Arena * arena) const override | ||
{ | ||
nested_function->insertMergeResultInto(place, to, arena); | ||
} | ||
|
||
AggregateFunctionPtr getNestedFunction() const override { return nested_function; } | ||
}; | ||
|
||
} |
6 changes: 3 additions & 3 deletions
6
...egateFunctions/AggregateFunctionArray.cpp → ...ns/Combinators/AggregateFunctionArray.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
4 changes: 2 additions & 2 deletions
4
...ns/AggregateFunctionCombinatorFactory.cpp → ...rs/AggregateFunctionCombinatorFactory.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 1 addition & 2 deletions
3
...ions/AggregateFunctionCombinatorFactory.h → ...tors/AggregateFunctionCombinatorFactory.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
...teFunctions/AggregateFunctionDistinct.cpp → ...Combinators/AggregateFunctionDistinct.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
5 changes: 3 additions & 2 deletions
5
...ateFunctions/AggregateFunctionForEach.cpp → .../Combinators/AggregateFunctionForEach.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
4 changes: 2 additions & 2 deletions
4
...ggregateFunctions/AggregateFunctionIf.cpp → ...tions/Combinators/AggregateFunctionIf.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
5 changes: 3 additions & 2 deletions
5
...gregateFunctions/AggregateFunctionMap.cpp → ...ions/Combinators/AggregateFunctionMap.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.