Skip to content
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 function arrayReduceInRanges #9598

Merged
merged 10 commits into from Mar 28, 2020
Merged

Conversation

hczhcz
Copy link
Contributor

@hczhcz hczhcz commented Mar 11, 2020

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 (a user-readable short description of the changes that goes to CHANGELOG.md):

Add function arrayReduceInRanges which aggregates array elements in given ranges.

Detailed description / Documentation draft:

Applies an aggregate function to array elements in given ranges and returns an array containing the result corresponding to each range. The function will return the same result as multiple arrayReduce(agg_func, arraySlice(arr1, index, length), ...).

Example:

SELECT arrayReduceInRanges(
   'sum',
   [1, 2],
   [2, 3],
   [100, 20, 3000, 4]
)
┌─arrayReduceInRanges('sum', [1, 2], [2, 3], [100, 20, 3000, 4])─┐
│ [120,3024]                                                     │
└────────────────────────────────────────────────────────────────┘

@amosbird
Copy link
Collaborator

The example is a bit misleading, as I interpret it as supplying two ranges : [1, 2] and [2, 3] at first sight.

@hczhcz
Copy link
Contributor Author

hczhcz commented Mar 11, 2020

This one might be better.

SELECT arrayReduceInRanges(
    'sum',
    [1, 2, 3, 4],
    [5, 3, 4, 4],
    [1000000, 200000, 30000, 4000, 500, 60, 7]
) AS res
┌─res─────────────────────────┐
│ [1234500,234000,34560,4567] │
└─────────────────────────────┘

@qoega qoega added doc-alert pr-feature Pull request with new product feature labels Mar 11, 2020
@qoega
Copy link
Member

qoega commented Mar 11, 2020

Will it be more clear if you use array of tuples?

SELECT arrayReduceInRanges(
    'sum',
    [(1, 5), (2, 3), (3, 4), (4, 4)],
    [1000000, 200000, 30000, 4000, 500, 60, 7]
) AS res

If you have a case, when index and length are in separate columns you can just use arrayZip

SELECT
    [1, 2, 3, 4, 5] AS x,
    [1, 2, 3, 4, 5] AS y,
    arrayZip(x, y) AS z

┌─x───────────┬─y───────────┬─z───────────────────────────────┐
│ [1,2,3,4,5] │ [1,2,3,4,5] │ [(1,1),(2,2),(3,3),(4,4),(5,5)] │
└─────────────┴─────────────┴─────────────────────────────────┘

│ [1234500,234500,34560,4567] │
└─────────────────────────────┘
```

Copy link
Member

Choose a reason for hiding this comment

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

It is better to explicitly describe parameters.
By the way there is a function doc template:
https://github.com/ClickHouse/ClickHouse/blob/master/docs/dscr-templates/template-function.md

@hczhcz hczhcz requested a review from a team as a code owner March 27, 2020 03:41
@alexey-milovidov
Copy link
Member

Sorry, it looks like this PR was not merged with upstream properly. Could you please rebase your commits?

}
catch (...)
{
agg_func.destroy(place);
Copy link
Member

Choose a reason for hiding this comment

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

This is wrong. If constructor has thrown an exception, destructor must not be called.

Copy link
Member

@alexey-milovidov alexey-milovidov left a comment

Choose a reason for hiding this comment

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

I will merge to read the algorithm more carefully (it's difficult to understand).

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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants