Skip to content

avgWeighted - #7898

Merged
KochetovNicolai merged 10 commits into
ClickHouse:masterfrom
apbodrov:avg_weighted
Dec 19, 2019
Merged

KochetovNicolai merged 10 commits into
ClickHouse:masterfrom
apbodrov:avg_weighted

Conversation

@apbodrov

Copy link
Copy Markdown
Contributor

I hereby agree to the terms of the CLA available at: https://yandex.ru/legal/cla/?lang=en

Changelog category (leave one):

  • New Feature

Changelog entry (up to few sentences, required except for Non-significant/Documentation categories):

new aggregate function avgWeighted

@abyss7 abyss7 added can be tested pr-feature Pull request with new product feature labels Nov 25, 2019
namespace DB
{
template <typename T>
struct AggregateFunctionAvgWeightedData

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see any difference with AggregateFunctionAvgData. Why don't we use it?

/// Calculates arithmetic mean of numbers.
template <typename T, typename Data>
class AggregateFunctionAvg final : public IAggregateFunctionDataHelper<Data, AggregateFunctionAvg<T, Data>>
template <typename Data, typename T, typename F>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

F is not very good name. maybe rename to Derived or something?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did we change T and Data order? Don't think it's important, but suspicious.

class AggregateFunctionAvg final : public AggregateFunctionAvgBase<Data, T, AggregateFunctionAvg<T, Data>>
{
public:
AggregateFunctionAvg(const DataTypes & argument_types_)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can probably inherit constructor from AggregateFunctionAvgBase. It's not very important and not necessary to fix.

@@ -0,0 +1 @@
SELECT avgWeighted(x, weight) FROM (SELECT t.1 AS x, t.2 AS weight FROM (SELECT arrayJoin([(1, 1), (2, 1), (3, 1), (4, 1), (5, 1)]) AS t));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, this test is not interesting if all weights are 1. Need test with different weights. And with all weights = 0.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And also need test with different types of x and weight (which could pass or throw specified exception).

using Function = AggregateFunctionAvgWeighted<T, AggregateFunctionAvgWeightedData<FieldType>>;
};

template <typename T>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now it is impossible to use different types for values and weights. It may be not very convenient.
it is possible to improve later.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@KochetovNicolai ,
We are wanting to use the avgWeighted function but with custom weights for different fields. Would be great if that improvement can be provided.

assertBinary(name, argument_types);

AggregateFunctionPtr res;
DataTypePtr data_type = argument_types[0];

@KochetovNicolai KochetovNicolai Nov 26, 2019 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


void registerAggregateFunctionAvgWeighted(AggregateFunctionFactory & factory)
{
factory.registerFunction("AvgWeighted", createAggregateFunctionAvgWeighted, AggregateFunctionFactory::CaseInsensitive);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to use AvgWeighted name by our style.
Also I don't think that it should be CaseInsensitive, cause AVGWEIGHTED looks wired. But can leave it if you want.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How?
I looked here #2014 (comment)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

avgWeighted for sure.
Because we are using camelCase (starting from lowercase letter except if it is starting by all caps acronym).

@KochetovNicolai KochetovNicolai left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.

@KochetovNicolai KochetovNicolai self-assigned this Nov 26, 2019
@alexey-milovidov

Copy link
Copy Markdown
Member

00463_long_sessions_in_http_interface - flacky test.

@KochetovNicolai

Copy link
Copy Markdown
Member

There is no 01035_avg_weighted.sql now. Did you forget to add it?

@KochetovNicolai

Copy link
Copy Markdown
Member

Everything else is ok.

@alexey-milovidov alexey-milovidov added the st-discussion When the implementation aspects are not clear or when the PR is on hold due to questions. label Dec 18, 2019
@KochetovNicolai
KochetovNicolai merged commit 5f3d7ca into ClickHouse:master Dec 19, 2019
This was referenced Mar 30, 2020
@alexey-milovidov

Copy link
Copy Markdown
Member

@apbodrov Why do you need avgWeighted aggregate function when you can write sum(x * weight) / sum(weight) instead?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-feature Pull request with new product feature st-discussion When the implementation aspects are not clear or when the PR is on hold due to questions.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants