Implementation:Eventual Inc Daft Session Sql
| Knowledge Sources | |
|---|---|
| Domains | Data_Engineering, SQL |
| Last Updated | 2026-02-08 00:00 GMT |
Overview
Concrete tool for executing SQL statements within a session context with full catalog awareness provided by the Daft library.
Description
The sql method on Session executes a SQL statement using the session's internal state for table resolution. It passes the SQL string, the session's Rust PySession, an empty CTE map, and the current planning configuration to the Rust sql_exec function. If the result is a PyBuilder (data statement), it wraps it in a DataFrame. If the result is None (non-data statement like DDL), it returns None. Any other return type raises a ValueError.
Usage
Call sess.sql("SQL_STATEMENT") on a Session instance. Use for catalog-aware SQL execution within session-managed workflows.
Code Reference
Source Location
- Repository: Daft
- File:
daft/session.py - Lines: L144-161
Signature
def sql(self, sql: str) -> DataFrame | None
Import
from daft.session import Session
sess = Session()
result = sess.sql("SELECT * FROM catalog.namespace.table")
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| sql | str | Yes | SQL statement to execute. Supports DQL (SELECT), DDL (CREATE TABLE), and DML (INSERT) statements. |
Outputs
| Name | Type | Description |
|---|---|---|
| return | None | A DataFrame for data statements (SELECT, etc.), or None for non-data statements (DDL operations). |
Usage Examples
Basic Usage
from daft.session import Session
import daft
sess = Session()
# Register a temp table
sess.create_temp_table("T", daft.from_pydict({"x": [1, 2, 3]}))
# Execute SQL query
df = sess.sql("SELECT * FROM T WHERE x > 1")
df.show()
Catalog-Aware Query
from daft.session import Session
sess = Session()
sess.attach_catalog(my_catalog, "analytics")
sess.set_catalog("analytics")
sess.set_namespace("production")
# Query resolves through the catalog
df = sess.sql("SELECT * FROM users WHERE active = true")