Lightweight Stateless SQL for Databricks

0.17.0 · active · verified Thu Apr 09

Databricks-labs-lsql is a Python library that provides lightweight, stateless SQL execution capabilities for Databricks, built on top of the Databricks SDK. It's designed for serverless or containerized short-lived applications where minimal dependencies and faster startup times are critical, fetching results in JSON format through presigned external links. The current version is 0.17.0, and it appears to have an active release cadence with regular updates.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the `StatementExecutionExt` client using the `databricks-sdk` `WorkspaceClient` and execute SQL queries. It shows how to fetch results by iterating over them and how to execute DDL/DML statements. Authentication relies on environment variables (`DATABRICKS_HOST`, `DATABRICKS_TOKEN`) for the `WorkspaceClient`.

import os
from databricks.sdk import WorkspaceClient
from databricks.labs.lsql.core import StatementExecutionExt

# Ensure DATABRICKS_HOST and DATABRICKS_TOKEN environment variables are set
# For local testing, you might need to export them (e.g., in bash:)
# export DATABRICKS_HOST="https://<your-databricks-workspace-url>"
# export DATABRICKS_TOKEN="<your-personal-access-token>"

w = WorkspaceClient()
# The warehouse_id can be optional if implicitly configured or running within DBR
see = StatementExecutionExt(w, warehouse_id=os.environ.get("TEST_DEFAULT_WAREHOUSE_ID", ""))

# Execute a query and iterate over results
print("Fetching results:")
for pickup_zip, dropoff_zip in see.fetch_all("SELECT pickup_zip, dropoff_zip FROM samples.nyctaxi.trips LIMIT 5"):
    print(f'  pickup_zip={pickup_zip}, dropoff_zip={dropoff_zip}')

# Execute a DDL/DML statement (e.g., create a temporary view)
print("\nExecuting a DDL statement:")
see.execute("CREATE OR REPLACE TEMPORARY VIEW my_temp_view AS SELECT 'hello' as greeting, 1 as number")

# Fetch from the temporary view
print("\nFetching from temporary view:")
for greeting, number in see.fetch_all("SELECT greeting, number FROM my_temp_view"):
    print(f"  greeting='{greeting}', number={number}")

view raw JSON →