Skip to main content
SQLFrame Logo

SQLFrame implements the PySpark DataFrame API in order to enable running transformation pipelines directly on database engines - no Spark clusters or dependencies required.

SQLFrame currently supports the following engines:

SQLFrame also has a "Standalone" session that be used to generate SQL without any connection to a database engine.

SQLFrame is great for:

  • Users who want a DataFrame API that leverages the full power of their engine to do the processing
  • Users who want to run PySpark code quickly locally without the overhead of starting a Spark session
  • Users who want a SQL representation of their DataFrame code for debugging or sharing with others
  • Users who want to run PySpark DataFrame code without the complexity of using Spark for processing

Installation

# BigQuery
pip install "sqlframe[bigquery]"
# Databricks
pip install "sqlframe[databricks]"
# DuckDB
pip install "sqlframe[duckdb]"
# Postgres
pip install "sqlframe[postgres]"
# Snowflake
pip install "sqlframe[snowflake]"
# Spark
pip install "sqlframe[spark]"
# Redshift (in development)
pip install "sqlframe[redshift]"
# Standalone
pip install sqlframe
# Or from conda-forge
conda install -c conda-forge sqlframe

See specific engine documentation for additional setup instructions.

Configuration

SQLFrame generates consistently accurate yet complex SQL for engine execution. However, when using df.sql(optimize=True), it produces more human-readable SQL. For details on how to configure this output and leverage OpenAI to enhance the SQL, see Generated SQL Configuration.

SQLFrame by default uses the Spark dialect for input and output. This can be changed to make SQLFrame feel more like a native DataFrame API for the engine you are using. See Input and Output Dialect Configuration.

Activating SQLFrame

SQLFrame can either replace pyspark imports or be used alongside them. To replace pyspark imports, use the activate function to set the engine to use.

from sqlframe import activate

# Activate SQLFrame to run directly on DuckDB
activate(engine="duckdb")

from pyspark.sql import SparkSession
session = SparkSession.builder.getOrCreate()

SQLFrame can also be directly imported which both maintains pyspark imports but also allows for a more engine-native DataFrame API:

from sqlframe.duckdb import DuckDBSession

session = DuckDBSession.builder.getOrCreate()

Example Usage

from sqlframe import activate

# Activate SQLFrame to run directly on BigQuery
activate(engine="bigquery")

from pyspark.sql import SparkSession
from pyspark.sql import functions as F
from pyspark.sql import Window

session = SparkSession.builder.getOrCreate()
table_path = '"bigquery-public-data".samples.natality'
# Top 5 years with the greatest year-over-year % change in new families with single child
df = (
  session.table(table_path)
  .where(F.col("ever_born") == 1)
  .groupBy("year")
  .agg(F.count("*").alias("num_single_child_families"))
  .withColumn(
    "last_year_num_single_child_families",
    F.lag(F.col("num_single_child_families"), 1).over(Window.orderBy("year"))
  )
  .withColumn(
    "percent_change",
    (F.col("num_single_child_families") - F.col("last_year_num_single_child_families"))
    / F.col("last_year_num_single_child_families")
  )
  .orderBy(F.abs(F.col("percent_change")).desc())
  .select(
    F.col("year").alias("year"),
    F.format_number("num_single_child_families", 0).alias("new families single child"),
    F.format_number(F.col("percent_change") * 100, 2).alias("percent change"),
  )
  .limit(5)
)
>>> df.sql(optimize=True)
WITH `t94228` AS (
  SELECT
    `natality`.`year` AS `year`,
    COUNT(*) AS `num_single_child_families`
  FROM `bigquery-public-data`.`samples`.`natality` AS `natality`
  WHERE
    `natality`.`ever_born` = 1
  GROUP BY
    `natality`.`year`
), `t39093` AS (
  SELECT
    `t94228`.`year` AS `year`,
    `t94228`.`num_single_child_families` AS `num_single_child_families`,
    LAG(`t94228`.`num_single_child_families`, 1) OVER (ORDER BY `t94228`.`year`) AS `last_year_num_single_child_families`
  FROM `t94228` AS `t94228`
)
SELECT
  `t39093`.`year` AS `year`,
  FORMAT('%\'.0f', ROUND(CAST(`t39093`.`num_single_child_families` AS FLOAT64), 0)) AS `new families single child`,
  FORMAT('%\'.2f', ROUND(CAST((((`t39093`.`num_single_child_families` - `t39093`.`last_year_num_single_child_families`) / `t39093`.`last_year_num_single_child_families`) * 100) AS FLOAT64), 2)) AS `percent change`
FROM `t39093` AS `t39093`
ORDER BY
  ABS(`percent_change`) DESC
LIMIT 5
>>> df.show()
+------+---------------------------+----------------+
| year | new families single child | percent change |
+------+---------------------------+----------------+
| 1989 |         1,650,246         |     25.02      |
| 1974 |          783,448          |     14.49      |
| 1977 |         1,057,379         |     11.38      |
| 1985 |         1,308,476         |     11.15      |
| 1975 |          868,985          |     10.92      |
+------+---------------------------+----------------+

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sqlframe-4.4.0.tar.gz (29.7 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sqlframe-4.4.0-py3-none-any.whl (222.2 kB view details)

Uploaded Python 3

File details

Details for the file sqlframe-4.4.0.tar.gz.

File metadata

  • Download URL: sqlframe-4.4.0.tar.gz
  • Upload date:
  • Size: 29.7 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for sqlframe-4.4.0.tar.gz
Algorithm Hash digest
SHA256 ba0716cdc71bac9aefc2fd76c15a118b7ccb61a3ca79e645935b95ec9c609113
MD5 d7af76401b7772e9738ed0bb9bd57f46
BLAKE2b-256 e7b60033601d1eac72422d0135bc4ac676fd16ddbede4b834fcb30f11f2d5682

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlframe-4.4.0.tar.gz:

Publisher: publish.workflow.yaml on eakmanrq/sqlframe

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sqlframe-4.4.0-py3-none-any.whl.

File metadata

  • Download URL: sqlframe-4.4.0-py3-none-any.whl
  • Upload date:
  • Size: 222.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for sqlframe-4.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ff73474ab8b21dc5eca9b650dd7191a682ddedc55ef7472cf60a3e91783ca43b
MD5 6a6b36aa9639fa6e5a4d83edae57eedb
BLAKE2b-256 ae81b251146309b797a8054804acf0ad19c24b0ac4bac0581a6332fd686be923

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlframe-4.4.0-py3-none-any.whl:

Publisher: publish.workflow.yaml on eakmanrq/sqlframe

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

4.4.0 This release

2 files

4.3.0

2 files

4.2.0

2 files

4.1.0

2 files

4.0.0

2 files

3.49.0

2 files

3.48.0

2 files

3.47.0

2 files

3.46.2

2 files

3.46.1

2 files

3.46.0

2 files

3.45.0

2 files

3.44.1

2 files

3.44.0

2 files

3.43.8

2 files

3.43.7

2 files

3.43.6

2 files

3.43.5

2 files

3.43.4

2 files

3.43.3

2 files

3.43.2

2 files

3.43.1

2 files

3.43.0

2 files

3.42.0

2 files

3.41.0

2 files

3.40.2

2 files

3.40.1

2 files

3.40.0

2 files

3.39.4

2 files

3.39.3

2 files

3.39.2

2 files

3.39.1

2 files

3.39.0

2 files

3.38.2

2 files

3.38.1

2 files

3.38.0

2 files

3.37.0

2 files

3.36.3

2 files

3.36.2

2 files

3.36.1

2 files

3.36.0

2 files

3.35.1

2 files

3.35.0

2 files

3.34.0

2 files

3.33.1

2 files

3.33.0

2 files

3.32.1

2 files

3.32.0

2 files

3.31.4

2 files

3.31.3

2 files

3.31.2

2 files

3.31.1

2 files

3.31.0

2 files

3.30.0

2 files

3.29.1

2 files

3.29.0

2 files

3.28.2

2 files

3.28.1

2 files

3.28.0

2 files

3.27.1

2 files

3.27.0

2 files

3.26.0

2 files

3.25.0

2 files

3.24.1

2 files

3.24.0

2 files

3.23.0

2 files

3.22.1

2 files

3.22.0

2 files

3.21.1

2 files

3.21.0

2 files

3.20.0

2 files

3.19.0

2 files

3.18.1

2 files

3.18.0

2 files

3.17.1

2 files

3.17.0

2 files

3.16.0

2 files

3.15.1

2 files

3.15.0

2 files

3.14.2

2 files

3.14.1

2 files

3.14.0

2 files

3.13.4

2 files

3.13.3

2 files

3.13.2

2 files

3.13.1

2 files

3.13.0

2 files

3.12.0

2 files

3.11.0

2 files

3.10.1

2 files

3.10.0

2 files

3.9.3

2 files

3.9.2

2 files

3.9.1

2 files

3.9.0

2 files

3.8.2

2 files

3.8.1

2 files

3.8.0

2 files

3.7.0

2 files

3.6.0

2 files

3.5.0

2 files

3.4.1

2 files

3.4.0

2 files

3.3.1

2 files

3.3.0

2 files

3.2.0

2 files

3.1.1

2 files

3.1.0

2 files

3.0.0

2 files

2.4.0

2 files

2.3.0

2 files

2.2.0

2 files

2.1.0

2 files

2.0.0

2 files

1.14.0

2 files

1.13.0

2 files

1.12.0

2 files

1.11.0

2 files

1.10.0

2 files

1.9.0

2 files

1.8.0

2 files

1.7.1

2 files

1.7.0

2 files

1.6.3

2 files

1.6.2

2 files

1.6.1

2 files

1.6.0

2 files

1.5.5

2 files

1.5.4

2 files

1.5.3

2 files

1.5.2

2 files

1.5.1

2 files

1.5.0

2 files

1.4.0

2 files

1.3.0

2 files

1.2.0

2 files

1.1.3

2 files

1.1.2

2 files

1.1.1

2 files

1.1.0

2 files

1.0.0

2 files

0.0.3

2 files

0.0.2

2 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