Skip to content

Read Primitives — Optimizer: Constant Folding

Read Primitives benchmark · all Read Primitives queries

Test constant folding at compile time

Representative SQL

Rendered for DataFusion with default parameters.

SELECT
l_orderkey,
l_partkey,
l_suppkey,
l_linenumber,
l_quantity * (
1.0 + 0.0
) AS simplified_qty,
l_extendedprice * (
2 * 3 + 4
) AS constant_folded,
l_discount + 0.0 - 0.0 AS zero_folded,
CASE WHEN 1 = 1 THEN l_tax ELSE 0 END AS condition_folded,
l_quantity / 1.0 AS division_folded,
l_extendedprice + (
5 - 5
) AS addition_folded
FROM lineitem
WHERE
l_quantity * 1 > 10
AND l_shipdate >= CAST('1995-01-01' AS DATE)
AND l_discount + 0 < 0.1
ORDER BY
l_quantity * (
1 + 0
) DESC,
l_orderkey,
l_linenumber
LIMIT 1000

Representative DataFrame

Expression family (Polars, PySpark, DataFusion), default parameters.

def optimizer_constant_folding_expression_impl(ctx: DataFrameContext) -> Any:
lineitem = ctx.get_table("lineitem")
col = ctx.col
lit = ctx.lit
filtered = (
lineitem.filter(col("l_quantity") > lit(10))
.filter(col("l_shipdate") >= lit(date(1995, 1, 1)))
.filter(col("l_discount") < lit(0.1))
.sort(["l_quantity", "l_orderkey", "l_linenumber"], descending=[True, False, False])
)
return filtered.select(
"l_orderkey",
"l_partkey",
"l_suppkey",
"l_linenumber",
(col("l_quantity") * (lit(1.0) + lit(0.0))).alias("simplified_qty"),
(col("l_extendedprice") * (lit(2) * lit(3) + lit(4))).alias("constant_folded"),
(col("l_discount") + lit(0.0) - lit(0.0)).alias("zero_folded"),
col("l_tax").alias("condition_folded"),
(col("l_quantity") / lit(1.0)).alias("division_folded"),
(col("l_extendedprice") + (lit(5) - lit(5))).alias("addition_folded"),
).limit(1000)

Get the exact query for your run

The blocks above are representative. To get the precise statement a specific platform runs at a specific scale, substitute your own values:

from benchbox import ReadPrimitives
bench = ReadPrimitives(scale_factor=0.01) # your scale factor
queries = bench.get_queries(dialect="datafusion") # your platform's dialect; drop dialect= for native SQL
print(queries['optimizer_constant_folding'])

For the DataFrame form:

from benchbox.core.query_catalog import get_dataframe_render
print(get_dataframe_render('read_primitives', 'optimizer_constant_folding', family="expression").source) # or family="pandas"

Source

Query text: benchbox/core/read_primitives/queries.py