Skip to content

Add arrayPermutations, arrayPartialPermutations, and arrayCombinations#101135

Open
ArtemovMichael wants to merge 4 commits intoClickHouse:masterfrom
ArtemovMichael:feat/combinatorics-functions
Open

Add arrayPermutations, arrayPartialPermutations, and arrayCombinations#101135
ArtemovMichael wants to merge 4 commits intoClickHouse:masterfrom
ArtemovMichael:feat/combinatorics-functions

Conversation

@ArtemovMichael
Copy link
Copy Markdown

@ArtemovMichael ArtemovMichael commented Mar 29, 2026

Closes #43175

Changelog category:

  • New Feature

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Added new array combinatorics functions: arrayPermutations, arrayPartialPermutations, and arrayCombinations. These functions generate permutations and combinations by element indexes (so equal values may produce duplicate rows), validate argument k, and enforce result-size limits via function_range_max_elements_in_block.

Documentation entry for user-facing changes

  • Documentation is written in code

Motivation:

  • Add built-in SQL functions for common combinatorics tasks on arrays without using external logic.

Parameters:

  • arrayPermutations(arr):
    • arr: source array, returns all full permutations.
  • arrayPartialPermutations(arr, k):
    • arr: source array.
    • k: selection length, must be non-negative and not exceed array length.
  • arrayCombinations(arr, k):
    • arr: source array.
    • k: combination length, must be non-negative and not exceed array length.
  • Result size is limited by function_range_max_elements_in_block.

Example use:

  • SELECT arrayPermutations([1, 2, 3]);
  • SELECT arrayPartialPermutations([1, 2, 3], 2);
  • SELECT arrayCombinations([1, 2, 3], 2);

@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Mar 29, 2026

CLA assistant check
All committers have signed the CLA.

@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Mar 30, 2026
@clickhouse-gh
Copy link
Copy Markdown
Contributor

clickhouse-gh bot commented Mar 30, 2026

Workflow [PR], commit [f7a323b]

Summary:

job_name test_name status info comment
Integration tests (amd_llvm_coverage, 2/5) failure
test_overcommit_tracker/test.py::test_user_overcommit FAIL cidb
Stress test (amd_msan) failure
Logical error: Block structure mismatch in A stream: different columns: (STID: 0993-27f0) FAIL cidb
Stress test (arm_debug) failure
Cannot start clickhouse-server FAIL cidb
Parse failure error failure cidb
Check failed failure cidb
Stress test (arm_tsan) failure
Cannot start clickhouse-server FAIL cidb
Parse failure error failure cidb
Check failed failure cidb
Stress test (arm_msan) failure
Logical error: Shard number is greater than shard count: shard_num=A shard_count=B cluster=C (STID: 5066-457d) FAIL cidb

AI Review

Summary

This PR adds three new array combinatorics SQL functions: arrayPermutations, arrayPartialPermutations, and arrayCombinations, plus stateless tests and function docs. The core algorithms, bounds checks (k, overflow guard, and function_range_max_elements_in_block), and test coverage are generally solid. I found one minor issue: a misleading typo in an internal comment (P(n, k) / C(n, k)), and left an inline suggestion.

Missing context
  • ⚠️ No CI logs or benchmark results were provided in the review input, so I could not validate runtime/perf behavior beyond static inspection.
Findings

💡 Nits

  • [src/Functions/array/arrayCombinatorics.cpp:152] The comment says P(n, k) / C(n, k), which reads as division, but the templated function computes either value depending on mode. Reword to P(n, k) or C(n, k) for clarity.
ClickHouse Rules
Item Status Notes
Deletion logging
Serialization versioning
Core-area scrutiny
No test removal
Experimental gate
No magic constants
Backward compatibility
SettingsChangesHistory.cpp
PR metadata quality
Safe rollout
Compilation time
Final Verdict
  • Status: ✅ Approve
  • Optional follow-up: apply the small wording fix from the inline nit comment.

@clickhouse-gh clickhouse-gh bot added the pr-feature Pull request with new product feature label Mar 30, 2026
factory.registerFunction<FunctionArrayCombinations>(combinations_documentation);
}

} // namespace DB
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Style check fails on this line (Found unnecessary namespace comments). Please remove the trailing namespace comment to keep CI green:

} // namespace DB

becomes

}

return true;
}

/// Returns true if P(n, k) / C(n, k) fits into count_limit and writes it to result.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Typo in comment: P(n, k) / C(n, k) reads as division, but the function handles either partial permutations or combinations depending on the template mode. Please reword to avoid confusion, e.g. P(n, k) or C(n, k).

@clickhouse-gh
Copy link
Copy Markdown
Contributor

clickhouse-gh bot commented Mar 31, 2026

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.10% 84.10% +0.00%
Functions 90.90% 90.90% +0.00%
Branches 76.70% 76.70% +0.00%

Changed lines: 98.34% (356/362) · Uncovered code

Full report · Diff report

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.

Combinatorics functions (on arrays)

3 participants