-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for reading Arrow files #6337
Changes from all commits
47048bf
ad475f9
a2742c7
46cfe8e
cb9fc10
3936a5e
80dc9cd
cfad814
f9053a3
718a21f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// 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. | ||
|
||
//! Apache Arrow format abstractions | ||
//! | ||
//! Works with files following the [Arrow IPC format](https://arrow.apache.org/docs/format/Columnar.html#ipc-file-format) | ||
|
||
use crate::datasource::file_format::FileFormat; | ||
use crate::error::Result; | ||
use crate::execution::context::SessionState; | ||
use crate::physical_plan::file_format::{ArrowExec, FileScanConfig}; | ||
use crate::physical_plan::ExecutionPlan; | ||
use arrow::ipc::reader::FileReader; | ||
use arrow_schema::{Schema, SchemaRef}; | ||
use async_trait::async_trait; | ||
use datafusion_common::Statistics; | ||
use datafusion_physical_expr::PhysicalExpr; | ||
use object_store::{GetResult, ObjectMeta, ObjectStore}; | ||
use std::any::Any; | ||
use std::io::{Read, Seek}; | ||
use std::sync::Arc; | ||
|
||
/// The default file extension of arrow files | ||
pub const DEFAULT_ARROW_EXTENSION: &str = ".arrow"; | ||
/// Arrow `FileFormat` implementation. | ||
#[derive(Default, Debug)] | ||
pub struct ArrowFormat; | ||
|
||
#[async_trait] | ||
impl FileFormat for ArrowFormat { | ||
fn as_any(&self) -> &dyn Any { | ||
self | ||
} | ||
|
||
async fn infer_schema( | ||
&self, | ||
_state: &SessionState, | ||
store: &Arc<dyn ObjectStore>, | ||
objects: &[ObjectMeta], | ||
) -> Result<SchemaRef> { | ||
let mut schemas = vec![]; | ||
for object in objects { | ||
let schema = match store.get(&object.location).await? { | ||
GetResult::File(mut file, _) => read_arrow_schema_from_reader(&mut file)?, | ||
r @ GetResult::Stream(_) => { | ||
// TODO: Fetching entire file to get schema is potentially wasteful | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes I agree it is more than potentially 😆 I don't think we need to fix it for this PR, however. Maybe @tustvold has some ideas, and if not then I think we can just file a follow on ticket to track fetching only the parts that are needed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Haha, yeah, I carried that comment over from the Avro reader as the situation is the same here. But maybe there's some way to look at the start of the stream and parse out the schema? |
||
let data = r.bytes().await?; | ||
let mut cursor = std::io::Cursor::new(&data); | ||
read_arrow_schema_from_reader(&mut cursor)? | ||
} | ||
}; | ||
schemas.push(schema.as_ref().clone()); | ||
} | ||
let merged_schema = Schema::try_merge(schemas)?; | ||
Ok(Arc::new(merged_schema)) | ||
} | ||
|
||
async fn infer_stats( | ||
&self, | ||
_state: &SessionState, | ||
_store: &Arc<dyn ObjectStore>, | ||
_table_schema: SchemaRef, | ||
_object: &ObjectMeta, | ||
) -> Result<Statistics> { | ||
Ok(Statistics::default()) | ||
} | ||
|
||
async fn create_physical_plan( | ||
&self, | ||
_state: &SessionState, | ||
conf: FileScanConfig, | ||
_filters: Option<&Arc<dyn PhysicalExpr>>, | ||
) -> Result<Arc<dyn ExecutionPlan>> { | ||
let exec = ArrowExec::new(conf); | ||
Ok(Arc::new(exec)) | ||
} | ||
} | ||
|
||
fn read_arrow_schema_from_reader<R: Read + Seek>(reader: R) -> Result<SchemaRef> { | ||
let reader = FileReader::try_new(reader, None)?; | ||
Ok(reader.schema()) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you link to the format docs to make it clear this writer writes the IPC format https://arrow.apache.org/docs/format/Columnar.html#ipc-file-format ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in f9053a3