alamb commented on code in PR #19042: URL: https://github.com/apache/datafusion/pull/19042#discussion_r2590170963
########## benchmarks/README.md: ########## @@ -804,3 +804,40 @@ Getting results... cancelling thread done dropping runtime in 83.531417ms ``` + +## Sorted Data Benchmarks + +### Data Sorted ClickBench + +Benchmark for queries on pre-sorted data to test sort order optimization. + +This benchmark uses a subset of the ClickBench dataset (hits_0.parquet, ~150MB) that has been pre-sorted by the EventTime column. The queries are designed to test DataFusion's performance when the data is already sorted. Review Comment: ```suggestion This benchmark uses a subset of the ClickBench dataset (hits_0.parquet, ~150MB) that has been pre-sorted by the EventTime column. The queries are designed to test DataFusion's performance when the data is already sorted as is common in timeseries workloads. ``` ########## benchmarks/sort_clickbench.py: ########## @@ -0,0 +1,244 @@ +#!/usr/bin/env python3 + +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +""" +Sort ClickBench data by EventTime for reverse scan benchmark. +Enhanced version with configurable row group size and optimization options. +""" + +import sys +import argparse +from pathlib import Path + +try: + import pyarrow.parquet as pq + import pyarrow.compute as pc +except ImportError: + print("Error: pyarrow is not installed.") + print("Please install it with: pip install pyarrow") + sys.exit(1) + + +def sort_clickbench_data( + input_path: str, + output_path: str, + row_group_size: int = 64 * 1024, # 64k rows default + compression: str = 'zstd', + verify: bool = True +): + """Sort parquet file by EventTime column with optimized settings.""" + + input_file = Path(input_path) + output_file = Path(output_path) + + if not input_file.exists(): + print(f"Error: Input file not found: {input_file}") + sys.exit(1) + + if output_file.exists(): + print(f"Sorted file already exists: {output_file}") + if verify: + verify_sorted_file(output_file) + return + + try: + print(f"Reading {input_file.name}...") + table = pq.read_table(str(input_file)) + + print(f"Original table has {len(table):,} rows") + print("Sorting by EventTime...") + + # Sort the table by EventTime + sorted_indices = pc.sort_indices(table, sort_keys=[("EventTime", "ascending")]) + sorted_table = pc.take(table, sorted_indices) + + print(f"Sorted table has {len(sorted_table):,} rows") + + # Verify sort + event_times = sorted_table.column('EventTime').to_pylist() + if event_times and verify: + print(f"First EventTime: {event_times[0]}") + print(f"Last EventTime: {event_times[-1]}") + # Verify ascending order + is_sorted = all(event_times[i] <= event_times[i+1] for i in range(min(1000, len(event_times)-1))) + print(f"Sort verification (first 1000 rows): {'✓ PASS' if is_sorted else '✗ FAIL'}") + + print(f"Writing sorted file to {output_file}...") + print(f" Row group size: {row_group_size:,} rows") + print(f" Compression: {compression}") + + # Write sorted table with optimized settings + pq.write_table( Review Comment: rather than using pq, would it be possible to use `datafusion-cli` for this (to reduce the number of dependencoes)? https://datafusion.apache.org/user-guide/sql/dml.html#copy For example, something like ```sql > COPY (SELECT * from 'hits.parquet' ORDER BY "EventTime") TO 'output.parquet' OPTIONS (MAX_ROW_GROUP_SIZE 64000); ``` I think that is pretty equialent ########## benchmarks/bench.sh: ########## @@ -1197,6 +1206,75 @@ compare_benchmarks() { } +# Creates sorted ClickBench data from hits_0.parquet (partitioned dataset) +# The data is sorted by EventTime in ascending order +# Using hits_0.parquet (~150MB) instead of full hits.parquet (~14GB) for faster testing +data_sorted_clickbench() { + SORTED_FILE="${DATA_DIR}/hits_0_sorted.parquet" + ORIGINAL_FILE="${DATA_DIR}/hits_partitioned/hits_0.parquet" + + echo "Creating sorted ClickBench dataset from hits_0.parquet..." Review Comment: Another thing you could do is sort hits.parquet (the entire dataset) rather than just 1% of the data. ########## benchmarks/src/clickbench.rs: ########## @@ -125,6 +138,18 @@ impl RunOpt { // configure parquet options let mut config = self.common.config()?; + + // CRITICAL: If sorted_by is specified, force target_partitions=1 Review Comment: I recommend using a different option, `datafusion.optimizer.prefer_existing_sort ` which I think is more likely what real systems would be using as it has the same effect but still allows more than one core to be used by other parts of the query https://datafusion.apache.org/user-guide/configs.html datafusion.optimizer.prefer_existing_sort | false | When true, DataFusion will opportunistically remove sorts when the data is already sorted, (i.e. setting preserve_order to true on RepartitionExec and using SortPreservingMergeExec) When false, DataFusion will maximize plan parallelism using RepartitionExec even if this requires subsequently resorting data using a SortExec. -- | -- | -- -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
