How eakmanrq/sqlframe works
1. What this repo is
SQLFrame is a Python library that recreates the PySpark DataFrame API while translating operations into SQL that can run on multiple backends. Its core architecture is an API compatibility layer plus engine-specific adapters.
2. Primary layering
The codebase is organized into three major layers:
- sqlframe/base: engine-agnostic DataFrame semantics and SQL construction
- sqlframe/<engine>: backend adapters for execution and dialect differences
- sqlframe/standalone: SQL generation without any live database connection
3. Core abstraction layer
sqlframe/base contains the shared object model: Session, DataFrame, Column, Group, Table, Catalog, ReaderWriter, Window, UDF, functions, and supporting transforms/operations/types. This is where query plans are represented and where API methods are normalized before being rendered to SQL.
4. How a transformation flows
A user constructs a DataFrame pipeline using Spark-like methods. Each method call updates the internal relational plan rather than executing immediately. When the user asks for SQL or triggers execution, the base layer composes the plan into a backend-specific SQL statement.
5. Engine adapters
Each engine package (bigquery, databricks, duckdb, postgres, redshift, snowflake, spark) subclasses or specializes the base abstractions. These adapters encapsulate:
- dialect-specific SQL rendering
- engine-specific function behavior
- session and catalog behavior
- table/read/write integration
- UDF and window function differences
6. Dialect and function boundaries
The engine folders include their own functions.py, column.py, dataframe.py, group.py, window.py, session.py, catalog.py, table.py, readwriter.py, types.py, and udf.py. That structure indicates the repo isolates engine-specific SQL semantics at the boundary, while preserving a common user-facing API.
7. Standalone mode
sqlframe/standalone is a special backend that does not connect to any engine. Its role is to produce SQL for inspection, debugging, or sharing. Architecturally, it is the pure compilation path: API objects in, SQL out, no execution backend.
8. Activation and Spark compatibility
The top-level sqlframe.activate entry point lets SQLFrame replace or interpose on PySpark imports. That means the library supports two integration styles:
- direct engine-specific sessions like DuckDBSession
- compatibility mode where pyspark.sql imports resolve to SQLFrame behavior
9. Execution and I/O boundary
Reading, writing, and table access are separated from transformation logic via session/readwriter/table/catelog modules. This keeps data source interaction at the edges while transformations remain engine-neutral until compilation/execution time.
10. Testing strategy
The tests mirror the architecture:
- unit tests for activation and engine-specific configuration
- integration tests for shared DataFrame/session/catalog/table behavior
- backend-specific integration suites per engine
- fixture data in CSV, JSON, Parquet, Delta, and TPC-DS SQL files
This suggests the project validates both API compatibility and cross-engine SQL correctness.
11. External systems
The main external dependencies are the supported execution engines themselves: BigQuery, Databricks, DuckDB, Postgres, Redshift, Snowflake, and Spark. The repo also includes generated docs and GitHub Actions for CI/publishing, but those support the library rather than changing runtime architecture.
12. What to draw in an architecture diagram
The most accurate diagram is:
user code / PySpark-like API → SQLFrame base objects → engine adapter layer → backend SQL dialect / execution engine
with a separate branch for standalone that stops at SQL generation, and a shared boundary for reading/writing/catalog/UDF/window behavior.
Main entry points: session.py Session · utils.py Testing · dataframe.py DataFrame · __init__.py Activate
Components 20
Public API
User-facing entry points and compatibility hooks
Core Layer
Engine-neutral DataFrame semantics and SQL plan construction
- Session · Coordinates frames, catalogs, readers, and SQL rendering
sqlframe/base/session.py - DataFrame · Spark-like transformation API backed by a query plan
sqlframe/base/dataframe.py - Column · Column expressions and SQL-ready operators
sqlframe/base/column.py - Functions · Shared function catalog and normalized helpers
sqlframe/base/functions.py - Table · Table access and schema-aware table operations
sqlframe/base/table.py - Read/Write · Input and output adapters for external data sources
sqlframe/base/readerwriter.py - Catalog · Database and schema lookup layer
sqlframe/base/catalog.py - Window · Window specification and analytic function support
sqlframe/base/window.py - UDFs · User-defined function normalization and SQL integration
sqlframe/base/udf.py
Engine Adapters
Backend-specific SQL dialect and execution integrations
- BigQuery · BigQuery-specific session, dialect, and I/O behavior
sqlframe/bigquery/session.py - Databricks · Databricks-specific session, dialect, and I/O behavior
sqlframe/databricks/session.py - DuckDB · DuckDB-specific session, dialect, and I/O behavior
sqlframe/duckdb/session.py - Postgres · PostgreSQL-specific session, dialect, and I/O behavior
sqlframe/postgres/session.py - Redshift · Redshift-specific session, dialect, and I/O behavior
sqlframe/redshift/session.py - Snowflake · Snowflake-specific session, dialect, and I/O behavior
sqlframe/snowflake/session.py - Spark · Spark-compatible session, dialect, and I/O behavior
sqlframe/spark/session.py
Standalone
Pure SQL generation without a live backend
- Standalone · SQL-only compilation path without database execution
sqlframe/standalone/session.py
Support
Testing and shared helpers
- Testing · Shared testing utilities and engine verification helpers
sqlframe/testing/utils.py
Connections 27
- sqlframe → Activate: exports · Package surface exposes compatibility activation
- Activate → Session: routes to · Compatibility mode resolves into SQLFrame sessions
- Session → DataFrame: creates · Sessions construct and manage dataframes
- DataFrame → Column: uses · Transformations are expressed with column objects
- DataFrame → Functions: calls · High-level API normalizes through shared functions
- DataFrame → Window: uses · Analytic operations flow through window specs
- DataFrame → UDFs: extends · DataFrame operations can reference UDFs
- Session → Catalog: consults · Session delegates metadata and schema lookup
- Session → Table: accesses · Session mediates table-level operations
- Session → Read/Write: reads/writes · Session coordinates external data access
- Session → BigQuery: adapts · Engine sessions specialize the shared base session
- Session → Databricks: adapts · Engine sessions specialize the shared base session
- Session → DuckDB: adapts · Engine sessions specialize the shared base session
- Session → Postgres: adapts · Engine sessions specialize the shared base session
- Session → Redshift: adapts · Engine sessions specialize the shared base session
- Session → Snowflake: adapts · Engine sessions specialize the shared base session
- Session → Spark: adapts · Engine sessions specialize the shared base session
- Session → Standalone: compiles · Standalone mode reuses core semantics for SQL generation only
- Testing → Activate: verifies · Tests cover compatibility activation behavior
- Testing → BigQuery: verifies · Tests cover engine-specific behavior
- Testing → Databricks: verifies · Tests cover engine-specific behavior
- Testing → DuckDB: verifies · Tests cover engine-specific behavior
- Testing → Postgres: verifies · Tests cover engine-specific behavior
- Testing → Redshift: verifies · Tests cover engine-specific behavior
- Testing → Snowflake: verifies · Tests cover engine-specific behavior
- Testing → Spark: verifies · Tests cover engine-specific behavior
- Testing → Standalone: verifies · Tests cover standalone SQL compilation