🧮 pyspark-vector
A lightweight, Spark-native library for computing vector similarity and distance directly in PySpark DataFrames — no UDFs, no Pandas overhead.
It lets you perform operations like cosine similarity, Euclidean distance, or dot product directly using Spark SQL column expressions, just like a mini vector database.
🚀 Features
Section titled “🚀 Features”- ✅ Native Spark column expressions (
zip_with,aggregate,transform) - ✅ Fast — no Python UDFs or Arrow overhead
- ✅ Extensible metric registry (
cosine,euclidean,dot,manhattan) - ✅ Functional API (
vector_distance) and friendly wrapper (vector_search) - ✅ Easy to integrate with DataFrames, SQL, or Dataiku flows
- ✅ Fully unit-tested with
pytest
📦 Installation
Section titled “📦 Installation”Clone the repo and install in editable mode (recommended for development):
git clone https://github.com/PySparky/pysparky-vector.gitcd pysparky-vectorpip install -e .[dev]Requirements
Section titled “Requirements”- Python ≥ 3.9
- PySpark ≥ 3.3.0
- pytest (for development)
🧱 Project Structure
Section titled “🧱 Project Structure”pysparky_vector/├── core/│ └── vector_ops.py # main logic (vector_distance, vector_search)├── metrics/│ ├── similarity.py # cosine, dot│ ├── distance.py # euclidean, manhattan│ └── registry.py # metric registry└── tests/ ├── conftest.py ├── test_metrics.py └── test_vector_ops.py🧠 Quick Start
Section titled “🧠 Quick Start”from pyspark.sql import SparkSessionfrom pysparky_vector import vector_search
spark = SparkSession.builder.appName("VectorDemo").getOrCreate()
data = [ ("A", [0.1, 0.2, 0.3]), ("B", [0.4, 0.5, 0.6]), ("C", [0.2, 0.1, 0.0])]df = spark.createDataFrame(data, ["id", "vector"])
query = [0.2, 0.1, 0.2]
# Cosine similarity (higher = closer)df_cos = vector_search(df, query, metric="cosine", top_k=2)df_cos.show(truncate=False)
# Euclidean distance (lower = closer)df_euc = vector_search(df, query, metric="euclidean")df_euc.show(truncate=False)⚙️ API Overview
Section titled “⚙️ API Overview”The package exports vector_distance for custom metric expressions and
vector_search for choosing a registered metric by name. Their signatures and
parameter documentation are generated from the Python source below.
Browse the generated API reference
vector_distancefunction#
def vector_distance(df, query_vector, metric_fn, vector_col='vector', top_k=None, alias=None, desc_order=False)vector_searchfunction#
def vector_search(df, query_vector, metric='euclidean', vector_col='vector', top_k=None, alias=None)User-friendly wrapper that looks up the metric from registry.
The registered metric keys and implementation links are documented in the generated registry reference.
🧪 Testing
Section titled “🧪 Testing”Run all unit tests:
pytest -vExample output:
tests/test_metrics.py::test_cosine_similarity PASSEDtests/test_vector_ops.py::test_vector_search_registry PASSEDTo build the package and verify license metadata in both distributions:
mise install "python@3.11.18" "uv@0.13.10"mise exec python@3.11.18 uv@0.13.10 -- uv build --out-dir distmise exec python@3.11.18 uv@0.13.10 -- python scripts/check_license_consistency.py --dist dist📘 Development Tips
Section titled “📘 Development Tips”| Command | Description |
|---|---|
pip install -e .[dev] |
Editable install for live code updates |
pytest -v |
Run tests |
black pysparky_vector |
Format code |
isort pysparky_vector |
Sort imports |
To verify import path:
import pysparky_vectorprint(pysparky_vector.__file__)🪶 Example: Add a New Metric
Section titled “🪶 Example: Add a New Metric”- Create
pysparky_vector/metrics/my_metric.py - Implement a new Spark expression:
def chebyshev_expr(col_vector, query_array):from pyspark.sql import functions as Freturn F.aggregate(F.zip_with(col_vector, query_array, lambda x, y: F.abs(x - y)),F.lit(0.0),lambda acc, x: F.greatest(acc, x))
- Register it in
metrics/registry.py:from .my_metric import chebyshev_exprMETRICS["chebyshev"] = {"fn": chebyshev_expr, "desc_order": False} - Use it:
vector_search(df, query, metric="chebyshev").show()
🪪 License
Section titled “🪪 License”Original project code and documentation are available under the MIT License; see LICENSE. Third-party material retains its original license; see the THIRD-PARTY-NOTICES.md.
❤️ Acknowledgements
Section titled “❤️ Acknowledgements”Inspired by the performance and expressiveness of Spark SQL, this project aims to bring vector-DB-like semantics into the PySpark ecosystem, making it easier to run large-scale similarity searches without leaving Spark.