Skip to content

Read Primitives — Struct Field Access

Read Primitives benchmark · all Read Primitives queries

Access struct fields by name

Representative SQL

Rendered for DataFusion with default parameters.

WITH customer_info AS (
SELECT
c_custkey,
STRUCT(c_name AS name, c_phone AS phone, c_acctbal AS balance) AS info
FROM customer
WHERE
c_nationkey = 1
)
SELECT
c_custkey,
info.name,
info.balance
FROM customer_info
WHERE
info.balance > 5000
ORDER BY
c_custkey
LIMIT 100

Representative DataFrame

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

def struct_access_expression_impl(ctx: DataFrameContext) -> Any:
customer = ctx.get_table("customer")
col = ctx.col
lit = ctx.lit
struct = ctx.struct
with_struct = customer.filter(col("c_nationkey") == lit(1)).select(
"c_custkey",
struct(col("c_name").alias("name"), col("c_phone").alias("phone"), col("c_acctbal").alias("balance")).alias(
"info"
),
)
return (
with_struct.select(
"c_custkey",
col("info").struct.field("name").alias("name"),
col("info").struct.field("balance").alias("balance"),
)
.filter(col("balance") > lit(5000))
.limit(100)
)

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['struct_access'])

For the DataFrame form:

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

Source

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