-
Notifications
You must be signed in to change notification settings - Fork 6.6k
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
Add aggregate function combinators: -OrNull & -OrDefault #7331
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#include <AggregateFunctions/AggregateFunctionOrFill.h> | ||
|
||
#include <AggregateFunctions/AggregateFunctionCombinatorFactory.h> | ||
|
||
|
||
namespace DB | ||
{ | ||
|
||
template <bool UseNull> | ||
class AggregateFunctionCombinatorOrFill final : public IAggregateFunctionCombinator | ||
{ | ||
public: | ||
String getName() const override | ||
{ | ||
if constexpr (UseNull) | ||
return "OrNull"; | ||
else | ||
return "OrDefault"; | ||
} | ||
|
||
AggregateFunctionPtr transformAggregateFunction( | ||
const AggregateFunctionPtr & nested_function, | ||
const DataTypes & arguments, | ||
const Array & params) const override | ||
{ | ||
return std::make_shared<AggregateFunctionOrFill<UseNull>>( | ||
nested_function, | ||
arguments, | ||
params); | ||
} | ||
}; | ||
|
||
void registerAggregateFunctionCombinatorOrFill(AggregateFunctionCombinatorFactory & factory) | ||
{ | ||
factory.registerCombinator(std::make_shared<AggregateFunctionCombinatorOrFill<false>>()); | ||
factory.registerCombinator(std::make_shared<AggregateFunctionCombinatorOrFill<true>>()); | ||
} | ||
|
||
} |
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,179 @@ | ||
#pragma once | ||
|
||
#include <AggregateFunctions/IAggregateFunction.h> | ||
#include <Columns/ColumnNullable.h> | ||
#include <Common/typeid_cast.h> | ||
#include <DataTypes/DataTypeNullable.h> | ||
|
||
|
||
namespace DB | ||
{ | ||
|
||
namespace ErrorCodes | ||
{ | ||
extern const int ARGUMENT_OUT_OF_BOUND; | ||
} | ||
|
||
/** | ||
* -OrDefault and -OrNull combinators for aggregate functions. | ||
* If there are no input values, return NULL or a default value, accordingly. | ||
* Use a single additional byte of data after the nested function data: | ||
* 0 means there was no input, 1 means there was some. | ||
*/ | ||
template <bool UseNull> | ||
class AggregateFunctionOrFill final : public IAggregateFunctionHelper<AggregateFunctionOrFill<UseNull>> | ||
{ | ||
private: | ||
AggregateFunctionPtr nested_function; | ||
|
||
size_t size_of_data; | ||
DataTypePtr inner_type; | ||
bool inner_nullable; | ||
|
||
public: | ||
AggregateFunctionOrFill(AggregateFunctionPtr nested_function_, const DataTypes & arguments, const Array & params) | ||
: IAggregateFunctionHelper<AggregateFunctionOrFill>{arguments, params} | ||
, nested_function{nested_function_} | ||
, size_of_data {nested_function->sizeOfData()} | ||
, inner_type {nested_function->getReturnType()} | ||
, inner_nullable {inner_type->isNullable()} | ||
{ | ||
// nothing | ||
} | ||
|
||
String getName() const override | ||
{ | ||
if constexpr (UseNull) | ||
return nested_function->getName() + "OrNull"; | ||
else | ||
return nested_function->getName() + "OrDefault"; | ||
} | ||
|
||
const char * getHeaderFilePath() const override | ||
{ | ||
return __FILE__; | ||
} | ||
|
||
bool isState() const override | ||
{ | ||
return nested_function->isState(); | ||
} | ||
|
||
bool allocatesMemoryInArena() const override | ||
{ | ||
return nested_function->allocatesMemoryInArena(); | ||
} | ||
|
||
bool hasTrivialDestructor() const override | ||
{ | ||
return nested_function->hasTrivialDestructor(); | ||
} | ||
|
||
size_t sizeOfData() const override | ||
{ | ||
return size_of_data + sizeof(char); | ||
} | ||
|
||
size_t alignOfData() const override | ||
{ | ||
return nested_function->alignOfData(); | ||
} | ||
|
||
void create(AggregateDataPtr place) const override | ||
{ | ||
nested_function->create(place); | ||
|
||
place[size_of_data] = 0; | ||
} | ||
|
||
void destroy(AggregateDataPtr place) const noexcept override | ||
{ | ||
nested_function->destroy(place); | ||
} | ||
|
||
void add( | ||
AggregateDataPtr place, | ||
const IColumn ** columns, | ||
size_t row_num, | ||
Arena * arena) const override | ||
{ | ||
nested_function->add(place, columns, row_num, arena); | ||
|
||
place[size_of_data] = 1; | ||
} | ||
|
||
void merge( | ||
AggregateDataPtr place, | ||
ConstAggregateDataPtr rhs, | ||
Arena * arena) const override | ||
{ | ||
nested_function->merge(place, rhs, arena); | ||
} | ||
|
||
void serialize( | ||
ConstAggregateDataPtr place, | ||
WriteBuffer & buf) const override | ||
{ | ||
nested_function->serialize(place, buf); | ||
} | ||
|
||
void deserialize( | ||
AggregateDataPtr place, | ||
ReadBuffer & buf, | ||
Arena * arena) const override | ||
{ | ||
nested_function->deserialize(place, buf, arena); | ||
} | ||
|
||
DataTypePtr getReturnType() const override | ||
{ | ||
if constexpr (UseNull) | ||
{ | ||
// -OrNull | ||
|
||
if (inner_nullable) | ||
return inner_type; | ||
|
||
return std::make_shared<DataTypeNullable>(inner_type); | ||
} | ||
else | ||
{ | ||
// -OrDefault | ||
|
||
return inner_type; | ||
} | ||
} | ||
|
||
void insertResultInto( | ||
ConstAggregateDataPtr place, | ||
IColumn & to) const override | ||
{ | ||
if (place[size_of_data]) | ||
{ | ||
if constexpr (UseNull) | ||
{ | ||
// -OrNull | ||
|
||
if (inner_nullable) | ||
nested_function->insertResultInto(place, to); | ||
else | ||
{ | ||
ColumnNullable & col = typeid_cast<ColumnNullable &>(to); | ||
|
||
col.getNullMapColumn().insertDefault(); | ||
nested_function->insertResultInto(place, col.getNestedColumn()); | ||
} | ||
} | ||
else | ||
{ | ||
// -OrDefault | ||
|
||
nested_function->insertResultInto(place, to); | ||
} | ||
} | ||
else | ||
to.insertDefault(); | ||
} | ||
}; | ||
|
||
} |
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
48 changes: 48 additions & 0 deletions
48
dbms/tests/queries/0_stateless/01018_empty_aggregation_filling.reference
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,48 @@ | ||
--- Int Empty --- | ||
0 | ||
\N | ||
0 | ||
\N | ||
0 | ||
\N | ||
0 | ||
\N | ||
0 | ||
\N | ||
0 | ||
\N | ||
--- Int Non-empty --- | ||
1 | ||
1 | ||
nan | ||
nan | ||
1 | ||
1 | ||
1 | ||
1 | ||
nan | ||
nan | ||
1 | ||
1 | ||
--- Other Types Empty --- | ||
|
||
\N | ||
\N | ||
\N | ||
0.00 | ||
\N | ||
0 | ||
\N | ||
0.00 | ||
\N | ||
--- Other Types Non-empty --- | ||
hello | ||
hello | ||
2011-04-05 14:19:19 | ||
2011-04-05 14:19:19 | ||
-123.45 | ||
-123.45 | ||
inf | ||
inf | ||
-123.45 | ||
-123.45 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.