|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +//! Tests for parquet content-defined chunking (CDC). |
| 19 | +//! |
| 20 | +//! These tests verify that CDC options are correctly wired through to the |
| 21 | +//! parquet writer by inspecting file metadata (compressed sizes, page |
| 22 | +//! boundaries) on the written files. |
| 23 | +
|
| 24 | +use arrow::array::{Int32Array, StringArray}; |
| 25 | +use arrow::datatypes::{DataType, Field, Schema}; |
| 26 | +use arrow::record_batch::RecordBatch; |
| 27 | +use datafusion::prelude::{ParquetReadOptions, SessionContext}; |
| 28 | +use datafusion_common::config::{CdcOptions, TableParquetOptions}; |
| 29 | +use parquet::arrow::ArrowWriter; |
| 30 | +use parquet::arrow::arrow_reader::ArrowReaderMetadata; |
| 31 | +use parquet::file::properties::WriterProperties; |
| 32 | +use std::fs::File; |
| 33 | +use std::sync::Arc; |
| 34 | +use tempfile::NamedTempFile; |
| 35 | + |
| 36 | +/// Create a RecordBatch with enough data to exercise CDC chunking. |
| 37 | +fn make_test_batch(num_rows: usize) -> RecordBatch { |
| 38 | + let ids: Vec<i32> = (0..num_rows as i32).collect(); |
| 39 | + // ~100 bytes per row to generate enough data for CDC page splits |
| 40 | + let payloads: Vec<String> = (0..num_rows) |
| 41 | + .map(|i| format!("row-{i:06}-payload-{}", "x".repeat(80))) |
| 42 | + .collect(); |
| 43 | + |
| 44 | + let schema = Arc::new(Schema::new(vec![ |
| 45 | + Field::new("id", DataType::Int32, false), |
| 46 | + Field::new("payload", DataType::Utf8, false), |
| 47 | + ])); |
| 48 | + |
| 49 | + RecordBatch::try_new( |
| 50 | + schema, |
| 51 | + vec![ |
| 52 | + Arc::new(Int32Array::from(ids)), |
| 53 | + Arc::new(StringArray::from(payloads)), |
| 54 | + ], |
| 55 | + ) |
| 56 | + .unwrap() |
| 57 | +} |
| 58 | + |
| 59 | +/// Build WriterProperties from TableParquetOptions, exercising the same |
| 60 | +/// code path that DataFusion's parquet sink uses. |
| 61 | +fn writer_props( |
| 62 | + opts: &mut TableParquetOptions, |
| 63 | + schema: &Arc<Schema>, |
| 64 | +) -> WriterProperties { |
| 65 | + opts.arrow_schema(schema); |
| 66 | + parquet::file::properties::WriterPropertiesBuilder::try_from( |
| 67 | + opts as &TableParquetOptions, |
| 68 | + ) |
| 69 | + .unwrap() |
| 70 | + .build() |
| 71 | +} |
| 72 | + |
| 73 | +/// Write a batch to a temp parquet file and return the file handle. |
| 74 | +fn write_parquet_file(batch: &RecordBatch, props: WriterProperties) -> NamedTempFile { |
| 75 | + let tmp = tempfile::Builder::new() |
| 76 | + .suffix(".parquet") |
| 77 | + .tempfile() |
| 78 | + .unwrap(); |
| 79 | + let mut writer = |
| 80 | + ArrowWriter::try_new(tmp.reopen().unwrap(), batch.schema(), Some(props)).unwrap(); |
| 81 | + writer.write(batch).unwrap(); |
| 82 | + writer.close().unwrap(); |
| 83 | + tmp |
| 84 | +} |
| 85 | + |
| 86 | +/// Read parquet metadata from a file. |
| 87 | +fn read_metadata(file: &NamedTempFile) -> parquet::file::metadata::ParquetMetaData { |
| 88 | + let f = File::open(file.path()).unwrap(); |
| 89 | + let reader_meta = ArrowReaderMetadata::load(&f, Default::default()).unwrap(); |
| 90 | + reader_meta.metadata().as_ref().clone() |
| 91 | +} |
| 92 | + |
| 93 | +/// Write parquet with CDC enabled, read it back via DataFusion, and verify |
| 94 | +/// the data round-trips correctly. |
| 95 | +#[tokio::test] |
| 96 | +async fn cdc_data_round_trip() { |
| 97 | + let batch = make_test_batch(5000); |
| 98 | + |
| 99 | + let mut opts = TableParquetOptions::default(); |
| 100 | + opts.global.use_content_defined_chunking = Some(CdcOptions::default()); |
| 101 | + let props = writer_props(&mut opts, &batch.schema()); |
| 102 | + |
| 103 | + let tmp = write_parquet_file(&batch, props); |
| 104 | + |
| 105 | + // Read back via DataFusion and verify row count |
| 106 | + let ctx = SessionContext::new(); |
| 107 | + ctx.register_parquet( |
| 108 | + "data", |
| 109 | + tmp.path().to_str().unwrap(), |
| 110 | + ParquetReadOptions::default(), |
| 111 | + ) |
| 112 | + .await |
| 113 | + .unwrap(); |
| 114 | + |
| 115 | + let result = ctx |
| 116 | + .sql("SELECT COUNT(*), MIN(id), MAX(id) FROM data") |
| 117 | + .await |
| 118 | + .unwrap() |
| 119 | + .collect() |
| 120 | + .await |
| 121 | + .unwrap(); |
| 122 | + |
| 123 | + let row = &result[0]; |
| 124 | + let count = row |
| 125 | + .column(0) |
| 126 | + .as_any() |
| 127 | + .downcast_ref::<arrow::array::Int64Array>() |
| 128 | + .unwrap() |
| 129 | + .value(0); |
| 130 | + let min_id = row |
| 131 | + .column(1) |
| 132 | + .as_any() |
| 133 | + .downcast_ref::<Int32Array>() |
| 134 | + .unwrap() |
| 135 | + .value(0); |
| 136 | + let max_id = row |
| 137 | + .column(2) |
| 138 | + .as_any() |
| 139 | + .downcast_ref::<Int32Array>() |
| 140 | + .unwrap() |
| 141 | + .value(0); |
| 142 | + |
| 143 | + assert_eq!(count, 5000); |
| 144 | + assert_eq!(min_id, 0); |
| 145 | + assert_eq!(max_id, 4999); |
| 146 | +} |
| 147 | + |
| 148 | +/// Verify that CDC options are reflected in the parquet file metadata. |
| 149 | +/// With small chunk sizes, CDC should produce different page boundaries |
| 150 | +/// compared to default (no CDC) writing. |
| 151 | +#[tokio::test] |
| 152 | +async fn cdc_affects_page_boundaries() { |
| 153 | + let batch = make_test_batch(5000); |
| 154 | + |
| 155 | + // Write WITHOUT CDC |
| 156 | + let mut no_cdc_opts = TableParquetOptions::default(); |
| 157 | + let no_cdc_file = |
| 158 | + write_parquet_file(&batch, writer_props(&mut no_cdc_opts, &batch.schema())); |
| 159 | + let no_cdc_meta = read_metadata(&no_cdc_file); |
| 160 | + |
| 161 | + // Write WITH CDC using small chunk sizes to maximize effect |
| 162 | + let mut cdc_opts = TableParquetOptions::default(); |
| 163 | + cdc_opts.global.use_content_defined_chunking = Some(CdcOptions { |
| 164 | + min_chunk_size: 512, |
| 165 | + max_chunk_size: 2048, |
| 166 | + norm_level: 0, |
| 167 | + }); |
| 168 | + let cdc_file = |
| 169 | + write_parquet_file(&batch, writer_props(&mut cdc_opts, &batch.schema())); |
| 170 | + let cdc_meta = read_metadata(&cdc_file); |
| 171 | + |
| 172 | + // Both files should have the same number of rows |
| 173 | + assert_eq!( |
| 174 | + no_cdc_meta.file_metadata().num_rows(), |
| 175 | + cdc_meta.file_metadata().num_rows(), |
| 176 | + ); |
| 177 | + |
| 178 | + // Compare the uncompressed sizes of columns across all row groups. |
| 179 | + // CDC with small chunk sizes should produce different page boundaries. |
| 180 | + let no_cdc_sizes: Vec<i64> = no_cdc_meta |
| 181 | + .row_groups() |
| 182 | + .iter() |
| 183 | + .flat_map(|rg| rg.columns().iter().map(|c| c.uncompressed_size())) |
| 184 | + .collect(); |
| 185 | + |
| 186 | + let cdc_sizes: Vec<i64> = cdc_meta |
| 187 | + .row_groups() |
| 188 | + .iter() |
| 189 | + .flat_map(|rg| rg.columns().iter().map(|c| c.uncompressed_size())) |
| 190 | + .collect(); |
| 191 | + |
| 192 | + assert_ne!( |
| 193 | + no_cdc_sizes, cdc_sizes, |
| 194 | + "CDC with small chunk sizes should produce different page layouts \ |
| 195 | + than default writing. no_cdc={no_cdc_sizes:?}, cdc={cdc_sizes:?}" |
| 196 | + ); |
| 197 | +} |
0 commit comments