Skip to content

Read Primitives — Array Slice

Read Primitives benchmark · all Read Primitives queries

Get slice/subset of array

Representative SQL

Rendered for DataFusion with default parameters.

WITH order_prices AS (
SELECT
o_custkey,
ARRAY_AGG(o_totalprice ORDER BY o_totalprice DESC) AS prices
FROM orders
GROUP BY
o_custkey
HAVING
COUNT(*) >= 5
)
SELECT
o_custkey,
ARRAY_SLICE(prices, 1, 3) AS top_3_orders
FROM order_prices
ORDER BY
o_custkey
LIMIT 100

Representative DataFrame

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

def array_slice_expression_impl(ctx: DataFrameContext) -> Any:
orders = ctx.get_table("orders")
col = ctx.col
order_prices = orders.group_by("o_custkey").agg(
col("o_totalprice").sort(descending=True).list().alias("prices"), col("o_totalprice").count().alias("cnt")
)
return (
order_prices.filter(col("cnt") >= 5)
.with_columns(col("prices").list.slice(0, 3).alias("top_3_orders"))
.select("o_custkey", "top_3_orders")
.sort("o_custkey")
.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['array_slice'])

For the DataFrame form:

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

Source

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