datafusion/execution/context/
avro.rs

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
18use super::super::options::{AvroReadOptions, ReadOptions};
19use super::{DataFilePaths, DataFrame, Result, SessionContext};
20use datafusion_common::TableReference;
21use std::sync::Arc;
22
23impl SessionContext {
24    /// Creates a [`DataFrame`] for reading an Avro data source.
25    ///
26    /// For more control such as reading multiple files, you can use
27    /// [`read_table`](Self::read_table) with a [`super::ListingTable`].
28    ///
29    /// For an example, see [`read_csv`](Self::read_csv)
30    pub async fn read_avro<P: DataFilePaths>(
31        &self,
32        table_paths: P,
33        options: AvroReadOptions<'_>,
34    ) -> Result<DataFrame> {
35        self._read_type(table_paths, options).await
36    }
37
38    /// Registers an Avro file as a table that can be referenced from
39    /// SQL statements executed against this context.
40    pub async fn register_avro(
41        &self,
42        table_ref: impl Into<TableReference>,
43        table_path: impl AsRef<str>,
44        options: AvroReadOptions<'_>,
45    ) -> Result<()> {
46        let listing_options = options
47            .to_listing_options(&self.copied_config(), self.copied_table_options());
48
49        self.register_type_check(table_path.as_ref(), &listing_options.file_extension)?;
50
51        self.register_listing_table(
52            table_ref,
53            table_path,
54            listing_options,
55            options.schema.map(|s| Arc::new(s.to_owned())),
56            None,
57        )
58        .await?;
59        Ok(())
60    }
61}