feat: add array_normalize scalar function#22013
Open
crm26 wants to merge 1 commit intoapache:mainfrom
Open
Conversation
Jefffrey
reviewed
May 5, 2026
| let mut new_values: Vec<f64> = Vec::with_capacity(values.len()); | ||
| let mut new_offsets: Vec<O> = Vec::with_capacity(list_array.len() + 1); | ||
| new_offsets.push(O::usize_as(0)); | ||
| let mut validity: Vec<bool> = Vec::with_capacity(list_array.len()); |
Contributor
There was a problem hiding this comment.
Use NullBufferBuilder here instead. One benefit is when finishing it, it may output None if there are no nulls (currently we always provide a null buffer even if there are no nulls)
| let offsets = list_array.value_offsets(); | ||
|
|
||
| let mut new_values: Vec<f64> = Vec::with_capacity(values.len()); | ||
| let mut new_offsets: Vec<O> = Vec::with_capacity(list_array.len() + 1); |
Contributor
There was a problem hiding this comment.
I think it might be simpler to use OffsetBufferBuilder here
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Which issue does this PR close?
Part of #21536 — split of #21371 into one-function-per-PR. Third in the series after #21542 (cosine_distance) and #21861 (inner_product).
Rationale for this change
Adds
array_normalize(array)— the L2-normalized version of a numeric input vector. Computed asarray[i] / sqrt(sum(array[i]^2))per element. Returns the same shape as the input (List<Float64>orLargeList<Float64>).Aliased as
list_normalizeto match thearray_X/list_Xconvention used across the crate.What changes are included in this PR?
Coercion shell mirrors the merged cosine_distance/inner_product pattern:
coerce_typesacceptsList/LargeList/FixedSizeListof any numeric inner type, plus bareNULL. After coercion the inner function only seesList(Float64)orLargeList(Float64).as_float64_array(list_array.values())downcast plusvalue_offsets()slicing — no per-row downcasts.Vec<f64>for values,Vec<O>for offsets,NullBufferfor row validity.Per-row semantics:
sqrt(sum-of-squares)Are these changes tested?
Yes. SLT covers:
NULLinput, NULL element in list, zero vector, empty arrayLargeList,FixedSizeList(via coercion),Float32andInt64inner types, integer literalsList(Float64))list_normalizealias coverage (constant + multi-row with NULL)Are there any user-facing changes?
New scalar function
array_normalize(aliaslist_normalize), documented indocs/source/user-guide/sql/scalar_functions.md.