Igosuki commented on a change in pull request #910: URL: https://github.com/apache/arrow-datafusion/pull/910#discussion_r696536060
########## File path: datafusion/src/avro_to_arrow/reader.rs ########## @@ -0,0 +1,289 @@ +// 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. + +use crate::arrow::datatypes::{Schema, SchemaRef}; +use crate::arrow::record_batch::RecordBatch; +use crate::avro_to_arrow::arrow_array_reader::AvroArrowArrayReader; +use crate::error::Result; +use arrow::error::Result as ArrowResult; +use avro_rs::Reader as AvroReader; +use std::io::{Read, Seek, SeekFrom}; +use std::sync::Arc; + +/// Avro file reader builder +#[derive(Debug)] +pub struct ReaderBuilder { + /// Optional schema for the Avro file + /// + /// If the schema is not supplied, the reader will try to infer the schema + /// based on the Avro structure. + schema: Option<SchemaRef>, + /// Batch size (number of records to load each time) + /// + /// The default batch size when using the `ReaderBuilder` is 1024 records + batch_size: usize, + /// Optional projection for which columns to load (zero-based column indices) + projection: Option<Vec<String>>, +} + +impl Default for ReaderBuilder { + fn default() -> Self { + Self { + schema: None, + batch_size: 1024, + projection: None, + } + } +} + +impl ReaderBuilder { + /// Create a new builder for configuring Avro parsing options. + /// + /// To convert a builder into a reader, call `Reader::from_builder` + /// + /// # Example + /// + /// ``` + /// extern crate avro_rs; + /// + /// use std::fs::File; + /// + /// fn example() -> crate::datafusion::avro_to_arrow::Reader<'static, File> { + /// let file = File::open("test/data/basic.avro").unwrap(); + /// + /// // create a builder, inferring the schema with the first 100 records + /// let builder = crate::datafusion::avro_to_arrow::ReaderBuilder::new().infer_schema().with_batch_size(100); + /// + /// let reader = builder.build::<File>(file).unwrap(); + /// + /// reader + /// } + /// ``` + pub fn new() -> Self { + Self::default() + } + + /// Set the Avro file's schema + pub fn with_schema(mut self, schema: SchemaRef) -> Self { + self.schema = Some(schema); + self + } + + /// Set the Avro reader to infer the schema of the file Review comment: It's the same docstring as in the arrow-rs crate regarding csv and json, if not provided, the schema will be taken from the records, but here every file is expected to have it schema set in the file header metadata and the module isn't (yet) able to grow a schema from streaming avro records, but it is a possibility for the future. -- 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]
