Skip to content

feat: add cosine_distance scalar function#21542

Open
crm26 wants to merge 1 commit intoapache:mainfrom
crm26:feat/cosine-distance
Open

feat: add cosine_distance scalar function#21542
crm26 wants to merge 1 commit intoapache:mainfrom
crm26:feat/cosine-distance

Conversation

@crm26
Copy link
Copy Markdown

@crm26 crm26 commented Apr 10, 2026

Summary

  • Adds cosine_distance(array1, array2) / list_cosine_distance — computes cosine distance (1 - cosine similarity) between two numeric arrays
  • Introduces shared vector_math.rs primitives (dot_product_f64, magnitude_f64, convert_to_f64_array) for reuse by follow-on vector functions
  • Returns NULL for zero-magnitude vectors; errors on mismatched lengths
  • Supports List, LargeList, and FixedSizeList with any numeric element type

Part of #21536 — first in a series of split PRs (replacing #21371).

Test plan

  • Unit tests: identical, orthogonal, opposite, 45-degree, zero-magnitude, mismatched-length, NULL, multi-row
  • sqllogictest: cosine_distance.slt covering all edge cases including empty arrays, LargeList, integer coercion, alias, return type
  • Full slt suite (426/426 pass)
  • cargo clippy, cargo fmt, taplo, prettier, cargo machete — all clean

🤖 Generated with Claude Code

Add cosine_distance (and list_cosine_distance alias) to compute cosine
distance between two numeric arrays. Includes shared vector math
primitives in vector_math.rs for reuse by follow-on functions.

Part of apache#21536.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@github-actions github-actions bot added documentation Improvements or additions to documentation sqllogictest SQL Logic Tests (.slt) functions Changes to functions implementation labels Apr 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation functions Changes to functions implementation sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant