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

[PR] feat: issue #9157 adding record_batches for Vec [arrow-datafusion]

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

   ## Which issue does this PR close?
   #9157
   <!--
   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 #.
   
   ## 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] Add `SessionContext::record_batches` [arrow-datafusion]

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


##########
datafusion/core/tests/dataframe/mod.rs:
##########
@@ -1431,6 +1432,89 @@ async fn unnest_analyze_metrics() -> Result<()> {
 
     Ok(())
 }
+#[tokio::test]
+async fn test_read_batches() -> Result<()> {
+    let config = SessionConfig::new();
+    let runtime = Arc::new(RuntimeEnv::default());
+    let state = SessionState::new_with_config_rt(config, runtime);
+    let ctx = SessionContext::new_with_state(state);
+
+    let schema = Arc::new(Schema::new(vec![
+        Field::new("id", DataType::Int32, false),
+        Field::new("number", DataType::Float32, false),
+    ]));
+
+    let batches = vec![
+        RecordBatch::try_new(
+            schema.clone(),
+            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])),
+            ],
+        )
+        .unwrap(),
+        RecordBatch::try_new(
+            schema.clone(),
+            vec![
+                Arc::new(Int32Array::from(vec![3, 4, 5])),
+                Arc::new(Float32Array::from(vec![1.11, 2.22, 3.33])),
+            ],
+        )
+        .unwrap(),
+        RecordBatch::try_new(

Review Comment:
   Minor: I suggest making the test shorter by feeding in 2 batches rather than 5
   
   



##########
datafusion/core/src/execution/context/mod.rs:
##########
@@ -934,7 +934,26 @@ impl SessionContext {
             .build()?,
         ))
     }
-
+    /// Create a [`DataFrame`] for reading a [`Vec[`RecordBatch`]`]
+    pub fn read_batches(
+        &self,
+        batches: impl IntoIterator<Item = RecordBatch>,
+    ) -> Result<DataFrame> {
+        // check schema uniqueness
+        let mut batches = batches.into_iter().peekable();
+        let schema: SchemaRef = batches.peek().unwrap().schema().clone();

Review Comment:
   Perhaps something like
   
   ```suggestion
           let schema: SchemaRef = if let Some(schema) = batches.peek() {
             schema.clone()
           } else {
             // use empty schema)
             Arc::new(Schema::new())
           };
   ```
   
   



-- 
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: issue #9157 adding record_batches for Vec [arrow-datafusion]

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


##########
datafusion/core/src/execution/context/mod.rs:
##########
@@ -934,7 +934,26 @@ impl SessionContext {
             .build()?,
         ))
     }
-
+    /// Create a [`DataFrame`] for reading a [`Vec[`RecordBatch`]`]
+    pub fn read_batches(
+        &self,
+        batches: impl IntoIterator<Item = RecordBatch>,
+    ) -> Result<DataFrame> {
+        // check schema uniqueness
+        let mut batches = batches.into_iter().peekable();
+        let schema: SchemaRef = batches.peek().unwrap().schema().clone();

Review Comment:
   Is there any way we can avoid an unwrap here?



-- 
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] Add `SessionContext::record_batches` [arrow-datafusion]

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


-- 
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] Add `SessionContext::record_batches` [arrow-datafusion]

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

   CI failure seems unrelated -- I retriggered 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


Re: [PR] Add `SessionContext::record_batches` [arrow-datafusion]

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


##########
datafusion/core/src/execution/context/mod.rs:
##########
@@ -934,7 +935,30 @@ impl SessionContext {
             .build()?,
         ))
     }
-
+    /// Create a [`DataFrame`] for reading a [`Vec[`RecordBatch`]`]
+    pub fn read_batches(
+        &self,
+        batches: impl IntoIterator<Item = RecordBatch>,
+    ) -> Result<DataFrame> {
+        // check schema uniqueness
+        let mut batches = batches.into_iter().peekable();
+        let schema = if let Some(batch) = batches.peek() {
+            batch.schema().clone()
+        } else {
+            Arc::new(Schema::empty())
+        };
+        let provider =
+            MemTable::try_new(schema, batches.map(|batch| vec![batch]).collect())?;

Review Comment:
   TLDR is that it is added by https://docs.rs/datafusion/latest/datafusion/physical_optimizer/enforce_distribution/struct.EnforceDistribution.html -- the rules about when it happens are non trivial but basically are "when it helps"



-- 
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] Add `SessionContext::record_batches` [arrow-datafusion]

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


##########
datafusion/core/src/execution/context/mod.rs:
##########
@@ -934,7 +935,30 @@ impl SessionContext {
             .build()?,
         ))
     }
-
+    /// Create a [`DataFrame`] for reading a [`Vec[`RecordBatch`]`]
+    pub fn read_batches(
+        &self,
+        batches: impl IntoIterator<Item = RecordBatch>,
+    ) -> Result<DataFrame> {
+        // check schema uniqueness
+        let mut batches = batches.into_iter().peekable();
+        let schema = if let Some(batch) = batches.peek() {
+            batch.schema().clone()
+        } else {
+            Arc::new(Schema::empty())
+        };
+        let provider =
+            MemTable::try_new(schema, batches.map(|batch| vec![batch]).collect())?;

Review Comment:
   There is a difference (as you say the code in this PR makes its own partition). I think you are right that a single partition might be better (and DataFusion will repartition the plan into multiple partitions) if necessary
   
   Is this something you can do @Lordworms ? Otherwise we can merge this PR as is and make it as a follow on change.



-- 
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] Add `SessionContext::record_batches` [arrow-datafusion]

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


##########
datafusion/core/tests/dataframe/mod.rs:
##########
@@ -1489,7 +1489,7 @@ async fn test_read_batches_empty() -> Result<()> {
     let state = SessionState::new_with_config_rt(config, runtime);
     let ctx = SessionContext::new_with_state(state);
 
-    let schema = Arc::new(Schema::new(vec![
+    let _schema = Arc::new(Schema::new(vec![

Review Comment:
   I think you could simply delete this statement entirely as the Schema is not needed



-- 
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] Add `SessionContext::record_batches` [arrow-datafusion]

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

   Thanks for this :+1: 


-- 
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] Add `SessionContext::record_batches` [arrow-datafusion]

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


##########
datafusion/core/src/execution/context/mod.rs:
##########
@@ -934,7 +934,26 @@ impl SessionContext {
             .build()?,
         ))
     }
-
+    /// Create a [`DataFrame`] for reading a [`Vec[`RecordBatch`]`]
+    pub fn read_batches(
+        &self,
+        batches: impl IntoIterator<Item = RecordBatch>,
+    ) -> Result<DataFrame> {
+        // check schema uniqueness
+        let mut batches = batches.into_iter().peekable();
+        let schema: SchemaRef = batches.peek().unwrap().schema().clone();

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


Re: [PR] Add `SessionContext::record_batches` [arrow-datafusion]

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


##########
datafusion/core/tests/dataframe/mod.rs:
##########
@@ -1489,7 +1489,7 @@ async fn test_read_batches_empty() -> Result<()> {
     let state = SessionState::new_with_config_rt(config, runtime);
     let ctx = SessionContext::new_with_state(state);
 
-    let schema = Arc::new(Schema::new(vec![
+    let _schema = Arc::new(Schema::new(vec![

Review Comment:
   done



-- 
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] Add `SessionContext::record_batches` [arrow-datafusion]

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


##########
datafusion/core/src/execution/context/mod.rs:
##########
@@ -934,7 +935,30 @@ impl SessionContext {
             .build()?,
         ))
     }
-
+    /// Create a [`DataFrame`] for reading a [`Vec[`RecordBatch`]`]
+    pub fn read_batches(
+        &self,
+        batches: impl IntoIterator<Item = RecordBatch>,
+    ) -> Result<DataFrame> {
+        // check schema uniqueness
+        let mut batches = batches.into_iter().peekable();
+        let schema = if let Some(batch) = batches.peek() {
+            batch.schema().clone()
+        } else {
+            Arc::new(Schema::empty())
+        };
+        let provider =
+            MemTable::try_new(schema, batches.map(|batch| vec![batch]).collect())?;

Review Comment:
   Hmm, is there any difference to mapping each batch into its own vec (which seemingly represents a different partition?) vs just collecting the batches into a single vec and passing in a single vec?
   
   e.g. something like
   
   ```suggestion
           let provider =
               MemTable::try_new(schema, vec![batches.collect()])?;
   ```



-- 
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] Add `SessionContext::record_batches` [arrow-datafusion]

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


##########
datafusion/core/src/execution/context/mod.rs:
##########
@@ -934,7 +935,30 @@ impl SessionContext {
             .build()?,
         ))
     }
-
+    /// Create a [`DataFrame`] for reading a [`Vec[`RecordBatch`]`]
+    pub fn read_batches(
+        &self,
+        batches: impl IntoIterator<Item = RecordBatch>,
+    ) -> Result<DataFrame> {
+        // check schema uniqueness
+        let mut batches = batches.into_iter().peekable();
+        let schema = if let Some(batch) = batches.peek() {
+            batch.schema().clone()
+        } else {
+            Arc::new(Schema::empty())
+        };
+        let provider =
+            MemTable::try_new(schema, batches.map(|batch| vec![batch]).collect())?;

Review Comment:
   sure, so what we need to do is to flatten all the batches into a vec? I wonder in what situation the datafusion would do the repartition? Is there any doc for this?



-- 
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] Add `SessionContext::record_batches` [arrow-datafusion]

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


##########
datafusion/core/src/execution/context/mod.rs:
##########
@@ -934,7 +935,30 @@ impl SessionContext {
             .build()?,
         ))
     }
-
+    /// Create a [`DataFrame`] for reading a [`Vec[`RecordBatch`]`]
+    pub fn read_batches(
+        &self,
+        batches: impl IntoIterator<Item = RecordBatch>,
+    ) -> Result<DataFrame> {
+        // check schema uniqueness
+        let mut batches = batches.into_iter().peekable();
+        let schema = if let Some(batch) = batches.peek() {
+            batch.schema().clone()
+        } else {
+            Arc::new(Schema::empty())
+        };
+        let provider =
+            MemTable::try_new(schema, batches.map(|batch| vec![batch]).collect())?;

Review Comment:
   Thanks, I would read about it and commit the changes soon.



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