Databricks

Query Databricks SQL warehouses from deployed models and write forecasts back to Unity Catalog tables.

rb.sources.databricks connects deployed Rebase code to a Databricks SQL warehouse. It shares the uniform data-source surface: read, read_bitemporal, and write.

Install

uv pip install "rebase-toolkit[databricks]"

Ships databricks-sql-connector and pyarrow. Declare it on the image you deploy:

image = rb.Image.python("3.12").uv_pip_install("rebase-toolkit[databricks]")

Authenticate

OAuth machine-to-machine (a Databricks service principal) is recommended — tokens are minted automatically and never stored:

# 1. Create a service principal in your Databricks account console and
#    generate an OAuth secret for it; grant it access to the SQL warehouse
#    and catalog.

# 2. Store the credentials as a Rebase secret bundle
rebase secret create acme-databricks \
    DATABRICKS_SERVER_HOSTNAME=adb-1234567890.1.azuredatabricks.net \
    DATABRICKS_HTTP_PATH=/sql/1.0/warehouses/abc123 \
    DATABRICKS_CLIENT_ID=xxxxxxxx-... \
    DATABRICKS_CLIENT_SECRET=-  < secret.txt

A personal access token (DATABRICKS_TOKEN) works too and takes precedence when set. Find server_hostname and http_path under your SQL warehouse's Connection details tab.

Settings

SettingEnvironment variableRequired
server_hostnameDATABRICKS_SERVER_HOSTNAME / DATABRICKS_HOST
http_pathDATABRICKS_HTTP_PATH
access_tokenDATABRICKS_TOKEN / DATABRICKS_ACCESS_TOKENone auth method
client_id + client_secretDATABRICKS_CLIENT_ID, DATABRICKS_CLIENT_SECRETone auth method
catalog, schemaDATABRICKS_CATALOG, DATABRICKS_SCHEMAoptional

Any setting can also be passed to the factory directly, or namespaced per connection as REBASE_SOURCE_<CONNECTION>_<FIELD> — see credential precedence.

Read

The connector binds named parameters with %(name)s and fetches results via Arrow:

import rebase as rb

src = rb.sources.databricks(connection="acme")
df = src.read(
    "SELECT ts, issued_at, load_mw FROM energy.demand WHERE site = %(site)s",
    params={"site": "site-001"},
)

For backtesting, declare knowledge time so emflow can prove there is no leakage:

from rebase.sources import BitemporalSpec

spec = BitemporalSpec(valid_time="ts", knowledge_time="issued_at")
df = src.read_bitemporal(
    "SELECT ts, issued_at, load_mw FROM energy.demand",
    spec,
)

Write forecasts back

Writes use batched INSERT statements (1,000 rows per statement) — suited to forecast-sized results. mode="replace" truncates the table first:

src.write(forecast_df, "energy.forecasts", mode="append")

For large bulk loads, stage Parquet in cloud storage and COPY INTO instead.

Reference

On this page