Implementation:Eventual Inc Daft Daft Sql Expr
| Knowledge Sources | |
|---|---|
| Domains | Data_Engineering, SQL |
| Last Updated | 2026-02-08 00:00 GMT |
Overview
Concrete tool for parsing SQL expression strings into Daft Expression objects provided by the Daft library.
Description
The daft.sql_expr function parses a SQL string into a Daft Expression. It delegates to the Rust _sql_expr function for parsing and wraps the result in a Python Expression object. The resulting expression can be used in any DataFrame operation that accepts expressions, such as select, with_column, where, and agg. This function is also called automatically by some DataFrame methods (like where) when a string argument is provided.
Usage
Import via import daft and call daft.sql_expr("expression"). Use when you want to construct DataFrame expressions using SQL syntax.
Code Reference
Source Location
- Repository: Daft
- File:
daft/sql/sql.py - Lines: L19-73
Signature
def sql_expr(sql: str) -> Expression
Import
import daft
expr = daft.sql_expr("col1 + col2")
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| sql | str | Yes | A SQL expression string to parse into a Daft Expression. |
Outputs
| Name | Type | Description |
|---|---|---|
| return | Expression | A Daft Expression representing the parsed SQL, usable in any DataFrame operation. |
Usage Examples
Basic Usage
import daft
# Create a simple expression
expr = daft.sql_expr("1 + 2")
print(expr) # lit(1) + lit(2)
# Use in a DataFrame operation
df = daft.from_pydict({"a": [1, 2, 3], "b": [4, 5, 6]})
df = df.with_column("c", daft.sql_expr("a + b"))
df.show()
Filter with SQL Expression
import daft
df = daft.from_pydict({"x": [1, 2, 3], "y": [4, 5, 6]})
# Using sql_expr explicitly
result = df.where(daft.sql_expr("x < 3 AND y > 4"))
# Or using automatic string conversion in where()
result = df.where("x < 3 AND y > 4")