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

[PR] Closes #8502: Parallel NDJSON file reading [arrow-datafusion]

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

   ## 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 #8502.
   
   ## 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.  
   -->
   As stated in the issue: 
   
   > DataFusion can now automatically read CSV and parquet files in parallel (see https://github.com/apache/arrow-datafusion/issues/6325 for CSV)
   > It would be great to do the same for "NDJSON" files -- namely files that have multiple JSON objects placed one after the other.
   
   ## 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.
   -->
   Basically the same approach as in #6325.
   However, I refactored and extracted some of the common functions like `find_first_newline` which is used by `calculate_range`, to be used by both the CSV and JSON implementation.
   
   ## 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)?
   -->
   Yes, basic tests are included.
   However, I was not sure about benchmarking the changes (since `benchmarks/bench.sh` does not provide JSON dataset?).
   
   ## Are there any user-facing changes?
   No.
   <!--
   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] Closes #8502: Parallel NDJSON file reading [arrow-datafusion]

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

   ...just as a sidenote and originally stated in #6801 and #6922; this implementation is suboptimal since it performs multiple get operations on the object store. This will be handled in a follow-up Issue / PR - which I will (most) likely tackle in the next week.


-- 
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] Closes #8502: Parallel NDJSON file reading [arrow-datafusion]

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

   Epic work @marvinlanhenke  -- thank you very much!


-- 
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] Closes #8502: Parallel NDJSON file reading [arrow-datafusion]

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

   > ... Some epic strg+c+v combos from the CSV implementation @alamb 😂 but thanks again for the kind review.
   
   Well, I think being able to extract and reuse code is far harder than just copy/paste/modify which you could have done ;) 
   
   > However, the stream handling is more complicated than I wished for...
   
   Yeah -- I agree -- it might need to get wrapped up into its own state machine / stream impl 🤔 


-- 
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] Closes #8502: Parallel NDJSON file reading [arrow-datafusion]

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

   @alamb
   Thank you so much for the review.
   
   I resolved the merge conflicts and updated the docs according to your comments. 


-- 
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] Closes #8502: Parallel NDJSON file reading [arrow-datafusion]

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


-- 
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] Closes #8502: Parallel NDJSON file reading [arrow-datafusion]

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

   Thank you for this @marvinlanhenke  - I plan to review it tomorrow.
   
   cc @devinjdangelo 


-- 
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] Closes #8502: Parallel NDJSON file reading [arrow-datafusion]

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


##########
datafusion/core/src/datasource/physical_plan/json.rs:
##########
@@ -193,54 +217,87 @@ impl JsonOpener {
 }
 
 impl FileOpener for JsonOpener {
+    /// Open a partitioned NDJSON file.
+    ///
+    /// If `file_meta.range` is `None`, the entire file is opened.
+    /// Else `file_meta.range` is `Some(FileRange{start, end})`, which corresponds to the byte range [start, end) within the file.
+    ///
+    /// Note: `start` or `end` might be in the middle of some lines. In such cases, the following rules
+    /// are applied to determine which lines to read:

Review Comment:
   👍 



##########
datafusion/core/src/datasource/physical_plan/json.rs:
##########
@@ -193,54 +217,87 @@ impl JsonOpener {
 }
 
 impl FileOpener for JsonOpener {
+    /// Open a partitioned NDJSON file.
+    ///
+    /// If `file_meta.range` is `None`, the entire file is opened.
+    /// Else `file_meta.range` is `Some(FileRange{start, end})`, which corresponds to the byte range [start, end) within the file.
+    ///
+    /// Note: `start` or `end` might be in the middle of some lines. In such cases, the following rules
+    /// are applied to determine which lines to read:

Review Comment:
   It could potentially help to link to the CsvOpener documentation too (which has an example)



##########
datafusion/core/src/datasource/file_format/json.rs:
##########
@@ -441,4 +446,94 @@ mod tests {
             .collect::<Vec<_>>();
         assert_eq!(vec!["a: Int64", "b: Float64", "c: Boolean"], fields);
     }
+
+    async fn count_num_partitions(ctx: &SessionContext, query: &str) -> Result<usize> {

Review Comment:
   Another potential way to figure out the file groups would be to make the physical plan and then walk it to find the `JsonExec` and its number of partitions



##########
datafusion/sqllogictest/test_files/repartition_scan.slt:
##########
@@ -210,9 +210,7 @@ Filter: json_table.column1 != Int32(42)
 physical_plan
 CoalesceBatchesExec: target_batch_size=8192
 --FilterExec: column1@0 != 42
-----RepartitionExec: partitioning=RoundRobinBatch(4), input_partitions=1
-------JsonExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/repartition_scan/json_table/1.json]]}, projection=[column1]
-
+----JsonExec: file_groups={4 groups: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/repartition_scan/json_table/1.json:0..18], [WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/repartition_scan/json_table/1.json:18..36], [WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/repartition_scan/json_table/1.json:36..54], [WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/repartition_scan/json_table/1.json:54..70]]}, projection=[column1]

Review Comment:
   🎉  -- can you also update the comment in this file to reflect the fact that it now reads the file in parallel 🥳 🦜 



##########
datafusion/core/src/datasource/physical_plan/csv.rs:
##########
@@ -408,55 +366,29 @@ impl FileOpener for CsvOpener {
             );
         }
 
+        let store = self.config.object_store.clone();
+
         Ok(Box::pin(async move {
-            let file_size = file_meta.object_meta.size;
             // Current partition contains bytes [start_byte, end_byte) (might contain incomplete lines at boundaries)
-            let range = match file_meta.range {
-                None => None,
-                Some(FileRange { start, end }) => {
-                    let (start, end) = (start as usize, end as usize);
-                    // Partition byte range is [start, end), the boundary might be in the middle of
-                    // some line. Need to find out the exact line boundaries.
-                    let start_delta = if start != 0 {
-                        find_first_newline(
-                            &config.object_store,
-                            file_meta.location(),
-                            start - 1,
-                            file_size,
-                        )
-                        .await?
-                    } else {
-                        0
-                    };
-                    let end_delta = if end != file_size {
-                        find_first_newline(
-                            &config.object_store,
-                            file_meta.location(),
-                            end - 1,
-                            file_size,
-                        )
-                        .await?
-                    } else {
-                        0
-                    };
-                    let range = start + start_delta..end + end_delta;
-                    if range.start == range.end {
-                        return Ok(
-                            futures::stream::poll_fn(move |_| Poll::Ready(None)).boxed()
-                        );
-                    }
-                    Some(range)
+
+            let calculated_range = calculate_range(&file_meta, &store).await?;
+
+            let range = match calculated_range {

Review Comment:
   This is a really nice refactoring 



-- 
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] Closes #8502: Parallel NDJSON file reading [arrow-datafusion]

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

   ... Some epic strg+c+v combos from the CSV implementation @alamb 😂 but thanks again for the kind review. 
   
   I am hoping to improve this implementation next week, by reducing the number of GetRequests on the object store down to one. However, the stream handling is more complicated than I wished for... 


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