You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "Weijun-H (via GitHub)" <gi...@apache.org> on 2023/10/29 16:06:09 UTC

[PR] feat: read files based on the file extention [arrow-datafusion]

Weijun-H opened a new pull request, #7972:
URL: https://github.com/apache/arrow-datafusion/pull/7972

   ## Which issue does this PR close?
   
   <!--
   We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123.
   -->
   
   Closes #7954
   
   ## Rationale for this change
   
   <!--
    Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
    Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.  
   -->
   
   ## What changes are included in this PR?
   
   <!--
   There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
   -->
   
   ## Are these changes tested?
   
   <!--
   We typically require tests for all PRs in order to:
   1. Prevent the code from being accidentally broken by subsequent changes
   2. Serve as another way to document the expected behavior of the code
   
   If tests are not included in your PR, please explain why (for example, are they covered by existing tests)?
   -->
   
   ## Are there any user-facing changes?
   
   <!--
   If there are user-facing changes then we may require documentation to be updated before approving the PR.
   -->
   
   <!--
   If there are any breaking changes to public APIs, please add the `api change` label.
   -->


-- 
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


Re: [PR] feat: read files based on the file extention [arrow-datafusion]

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


##########
datafusion/core/src/execution/context/mod.rs:
##########
@@ -849,6 +849,32 @@ impl SessionContext {
         let table_paths = table_paths.to_urls()?;
         let session_config = self.copied_config();
         let listing_options = options.to_listing_options(&session_config);
+
+        let option_extension = listing_options.file_extension.clone();
+
+        if table_paths.is_empty() {
+            return exec_err!("No table paths were provided");
+        }
+
+        let extension = table_paths[0]

Review Comment:
   I think this code is somewhat unclear if you don't have the context of this PR.
   
   Also, why does it only look at a single file? What if the files were like
   
   ```
   output1.parquet
   output2.parquet.snappy
   ```
   
   What would you think about extracting this logic into its own function like
   
   ```fn
        /// Heuristically determines the format (e.g. parquet, csv) to use with the `table_paths` 
        fn infer_types<'a, P: DataFilePaths>(
           table_paths: P,
       ) -> Option<String> {
   ...
   }
   ``` 
   
   A unit test could then be added that demonstrates the behavior of examples such as @comphead 's in https://github.com/apache/arrow-datafusion/pull/7972/files#r1376546426 



-- 
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


Re: [PR] feat: read files based on the file extention [arrow-datafusion]

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


##########
datafusion/core/src/execution/context/mod.rs:
##########
@@ -849,6 +849,33 @@ impl SessionContext {
         let table_paths = table_paths.to_urls()?;
         let session_config = self.copied_config();
         let listing_options = options.to_listing_options(&session_config);
+
+        let option_extention = listing_options.file_extension.clone();

Review Comment:
   please rename is to `extension` not extention



-- 
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


Re: [PR] feat: Support determining extensions from names like `foo.parquet.snappy` as well as `foo.parquet` [arrow-datafusion]

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


-- 
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


Re: [PR] feat: read files based on the file extention [arrow-datafusion]

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

   I feel it is very close, please remove the parquet file from the PR


-- 
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


Re: [PR] feat: Support determining extensions from names like `foo.parquet.snappy` as well as `foo.parquet` [arrow-datafusion]

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


##########
datafusion/core/src/execution/context/mod.rs:
##########
@@ -849,6 +859,28 @@ impl SessionContext {
         let table_paths = table_paths.to_urls()?;
         let session_config = self.copied_config();
         let listing_options = options.to_listing_options(&session_config);
+
+        let option_extension = listing_options.file_extension.clone();

Review Comment:
   I am pretty confused about this implementation. If the goal is to detect files that don't end with the expected extension, can't we just do something like
   
   ```rust
           for path in &table_paths {
               if !path.as_str().ends_with(&extension) {
                       return exec_err!(
                           "File '{path}' does not match the expected extension '{extension}'"
                       );
               }
           }
   ```
   
   



##########
datafusion/core/src/execution/context/parquet.rs:
##########
@@ -132,6 +137,78 @@ mod tests {
         Ok(())
     }
 
+    #[tokio::test]
+    async fn read_from_wrong_file_extentnion() -> Result<()> {

Review Comment:
   ```suggestion
       async fn read_from_wrong_file_extension() -> Result<()> {
   ```



-- 
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


Re: [PR] feat: Support determining extensions from names like `foo.parquet.snappy` as well as `foo.parquet` [arrow-datafusion]

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


##########
datafusion/core/src/execution/context/mod.rs:
##########
@@ -849,6 +859,28 @@ impl SessionContext {
         let table_paths = table_paths.to_urls()?;
         let session_config = self.copied_config();
         let listing_options = options.to_listing_options(&session_config);
+
+        let option_extension = listing_options.file_extension.clone();

Review Comment:
   Yes, you are right. I made this logic too complicated.



-- 
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


Re: [PR] feat: read files based on the file extention [arrow-datafusion]

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


##########
datafusion/core/src/execution/context/mod.rs:
##########
@@ -849,6 +849,33 @@ impl SessionContext {
         let table_paths = table_paths.to_urls()?;
         let session_config = self.copied_config();
         let listing_options = options.to_listing_options(&session_config);
+
+        let option_extention = listing_options.file_extension.clone();
+        let filename = table_paths[0].prefix().filename();
+        let extention = if let Some(filename) = filename {
+            let parts: Vec<&str> = filename.split('.').collect();
+
+            if parts.len() > 1 {

Review Comment:
   its likely can be simplified with `.and_then` or `.map`



-- 
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


Re: [PR] feat: read files based on the file extention [arrow-datafusion]

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


##########
datafusion/core/src/execution/context/parquet.rs:
##########
@@ -132,6 +137,49 @@ mod tests {
         Ok(())
     }
 
+    #[tokio::test]
+    async fn read_from_wrong_file_extentnion() -> Result<()> {
+        let ctx = SessionContext::new();
+
+        // Make up a new dataframe.
+        let write_df = ctx.read_batch(RecordBatch::try_new(
+            Arc::new(Schema::new(vec![
+                Field::new("purchase_id", DataType::Int32, false),
+                Field::new("price", DataType::Float32, false),
+                Field::new("quantity", DataType::Int32, false),
+            ])),
+            vec![
+                Arc::new(Int32Array::from(vec![1, 2, 3, 4, 5])),
+                Arc::new(Float32Array::from(vec![1.12, 3.40, 2.33, 9.10, 6.66])),
+                Arc::new(Int32Array::from(vec![1, 3, 2, 4, 3])),
+            ],
+        )?)?;
+
+        write_df
+            .write_parquet(
+                "output.parquet.snappy",
+                DataFrameWriteOptions::new().with_single_file_output(true),
+                Some(
+                    WriterProperties::builder()
+                        .set_compression(Compression::SNAPPY)
+                        .build(),
+                ),
+            )
+            .await?;
+
+        let read_df = ctx

Review Comment:
   wondering what will happen on `output.parquet.snappy.parquet` ? or if there is no extension at all? 



-- 
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


Re: [PR] feat: read files based on the file extention [arrow-datafusion]

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


##########
datafusion/core/src/execution/context/parquet.rs:
##########
@@ -132,6 +137,49 @@ mod tests {
         Ok(())
     }
 
+    #[tokio::test]
+    async fn read_from_wrong_file_extentnion() -> Result<()> {
+        let ctx = SessionContext::new();
+
+        // Make up a new dataframe.
+        let write_df = ctx.read_batch(RecordBatch::try_new(
+            Arc::new(Schema::new(vec![
+                Field::new("purchase_id", DataType::Int32, false),
+                Field::new("price", DataType::Float32, false),
+                Field::new("quantity", DataType::Int32, false),
+            ])),
+            vec![
+                Arc::new(Int32Array::from(vec![1, 2, 3, 4, 5])),
+                Arc::new(Float32Array::from(vec![1.12, 3.40, 2.33, 9.10, 6.66])),
+                Arc::new(Int32Array::from(vec![1, 3, 2, 4, 3])),
+            ],
+        )?)?;
+
+        write_df
+            .write_parquet(
+                "output.parquet.snappy",
+                DataFrameWriteOptions::new().with_single_file_output(true),
+                Some(
+                    WriterProperties::builder()
+                        .set_compression(Compression::SNAPPY)
+                        .build(),
+                ),
+            )
+            .await?;
+
+        let read_df = ctx

Review Comment:
   It still works as long as we provide the correct `file_extension`



-- 
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


Re: [PR] feat: read files based on the file extention [arrow-datafusion]

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


##########
datafusion/core/src/execution/context/mod.rs:
##########
@@ -849,6 +849,33 @@ impl SessionContext {
         let table_paths = table_paths.to_urls()?;
         let session_config = self.copied_config();
         let listing_options = options.to_listing_options(&session_config);
+
+        let option_extention = listing_options.file_extension.clone();
+        let filename = table_paths[0].prefix().filename();
+        let extention = if let Some(filename) = filename {
+            let parts: Vec<&str> = filename.split('.').collect();
+
+            if parts.len() > 1 {
+                parts[1..].join(".")
+            } else {
+                "".to_owned()
+            }
+        } else {
+            "".to_owned()
+        };
+        // some the file extension might be started with "." and some not
+        let extention_alternative = ".".to_string() + extention.as_str();
+
+        if option_extention != extention
+            && option_extention != extention_alternative
+            && !extention.is_empty()
+        {
+            return Err(DataFusionError::Execution(format!(

Review Comment:
   please replace with `exec_err!` macros



-- 
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


Re: [PR] feat: read files based on the file extention [arrow-datafusion]

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


##########
datafusion/core/src/execution/context/parquet.rs:
##########
@@ -132,6 +137,49 @@ mod tests {
         Ok(())
     }
 
+    #[tokio::test]
+    async fn read_from_wrong_file_extentnion() -> Result<()> {
+        let ctx = SessionContext::new();
+
+        // Make up a new dataframe.
+        let write_df = ctx.read_batch(RecordBatch::try_new(
+            Arc::new(Schema::new(vec![
+                Field::new("purchase_id", DataType::Int32, false),
+                Field::new("price", DataType::Float32, false),
+                Field::new("quantity", DataType::Int32, false),
+            ])),
+            vec![
+                Arc::new(Int32Array::from(vec![1, 2, 3, 4, 5])),
+                Arc::new(Float32Array::from(vec![1.12, 3.40, 2.33, 9.10, 6.66])),
+                Arc::new(Int32Array::from(vec![1, 3, 2, 4, 3])),
+            ],
+        )?)?;
+
+        write_df
+            .write_parquet(
+                "output.parquet.snappy",
+                DataFrameWriteOptions::new().with_single_file_output(true),
+                Some(
+                    WriterProperties::builder()
+                        .set_compression(Compression::SNAPPY)
+                        .build(),
+                ),
+            )
+            .await?;
+
+        let read_df = ctx
+            .read_parquet(
+                "output.parquet.snappy",
+                ParquetReadOptions {
+                    ..Default::default()
+                },
+            )
+            .await;
+        assert!(read_df.is_err());

Review Comment:
   Please test the error text message as well. 



-- 
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


Re: [PR] feat: read files based on the file extention [arrow-datafusion]

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

   Thanks @Weijun-H 
   Ive added some real quick minors


-- 
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


Re: [PR] feat: read files based on the file extention [arrow-datafusion]

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


##########
datafusion/core/src/execution/context/mod.rs:
##########
@@ -849,6 +849,33 @@ impl SessionContext {
         let table_paths = table_paths.to_urls()?;
         let session_config = self.copied_config();
         let listing_options = options.to_listing_options(&session_config);
+
+        let option_extention = listing_options.file_extension.clone();
+        let filename = table_paths[0].prefix().filename();

Review Comment:
   we prob should check `table_paths` its not empty before accessing the element by index



-- 
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