Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions benchmarks/bench.sh
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ smj: Benchmark for simple sort merge joins, testing various j
dict: Benchmark for dictionary-encoded group-by scenarios
array_agg_distinct: 1000K-group, two-row-per-group array_agg(DISTINCT) benchmark
grouped_count_distinct: Grouped count(DISTINCT) by key type, group count and distinct cardinality
first_last_tz: Grouped first_value and DISTINCT ON ordered by a time zone aware timestamp vs a naive one
compile_profile: Compile and execute TPC-H across selected Cargo profiles, reporting timing and binary size


Expand Down Expand Up @@ -284,6 +285,9 @@ main() {
grouped_count_distinct)
echo "grouped_count_distinct: no external data to generate"
;;
first_last_tz)
echo "first_last_tz: no external data to generate"
;;
asof_join)
data_asof_join
;;
Expand Down Expand Up @@ -688,6 +692,9 @@ main() {
grouped_count_distinct)
run_grouped_count_distinct
;;
first_last_tz)
run_first_last_tz
;;
compile_profile)
run_compile_profile "${PROFILE_ARGS[@]}"
;;
Expand Down Expand Up @@ -1829,6 +1836,16 @@ run_grouped_count_distinct() {
bash -c "$SQL_CARGO_COMMAND"
}

# Runs grouped first_value and DISTINCT ON ordered by a time zone aware timestamp
# and by the same instants without a time zone, over inline data.
run_first_last_tz() {
debug_run env BENCH_NAME=first_last_tz \
BENCH_RESULTS_FILE="$(sql_results_file first_last_tz)" \
FIRST_LAST_TZ_ROWS="${FIRST_LAST_TZ_ROWS:-2000000}" \
${QUERY:+BENCH_QUERY="${QUERY}"} \
bash -c "$SQL_CARGO_COMMAND"
}

# Runs the data-free high-cardinality array_agg(DISTINCT) SQL benchmark.
run_array_agg_distinct() {
echo "Running array_agg_distinct benchmark..."
Expand Down
2 changes: 2 additions & 0 deletions benchmarks/sql_benchmarks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ in the community:
| `clickbench` | ClickBench benchmark |
| `clickbench extended` | 12 additional, more complex queries against the Clickbench dataset |
| `clickbench_sorted` | ClickBench benchmark using a pre-sorted hits file. |
| `first_last_tz` | Grouped `first_value` (Q01) and `DISTINCT ON` (Q03) ordered by a time zone aware timestamp, and the same queries over the same instants without a time zone (Q02, Q04), from [#27046](https://github.com/apache/datafusion/issues/27046). Each pair should take about the same time at any `--partitions`. Size the inline table with `FIRST_LAST_TZ_ROWS` (default `2000000`, two rows per group). |
| `h2o` | The `h2o` benchmark |
| `hj` | Hash join benchmark |
| `imdb` | IMDb benchmark |
Expand Down Expand Up @@ -192,6 +193,7 @@ Some benchmarks use custom environment variables as outlined below:
| PSQ_ROWS | Used in the projection_subquery benchmark to size the two inline tables. | `30000` |
| SPILL_VIEWS_LIMIT_REPEATED | Used in the spill_views benchmark as the memory limit of the `repeated` subgroup. | `40M` |
| SPILL_VIEWS_LIMIT_DISTINCT | Used in the spill_views benchmark as the memory limit of the `distinct` subgroup. | `96M` |
| FIRST_LAST_TZ_ROWS | Used in the first_last_tz benchmark to size the inline table (two rows per group). | `2000000` |

## How it works

Expand Down
29 changes: 29 additions & 0 deletions benchmarks/sql_benchmarks/first_last_tz/benchmarks/q01.benchmark
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name Q01
group first_last_tz

load sql_benchmarks/first_last_tz/init/load.sql

# The time zone does not take part in the ordering: every group must pick the
# same row as with the time zone free column.
assert I
WITH tz AS (
SELECT k, first_value(v ORDER BY ts, o) AS a FROM first_last_tz GROUP BY k
), naive AS (
SELECT k, first_value(v ORDER BY tsn, o) AS a FROM first_last_tz GROUP BY k
)
SELECT NOT EXISTS (SELECT * FROM tz EXCEPT SELECT * FROM naive)
AND NOT EXISTS (SELECT * FROM naive EXCEPT SELECT * FROM tz);
----
true

expect_plan AggregateExec

run
-- first_value ordered by a time zone aware timestamp (issue #27046). The
-- second ORDER BY key keeps the query on the general accumulator, which stores
-- each group's ordering values as ScalarValues; a single primitive key takes a
-- specialized path.
SELECT count(*), sum(a)
FROM (SELECT k, first_value(v ORDER BY ts, o) AS a FROM first_last_tz GROUP BY k);

cleanup sql_benchmarks/first_last_tz/init/cleanup.sql
13 changes: 13 additions & 0 deletions benchmarks/sql_benchmarks/first_last_tz/benchmarks/q02.benchmark
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name Q02
group first_last_tz

load sql_benchmarks/first_last_tz/init/load.sql

expect_plan AggregateExec

run
-- Control for Q01: the same instants without a time zone.
SELECT count(*), sum(a)
FROM (SELECT k, first_value(v ORDER BY tsn, o) AS a FROM first_last_tz GROUP BY k);

cleanup sql_benchmarks/first_last_tz/init/cleanup.sql
27 changes: 27 additions & 0 deletions benchmarks/sql_benchmarks/first_last_tz/benchmarks/q03.benchmark
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name Q03
group first_last_tz

load sql_benchmarks/first_last_tz/init/load.sql

# The time zone does not take part in the ordering: every key must pick the
# same row as with the time zone free column.
assert I
WITH tz AS (
SELECT DISTINCT ON (k) k, v FROM first_last_tz ORDER BY k, ts DESC, o
), naive AS (
SELECT DISTINCT ON (k) k, v FROM first_last_tz ORDER BY k, tsn DESC, o
)
SELECT NOT EXISTS (SELECT * FROM tz EXCEPT SELECT * FROM naive)
AND NOT EXISTS (SELECT * FROM naive EXCEPT SELECT * FROM tz);
----
true

expect_plan AggregateExec

run
-- Latest row per key by a time zone aware timestamp (issue #27046). DISTINCT ON
-- is planned as one first_value(... ORDER BY k, ts DESC, o) per output column.
SELECT count(*), sum(v)
FROM (SELECT DISTINCT ON (k) v FROM first_last_tz ORDER BY k, ts DESC, o);

cleanup sql_benchmarks/first_last_tz/init/cleanup.sql
13 changes: 13 additions & 0 deletions benchmarks/sql_benchmarks/first_last_tz/benchmarks/q04.benchmark
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name Q04
group first_last_tz

load sql_benchmarks/first_last_tz/init/load.sql

expect_plan AggregateExec

run
-- Control for Q03: the same instants without a time zone.
SELECT count(*), sum(v)
FROM (SELECT DISTINCT ON (k) v FROM first_last_tz ORDER BY k, tsn DESC, o);

cleanup sql_benchmarks/first_last_tz/init/cleanup.sql
17 changes: 17 additions & 0 deletions benchmarks/sql_benchmarks/first_last_tz/first_last_tz.suite
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
description = "Grouped first_value and DISTINCT ON ordered by a time zone aware timestamp vs the same instants without a time zone (issue #27046); compare --partitions 1, 4 and 12"

query_pattern = "q{QUERY_ID_PADDED}.benchmark"

[[options]]
name = "rows"
env = "FIRST_LAST_TZ_ROWS"
default = "2000000"
help = "Input rows generated outside the timed query, in groups of two rows."

[[examples]]
command = "cargo run --release --bin benchmark_runner -- first_last_tz"
description = "Run first_value (Q01) and DISTINCT ON (Q03) over a time zone aware timestamp, and their controls without a time zone (Q02, Q04)."

[[examples]]
command = "cargo run --release --bin benchmark_runner -- first_last_tz --partitions 12"
description = "Run at 12 target partitions, where the time zone aware queries were several times slower than the controls."
1 change: 1 addition & 0 deletions benchmarks/sql_benchmarks/first_last_tz/init/cleanup.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE first_last_tz;
13 changes: 13 additions & 0 deletions benchmarks/sql_benchmarks/first_last_tz/init/load.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-- 1M groups of two rows whose order in `ts` is scrambled, so the second row
-- wins in about two thirds of the groups. `ts` is Timestamp(Microsecond, "UTC"),
-- `tsn` the same instants without a time zone.
CREATE TABLE first_last_tz AS
SELECT value / 2 AS k,
(value * 7919) % 1000003 AS o,
value % 977 AS v,
arrow_cast(
to_timestamp_micros(1735689600000000 + ((value * 2654435761) % 1000000007) * 1000),
'Timestamp(Microsecond, Some("UTC"))'
) AS ts,
to_timestamp_micros(1735689600000000 + ((value * 2654435761) % 1000000007) * 1000) AS tsn
FROM range(${FIRST_LAST_TZ_ROWS:-2000000});
Loading