Skip to main content

pygen-spark

A code generation library that extends pygen to generate Python User-Defined Table Functions (UDTFs) for CDF Data Models, enabling you to query CDF data directly from Spark SQL.

Latest Release: Version 0.3.2 reduces memory usage in generated view UDTFs for wide views (adaptive API page size, single-pass property resolution, and page memory cleanup). Version 0.3.1 updates security-related dependencies. Version 0.3.0 adds CDF audit headers to generated UDTF HTTP clients. Earlier releases: 0.2.x improved error handling, time series UDTFs, and reserved-word property names.

Full release notes are published on GitHub Releases. Patch releases may update locked dependencies and minimum constraints without API changes.

Note: This document uses PyPI package names for references:

  • PyPI: cognite-pygen (repository: pygen)
  • PyPI: cognite-pygen-spark (repository: pygen-spark)
  • Import paths: cognite.pygen, cognite.pygen_spark

Overview

cognite.pygen_spark (PyPI: cognite-pygen-spark) is a generic Spark UDTF code generation library that works with any Spark cluster (standalone, YARN, Kubernetes, or local development). It generates strongly-typed Python UDTF functions from CDF Data Models using Jinja2 templates, allowing you to query CDF data directly from Spark SQL.

Package Purpose:

  • Generic Spark Support: Works with any Spark cluster, not limited to Databricks
  • Template-Based Generation: Uses Jinja2 templates to generate UDTF code for both Data Model UDTFs and Time Series UDTFs
  • Type Conversion Utilities: Provides TypeConverter class for converting between CDF types, PySpark DataTypes, and SQL DDL
  • Connection Configuration: Provides CDFConnectionConfig Pydantic model for managing CDF credentials from TOML/YAML files
  • Utility Functions: Helper functions for consistent UDTF naming and other generic Spark utilities

Features

  • UDTF Generation: Automatically generates Python UDTF functions for each View in a CDF Data Model
  • Time Series UDTFs: Template-generated UDTFs for querying CDF time series datapoints (single, multiple, latest) using the same template-based generation as Data Model UDTFs
  • Type Safety: Leverages pygen's internal representation for strongly-typed code generation
  • Predicate Pushdown: Generated UDTFs push WHERE filters, LIMIT, COUNT/MIN/MAX (numeric properties), and grouped aggregates to CDF instances/list and instances/aggregate. A grouped row copies space and externalId onto the result and returns at most 1000 groups. Regenerate and re-register every data-model UDTF after upgrading so existing registrations pick up base_url and the group-row identity columns. A trailing base_url parameter is read at query time for Private Link and dedicated clusters.
  • CDF audit headers: Generated UDTF HTTP clients send pygen-main-style x-cdp-app, x-cdp-sdk, and User-Agent for attributable CDF traffic
  • Configuration File Support: Uses TOML/YAML configuration files for secure credential management
  • Generic Spark Support: Works with any Spark cluster, not limited to Databricks
  • Type Conversion Utilities: TypeConverter class for converting between CDF types, PySpark DataTypes, and SQL DDL
  • Connection Configuration: CDFConnectionConfig Pydantic model for managing CDF credentials
  • Utility Functions: Helper functions for consistent UDTF naming and other utilities

Using Generic Spark Utilities

pygen-spark provides generic utilities that work with any Spark cluster:

from cognite.pygen_spark import TypeConverter, CDFConnectionConfig, to_udtf_function_name

# Type conversion utilities
from cognite.client import data_modeling as dm
from pyspark.sql.types import StringType

# Convert CDF property type to PySpark DataType
spark_type = TypeConverter.cdf_to_spark(dm.Text(), is_array=False)
# Returns: StringType()

# Convert PySpark DataType to SQL DDL
sql_ddl = TypeConverter.spark_to_sql_ddl(spark_type)
# Returns: "STRING"

# Connection configuration from TOML
config = CDFConnectionConfig.from_toml("config.toml")
client = config.create_client()

# Convert view external_id to UDTF function name
udtf_name = to_udtf_function_name("MyView")
# Returns: "my_view_udtf"

These utilities are generic and work with any Spark cluster, not just Databricks.

Installation

pip install cognite-pygen-spark

Quick Start

from pathlib import Path
from cognite.client.data_classes.data_modeling.ids import DataModelId
from cognite.pygen import load_cognite_client_from_toml
from cognite.pygen_spark import SparkUDTFGenerator

# Load client from TOML file
client = load_cognite_client_from_toml("config.toml")

# Create generator
generator = SparkUDTFGenerator(
    client=client,
    output_dir=Path("./generated_udtfs"),
    data_model=DataModelId(space="sailboat", external_id="sailboat", version="1"),
    top_level_package="cognite_udtfs",
)

# Generate UDTFs for a Data Model
result = generator.generate_udtfs()

print(f"Generated {result.total_count} UDTF(s)")
for view_id, file_path in result.generated_files.items():
    print(f"  - {view_id}: {file_path}")

# Generate time series UDTFs (template-generated, same as data model UDTFs)
ts_result = generator.generate_time_series_udtfs()
print(f"Generated {ts_result.total_count} time series UDTF(s)")
for udtf_name, file_path in ts_result.generated_files.items():
    print(f"  - {udtf_name}: {file_path}")

See the User Guide for complete documentation on generating, registering, and querying UDTFs.

Architecture

cognite.pygen_spark extends cognite.pygen's architecture:

  • Reuses pygen's View parsing: Leverages pygen's internal representation of CDF Data Models
  • Custom template engine: Uses Jinja2 templates to generate UDTF Python code and SQL Views
  • Extends MultiAPIGenerator: Builds on pygen's code generation infrastructure
  • Consistent template-based generation: Both Data Model UDTFs and Time Series UDTFs use the same Jinja2 template-based generation approach for consistent behavior, error handling, and initialization patterns

See the Technical Plan for detailed architecture documentation.

Requirements

  • Python 3.9+
  • PySpark 3.5+ (required for UDTF support)
  • cognite-pygen (PyPI package name; import: cognite.pygen)
  • cognite-sdk-python (must be installed on all Spark worker nodes)
  • Spark cluster (standalone, YARN, Kubernetes, or local)

Package Structure

pygen-spark/
├── cognite/
│   └── pygen_spark/
│       ├── __init__.py
│       ├── generator.py          # SparkUDTFGenerator
│       ├── udtf_generator.py    # SparkMultiAPIGenerator
│       └── templates/
│           ├── udtf_function.py.jinja
│           ├── view_sql.py.jinja
│           └── udtf_init.py.jinja
├── pyproject.toml
└── README.md

Development

Setup

git clone <repository-url>
cd pygen-spark
pip install -e ".[dev]"

Running Tests

pytest tests/

Spark Cluster Compatibility

This package generates UDTF code that works with any Spark cluster:

  • Code Generation: Works on all Spark versions ✅
  • UDTF Templates: Compatible with PySpark 3.5+ ✅
  • Dependency Management: Requires cognite-sdk on all Spark worker nodes ⚠️

For standalone Spark clusters, ensure cognite-sdk is installed on all worker nodes. See the Installation Guide for details.

Documentation

User Guide

Examples

Technical Documentation

License

[License information]

Contributing

[Contributing guidelines]

Metadata

Release files for cognite-pygen-spark 0.4.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cognite-pygen-spark 0.4.2
File Size Uploaded
cognite_pygen_spark-0.4.2.tar.gz 64.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for cognite-pygen-spark 0.4.2
File Interpreter ABI Platform
cognite_pygen_spark-0.4.2-py3-none-any.whl Python 3 none any Details

Total release size: 145.5 kB

Release files / cognite_pygen_spark-0.4.2.tar.gz

Download URL cognite_pygen_spark-0.4.2.tar.gz
Size 64.2 kB
Tags Source
SHA-256 checksum
How to use checksums
d4aae34300c012fdaf30e1378c9975c57bd9e6f1863a8542639637d5a815b0b3
BLAKE2b-256 checksum
How to use checksums
99f05b322e1334aa14126b386423e953976955ed30601ae12a3364416b4bd2d3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.22

Release files / cognite_pygen_spark-0.4.2-py3-none-any.whl

Download URL cognite_pygen_spark-0.4.2-py3-none-any.whl
Size 81.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
db02be7906992940f1631ddb3da1a403f77bea5b70d1e3c80dfa70fc5c7335e7
BLAKE2b-256 checksum
How to use checksums
cdec79795ce7c716122514770c17b8a41b188f294e0ca0f83e521e1ec06366d8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.22

Release history Release notifications | RSS feed

This release

0.4.2 This release

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page