Skip to content

Commit 209eafa

Browse files
authored
Avoid panics and warnings when building avro without default features (#8576)
# Which issue does this PR close? None. # Rationale for this change `cargo -p arrow-avro test --no-default-features` builds with warnings and panics because of missing features. # What changes are included in this PR? Add the required feature gates to avoid problems when building without default features. # Are these changes tested? Tested: `cargo -p arrow-avro test --no-default-features` # Are there any user-facing changes? No.
1 parent 56e9c86 commit 209eafa

File tree

4 files changed

+97
-50
lines changed

4 files changed

+97
-50
lines changed

arrow-avro/benches/avro_writer.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@ extern crate criterion;
2222
extern crate once_cell;
2323

2424
use arrow_array::{
25-
ArrayRef, BinaryArray, BooleanArray, Decimal32Array, Decimal64Array, Decimal128Array,
26-
Decimal256Array, FixedSizeBinaryArray, Float32Array, Float64Array, ListArray, PrimitiveArray,
27-
RecordBatch, StringArray, StructArray,
25+
ArrayRef, BinaryArray, BooleanArray, Decimal128Array, Decimal256Array, FixedSizeBinaryArray,
26+
Float32Array, Float64Array, ListArray, PrimitiveArray, RecordBatch, StringArray, StructArray,
2827
builder::{ListBuilder, StringBuilder},
2928
types::{Int32Type, Int64Type, IntervalMonthDayNanoType, TimestampMicrosecondType},
3029
};
30+
#[cfg(feature = "small_decimals")]
31+
use arrow_array::{Decimal32Array, Decimal64Array};
3132
use arrow_avro::writer::AvroWriter;
3233
use arrow_buffer::{Buffer, i256};
3334
use arrow_schema::{DataType, Field, IntervalUnit, Schema, TimeUnit, UnionFields, UnionMode};
@@ -177,11 +178,13 @@ fn make_ts_micros_array_with_tag(n: usize, tag: u64) -> PrimitiveArray<Timestamp
177178
// === Decimal helpers & generators ===
178179

179180
#[inline]
181+
#[cfg(feature = "small_decimals")]
180182
fn pow10_i32(p: u8) -> i32 {
181183
(0..p).fold(1i32, |acc, _| acc.saturating_mul(10))
182184
}
183185

184186
#[inline]
187+
#[cfg(feature = "small_decimals")]
185188
fn pow10_i64(p: u8) -> i64 {
186189
(0..p).fold(1i64, |acc, _| acc.saturating_mul(10))
187190
}
@@ -192,6 +195,7 @@ fn pow10_i128(p: u8) -> i128 {
192195
}
193196

194197
#[inline]
198+
#[cfg(feature = "small_decimals")]
195199
fn make_decimal32_array_with_tag(n: usize, tag: u64, precision: u8, scale: i8) -> Decimal32Array {
196200
let mut rng = rng_for(tag, n);
197201
let max = pow10_i32(precision).saturating_sub(1);
@@ -202,6 +206,7 @@ fn make_decimal32_array_with_tag(n: usize, tag: u64, precision: u8, scale: i8) -
202206
}
203207

204208
#[inline]
209+
#[cfg(feature = "small_decimals")]
205210
fn make_decimal64_array_with_tag(n: usize, tag: u64, precision: u8, scale: i8) -> Decimal64Array {
206211
let mut rng = rng_for(tag, n);
207212
let max = pow10_i64(precision).saturating_sub(1);
@@ -539,6 +544,7 @@ static STRUCT_DATA: Lazy<Vec<RecordBatch>> = Lazy::new(|| {
539544
.collect()
540545
});
541546

547+
#[cfg(feature = "small_decimals")]
542548
static DECIMAL32_DATA: Lazy<Vec<RecordBatch>> = Lazy::new(|| {
543549
// Choose a representative precision/scale within Decimal32 limits
544550
let precision: u8 = 7;
@@ -554,6 +560,7 @@ static DECIMAL32_DATA: Lazy<Vec<RecordBatch>> = Lazy::new(|| {
554560
.collect()
555561
});
556562

563+
#[cfg(feature = "small_decimals")]
557564
static DECIMAL64_DATA: Lazy<Vec<RecordBatch>> = Lazy::new(|| {
558565
let precision: u8 = 13;
559566
let scale: i8 = 3;
@@ -821,7 +828,9 @@ fn criterion_benches(c: &mut Criterion) {
821828
bench_writer_scenario(c, "write-FixedSizeBinary16", &FIXED16_DATA);
822829
bench_writer_scenario(c, "write-UUID(logicalType)", &UUID16_DATA);
823830
bench_writer_scenario(c, "write-IntervalMonthDayNanoDuration", &INTERVAL_MDN_DATA);
831+
#[cfg(feature = "small_decimals")]
824832
bench_writer_scenario(c, "write-Decimal32(bytes)", &DECIMAL32_DATA);
833+
#[cfg(feature = "small_decimals")]
825834
bench_writer_scenario(c, "write-Decimal64(bytes)", &DECIMAL64_DATA);
826835
bench_writer_scenario(c, "write-Decimal128(bytes)", &DECIMAL128_BYTES_DATA);
827836
bench_writer_scenario(c, "write-Decimal128(fixed16)", &DECIMAL128_FIXED16_DATA);

arrow-avro/src/compression.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@
1616
// under the License.
1717

1818
use arrow_schema::ArrowError;
19+
#[cfg(any(
20+
feature = "deflate",
21+
feature = "zstd",
22+
feature = "bzip2",
23+
feature = "xz"
24+
))]
1925
use std::io::{Read, Write};
2026

2127
/// The metadata key used for storing the JSON encoded [`CompressionCodec`]
@@ -40,6 +46,7 @@ pub enum CompressionCodec {
4046
}
4147

4248
impl CompressionCodec {
49+
#[allow(unused_variables)]
4350
pub(crate) fn decompress(&self, block: &[u8]) -> Result<Vec<u8>, ArrowError> {
4451
match self {
4552
#[cfg(feature = "deflate")]
@@ -112,6 +119,7 @@ impl CompressionCodec {
112119
}
113120
}
114121

122+
#[allow(unused_variables)]
115123
pub(crate) fn compress(&self, data: &[u8]) -> Result<Vec<u8>, ArrowError> {
116124
match self {
117125
#[cfg(feature = "deflate")]

arrow-avro/src/reader/mod.rs

Lines changed: 54 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,11 +1273,12 @@ mod test {
12731273
};
12741274
use crate::test_util::arrow_test_data;
12751275
use crate::writer::AvroWriter;
1276-
use arrow::array::ArrayDataBuilder;
12771276
use arrow_array::builder::{
1278-
ArrayBuilder, BooleanBuilder, Float32Builder, Float64Builder, Int32Builder, Int64Builder,
1279-
ListBuilder, MapBuilder, MapFieldNames, StringBuilder, StructBuilder,
1277+
ArrayBuilder, BooleanBuilder, Float32Builder, Int32Builder, Int64Builder, ListBuilder,
1278+
MapBuilder, StringBuilder, StructBuilder,
12801279
};
1280+
#[cfg(feature = "snappy")]
1281+
use arrow_array::builder::{Float64Builder, MapFieldNames};
12811282
use arrow_array::cast::AsArray;
12821283
#[cfg(not(feature = "avro_custom_types"))]
12831284
use arrow_array::types::Int64Type;
@@ -1288,9 +1289,9 @@ mod test {
12881289
};
12891290
use arrow_array::types::{Int32Type, IntervalMonthDayNanoType};
12901291
use arrow_array::*;
1291-
use arrow_buffer::{
1292-
Buffer, IntervalMonthDayNano, NullBuffer, OffsetBuffer, ScalarBuffer, i256,
1293-
};
1292+
#[cfg(feature = "snappy")]
1293+
use arrow_buffer::{Buffer, NullBuffer};
1294+
use arrow_buffer::{IntervalMonthDayNano, OffsetBuffer, ScalarBuffer, i256};
12941295
#[cfg(feature = "avro_custom_types")]
12951296
use arrow_schema::{
12961297
ArrowError, DataType, Field, FieldRef, Fields, IntervalUnit, Schema, TimeUnit, UnionFields,
@@ -1309,6 +1310,23 @@ mod test {
13091310
use std::io::{BufReader, Cursor};
13101311
use std::sync::Arc;
13111312

1313+
fn files() -> impl Iterator<Item = &'static str> {
1314+
[
1315+
// TODO: avoid requiring snappy for this file
1316+
#[cfg(feature = "snappy")]
1317+
"avro/alltypes_plain.avro",
1318+
#[cfg(feature = "snappy")]
1319+
"avro/alltypes_plain.snappy.avro",
1320+
#[cfg(feature = "zstd")]
1321+
"avro/alltypes_plain.zstandard.avro",
1322+
#[cfg(feature = "bzip2")]
1323+
"avro/alltypes_plain.bzip2.avro",
1324+
#[cfg(feature = "xz")]
1325+
"avro/alltypes_plain.xz.avro",
1326+
]
1327+
.into_iter()
1328+
}
1329+
13121330
fn read_file(path: &str, batch_size: usize, utf8_view: bool) -> RecordBatch {
13131331
let file = File::open(path).unwrap();
13141332
let reader = ReaderBuilder::new()
@@ -1714,14 +1732,7 @@ mod test {
17141732

17151733
#[test]
17161734
fn test_alltypes_schema_promotion_mixed() {
1717-
let files = [
1718-
"avro/alltypes_plain.avro",
1719-
"avro/alltypes_plain.snappy.avro",
1720-
"avro/alltypes_plain.zstandard.avro",
1721-
"avro/alltypes_plain.bzip2.avro",
1722-
"avro/alltypes_plain.xz.avro",
1723-
];
1724-
for file in files {
1735+
for file in files() {
17251736
let file = arrow_test_data(file);
17261737
let mut promotions: HashMap<&str, &str> = HashMap::new();
17271738
promotions.insert("id", "long");
@@ -1829,14 +1840,7 @@ mod test {
18291840

18301841
#[test]
18311842
fn test_alltypes_schema_promotion_long_to_float_only() {
1832-
let files = [
1833-
"avro/alltypes_plain.avro",
1834-
"avro/alltypes_plain.snappy.avro",
1835-
"avro/alltypes_plain.zstandard.avro",
1836-
"avro/alltypes_plain.bzip2.avro",
1837-
"avro/alltypes_plain.xz.avro",
1838-
];
1839-
for file in files {
1843+
for file in files() {
18401844
let file = arrow_test_data(file);
18411845
let mut promotions: HashMap<&str, &str> = HashMap::new();
18421846
promotions.insert("bigint_col", "float");
@@ -1933,14 +1937,7 @@ mod test {
19331937

19341938
#[test]
19351939
fn test_alltypes_schema_promotion_bytes_to_string_only() {
1936-
let files = [
1937-
"avro/alltypes_plain.avro",
1938-
"avro/alltypes_plain.snappy.avro",
1939-
"avro/alltypes_plain.zstandard.avro",
1940-
"avro/alltypes_plain.bzip2.avro",
1941-
"avro/alltypes_plain.xz.avro",
1942-
];
1943-
for file in files {
1940+
for file in files() {
19441941
let file = arrow_test_data(file);
19451942
let mut promotions: HashMap<&str, &str> = HashMap::new();
19461943
promotions.insert("date_string_col", "string");
@@ -2033,6 +2030,8 @@ mod test {
20332030
}
20342031

20352032
#[test]
2033+
// TODO: avoid requiring snappy for this file
2034+
#[cfg(feature = "snappy")]
20362035
fn test_alltypes_illegal_promotion_bool_to_double_errors() {
20372036
let file = arrow_test_data("avro/alltypes_plain.avro");
20382037
let mut promotions: HashMap<&str, &str> = HashMap::new();
@@ -2691,6 +2690,8 @@ mod test {
26912690
}
26922691

26932692
#[test]
2693+
// TODO: avoid requiring snappy for this file
2694+
#[cfg(feature = "snappy")]
26942695
fn test_alltypes_skip_writer_fields_keep_double_only() {
26952696
let file = arrow_test_data("avro/alltypes_plain.avro");
26962697
let reader_schema =
@@ -2708,6 +2709,8 @@ mod test {
27082709
}
27092710

27102711
#[test]
2712+
// TODO: avoid requiring snappy for this file
2713+
#[cfg(feature = "snappy")]
27112714
fn test_alltypes_skip_writer_fields_reorder_and_skip_many() {
27122715
let file = arrow_test_data("avro/alltypes_plain.avro");
27132716
let reader_schema =
@@ -4470,14 +4473,6 @@ mod test {
44704473

44714474
#[test]
44724475
fn test_alltypes() {
4473-
let files = [
4474-
"avro/alltypes_plain.avro",
4475-
"avro/alltypes_plain.snappy.avro",
4476-
"avro/alltypes_plain.zstandard.avro",
4477-
"avro/alltypes_plain.bzip2.avro",
4478-
"avro/alltypes_plain.xz.avro",
4479-
];
4480-
44814476
let expected = RecordBatch::try_from_iter_with_nullable([
44824477
(
44834478
"id",
@@ -4562,7 +4557,7 @@ mod test {
45624557
])
45634558
.unwrap();
45644559

4565-
for file in files {
4560+
for file in files() {
45664561
let file = arrow_test_data(file);
45674562

45684563
assert_eq!(read_file(&file, 8, false), expected);
@@ -4571,6 +4566,8 @@ mod test {
45714566
}
45724567

45734568
#[test]
4569+
// TODO: avoid requiring snappy for this file
4570+
#[cfg(feature = "snappy")]
45744571
fn test_alltypes_dictionary() {
45754572
let file = "avro/alltypes_dictionary.avro";
45764573
let expected = RecordBatch::try_from_iter_with_nullable([
@@ -4693,6 +4690,8 @@ mod test {
46934690
}
46944691

46954692
#[test]
4693+
// TODO: avoid requiring snappy for this file
4694+
#[cfg(feature = "snappy")]
46964695
fn test_binary() {
46974696
let file = arrow_test_data("avro/binary.avro");
46984697
let batch = read_file(&file, 8, false);
@@ -4719,6 +4718,8 @@ mod test {
47194718
}
47204719

47214720
#[test]
4721+
// TODO: avoid requiring snappy for these files
4722+
#[cfg(feature = "snappy")]
47224723
fn test_decimal() {
47234724
// Choose expected Arrow types depending on the `small_decimals` feature flag.
47244725
// With `small_decimals` enabled, Decimal32/Decimal64 are used where their
@@ -5040,6 +5041,8 @@ mod test {
50405041
}
50415042

50425043
#[test]
5044+
// TODO: avoid requiring snappy for this file
5045+
#[cfg(feature = "snappy")]
50435046
fn test_dict_pages_offset_zero() {
50445047
let file = arrow_test_data("avro/dict-page-offset-zero.avro");
50455048
let batch = read_file(&file, 32, false);
@@ -5055,6 +5058,8 @@ mod test {
50555058
}
50565059

50575060
#[test]
5061+
// TODO: avoid requiring snappy for this file
5062+
#[cfg(feature = "snappy")]
50585063
fn test_list_columns() {
50595064
let file = arrow_test_data("avro/list_columns.avro");
50605065
let mut int64_list_builder = ListBuilder::new(Int64Builder::new());
@@ -5117,6 +5122,7 @@ mod test {
51175122
}
51185123

51195124
#[test]
5125+
#[cfg(feature = "snappy")]
51205126
fn test_nested_lists() {
51215127
use arrow_data::ArrayDataBuilder;
51225128
let file = arrow_test_data("avro/nested_lists.snappy.avro");
@@ -5314,6 +5320,7 @@ mod test {
53145320
}
53155321

53165322
#[test]
5323+
#[cfg(feature = "snappy")]
53175324
fn test_single_nan() {
53185325
let file = arrow_test_data("avro/single_nan.avro");
53195326
let actual = read_file(&file, 1, false);
@@ -5392,6 +5399,7 @@ mod test {
53925399
}
53935400

53945401
#[test]
5402+
#[cfg(feature = "snappy")]
53955403
fn test_datapage_v2() {
53965404
let file = arrow_test_data("avro/datapage_v2.snappy.avro");
53975405
let batch = read_file(&file, 8, false);
@@ -5653,7 +5661,10 @@ mod test {
56535661
}
56545662

56555663
#[test]
5664+
// TODO: avoid requiring snappy for this file
5665+
#[cfg(feature = "snappy")]
56565666
fn test_repeated_no_annotation() {
5667+
use arrow_data::ArrayDataBuilder;
56575668
let file = arrow_test_data("avro/repeated_no_annotation.avro");
56585669
let batch_large = read_file(&file, 8, false);
56595670
// id column
@@ -5742,6 +5753,8 @@ mod test {
57425753
}
57435754

57445755
#[test]
5756+
// TODO: avoid requiring snappy for this file
5757+
#[cfg(feature = "snappy")]
57455758
fn test_nonnullable_impala() {
57465759
let file = arrow_test_data("avro/nonnullable.impala.avro");
57475760
let id = Int64Array::from(vec![Some(8)]);
@@ -6057,6 +6070,8 @@ mod test {
60576070
}
60586071

60596072
#[test]
6073+
// TODO: avoid requiring snappy for this file
6074+
#[cfg(feature = "snappy")]
60606075
fn test_nullable_impala() {
60616076
let file = arrow_test_data("avro/nullable.impala.avro");
60626077
let batch1 = read_file(&file, 3, false);

0 commit comments

Comments
 (0)