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

Added function arrayFold() #49794

Merged
merged 26 commits into from Oct 9, 2023
Merged

Added function arrayFold() #49794

merged 26 commits into from Oct 9, 2023

Conversation

Lirikl
Copy link
Contributor

@Lirikl Lirikl commented May 11, 2023

Fixes #34155

Implemention of function arrayFold(x1 ,..., xn, accum -> expression, array1,...,arrayn, init_accum), applying a lambda function to a number of array columns, collecting the result in an accumulator. The accumulator can be either constant or a column.

The implementation gets the columns of the N-th elements for every array and applies them sequentially.

Examples:

SELECT arrayFold(x, acc -> x + acc, [1, 2, 3, 4, 5], toInt64(0)); -- sums the elements of array;
SELECT arrayFold(x + acc.1, x * acc.2), [1, 2, 3, 4, 5], (toInt64(0), toInt64(1)); -- sums and multiplies the elements of array;
SELECT arrayFold(x, acc -> arrayPushFront(ACC, x), [1, 2, 3, 4, 5], emptyArrayInt64()); -- reverses array;

Changelog category :

  • New Feature

Changelog entry :

Add function "arrayFold(x1, ..., xn, accum -> expression, array1, ..., arrayn, init_accum)" which applies a lambda function to multiple arrays of the same cardinality and collects the result in an accumulator.

@CLAassistant
Copy link

CLAassistant commented May 11, 2023

CLA assistant check
All committers have signed the CLA.

@UnamedRus UnamedRus mentioned this pull request May 11, 2023
@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label May 11, 2023
@robot-clickhouse
Copy link
Member

robot-clickhouse commented May 11, 2023

This is an automated comment for commit 878e36d with description of existing statuses. It's updated for the latest CI running

❌ Click here to open a full report in a separate page

Successful checks
Check nameDescriptionStatus
AST fuzzerRuns randomly generated queries to catch program errors. The build type is optionally given in parenthesis. If it fails, ask a maintainer for help✅ success
CI runningA meta-check that indicates the running CI. Normally, it's in success or pending state. The failed status indicates some problems with the PR✅ success
ClickHouse build checkBuilds ClickHouse in various configurations for use in further steps. You have to fix the builds that fail. Build logs often has enough information to fix the error, but you might have to reproduce the failure locally. The cmake options can be found in the build log, grepping for cmake. Use these options and follow the general build process✅ success
Compatibility checkChecks that clickhouse binary runs on distributions with old libc versions. If it fails, ask a maintainer for help✅ success
Docker image for serversThe check to build and optionally push the mentioned image to docker hub✅ success
Docs CheckBuilds and tests the documentation✅ success
Fast testNormally this is the first check that is ran for a PR. It builds ClickHouse and runs most of stateless functional tests, omitting some. If it fails, further checks are not started until it is fixed. Look at the report to see which tests fail, then reproduce the failure locally as described here✅ success
Flaky testsChecks if new added or modified tests are flaky by running them repeatedly, in parallel, with more randomization. Functional tests are run 100 times with address sanitizer, and additional randomization of thread scheduling. Integrational tests are run up to 10 times. If at least once a new test has failed, or was too long, this check will be red. We don't allow flaky tests, read the doc✅ success
Install packagesChecks that the built packages are installable in a clear environment✅ success
Mergeable CheckChecks if all other necessary checks are successful✅ success
Push to DockerhubThe check for building and pushing the CI related docker images to docker hub✅ success
SQLTestThere's no description for the check yet, please add it to tests/ci/ci_config.py:CHECK_DESCRIPTIONS✅ success
SQLancerFuzzing tests that detect logical bugs with SQLancer tool✅ success
SqllogicRun clickhouse on the sqllogic test set against sqlite and checks that all statements are passed✅ success
Stateful testsRuns stateful functional tests for ClickHouse binaries built in various configurations -- release, debug, with sanitizers, etc✅ success
Stress testRuns stateless functional tests concurrently from several clients to detect concurrency-related errors✅ success
Style CheckRuns a set of checks to keep the code style clean. If some of tests failed, see the related log from the report✅ success
Unit testsRuns the unit tests for different release types✅ success
Upgrade checkRuns stress tests on server version from last release and then tries to upgrade it to the version from the PR. It checks if the new server can successfully startup without any errors, crashes or sanitizer asserts✅ success
Check nameDescriptionStatus
Integration testsThe integration tests report. In parenthesis the package type is given, and in square brackets are the optional part/total tests❌ failure
Performance ComparisonMeasure changes in query performance. The performance test report is described in detail here. In square brackets are the optional part/total tests❌ failure
Stateless testsRuns stateless functional tests for ClickHouse binaries built in various configurations -- release, debug, with sanitizers, etc❌ failure

@clickhouse-ci
Copy link

clickhouse-ci bot commented May 12, 2023

This is an automatic comment. The PR descriptions does not match the template.

Please, edit it accordingly.

The error is: More than one changelog category specified: 'New Feature', 'Implementaеion of function arrayFold(x1,...,xn,accum -> expression, array1,...,arrayn, init_accum) applying lambda function to a number of same sized array columns and collecting result in accumulator. Accumulator can be either constant or column.'

@clickhouse-ci
Copy link

clickhouse-ci bot commented May 12, 2023

This is an automatic comment. The PR descriptions does not match the template.

Please, edit it accordingly.

The error is: More than one changelog category specified: 'New Feature', '### Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):'

2 similar comments
@clickhouse-ci
Copy link

clickhouse-ci bot commented May 12, 2023

This is an automatic comment. The PR descriptions does not match the template.

Please, edit it accordingly.

The error is: More than one changelog category specified: 'New Feature', '### Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):'

@clickhouse-ci
Copy link

clickhouse-ci bot commented May 12, 2023

This is an automatic comment. The PR descriptions does not match the template.

Please, edit it accordingly.

The error is: More than one changelog category specified: 'New Feature', '### Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):'

@robot-clickhouse robot-clickhouse added the pr-feature Pull request with new product feature label May 12, 2023
@rschu1ze rschu1ze self-assigned this May 29, 2023
Copy link
Member

@rschu1ze rschu1ze left a comment

Choose a reason for hiding this comment

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

Thanks a lot! Would you add some docs to docs/en/sql-reference/functions/array-functions.md?

And just out of interest: did you have a look at the previous attempts to add arrayFold? #34155. Asking because there were some performance issues and I am curious how this PR addresses them?

@Lirikl
Copy link
Contributor Author

Lirikl commented May 29, 2023

Thanks a lot! Would you add some docs to docs/en/sql-reference/functions/array-functions.md?

And just out of interest: did you have a look at the previous attempts to add arrayFold? #34155. Asking because there were some performance issues and I am curious how this PR addresses them?

Yes, I had a look at previous solution, and implemented function in vectorized fashion as was suggested by alexey-milovidov.
I am approximating this solution as at least 100x and some magnitudes faster that implementation in #21589 depending on array sizes based on few tests including those added in performance tests.

In a week I am planning to add docs and some tests comparing this functions against specified functions such as arraySum

Copy link
Member

@rschu1ze rschu1ze left a comment

Choose a reason for hiding this comment

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

Will check the code probably tomorrow. It would be nice if you could add some comments to the code that document the design decisions (so novice readers like me will have an easier time 😄 )

tests/queries/0_stateless/02718_array_fold.sql Outdated Show resolved Hide resolved
tests/queries/0_stateless/02718_array_fold.sql Outdated Show resolved Hide resolved
@Lirikl
Copy link
Contributor Author

Lirikl commented May 29, 2023

Will check the code probably tomorrow. It would be nice if you could add some comments to the code that document the design decisions (so novice readers like me will have an easier time 😄 )

eah, alexey-milovidov had the same comment about the lack of comments, surely will do it too

@rschu1ze rschu1ze changed the title arrayFold function Added function arrayFold() May 30, 2023
tests/queries/0_stateless/02718_array_fold.sql Outdated Show resolved Hide resolved

DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override
{
if (arguments.size() < 2)
Copy link
Member

Choose a reason for hiding this comment

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

Don't we need at least three arguments?

size_t getNumberOfArguments() const override { return 0; }
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return true; }

void getLambdaArgumentTypes(DataTypes & arguments) const override
Copy link
Member

Choose a reason for hiding this comment

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

Function getLambdaArgumentTypes isn't implemented often in the codebase. Suggest to add some comments what the elements arguments represent. I think they are simply the function arguments (lambda function, array arguments, initial accumulator) but a naive reader could think they are the lambda's arguments.

{
const DataTypeArray * array_type = checkAndGetDataType<DataTypeArray>(&*arguments[i + 1]);
if (!array_type)
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Argument {} of function {} must be array. Found {} instead.", toString(i + 2), getName(), arguments[i + 1]->getName());
Copy link
Member

Choose a reason for hiding this comment

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

(toString() is superfluous)

throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Argument {} of function {} must be array. Found {} instead.", toString(i + 2), getName(), arguments[i + 1]->getName());
nested_types[i] = recursiveRemoveLowCardinality(array_type->getNestedType());
}
nested_types[nested_types.size() - 1] = arguments[arguments.size() - 1];
Copy link
Member

Choose a reason for hiding this comment

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

nested_types.back() = arguments.back()?

src/Functions/array/arrayFold.cpp Outdated Show resolved Hide resolved
src/Functions/array/arrayFold.cpp Outdated Show resolved Hide resolved
src/Functions/array/arrayFold.cpp Show resolved Hide resolved
src/Functions/array/arrayFold.cpp Outdated Show resolved Hide resolved
}
}

std::vector<MutableColumns> data_arrays;
Copy link
Member

Choose a reason for hiding this comment

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

As written elsewhere, below code is a bit hard to grasp. Let's add comments.

@zhiqiang-hhhh zhiqiang-hhhh mentioned this pull request Jun 13, 2023
@rschu1ze rschu1ze changed the title Added function arrayFold() Added function arrayFold() Oct 8, 2023
@rschu1ze rschu1ze merged commit 624dbcd into ClickHouse:master Oct 9, 2023
269 of 277 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
can be tested Allows running workflows for external contributors pr-feature Pull request with new product feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

arrayFold function
5 participants