Skip to content

Read Primitives — Array of Struct

Read Primitives benchmark · all Read Primitives queries

Create array of structs from grouped data

Representative SQL

Rendered for DataFusion with default parameters.

SELECT
o_orderkey,
ARRAY_AGG(
STRUCT(l_linenumber, l_partkey, l_quantity, l_extendedprice) ORDER BY l_linenumber
) AS line_items
FROM orders AS o
JOIN lineitem AS l
ON o.o_orderkey = l.l_orderkey
WHERE
o_orderdate = CAST('1995-03-15' AS DATE)
GROUP BY
o_orderkey
ORDER BY
o_orderkey
LIMIT 50

Representative DataFrame

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

def array_of_struct_expression_impl(ctx: DataFrameContext) -> Any:
orders, lineitem = _tables(ctx, "orders", "lineitem")
col = ctx.col
lit = ctx.lit
struct = ctx.struct
filtered_orders = orders.filter(col("o_orderdate") == lit(date(1995, 3, 15)))
joined = lineitem.join(filtered_orders, col("l_orderkey") == col("o_orderkey")).sort("l_linenumber")
return (
joined.group_by("o_orderkey")
.agg(
struct(col("l_linenumber"), col("l_partkey"), col("l_quantity"), col("l_extendedprice"))
.list()
.alias("line_items")
)
.sort("o_orderkey")
.limit(50)
)

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

For the DataFrame form:

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

Source

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