You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "jonmmease (via GitHub)" <gi...@apache.org> on 2023/05/11 12:18:28 UTC

[GitHub] [arrow-datafusion] jonmmease opened a new pull request, #6337: Add support for reading Arrow files

jonmmease opened a new pull request, #6337:
URL: https://github.com/apache/arrow-datafusion/pull/6337

   # Which issue does this PR close?
   Closes #5594
   
   # What changes are included in this PR?
   
   This PR adds support for registering Arrow files with the `SessionContext` and reading them. It's largely pattern matched on the existing Avro support, with some inspiration from #1858.
   
   # Are these changes tested?
   There is one test, but I'd appreciate guidance on what additional testing we'd like to have here.
   
   # Are there any user-facing changes?
   Yes, there is now a `register_arrow` method on the `SessionContext`.


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion] alamb commented on a diff in pull request #6337: Add support for reading Arrow files

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #6337:
URL: https://github.com/apache/arrow-datafusion/pull/6337#discussion_r1194292621


##########
datafusion/core/src/datasource/file_format/arrow.rs:
##########
@@ -0,0 +1,94 @@
+// 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
+
+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

Review Comment:
   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



##########
datafusion/core/tests/sql/arrow_files.rs:
##########
@@ -0,0 +1,70 @@
+// 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 datafusion::execution::options::ArrowReadOptions;
+
+use super::*;
+
+async fn register_arrow(ctx: &mut SessionContext) {

Review Comment:
   I actually tried creating an arrow table via SQL and unfortunately it did not work:
   
   ```
   datafusion-cli
   ❯ create external table example stored as arrow location '../datafusion/core/tests/data/example.arrow';
   Execution error: Unable to find factory for ARROW
   ```
   
   To support that I think all we need is to add another entry to this list: 
   
   https://github.com/apache/arrow-datafusion/blob/b578c5819fc05801f2972dd427fbc13cf4773ea8/datafusion/core/src/execution/context.rs#L1358-L1362



##########
datafusion/core/tests/sql/arrow_files.rs:
##########
@@ -0,0 +1,70 @@
+// 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 datafusion::execution::options::ArrowReadOptions;
+
+use super::*;
+
+async fn register_arrow(ctx: &mut SessionContext) {

Review Comment:
   Looks good -- I think we should also write a sqllogictest version of this (e.g. https://github.com/apache/arrow-datafusion/blob/b578c5819fc05801f2972dd427fbc13cf4773ea8/datafusion/core/tests/sqllogictests/test_files/ddl.slt#LL259C1-L260C1)
   
   So that we have coverage of creating an arrow backed table via SQL
   
   But I also think that could be added as a follow on PR



##########
datafusion/core/src/datasource/file_format/arrow.rs:
##########
@@ -0,0 +1,94 @@
+// 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

Review Comment:
   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 ?



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion] jonmmease commented on a diff in pull request #6337: Add support for reading Arrow files

Posted by "jonmmease (via GitHub)" <gi...@apache.org>.
jonmmease commented on code in PR #6337:
URL: https://github.com/apache/arrow-datafusion/pull/6337#discussion_r1194400549


##########
datafusion/core/src/datasource/file_format/arrow.rs:
##########
@@ -0,0 +1,94 @@
+// 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

Review Comment:
   Done in f9053a37c3d158d5068855ba90d47b35f67edc73



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion] jonmmease commented on pull request #6337: Add support for reading Arrow files

Posted by "jonmmease (via GitHub)" <gi...@apache.org>.
jonmmease commented on PR #6337:
URL: https://github.com/apache/arrow-datafusion/pull/6337#issuecomment-1548668877

   Thanks for taking a look and for the kind works @alamb! I think I've addressed your feedback above. Let me know if you'd like me to open the issue to track the schema-from-stream TODO.


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion] jonmmease commented on a diff in pull request #6337: Add support for reading Arrow files

Posted by "jonmmease (via GitHub)" <gi...@apache.org>.
jonmmease commented on code in PR #6337:
URL: https://github.com/apache/arrow-datafusion/pull/6337#discussion_r1194405964


##########
datafusion/core/src/datasource/file_format/arrow.rs:
##########
@@ -0,0 +1,94 @@
+// 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
+
+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

Review Comment:
   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?



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion] jonmmease commented on pull request #6337: Add support for reading Arrow files

Posted by "jonmmease (via GitHub)" <gi...@apache.org>.
jonmmease commented on PR #6337:
URL: https://github.com/apache/arrow-datafusion/pull/6337#issuecomment-1550465345

   Thanks a lot for the helpful review @alamb!  I opened https://github.com/apache/arrow-datafusion/issues/6368 as discussed.


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion] alamb merged pull request #6337: Add support for reading Arrow files

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb merged PR #6337:
URL: https://github.com/apache/arrow-datafusion/pull/6337


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion] jonmmease commented on a diff in pull request #6337: Add support for reading Arrow files

Posted by "jonmmease (via GitHub)" <gi...@apache.org>.
jonmmease commented on code in PR #6337:
URL: https://github.com/apache/arrow-datafusion/pull/6337#discussion_r1194400549


##########
datafusion/core/src/datasource/file_format/arrow.rs:
##########
@@ -0,0 +1,94 @@
+// 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

Review Comment:
   Done in cfad814d21e51a2a3bbea9dacf6a9dc42977397a



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion] jonmmease commented on a diff in pull request #6337: Add support for reading Arrow files

Posted by "jonmmease (via GitHub)" <gi...@apache.org>.
jonmmease commented on code in PR #6337:
URL: https://github.com/apache/arrow-datafusion/pull/6337#discussion_r1194402573


##########
datafusion/core/tests/sql/arrow_files.rs:
##########
@@ -0,0 +1,70 @@
+// 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 datafusion::execution::options::ArrowReadOptions;
+
+use super::*;
+
+async fn register_arrow(ctx: &mut SessionContext) {

Review Comment:
   registration support added in cfad814d21e51a2a3bbea9dacf6a9dc42977397a, and sqllogictest added in 718a21ffaaef71f71d693f0b6c9d705373c75702



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion] jonmmease commented on pull request #6337: Add support for reading Arrow files

Posted by "jonmmease (via GitHub)" <gi...@apache.org>.
jonmmease commented on PR #6337:
URL: https://github.com/apache/arrow-datafusion/pull/6337#issuecomment-1545654190

   Ok, tests are passing and this is ready for a look. Thanks!


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion] alamb commented on pull request #6337: Add support for reading Arrow files

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #6337:
URL: https://github.com/apache/arrow-datafusion/pull/6337#issuecomment-1545998762

   Thank you @jonmmease  -- this looks great. I plan to review it in the next day or so if no one beats me to it. 


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion] alamb commented on pull request #6337: Add support for reading Arrow files

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #6337:
URL: https://github.com/apache/arrow-datafusion/pull/6337#issuecomment-1550385491

   > Let me know if you'd like me to open the issue to track the schema-from-stream TODO.
   
   Yes, please, if you can that would be awesome 🙏  thank you


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org