Skip to content

feat(cel): add percentile and stats - #188

Merged
moshloop merged 2 commits into
mainfrom
feat/cel-percentile-stats-dn49yxazvx6o
Nov 25, 2026
Merged

moshloop merged 2 commits into
mainfrom
feat/cel-percentile-stats-dn49yxazvx6o

Conversation

@moshloop

@moshloop moshloop commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

What

  • Adds percentile and stats CEL functions for list samples (cel_statistics.go, documented in CEL.md).
  • Fixes percentile rank rounding by multiplying before dividing, so whole-number ranks stay exact.
  • Keeps averages finite when the sum overflows by scaling samples by the largest magnitude.

Notes

  • Spec coverage is in cel_collections_spec_test.go.

Summary by CodeRabbit

  • New Features
    • Added percentile() for selecting a nearest-rank value from a list of numeric samples.
    • Added stats() to return the minimum, average, maximum, selected percentiles, and sample count.
    • Both functions report errors for empty lists and invalid samples; percentile() also validates the requested rank.
  • Documentation
    • Documented the functions, supported inputs, returned values, and error cases.

Claude-Session-Id: f0bc1457-c4a3-4236-b30e-7e798ada791e
Multiplying q before dividing keeps whole-number ranks exact (7/100*100 yields 7.000000000000001 and rounded up a sample). Averages now stay finite when the sum overflows by scaling samples by the largest magnitude.
@coderabbitai

coderabbitai Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Explain Change →

Caution

Review failed

The pull request is closed.

Walkthrough

The change adds percentile and stats CEL collection functions. It registers both functions in the static CEL environment and adds tests and documentation for their behavior.

Changes

CEL collection statistics

Layer / File(s) Summary
Implement and verify statistical collection functions
cel_statistics.go, cel.go, cel_collections_spec_test.go, CEL.md
The CEL environment registers percentile and stats. The functions validate and sort numeric samples, calculate nearest-rank percentiles and summary values, and reject invalid inputs. Tests and documentation cover their behavior.

Suggested reviewers: hairyhenderson, adityathebe

Change: Feature

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@github-actions

github-actions Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Benchstat

Base: 03ba7cb8cd7aa276757fc98d2fb0093b2012d7c4
Head: 599ccf5190366905a7db08faf74bc6934e7fa7da

5 minor regression(s) (all within 5% threshold)

Benchmark Metric Base Head Change p-value
Serialize_NoNativeTypes/items=1000/native_values=false-4 sec/op 1.34 ms/op 1.36 ms/op +1.37% 0.035
RunExpressionContextCompile/cache=miss/environment=small-4 allocs/op 2.36e+03 allocs/op 2.37e+03 allocs/op +0.42% 0.000
RunExpressionContextCompile/cache=miss/environment=large-4 allocs/op 2.37e+03 allocs/op 2.38e+03 allocs/op +0.42% 0.000
RunExpressionContextCompile/cache=miss/environment=small-4 B/op 2.24e+05 B/op 2.25e+05 B/op +0.22% 0.000
RunExpressionContextCompile/cache=miss/environment=large-4 B/op 2.25e+05 B/op 2.25e+05 B/op +0.21% 0.000
Full benchstat output
goos: linux
goarch: amd64
cpu: AMD EPYC 9V74 80-Core Processor                
                                                                         │ bench-base.txt │           bench-head.txt           │
                                                                         │     sec/op     │   sec/op     vs base               │
CELEnvExtendCustomFunction-4                                                  81.36µ ± 1%   81.24µ ± 1%       ~ (p=0.853 n=10)
CELProgramEvaluation/expression=scalar/optimized=false-4                      181.0n ± 0%   183.1n ± 1%       ~ (p=0.055 n=10)
CELProgramEvaluation/expression=scalar/optimized=true-4                       180.7n ± 1%   181.5n ± 1%       ~ (p=0.393 n=10)
CELProgramEvaluation/expression=list_optional_regex/optimized=false-4         4.662µ ± 6%   4.622µ ± 2%       ~ (p=0.105 n=10)
CELProgramEvaluation/expression=list_optional_regex/optimized=true-4          1.692µ ± 2%   1.684µ ± 1%       ~ (p=0.270 n=10)
CELProgramEvaluation/expression=comprehension/optimized=false-4               6.061µ ± 1%   6.043µ ± 1%       ~ (p=0.617 n=10)
CELProgramEvaluation/expression=comprehension/optimized=true-4                5.792µ ± 2%   5.825µ ± 1%       ~ (p=0.684 n=10)
RunExpressionNativeInput/input=map-4                                          2.958µ ± 1%   2.973µ ± 2%       ~ (p=0.404 n=10)
RunExpressionNativeInput/input=native_struct-4                                3.018µ ± 2%   3.009µ ± 5%       ~ (p=0.424 n=10)
RunExpressionContext/cache=hit/environment=small-4                            1.705µ ± 2%   1.723µ ± 2%       ~ (p=0.093 n=10)
RunExpressionContext/cache=hit/environment=large-4                            1.849µ ± 1%   1.857µ ± 1%       ~ (p=0.447 n=10)
RunExpressionContextCompile/cache=miss/environment=small-4                    324.4µ ± 2%   328.7µ ± 1%       ~ (p=0.143 n=10)
RunExpressionContextCompile/cache=miss/environment=large-4                    323.9µ ± 2%   325.8µ ± 1%       ~ (p=0.481 n=10)
CELEnvExtend/variables=1/functions=0-4                                        6.131µ ± 2%   6.150µ ± 2%       ~ (p=1.000 n=10)
CELEnvExtend/variables=10/functions=0-4                                       7.856µ ± 3%   7.901µ ± 2%       ~ (p=0.971 n=10)
CELEnvExtend/variables=100/functions=0-4                                      23.24µ ± 2%   23.30µ ± 2%       ~ (p=0.424 n=10)
RunExpressionContextFunctions/celFunctions/cache=hit/environment=large-4      4.081µ ± 2%   4.088µ ± 1%       ~ (p=0.781 n=10)
RunExpressionContextFunctions/functions/cache=hit/environment=large-4         4.121µ ± 1%   4.117µ ± 3%       ~ (p=0.839 n=10)
Serialize/items=10/native_values=true-4                                       39.89µ ± 2%   39.44µ ± 3%       ~ (p=0.529 n=10)
Serialize/items=100/native_values=true-4                                      333.2µ ± 3%   333.4µ ± 1%       ~ (p=0.739 n=10)
Serialize/items=1000/native_values=true-4                                     3.424m ± 2%   3.410m ± 1%       ~ (p=0.739 n=10)
Serialize/items=10000/native_values=true-4                                    37.48m ± 1%   37.12m ± 2%       ~ (p=0.218 n=10)
Serialize_NoNativeTypes/items=100/native_values=false-4                       139.2µ ± 1%   139.2µ ± 1%       ~ (p=0.796 n=10)
Serialize_NoNativeTypes/items=1000/native_values=false-4                      1.337m ± 1%   1.355m ± 1%  +1.37% (p=0.035 n=10)
Serialize_NoNativeTypes/items=10000/native_values=false-4                     14.39m ± 1%   14.50m ± 4%       ~ (p=0.436 n=10)
RunTemplate_LargeEnv/items=10-4                                               5.046µ ± 1%   5.066µ ± 1%       ~ (p=0.190 n=10)
RunTemplate_LargeEnv/items=1000-4                                             5.018µ ± 2%   5.044µ ± 1%       ~ (p=0.183 n=10)
RunTemplate_LargeEnv/items=10000-4                                            4.965µ ± 2%   4.970µ ± 2%       ~ (p=0.541 n=10)
geomean                                                                       21.61µ        21.66µ       +0.21%

                                                                         │ bench-base.txt │            bench-head.txt             │
                                                                         │      B/op      │     B/op      vs base                 │
CELEnvExtendCustomFunction-4                                                 46.31Ki ± 0%   46.31Ki ± 0%       ~ (p=1.000 n=10) ¹
CELProgramEvaluation/expression=scalar/optimized=false-4                       96.00 ± 0%     96.00 ± 0%       ~ (p=1.000 n=10) ¹
CELProgramEvaluation/expression=scalar/optimized=true-4                        96.00 ± 0%     96.00 ± 0%       ~ (p=1.000 n=10) ¹
CELProgramEvaluation/expression=list_optional_regex/optimized=false-4        4.241Ki ± 0%   4.241Ki ± 0%       ~ (p=0.466 n=10)
CELProgramEvaluation/expression=list_optional_regex/optimized=true-4         1.148Ki ± 0%   1.148Ki ± 0%       ~ (p=1.000 n=10)
CELProgramEvaluation/expression=comprehension/optimized=false-4              3.063Ki ± 0%   3.063Ki ± 0%       ~ (p=1.000 n=10) ¹
CELProgramEvaluation/expression=comprehension/optimized=true-4               2.798Ki ± 0%   2.798Ki ± 0%       ~ (p=1.000 n=10) ¹
RunExpressionNativeInput/input=map-4                                         1.375Ki ± 0%   1.375Ki ± 0%       ~ (p=1.000 n=10) ¹
RunExpressionNativeInput/input=native_struct-4                               1.250Ki ± 0%   1.250Ki ± 0%       ~ (p=1.000 n=10) ¹
RunExpressionContext/cache=hit/environment=small-4                             840.0 ± 0%     840.0 ± 0%       ~ (p=1.000 n=10) ¹
RunExpressionContext/cache=hit/environment=large-4                             936.0 ± 0%     936.0 ± 0%       ~ (p=1.000 n=10) ¹
RunExpressionContextCompile/cache=miss/environment=small-4                   219.1Ki ± 0%   219.6Ki ± 0%  +0.22% (p=0.000 n=10)
RunExpressionContextCompile/cache=miss/environment=large-4                   219.4Ki ± 0%   219.9Ki ± 0%  +0.21% (p=0.000 n=10)
CELEnvExtend/variables=1/functions=0-4                                       4.477Ki ± 0%   4.477Ki ± 0%       ~ (p=1.000 n=10) ¹
CELEnvExtend/variables=10/functions=0-4                                      6.157Ki ± 0%   6.157Ki ± 0%       ~ (p=0.350 n=10)
CELEnvExtend/variables=100/functions=0-4                                     23.63Ki ± 0%   23.63Ki ± 0%       ~ (p=1.000 n=10) ¹
RunExpressionContextFunctions/celFunctions/cache=hit/environment=large-4     2.297Ki ± 0%   2.297Ki ± 0%       ~ (p=1.000 n=10) ¹
RunExpressionContextFunctions/functions/cache=hit/environment=large-4        2.359Ki ± 0%   2.359Ki ± 0%       ~ (p=1.000 n=10) ¹
Serialize/items=10/native_values=true-4                                      34.39Ki ± 0%   34.39Ki ± 0%       ~ (p=1.000 n=10)
Serialize/items=100/native_values=true-4                                     304.6Ki ± 0%   304.6Ki ± 0%       ~ (p=1.000 n=10) ¹
Serialize/items=1000/native_values=true-4                                    2.946Mi ± 0%   2.946Mi ± 0%       ~ (p=0.257 n=10)
Serialize/items=10000/native_values=true-4                                   29.45Mi ± 0%   29.45Mi ± 0%       ~ (p=0.691 n=10)
Serialize_NoNativeTypes/items=100/native_values=false-4                      104.0Ki ± 0%   104.0Ki ± 0%       ~ (p=1.000 n=10) ¹
Serialize_NoNativeTypes/items=1000/native_values=false-4                     1.007Mi ± 0%   1.007Mi ± 0%       ~ (p=1.000 n=10)
Serialize_NoNativeTypes/items=10000/native_values=false-4                    10.07Mi ± 0%   10.07Mi ± 0%       ~ (p=1.000 n=10)
RunTemplate_LargeEnv/items=10-4                                              5.273Ki ± 0%   5.273Ki ± 0%       ~ (p=1.000 n=10) ¹
RunTemplate_LargeEnv/items=1000-4                                            5.273Ki ± 0%   5.273Ki ± 0%       ~ (p=1.000 n=10) ¹
RunTemplate_LargeEnv/items=10000-4                                           5.273Ki ± 0%   5.273Ki ± 0%       ~ (p=1.000 n=10) ¹
geomean                                                                      14.79Ki        14.79Ki       +0.02%
¹ all samples are equal

                                                                         │ bench-base.txt │            bench-head.txt            │
                                                                         │   allocs/op    │  allocs/op   vs base                 │
CELEnvExtendCustomFunction-4                                                   112.0 ± 0%    112.0 ± 0%       ~ (p=1.000 n=10) ¹
CELProgramEvaluation/expression=scalar/optimized=false-4                       3.000 ± 0%    3.000 ± 0%       ~ (p=1.000 n=10) ¹
CELProgramEvaluation/expression=scalar/optimized=true-4                        3.000 ± 0%    3.000 ± 0%       ~ (p=1.000 n=10) ¹
CELProgramEvaluation/expression=list_optional_regex/optimized=false-4          79.00 ± 0%    79.00 ± 0%       ~ (p=1.000 n=10) ¹
CELProgramEvaluation/expression=list_optional_regex/optimized=true-4           30.00 ± 0%    30.00 ± 0%       ~ (p=1.000 n=10) ¹
CELProgramEvaluation/expression=comprehension/optimized=false-4                91.00 ± 0%    91.00 ± 0%       ~ (p=1.000 n=10) ¹
CELProgramEvaluation/expression=comprehension/optimized=true-4                 87.00 ± 0%    87.00 ± 0%       ~ (p=1.000 n=10) ¹
RunExpressionNativeInput/input=map-4                                           36.00 ± 0%    36.00 ± 0%       ~ (p=1.000 n=10) ¹
RunExpressionNativeInput/input=native_struct-4                                 29.00 ± 0%    29.00 ± 0%       ~ (p=1.000 n=10) ¹
RunExpressionContext/cache=hit/environment=small-4                             20.00 ± 0%    20.00 ± 0%       ~ (p=1.000 n=10) ¹
RunExpressionContext/cache=hit/environment=large-4                             23.00 ± 0%    23.00 ± 0%       ~ (p=1.000 n=10) ¹
RunExpressionContextCompile/cache=miss/environment=small-4                    2.363k ± 0%   2.373k ± 0%  +0.42% (p=0.000 n=10)
RunExpressionContextCompile/cache=miss/environment=large-4                    2.368k ± 0%   2.378k ± 0%  +0.42% (p=0.000 n=10)
CELEnvExtend/variables=1/functions=0-4                                         72.00 ± 0%    72.00 ± 0%       ~ (p=1.000 n=10) ¹
CELEnvExtend/variables=10/functions=0-4                                        91.00 ± 0%    91.00 ± 0%       ~ (p=1.000 n=10) ¹
CELEnvExtend/variables=100/functions=0-4                                       193.0 ± 0%    193.0 ± 0%       ~ (p=1.000 n=10) ¹
RunExpressionContextFunctions/celFunctions/cache=hit/environment=large-4       44.00 ± 0%    44.00 ± 0%       ~ (p=1.000 n=10) ¹
RunExpressionContextFunctions/functions/cache=hit/environment=large-4          48.00 ± 0%    48.00 ± 0%       ~ (p=1.000 n=10) ¹
Serialize/items=10/native_values=true-4                                        424.0 ± 0%    424.0 ± 0%       ~ (p=1.000 n=10) ¹
Serialize/items=100/native_values=true-4                                      3.844k ± 0%   3.844k ± 0%       ~ (p=1.000 n=10) ¹
Serialize/items=1000/native_values=true-4                                     39.53k ± 0%   39.53k ± 0%       ~ (p=1.000 n=10) ¹
Serialize/items=10000/native_values=true-4                                    399.5k ± 0%   399.5k ± 0%       ~ (p=1.000 n=10) ¹
Serialize_NoNativeTypes/items=100/native_values=false-4                       1.721k ± 0%   1.721k ± 0%       ~ (p=1.000 n=10) ¹
Serialize_NoNativeTypes/items=1000/native_values=false-4                      17.77k ± 0%   17.77k ± 0%       ~ (p=1.000 n=10) ¹
Serialize_NoNativeTypes/items=10000/native_values=false-4                     179.8k ± 0%   179.8k ± 0%       ~ (p=1.000 n=10) ¹
RunTemplate_LargeEnv/items=10-4                                                39.00 ± 0%    39.00 ± 0%       ~ (p=1.000 n=10) ¹
RunTemplate_LargeEnv/items=1000-4                                              39.00 ± 0%    39.00 ± 0%       ~ (p=1.000 n=10) ¹
RunTemplate_LargeEnv/items=10000-4                                             39.00 ± 0%    39.00 ± 0%       ~ (p=1.000 n=10) ¹
geomean                                                                        228.6         228.7       +0.03%
¹ all samples are equal

@moshloop
moshloop enabled auto-merge (rebase) November 25, 2026 12:04
@moshloop
moshloop merged commit 8e753f0 into main Nov 25, 2026
11 of 13 checks passed
@moshloop
moshloop deleted the feat/cel-percentile-stats-dn49yxazvx6o branch November 25, 2026 12:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant