Skip to content

Commit fc16607

Browse files
authored
parquet plain dict bench (#10591)
# Which issue does this PR close? <!-- We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. --> - works towards #10590 - works towards apache/datafusion#24111 # Rationale for this change see #10590 <!-- Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed. Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes. --> # What changes are included in this PR? introduces benchmarks for reading string values into dictionary arrays <!-- There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR. --> # Are these changes tested? n/a <!-- We typically require tests for all PRs in order to: 1. Prevent the code from being accidentally broken by subsequent changes 2. Serve as another way to document the expected behavior of the code If tests are not included in your PR, please explain why (for example, are they covered by existing tests)? If this PR claims a performance improvement, please include evidence such as benchmark results. --> # Are there any user-facing changes? no <!-- If there are user-facing changes then we may require documentation to be updated before approving the PR. If there are any breaking changes to public APIs, please call them out. -->
1 parent 6fe933e commit fc16607

1 file changed

Lines changed: 78 additions & 5 deletions

File tree

‎parquet/benches/arrow_reader.rs‎

Lines changed: 78 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,27 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18-
use arrow::array::Array;
19-
use arrow::datatypes::DataType;
18+
use arrow::array::{Array, StringArray};
19+
use arrow::datatypes::{DataType, Schema, SchemaRef};
20+
use arrow::record_batch::RecordBatch;
2021
use arrow_schema::Field;
22+
use bytes::Bytes;
2123
use criterion::measurement::WallTime;
2224
use criterion::{BenchmarkGroup, Criterion, criterion_group, criterion_main};
2325
use half::f16;
2426
use num_bigint::BigInt;
2527
use num_traits::FromPrimitive;
28+
use parquet::arrow::ArrowWriter;
2629
use parquet::arrow::array_reader::{
2730
ListArrayReader, make_byte_array_reader, make_byte_view_array_reader,
2831
make_fixed_len_byte_array_reader,
2932
};
30-
use parquet::arrow::arrow_reader::DEFAULT_BATCH_SIZE;
31-
use parquet::basic::Type;
33+
use parquet::arrow::arrow_reader::{
34+
ArrowReaderOptions, DEFAULT_BATCH_SIZE, ParquetRecordBatchReaderBuilder,
35+
};
36+
use parquet::basic::{Compression, Type};
3237
use parquet::data_type::{ByteArray, FixedLenByteArrayType};
38+
use parquet::file::properties::WriterProperties;
3339
use parquet::util::{DataPageBuilder, DataPageBuilderImpl, InMemoryPageIterator};
3440
use parquet::{
3541
arrow::array_reader::ArrayReader,
@@ -2614,5 +2620,72 @@ fn add_benches(c: &mut Criterion) {
26142620
group.finish();
26152621
}
26162622

2617-
criterion_group!(benches, add_benches, decimal_benches, float16_benches,);
2623+
fn bench_plain_string_to_dict(c: &mut Criterion) {
2624+
fn make_parquet(num_rows: usize, cardinality: usize) -> Bytes {
2625+
let schema = Arc::new(Schema::new(vec![Field::new("s", DataType::Utf8, false)]));
2626+
let values: StringArray = (0..num_rows)
2627+
.map(|i| Some(format!("{:032}", i % cardinality)))
2628+
.collect();
2629+
let batch = RecordBatch::try_new(Arc::clone(&schema), vec![Arc::new(values) as _]).unwrap();
2630+
let props = WriterProperties::builder()
2631+
.set_compression(Compression::UNCOMPRESSED)
2632+
.set_dictionary_enabled(false)
2633+
.set_encoding(Encoding::PLAIN)
2634+
.build();
2635+
let mut buf = Vec::new();
2636+
let mut writer = ArrowWriter::try_new(&mut buf, schema, Some(props)).unwrap();
2637+
writer.write(&batch).unwrap();
2638+
writer.close().unwrap();
2639+
Bytes::from(buf)
2640+
}
2641+
2642+
let num_rows = 8192 * 8;
2643+
let dict_schema: SchemaRef = Arc::new(Schema::new(vec![Field::new(
2644+
"s",
2645+
DataType::Dictionary(Box::new(DataType::Int32), Box::new(DataType::Utf8)),
2646+
false,
2647+
)]));
2648+
2649+
let high_card = make_parquet(num_rows, num_rows); // every value unique
2650+
let med_card_1 = make_parquet(num_rows, 1_000); // 1 000 distinct values
2651+
let med_card_2 = make_parquet(num_rows, 100); // 100 distinct values
2652+
let low_card = make_parquet(num_rows, 10); // 10 distinct values
2653+
2654+
let mut group = c.benchmark_group("arrow_array_reader/PlainStringToDictionary");
2655+
2656+
for (name, data) in [
2657+
("high cardinality", high_card),
2658+
("medium cardinality 1", med_card_1),
2659+
("medium cardinality 2", med_card_2),
2660+
("low cardinality", low_card),
2661+
] {
2662+
let schema = Arc::clone(&dict_schema);
2663+
group.bench_function(name, |b| {
2664+
b.iter(|| {
2665+
let opts = ArrowReaderOptions::new().with_schema(Arc::clone(&schema));
2666+
let reader =
2667+
ParquetRecordBatchReaderBuilder::try_new_with_options(data.clone(), opts)
2668+
.unwrap()
2669+
.build()
2670+
.unwrap();
2671+
let mut count = 0usize;
2672+
2673+
for batch in reader {
2674+
count += batch.unwrap().num_rows();
2675+
}
2676+
assert_eq!(count, num_rows);
2677+
});
2678+
});
2679+
}
2680+
2681+
group.finish();
2682+
}
2683+
2684+
criterion_group!(
2685+
benches,
2686+
add_benches,
2687+
decimal_benches,
2688+
float16_benches,
2689+
bench_plain_string_to_dict,
2690+
);
26182691
criterion_main!(benches);

0 commit comments

Comments
 (0)