You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "pitrou (via GitHub)" <gi...@apache.org> on 2023/11/15 22:59:34 UTC
[PR] Add C Data Interface integration testing entrypoints [arrow-rs]
pitrou opened a new pull request, #5080:
URL: https://github.com/apache/arrow-rs/pull/5080
# 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 #4867.
# 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 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 `breaking 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 C Data Interface integration testing entrypoints [arrow-rs]
Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold commented on code in PR #5080:
URL: https://github.com/apache/arrow-rs/pull/5080#discussion_r1395469788
##########
arrow-integration-testing/src/lib.rs:
##########
@@ -100,3 +166,147 @@ pub fn read_gzip_json(version: &str, path: &str) -> ArrowJson {
let arrow_json: ArrowJson = serde_json::from_str(&s).unwrap();
arrow_json
}
+
+//
+// C Data Integration entrypoints
+//
+
+fn cdata_integration_export_schema_from_json(
+ c_json_name: *const i8,
+ out: *mut FFI_ArrowSchema,
+) -> Result<()> {
+ let json_name = unsafe { CStr::from_ptr(c_json_name) };
+ let f = read_json_file_metadata(json_name.to_str()?)?;
+ let c_schema = FFI_ArrowSchema::try_from(&f.schema)?;
+ // Move exported schema into output struct
+ unsafe { ptr::write(out, c_schema) };
+ Ok(())
+}
+
+fn cdata_integration_export_batch_from_json(
+ c_json_name: *const i8,
+ batch_num: c_int,
+ out: *mut FFI_ArrowArray,
+) -> Result<()> {
+ let json_name = unsafe { CStr::from_ptr(c_json_name) };
+ let b = read_single_batch_from_json_file(json_name.to_str()?, batch_num.try_into().unwrap())?;
+ let a = StructArray::from(b).into_data();
+ let c_array = FFI_ArrowArray::new(&a);
+ // Move exported array into output struct
+ unsafe { ptr::write(out, c_array) };
+ Ok(())
+}
+
+fn cdata_integration_import_schema_and_compare_to_json(
+ c_json_name: *const i8,
+ c_schema: *mut FFI_ArrowSchema,
+) -> Result<()> {
+ let json_name = unsafe { CStr::from_ptr(c_json_name) };
+ let json_schema = read_json_file_metadata(json_name.to_str()?)?.schema;
+
+ // The source ArrowSchema will be released when this is dropped
+ let imported_schema = unsafe { std::ptr::replace(c_schema, FFI_ArrowSchema::empty()) };
Review Comment:
I don't think you need this, you can just convert the pointer to a reference?
##########
arrow-integration-testing/src/lib.rs:
##########
@@ -100,3 +166,146 @@ pub fn read_gzip_json(version: &str, path: &str) -> ArrowJson {
let arrow_json: ArrowJson = serde_json::from_str(&s).unwrap();
arrow_json
}
+
+//
+// C Data Integration entrypoints
+//
+
+fn cdata_integration_export_schema_from_json(
+ c_json_name: *const i8,
+ out: *mut FFI_ArrowSchema,
+) -> Result<()> {
+ let json_name = unsafe { CStr::from_ptr(c_json_name) };
+ let f = read_json_file_metadata(json_name.to_str()?)?;
+ let c_schema = FFI_ArrowSchema::try_from(&f.schema)?;
+ // Move exported schema into output struct
+ unsafe { ptr::write(out, c_schema) };
+ Ok(())
+}
+
+fn cdata_integration_export_batch_from_json(
+ c_json_name: *const i8,
+ batch_num: c_int,
+ out: *mut FFI_ArrowArray,
+) -> Result<()> {
+ let json_name = unsafe { CStr::from_ptr(c_json_name) };
+ let b = read_single_batch_from_json_file(json_name.to_str()?, batch_num.try_into().unwrap())?;
+ let a = StructArray::from(b).into_data();
+ let c_array = FFI_ArrowArray::new(&a);
+ // Move exported array into output struct
+ unsafe { ptr::write(out, c_array) };
Review Comment:
I think you need to mem forget the FFI Array so that Drop doesn't free the array
--
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 C Data Interface integration testing entrypoints [arrow-rs]
Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold commented on code in PR #5080:
URL: https://github.com/apache/arrow-rs/pull/5080#discussion_r1395484434
##########
arrow-integration-testing/src/lib.rs:
##########
@@ -100,3 +166,147 @@ pub fn read_gzip_json(version: &str, path: &str) -> ArrowJson {
let arrow_json: ArrowJson = serde_json::from_str(&s).unwrap();
arrow_json
}
+
+//
+// C Data Integration entrypoints
+//
+
+fn cdata_integration_export_schema_from_json(
+ c_json_name: *const i8,
+ out: *mut FFI_ArrowSchema,
+) -> Result<()> {
+ let json_name = unsafe { CStr::from_ptr(c_json_name) };
+ let f = read_json_file_metadata(json_name.to_str()?)?;
+ let c_schema = FFI_ArrowSchema::try_from(&f.schema)?;
+ // Move exported schema into output struct
+ unsafe { ptr::write(out, c_schema) };
+ Ok(())
+}
+
+fn cdata_integration_export_batch_from_json(
+ c_json_name: *const i8,
+ batch_num: c_int,
+ out: *mut FFI_ArrowArray,
+) -> Result<()> {
+ let json_name = unsafe { CStr::from_ptr(c_json_name) };
+ let b = read_single_batch_from_json_file(json_name.to_str()?, batch_num.try_into().unwrap())?;
+ let a = StructArray::from(b).into_data();
+ let c_array = FFI_ArrowArray::new(&a);
+ // Move exported array into output struct
+ unsafe { ptr::write(out, c_array) };
+ Ok(())
+}
+
+fn cdata_integration_import_schema_and_compare_to_json(
+ c_json_name: *const i8,
+ c_schema: *mut FFI_ArrowSchema,
+) -> Result<()> {
+ let json_name = unsafe { CStr::from_ptr(c_json_name) };
+ let json_schema = read_json_file_metadata(json_name.to_str()?)?.schema;
+
+ // The source ArrowSchema will be released when this is dropped
+ let imported_schema = unsafe { std::ptr::replace(c_schema, FFI_ArrowSchema::empty()) };
Review Comment:
Is that not handled by the caller?
--
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 C Data Interface integration testing entrypoints [arrow-rs]
Posted by "pitrou (via GitHub)" <gi...@apache.org>.
pitrou commented on PR #5080:
URL: https://github.com/apache/arrow-rs/pull/5080#issuecomment-1819200867
I think I addressed all review comments now.
--
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 C Data Interface integration testing entrypoints [arrow-rs]
Posted by "pitrou (via GitHub)" <gi...@apache.org>.
pitrou commented on PR #5080:
URL: https://github.com/apache/arrow-rs/pull/5080#issuecomment-1814202170
Ok, I've now checked this works properly against C++, Go, Java and C#. Will mark ready for review.
--
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 C Data Interface integration testing entrypoints [arrow-rs]
Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold commented on code in PR #5080:
URL: https://github.com/apache/arrow-rs/pull/5080#discussion_r1397731867
##########
arrow/src/ffi.rs:
##########
@@ -234,46 +235,59 @@ pub fn to_ffi(data: &ArrayData) -> Result<(FFI_ArrowArray, FFI_ArrowSchema)> {
///
/// This struct assumes that the incoming data agrees with the C data interface.
pub fn from_ffi(array: FFI_ArrowArray, schema: &FFI_ArrowSchema) -> Result<ArrayData> {
Review Comment:
This is true, but there is nothing that asserts the array actually matches the schema provided, so I think it should still be unsafe
--
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 C Data Interface integration testing entrypoints [arrow-rs]
Posted by "pitrou (via GitHub)" <gi...@apache.org>.
pitrou commented on code in PR #5080:
URL: https://github.com/apache/arrow-rs/pull/5080#discussion_r1399104569
##########
arrow/src/ffi_stream.rs:
##########
@@ -357,9 +357,11 @@ impl Iterator for ArrowArrayStreamReader {
}
let schema_ref = self.schema();
+ // NOTE: this parses the FFI_ArrowSchema again on each iterator call;
+ // should probably use from_ffi_and_data_type() instead.
Review Comment:
Perhaps a separate issue should be opened for this. @tustvold
--
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 C Data Interface integration testing entrypoints [arrow-rs]
Posted by "pitrou (via GitHub)" <gi...@apache.org>.
pitrou commented on code in PR #5080:
URL: https://github.com/apache/arrow-rs/pull/5080#discussion_r1399286702
##########
arrow/src/ffi_stream.rs:
##########
@@ -357,9 +357,11 @@ impl Iterator for ArrowArrayStreamReader {
}
let schema_ref = self.schema();
+ // NOTE: this parses the FFI_ArrowSchema again on each iterator call;
+ // should probably use from_ffi_and_data_type() instead.
Review Comment:
See #5103
--
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 C Data Interface integration testing entrypoints [arrow-rs]
Posted by "pitrou (via GitHub)" <gi...@apache.org>.
pitrou commented on PR #5080:
URL: https://github.com/apache/arrow-rs/pull/5080#issuecomment-1816592132
@tustvold What would you think of adding a macro like this to ease error reporting?
https://gist.github.com/pitrou/698144c6277d913448398c996aaac3eb
--
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 C Data Interface integration testing entrypoints [arrow-rs]
Posted by "pitrou (via GitHub)" <gi...@apache.org>.
pitrou commented on code in PR #5080:
URL: https://github.com/apache/arrow-rs/pull/5080#discussion_r1399102927
##########
arrow/src/ffi.rs:
##########
@@ -234,46 +235,59 @@ pub fn to_ffi(data: &ArrayData) -> Result<(FFI_ArrowArray, FFI_ArrowSchema)> {
///
/// This struct assumes that the incoming data agrees with the C data interface.
pub fn from_ffi(array: FFI_ArrowArray, schema: &FFI_ArrowSchema) -> Result<ArrayData> {
+ let dt = DataType::try_from(schema)?;
let array = Arc::new(array);
- let tmp = ArrowArray {
+ let tmp = ImportedArrowArray {
array: &array,
- schema,
+ data_type: dt,
+ owner: &array,
+ };
+ tmp.consume()
+}
+
+/// Import [ArrayData] from the C Data Interface
+///
+/// # Safety
+///
+/// This struct assumes that the incoming data agrees with the C data interface.
+pub fn from_ffi_and_data_type(array: FFI_ArrowArray, data_type: DataType) -> Result<ArrayData> {
Review Comment:
Ok, 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 C Data Interface integration testing entrypoints [arrow-rs]
Posted by "pitrou (via GitHub)" <gi...@apache.org>.
pitrou commented on PR #5080:
URL: https://github.com/apache/arrow-rs/pull/5080#issuecomment-1814204876
@tustvold Is it possible to query the amount of memory allocated by Arrow Rust? It is not mandatory but allows checking for memory leaks during integration testing.
--
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 C Data Interface integration testing entrypoints [arrow-rs]
Posted by "pitrou (via GitHub)" <gi...@apache.org>.
pitrou commented on code in PR #5080:
URL: https://github.com/apache/arrow-rs/pull/5080#discussion_r1397614523
##########
arrow-integration-testing/src/lib.rs:
##########
@@ -100,3 +166,147 @@ pub fn read_gzip_json(version: &str, path: &str) -> ArrowJson {
let arrow_json: ArrowJson = serde_json::from_str(&s).unwrap();
arrow_json
}
+
+//
+// C Data Integration entrypoints
+//
+
+fn cdata_integration_export_schema_from_json(
+ c_json_name: *const i8,
+ out: *mut FFI_ArrowSchema,
+) -> Result<()> {
+ let json_name = unsafe { CStr::from_ptr(c_json_name) };
+ let f = read_json_file_metadata(json_name.to_str()?)?;
+ let c_schema = FFI_ArrowSchema::try_from(&f.schema)?;
+ // Move exported schema into output struct
+ unsafe { ptr::write(out, c_schema) };
+ Ok(())
+}
+
+fn cdata_integration_export_batch_from_json(
+ c_json_name: *const i8,
+ batch_num: c_int,
+ out: *mut FFI_ArrowArray,
+) -> Result<()> {
+ let json_name = unsafe { CStr::from_ptr(c_json_name) };
+ let b = read_single_batch_from_json_file(json_name.to_str()?, batch_num.try_into().unwrap())?;
+ let a = StructArray::from(b).into_data();
+ let c_array = FFI_ArrowArray::new(&a);
+ // Move exported array into output struct
+ unsafe { ptr::write(out, c_array) };
+ Ok(())
+}
+
+fn cdata_integration_import_schema_and_compare_to_json(
+ c_json_name: *const i8,
+ c_schema: *mut FFI_ArrowSchema,
+) -> Result<()> {
+ let json_name = unsafe { CStr::from_ptr(c_json_name) };
+ let json_schema = read_json_file_metadata(json_name.to_str()?)?.schema;
+
+ // The source ArrowSchema will be released when this is dropped
+ let imported_schema = unsafe { std::ptr::replace(c_schema, FFI_ArrowSchema::empty()) };
Review Comment:
Yes, you're looking at an outdated comment :-)
--
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 C Data Interface integration testing entrypoints [arrow-rs]
Posted by "pitrou (via GitHub)" <gi...@apache.org>.
pitrou commented on code in PR #5080:
URL: https://github.com/apache/arrow-rs/pull/5080#discussion_r1395473709
##########
arrow-integration-testing/src/lib.rs:
##########
@@ -100,3 +166,147 @@ pub fn read_gzip_json(version: &str, path: &str) -> ArrowJson {
let arrow_json: ArrowJson = serde_json::from_str(&s).unwrap();
arrow_json
}
+
+//
+// C Data Integration entrypoints
+//
+
+fn cdata_integration_export_schema_from_json(
+ c_json_name: *const i8,
+ out: *mut FFI_ArrowSchema,
+) -> Result<()> {
+ let json_name = unsafe { CStr::from_ptr(c_json_name) };
+ let f = read_json_file_metadata(json_name.to_str()?)?;
+ let c_schema = FFI_ArrowSchema::try_from(&f.schema)?;
+ // Move exported schema into output struct
+ unsafe { ptr::write(out, c_schema) };
+ Ok(())
+}
+
+fn cdata_integration_export_batch_from_json(
+ c_json_name: *const i8,
+ batch_num: c_int,
+ out: *mut FFI_ArrowArray,
+) -> Result<()> {
+ let json_name = unsafe { CStr::from_ptr(c_json_name) };
+ let b = read_single_batch_from_json_file(json_name.to_str()?, batch_num.try_into().unwrap())?;
+ let a = StructArray::from(b).into_data();
+ let c_array = FFI_ArrowArray::new(&a);
+ // Move exported array into output struct
+ unsafe { ptr::write(out, c_array) };
+ Ok(())
+}
+
+fn cdata_integration_import_schema_and_compare_to_json(
+ c_json_name: *const i8,
+ c_schema: *mut FFI_ArrowSchema,
+) -> Result<()> {
+ let json_name = unsafe { CStr::from_ptr(c_json_name) };
+ let json_schema = read_json_file_metadata(json_name.to_str()?)?.schema;
+
+ // The source ArrowSchema will be released when this is dropped
+ let imported_schema = unsafe { std::ptr::replace(c_schema, FFI_ArrowSchema::empty()) };
Review Comment:
Would the referenced struct be dropped if I did that?
--
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 C Data Interface integration testing entrypoints [arrow-rs]
Posted by "pitrou (via GitHub)" <gi...@apache.org>.
pitrou commented on code in PR #5080:
URL: https://github.com/apache/arrow-rs/pull/5080#discussion_r1395474970
##########
arrow-integration-testing/src/lib.rs:
##########
@@ -100,3 +166,146 @@ pub fn read_gzip_json(version: &str, path: &str) -> ArrowJson {
let arrow_json: ArrowJson = serde_json::from_str(&s).unwrap();
arrow_json
}
+
+//
+// C Data Integration entrypoints
+//
+
+fn cdata_integration_export_schema_from_json(
+ c_json_name: *const i8,
+ out: *mut FFI_ArrowSchema,
+) -> Result<()> {
+ let json_name = unsafe { CStr::from_ptr(c_json_name) };
+ let f = read_json_file_metadata(json_name.to_str()?)?;
+ let c_schema = FFI_ArrowSchema::try_from(&f.schema)?;
+ // Move exported schema into output struct
+ unsafe { ptr::write(out, c_schema) };
+ Ok(())
+}
+
+fn cdata_integration_export_batch_from_json(
+ c_json_name: *const i8,
+ batch_num: c_int,
+ out: *mut FFI_ArrowArray,
+) -> Result<()> {
+ let json_name = unsafe { CStr::from_ptr(c_json_name) };
+ let b = read_single_batch_from_json_file(json_name.to_str()?, batch_num.try_into().unwrap())?;
+ let a = StructArray::from(b).into_data();
+ let c_array = FFI_ArrowArray::new(&a);
+ // Move exported array into output struct
+ unsafe { ptr::write(out, c_array) };
Review Comment:
You mean `c_array`? It should be moved by `ptr::write` AFAIU.
--
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 C Data Interface integration testing entrypoints [arrow-rs]
Posted by "pitrou (via GitHub)" <gi...@apache.org>.
pitrou commented on code in PR #5080:
URL: https://github.com/apache/arrow-rs/pull/5080#discussion_r1395526250
##########
arrow-integration-testing/src/lib.rs:
##########
@@ -100,3 +166,147 @@ pub fn read_gzip_json(version: &str, path: &str) -> ArrowJson {
let arrow_json: ArrowJson = serde_json::from_str(&s).unwrap();
arrow_json
}
+
+//
+// C Data Integration entrypoints
+//
+
+fn cdata_integration_export_schema_from_json(
+ c_json_name: *const i8,
+ out: *mut FFI_ArrowSchema,
+) -> Result<()> {
+ let json_name = unsafe { CStr::from_ptr(c_json_name) };
+ let f = read_json_file_metadata(json_name.to_str()?)?;
+ let c_schema = FFI_ArrowSchema::try_from(&f.schema)?;
+ // Move exported schema into output struct
+ unsafe { ptr::write(out, c_schema) };
+ Ok(())
+}
+
+fn cdata_integration_export_batch_from_json(
+ c_json_name: *const i8,
+ batch_num: c_int,
+ out: *mut FFI_ArrowArray,
+) -> Result<()> {
+ let json_name = unsafe { CStr::from_ptr(c_json_name) };
+ let b = read_single_batch_from_json_file(json_name.to_str()?, batch_num.try_into().unwrap())?;
+ let a = StructArray::from(b).into_data();
+ let c_array = FFI_ArrowArray::new(&a);
+ // Move exported array into output struct
+ unsafe { ptr::write(out, c_array) };
+ Ok(())
+}
+
+fn cdata_integration_import_schema_and_compare_to_json(
+ c_json_name: *const i8,
+ c_schema: *mut FFI_ArrowSchema,
+) -> Result<()> {
+ let json_name = unsafe { CStr::from_ptr(c_json_name) };
+ let json_schema = read_json_file_metadata(json_name.to_str()?)?.schema;
+
+ // The source ArrowSchema will be released when this is dropped
+ let imported_schema = unsafe { std::ptr::replace(c_schema, FFI_ArrowSchema::empty()) };
Review Comment:
Generally, the importer is supposed to take ownership. It might release the contents immediately (for example, you typically don't need to keep the exported schema alive) or keep the contents around as needed (for arrays).
Since this is a C-level ABI, the guarantees are purely by convention anyway...
That said, I myself have marinated in C and C++ for a very long time :-)
cc @wjones127 for another opinion on 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 C Data Interface integration testing entrypoints [arrow-rs]
Posted by "pitrou (via GitHub)" <gi...@apache.org>.
pitrou commented on code in PR #5080:
URL: https://github.com/apache/arrow-rs/pull/5080#discussion_r1399078096
##########
arrow/src/ffi.rs:
##########
@@ -388,25 +437,20 @@ impl<'a> ArrowArray<'a> {
unsafe { create_buffer(self.owner.clone(), self.array, 0, buffer_len) }
}
- fn child(&self, index: usize) -> ArrowArray {
- ArrowArray {
- array: self.array.child(index),
- schema: self.schema.child(index),
- owner: self.owner,
- }
- }
-
- fn dictionary(&self) -> Option<ArrowArray> {
- match (self.array.dictionary(), self.schema.dictionary()) {
- (Some(array), Some(schema)) => Some(ArrowArray {
+ fn dictionary(&self) -> Result<Option<ImportedArrowArray>> {
+ match (self.array.dictionary(), &self.data_type) {
+ (Some(array), DataType::Dictionary(_, value_type)) => Ok(Some(ImportedArrowArray {
array,
- schema,
+ data_type: *value_type.clone(),
Review Comment:
Dereferencing is required, but this spelling seems to clone the Box in addition to the DataType. Will replace it with `value_type.as_ref().clone()` which AFAICU only clones the DataType.
--
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 C Data Interface integration testing entrypoints [arrow-rs]
Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold commented on code in PR #5080:
URL: https://github.com/apache/arrow-rs/pull/5080#discussion_r1399107442
##########
arrow-integration-testing/src/lib.rs:
##########
@@ -44,7 +50,50 @@ pub struct ArrowFile {
pub batches: Vec<RecordBatch>,
}
-pub fn read_json_file(json_name: &str) -> Result<ArrowFile> {
+// Canonicalize the names of map fields in a schema
+pub fn canonicalize_schema(schema: &Schema) -> Schema {
+ let fields = schema
+ .fields()
+ .iter()
+ .map(|field| match field.data_type() {
+ DataType::Map(child_field, sorted) => match child_field.data_type() {
+ DataType::Struct(fields) if fields.len() == 2 => {
+ let first_field = fields.get(0).unwrap();
+ let key_field =
+ Arc::new(Field::new("key", first_field.data_type().clone(), false));
+ let second_field = fields.get(1).unwrap();
+ let value_field = Arc::new(Field::new(
+ "value",
+ second_field.data_type().clone(),
+ second_field.is_nullable(),
+ ));
+
+ let fields = Fields::from([key_field, value_field]);
+ let struct_type = DataType::Struct(fields);
+ let child_field = Field::new("entries", struct_type, false);
+
+ Arc::new(Field::new(
+ field.name().as_str(),
+ DataType::Map(Arc::new(child_field), *sorted),
+ field.is_nullable(),
+ ))
+ }
+ _ => panic!("The child field of Map type should be Struct type with 2 fields."),
+ },
+ _ => field.clone(),
+ })
+ .collect::<Fields>();
+
+ Schema::new(fields).with_metadata(schema.metadata().clone())
+}
+
+struct PartialArrowFile {
Review Comment:
This crate isn't published, so you can go wild :smile:
--
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 C Data Interface integration testing entrypoints [arrow-rs]
Posted by "pitrou (via GitHub)" <gi...@apache.org>.
pitrou commented on PR #5080:
URL: https://github.com/apache/arrow-rs/pull/5080#issuecomment-1813426719
For now I have tested this locally against the C++ implementation, using these changes to the Archery machinery:
https://github.com/apache/arrow/compare/main...pitrou:arrow:rust-cdata-integration
--
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 C Data Interface integration testing entrypoints [arrow-rs]
Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold commented on PR #5080:
URL: https://github.com/apache/arrow-rs/pull/5080#issuecomment-1814225737
Not easily afaik, you could register a global allocator that supports this, but nothing out of the box
--
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 C Data Interface integration testing entrypoints [arrow-rs]
Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold commented on PR #5080:
URL: https://github.com/apache/arrow-rs/pull/5080#issuecomment-1816621068
> What would you think of adding a macro like this to ease error reporting?
No objections from me, although FYI https://github.com/apache/arrow-rs/issues/2725 tracks the broader issue that the error variants in arrow-rs are not particularly meaningful.
--
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 C Data Interface integration testing entrypoints [arrow-rs]
Posted by "kylebarron (via GitHub)" <gi...@apache.org>.
kylebarron commented on code in PR #5080:
URL: https://github.com/apache/arrow-rs/pull/5080#discussion_r1397728170
##########
arrow/src/ffi.rs:
##########
@@ -234,46 +235,59 @@ pub fn to_ffi(data: &ArrayData) -> Result<(FFI_ArrowArray, FFI_ArrowSchema)> {
///
/// This struct assumes that the incoming data agrees with the C data interface.
pub fn from_ffi(array: FFI_ArrowArray, schema: &FFI_ArrowSchema) -> Result<ArrayData> {
Review Comment:
Maybe the unsafe part is prior to this in casting to `FFI_ArrowArray` and `FFI_ArrowSchema`? If you _know_ you have valid structs already, then it isn't unsafe?
--
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 C Data Interface integration testing entrypoints [arrow-rs]
Posted by "pitrou (via GitHub)" <gi...@apache.org>.
pitrou commented on code in PR #5080:
URL: https://github.com/apache/arrow-rs/pull/5080#discussion_r1394975959
##########
arrow-integration-testing/src/lib.rs:
##########
@@ -100,3 +166,146 @@ pub fn read_gzip_json(version: &str, path: &str) -> ArrowJson {
let arrow_json: ArrowJson = serde_json::from_str(&s).unwrap();
arrow_json
}
+
+//
+// C Data Integration entrypoints
+//
+
+fn cdata_integration_export_schema_from_json(
+ c_json_name: *const i8,
+ out: *mut FFI_ArrowSchema,
+) -> Result<()> {
+ let json_name = unsafe { CStr::from_ptr(c_json_name) };
+ let f = read_json_file_metadata(json_name.to_str()?)?;
+ let c_schema = FFI_ArrowSchema::try_from(&f.schema)?;
+ // Move exported schema into output struct
+ unsafe { ptr::replace(out, c_schema) };
Review Comment:
Should probably use `ptr::write` instead (`out` is not supposed to be initialized).
--
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 C Data Interface integration testing entrypoints [arrow-rs]
Posted by "pitrou (via GitHub)" <gi...@apache.org>.
pitrou commented on code in PR #5080:
URL: https://github.com/apache/arrow-rs/pull/5080#discussion_r1395499722
##########
arrow-integration-testing/src/lib.rs:
##########
@@ -100,3 +166,147 @@ pub fn read_gzip_json(version: &str, path: &str) -> ArrowJson {
let arrow_json: ArrowJson = serde_json::from_str(&s).unwrap();
arrow_json
}
+
+//
+// C Data Integration entrypoints
+//
+
+fn cdata_integration_export_schema_from_json(
+ c_json_name: *const i8,
+ out: *mut FFI_ArrowSchema,
+) -> Result<()> {
+ let json_name = unsafe { CStr::from_ptr(c_json_name) };
+ let f = read_json_file_metadata(json_name.to_str()?)?;
+ let c_schema = FFI_ArrowSchema::try_from(&f.schema)?;
+ // Move exported schema into output struct
+ unsafe { ptr::write(out, c_schema) };
+ Ok(())
+}
+
+fn cdata_integration_export_batch_from_json(
+ c_json_name: *const i8,
+ batch_num: c_int,
+ out: *mut FFI_ArrowArray,
+) -> Result<()> {
+ let json_name = unsafe { CStr::from_ptr(c_json_name) };
+ let b = read_single_batch_from_json_file(json_name.to_str()?, batch_num.try_into().unwrap())?;
+ let a = StructArray::from(b).into_data();
+ let c_array = FFI_ArrowArray::new(&a);
+ // Move exported array into output struct
+ unsafe { ptr::write(out, c_array) };
+ Ok(())
+}
+
+fn cdata_integration_import_schema_and_compare_to_json(
+ c_json_name: *const i8,
+ c_schema: *mut FFI_ArrowSchema,
+) -> Result<()> {
+ let json_name = unsafe { CStr::from_ptr(c_json_name) };
+ let json_schema = read_json_file_metadata(json_name.to_str()?)?.schema;
+
+ // The source ArrowSchema will be released when this is dropped
+ let imported_schema = unsafe { std::ptr::replace(c_schema, FFI_ArrowSchema::empty()) };
Review Comment:
This is also why I would suggest higher-level functions to avoid reinventing this in every consumer:
```rust
/// Import schema from C Data Interface
///
/// # Safety
///
/// This function ensures the given ArrowSchema struct has been filled
/// by a compliant exporter.
pub unsafe fn import_schema(c_schema: *mut FFI_ArrowSchema) -> Result<Schema> {
let imported_schema = unsafe { std::ptr::replace(c_schema, FFI_ArrowSchema::empty()) };
Schema::try_from(&imported_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
Re: [PR] Add C Data Interface integration testing entrypoints [arrow-rs]
Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold commented on code in PR #5080:
URL: https://github.com/apache/arrow-rs/pull/5080#discussion_r1395521974
##########
arrow-integration-testing/src/lib.rs:
##########
@@ -100,3 +166,147 @@ pub fn read_gzip_json(version: &str, path: &str) -> ArrowJson {
let arrow_json: ArrowJson = serde_json::from_str(&s).unwrap();
arrow_json
}
+
+//
+// C Data Integration entrypoints
+//
+
+fn cdata_integration_export_schema_from_json(
+ c_json_name: *const i8,
+ out: *mut FFI_ArrowSchema,
+) -> Result<()> {
+ let json_name = unsafe { CStr::from_ptr(c_json_name) };
+ let f = read_json_file_metadata(json_name.to_str()?)?;
+ let c_schema = FFI_ArrowSchema::try_from(&f.schema)?;
+ // Move exported schema into output struct
+ unsafe { ptr::write(out, c_schema) };
+ Ok(())
+}
+
+fn cdata_integration_export_batch_from_json(
+ c_json_name: *const i8,
+ batch_num: c_int,
+ out: *mut FFI_ArrowArray,
+) -> Result<()> {
+ let json_name = unsafe { CStr::from_ptr(c_json_name) };
+ let b = read_single_batch_from_json_file(json_name.to_str()?, batch_num.try_into().unwrap())?;
+ let a = StructArray::from(b).into_data();
+ let c_array = FFI_ArrowArray::new(&a);
+ // Move exported array into output struct
+ unsafe { ptr::write(out, c_array) };
+ Ok(())
+}
+
+fn cdata_integration_import_schema_and_compare_to_json(
+ c_json_name: *const i8,
+ c_schema: *mut FFI_ArrowSchema,
+) -> Result<()> {
+ let json_name = unsafe { CStr::from_ptr(c_json_name) };
+ let json_schema = read_json_file_metadata(json_name.to_str()?)?.schema;
+
+ // The source ArrowSchema will be released when this is dropped
+ let imported_schema = unsafe { std::ptr::replace(c_schema, FFI_ArrowSchema::empty()) };
Review Comment:
Is that not a bug in archery, perhaps I've been marinating in Rust for too long, but passing ownership with a pointer seems to be relying on very weak implicit guarantees?
--
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 C Data Interface integration testing entrypoints [arrow-rs]
Posted by "pitrou (via GitHub)" <gi...@apache.org>.
pitrou commented on PR #5080:
URL: https://github.com/apache/arrow-rs/pull/5080#issuecomment-1816514290
> Not easily afaik, you could register a global allocator that supports this, but nothing out of the box. Rust's custom allocators are not yet stable
Ok, I see. We can revisit this later.
--
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 C Data Interface integration testing entrypoints [arrow-rs]
Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold commented on code in PR #5080:
URL: https://github.com/apache/arrow-rs/pull/5080#discussion_r1395521974
##########
arrow-integration-testing/src/lib.rs:
##########
@@ -100,3 +166,147 @@ pub fn read_gzip_json(version: &str, path: &str) -> ArrowJson {
let arrow_json: ArrowJson = serde_json::from_str(&s).unwrap();
arrow_json
}
+
+//
+// C Data Integration entrypoints
+//
+
+fn cdata_integration_export_schema_from_json(
+ c_json_name: *const i8,
+ out: *mut FFI_ArrowSchema,
+) -> Result<()> {
+ let json_name = unsafe { CStr::from_ptr(c_json_name) };
+ let f = read_json_file_metadata(json_name.to_str()?)?;
+ let c_schema = FFI_ArrowSchema::try_from(&f.schema)?;
+ // Move exported schema into output struct
+ unsafe { ptr::write(out, c_schema) };
+ Ok(())
+}
+
+fn cdata_integration_export_batch_from_json(
+ c_json_name: *const i8,
+ batch_num: c_int,
+ out: *mut FFI_ArrowArray,
+) -> Result<()> {
+ let json_name = unsafe { CStr::from_ptr(c_json_name) };
+ let b = read_single_batch_from_json_file(json_name.to_str()?, batch_num.try_into().unwrap())?;
+ let a = StructArray::from(b).into_data();
+ let c_array = FFI_ArrowArray::new(&a);
+ // Move exported array into output struct
+ unsafe { ptr::write(out, c_array) };
+ Ok(())
+}
+
+fn cdata_integration_import_schema_and_compare_to_json(
+ c_json_name: *const i8,
+ c_schema: *mut FFI_ArrowSchema,
+) -> Result<()> {
+ let json_name = unsafe { CStr::from_ptr(c_json_name) };
+ let json_schema = read_json_file_metadata(json_name.to_str()?)?.schema;
+
+ // The source ArrowSchema will be released when this is dropped
+ let imported_schema = unsafe { std::ptr::replace(c_schema, FFI_ArrowSchema::empty()) };
Review Comment:
Is that not a bug in archery, perhaps I've been marinating in Rust for too long, but passing ownership with a pointer seems to be relying on very weak implicit guarantees? What happens if the function bails out before taking ownership or something? I would have expected it to call release unless the data had been moved out?
--
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] Improve C Data Interface and Add Integration Testing Entrypoints [arrow-rs]
Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold merged PR #5080:
URL: https://github.com/apache/arrow-rs/pull/5080
--
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] Improve C Data Interface and Add Integration Testing Entrypoints [arrow-rs]
Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold commented on PR #5080:
URL: https://github.com/apache/arrow-rs/pull/5080#issuecomment-1819381318
Thanks once again :+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 C Data Interface integration testing entrypoints [arrow-rs]
Posted by "pitrou (via GitHub)" <gi...@apache.org>.
pitrou commented on code in PR #5080:
URL: https://github.com/apache/arrow-rs/pull/5080#discussion_r1399103598
##########
arrow-integration-testing/src/lib.rs:
##########
@@ -44,7 +50,50 @@ pub struct ArrowFile {
pub batches: Vec<RecordBatch>,
}
-pub fn read_json_file(json_name: &str) -> Result<ArrowFile> {
+// Canonicalize the names of map fields in a schema
+pub fn canonicalize_schema(schema: &Schema) -> Schema {
+ let fields = schema
+ .fields()
+ .iter()
+ .map(|field| match field.data_type() {
+ DataType::Map(child_field, sorted) => match child_field.data_type() {
+ DataType::Struct(fields) if fields.len() == 2 => {
+ let first_field = fields.get(0).unwrap();
+ let key_field =
+ Arc::new(Field::new("key", first_field.data_type().clone(), false));
+ let second_field = fields.get(1).unwrap();
+ let value_field = Arc::new(Field::new(
+ "value",
+ second_field.data_type().clone(),
+ second_field.is_nullable(),
+ ));
+
+ let fields = Fields::from([key_field, value_field]);
+ let struct_type = DataType::Struct(fields);
+ let child_field = Field::new("entries", struct_type, false);
+
+ Arc::new(Field::new(
+ field.name().as_str(),
+ DataType::Map(Arc::new(child_field), *sorted),
+ field.is_nullable(),
+ ))
+ }
+ _ => panic!("The child field of Map type should be Struct type with 2 fields."),
+ },
+ _ => field.clone(),
+ })
+ .collect::<Fields>();
+
+ Schema::new(fields).with_metadata(schema.metadata().clone())
+}
+
+struct PartialArrowFile {
Review Comment:
I've renamed to LazyArrowFile, which indeed sounds better. As for ArrowFile, it's marked public, is it ok to change its API?
--
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 C Data Interface integration testing entrypoints [arrow-rs]
Posted by "wjones127 (via GitHub)" <gi...@apache.org>.
wjones127 commented on code in PR #5080:
URL: https://github.com/apache/arrow-rs/pull/5080#discussion_r1397613060
##########
arrow-integration-testing/src/lib.rs:
##########
@@ -100,3 +166,147 @@ pub fn read_gzip_json(version: &str, path: &str) -> ArrowJson {
let arrow_json: ArrowJson = serde_json::from_str(&s).unwrap();
arrow_json
}
+
+//
+// C Data Integration entrypoints
+//
+
+fn cdata_integration_export_schema_from_json(
+ c_json_name: *const i8,
+ out: *mut FFI_ArrowSchema,
+) -> Result<()> {
+ let json_name = unsafe { CStr::from_ptr(c_json_name) };
+ let f = read_json_file_metadata(json_name.to_str()?)?;
+ let c_schema = FFI_ArrowSchema::try_from(&f.schema)?;
+ // Move exported schema into output struct
+ unsafe { ptr::write(out, c_schema) };
+ Ok(())
+}
+
+fn cdata_integration_export_batch_from_json(
+ c_json_name: *const i8,
+ batch_num: c_int,
+ out: *mut FFI_ArrowArray,
+) -> Result<()> {
+ let json_name = unsafe { CStr::from_ptr(c_json_name) };
+ let b = read_single_batch_from_json_file(json_name.to_str()?, batch_num.try_into().unwrap())?;
+ let a = StructArray::from(b).into_data();
+ let c_array = FFI_ArrowArray::new(&a);
+ // Move exported array into output struct
+ unsafe { ptr::write(out, c_array) };
+ Ok(())
+}
+
+fn cdata_integration_import_schema_and_compare_to_json(
+ c_json_name: *const i8,
+ c_schema: *mut FFI_ArrowSchema,
+) -> Result<()> {
+ let json_name = unsafe { CStr::from_ptr(c_json_name) };
+ let json_schema = read_json_file_metadata(json_name.to_str()?)?.schema;
+
+ // The source ArrowSchema will be released when this is dropped
+ let imported_schema = unsafe { std::ptr::replace(c_schema, FFI_ArrowSchema::empty()) };
Review Comment:
Sounds like this was resolve with https://github.com/apache/arrow-rs/pull/5082 ?
--
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 C Data Interface integration testing entrypoints [arrow-rs]
Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold commented on code in PR #5080:
URL: https://github.com/apache/arrow-rs/pull/5080#discussion_r1397484346
##########
arrow-integration-testing/src/lib.rs:
##########
@@ -44,7 +50,50 @@ pub struct ArrowFile {
pub batches: Vec<RecordBatch>,
}
-pub fn read_json_file(json_name: &str) -> Result<ArrowFile> {
+// Canonicalize the names of map fields in a schema
+pub fn canonicalize_schema(schema: &Schema) -> Schema {
+ let fields = schema
+ .fields()
+ .iter()
+ .map(|field| match field.data_type() {
+ DataType::Map(child_field, sorted) => match child_field.data_type() {
+ DataType::Struct(fields) if fields.len() == 2 => {
+ let first_field = fields.get(0).unwrap();
+ let key_field =
+ Arc::new(Field::new("key", first_field.data_type().clone(), false));
+ let second_field = fields.get(1).unwrap();
+ let value_field = Arc::new(Field::new(
+ "value",
+ second_field.data_type().clone(),
+ second_field.is_nullable(),
+ ));
+
+ let fields = Fields::from([key_field, value_field]);
+ let struct_type = DataType::Struct(fields);
+ let child_field = Field::new("entries", struct_type, false);
+
+ Arc::new(Field::new(
+ field.name().as_str(),
+ DataType::Map(Arc::new(child_field), *sorted),
+ field.is_nullable(),
+ ))
+ }
+ _ => panic!("The child field of Map type should be Struct type with 2 fields."),
+ },
+ _ => field.clone(),
+ })
+ .collect::<Fields>();
+
+ Schema::new(fields).with_metadata(schema.metadata().clone())
+}
+
+struct PartialArrowFile {
Review Comment:
Perhaps we could get a doc comment explaining how this differs from ArrowFile? Perhaps LazyArrowFile might be a better name??
Alternatively I wonder if we could just make ArrowFile lazy, and have member functions for decoding a batch to RecordBatch by index or something
##########
arrow/src/ffi.rs:
##########
@@ -234,46 +235,59 @@ pub fn to_ffi(data: &ArrayData) -> Result<(FFI_ArrowArray, FFI_ArrowSchema)> {
///
/// This struct assumes that the incoming data agrees with the C data interface.
pub fn from_ffi(array: FFI_ArrowArray, schema: &FFI_ArrowSchema) -> Result<ArrayData> {
+ let dt = DataType::try_from(schema)?;
let array = Arc::new(array);
- let tmp = ArrowArray {
+ let tmp = ImportedArrowArray {
array: &array,
- schema,
+ data_type: dt,
+ owner: &array,
+ };
+ tmp.consume()
+}
+
+/// Import [ArrayData] from the C Data Interface
+///
+/// # Safety
+///
+/// This struct assumes that the incoming data agrees with the C data interface.
+pub fn from_ffi_and_data_type(array: FFI_ArrowArray, data_type: DataType) -> Result<ArrayData> {
Review Comment:
I think this method probably should be unsafe
##########
arrow/src/ffi.rs:
##########
@@ -234,46 +235,59 @@ pub fn to_ffi(data: &ArrayData) -> Result<(FFI_ArrowArray, FFI_ArrowSchema)> {
///
/// This struct assumes that the incoming data agrees with the C data interface.
pub fn from_ffi(array: FFI_ArrowArray, schema: &FFI_ArrowSchema) -> Result<ArrayData> {
Review Comment:
This method should also be unsafe, I don't know why it isn't
##########
arrow/src/ffi.rs:
##########
@@ -388,25 +437,20 @@ impl<'a> ArrowArray<'a> {
unsafe { create_buffer(self.owner.clone(), self.array, 0, buffer_len) }
}
- fn child(&self, index: usize) -> ArrowArray {
- ArrowArray {
- array: self.array.child(index),
- schema: self.schema.child(index),
- owner: self.owner,
- }
- }
-
- fn dictionary(&self) -> Option<ArrowArray> {
- match (self.array.dictionary(), self.schema.dictionary()) {
- (Some(array), Some(schema)) => Some(ArrowArray {
+ fn dictionary(&self) -> Result<Option<ImportedArrowArray>> {
+ match (self.array.dictionary(), &self.data_type) {
+ (Some(array), DataType::Dictionary(_, value_type)) => Ok(Some(ImportedArrowArray {
array,
- schema,
+ data_type: *value_type.clone(),
Review Comment:
Is the asterisk 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 C Data Interface integration testing entrypoints [arrow-rs]
Posted by "pitrou (via GitHub)" <gi...@apache.org>.
pitrou commented on code in PR #5080:
URL: https://github.com/apache/arrow-rs/pull/5080#discussion_r1399287863
##########
arrow-integration-testing/src/lib.rs:
##########
@@ -44,7 +50,50 @@ pub struct ArrowFile {
pub batches: Vec<RecordBatch>,
}
-pub fn read_json_file(json_name: &str) -> Result<ArrowFile> {
+// Canonicalize the names of map fields in a schema
+pub fn canonicalize_schema(schema: &Schema) -> Schema {
+ let fields = schema
+ .fields()
+ .iter()
+ .map(|field| match field.data_type() {
+ DataType::Map(child_field, sorted) => match child_field.data_type() {
+ DataType::Struct(fields) if fields.len() == 2 => {
+ let first_field = fields.get(0).unwrap();
+ let key_field =
+ Arc::new(Field::new("key", first_field.data_type().clone(), false));
+ let second_field = fields.get(1).unwrap();
+ let value_field = Arc::new(Field::new(
+ "value",
+ second_field.data_type().clone(),
+ second_field.is_nullable(),
+ ));
+
+ let fields = Fields::from([key_field, value_field]);
+ let struct_type = DataType::Struct(fields);
+ let child_field = Field::new("entries", struct_type, false);
+
+ Arc::new(Field::new(
+ field.name().as_str(),
+ DataType::Map(Arc::new(child_field), *sorted),
+ field.is_nullable(),
+ ))
+ }
+ _ => panic!("The child field of Map type should be Struct type with 2 fields."),
+ },
+ _ => field.clone(),
+ })
+ .collect::<Fields>();
+
+ Schema::new(fields).with_metadata(schema.metadata().clone())
+}
+
+struct PartialArrowFile {
Review Comment:
Ok, I did the suggested changes.
--
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 C Data Interface integration testing entrypoints [arrow-rs]
Posted by "pitrou (via GitHub)" <gi...@apache.org>.
pitrou commented on code in PR #5080:
URL: https://github.com/apache/arrow-rs/pull/5080#discussion_r1394977295
##########
arrow-integration-testing/src/lib.rs:
##########
@@ -100,3 +166,146 @@ pub fn read_gzip_json(version: &str, path: &str) -> ArrowJson {
let arrow_json: ArrowJson = serde_json::from_str(&s).unwrap();
arrow_json
}
+
+//
+// C Data Integration entrypoints
+//
+
+fn cdata_integration_export_schema_from_json(
+ c_json_name: *const i8,
+ out: *mut FFI_ArrowSchema,
+) -> Result<()> {
+ let json_name = unsafe { CStr::from_ptr(c_json_name) };
+ let f = read_json_file_metadata(json_name.to_str()?)?;
+ let c_schema = FFI_ArrowSchema::try_from(&f.schema)?;
+ // Move exported schema into output struct
+ unsafe { ptr::replace(out, c_schema) };
+ Ok(())
+}
+
+fn cdata_integration_export_batch_from_json(
+ c_json_name: *const i8,
+ batch_num: c_int,
+ out: *mut FFI_ArrowArray,
+) -> Result<()> {
+ let json_name = unsafe { CStr::from_ptr(c_json_name) };
+ let b = read_single_batch_from_json_file(json_name.to_str()?, batch_num.try_into().unwrap())?;
+ let a = StructArray::from(b).into_data();
+ let c_array = FFI_ArrowArray::new(&a);
+ // Move exported array into output struct
+ unsafe { ptr::replace(out, c_array) };
+ Ok(())
+}
+
+fn cdata_integration_import_schema_and_compare_to_json(
+ c_json_name: *const i8,
+ c_schema: *mut FFI_ArrowSchema,
+) -> Result<()> {
+ let json_name = unsafe { CStr::from_ptr(c_json_name) };
+ let json_schema = read_json_file_metadata(json_name.to_str()?)?.schema;
+
+ // The source ArrowSchema will be released when this is dropped
+ let imported_schema = unsafe { std::ptr::replace(c_schema, FFI_ArrowSchema::empty()) };
+ let imported_schema = Schema::try_from(&imported_schema)?;
Review Comment:
This pattern would probably benefit from an API function. It's easy to get it wrong.
--
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 C Data Interface integration testing entrypoints [arrow-rs]
Posted by "pitrou (via GitHub)" <gi...@apache.org>.
pitrou commented on code in PR #5080:
URL: https://github.com/apache/arrow-rs/pull/5080#discussion_r1395493082
##########
arrow-integration-testing/src/lib.rs:
##########
@@ -100,3 +166,147 @@ pub fn read_gzip_json(version: &str, path: &str) -> ArrowJson {
let arrow_json: ArrowJson = serde_json::from_str(&s).unwrap();
arrow_json
}
+
+//
+// C Data Integration entrypoints
+//
+
+fn cdata_integration_export_schema_from_json(
+ c_json_name: *const i8,
+ out: *mut FFI_ArrowSchema,
+) -> Result<()> {
+ let json_name = unsafe { CStr::from_ptr(c_json_name) };
+ let f = read_json_file_metadata(json_name.to_str()?)?;
+ let c_schema = FFI_ArrowSchema::try_from(&f.schema)?;
+ // Move exported schema into output struct
+ unsafe { ptr::write(out, c_schema) };
+ Ok(())
+}
+
+fn cdata_integration_export_batch_from_json(
+ c_json_name: *const i8,
+ batch_num: c_int,
+ out: *mut FFI_ArrowArray,
+) -> Result<()> {
+ let json_name = unsafe { CStr::from_ptr(c_json_name) };
+ let b = read_single_batch_from_json_file(json_name.to_str()?, batch_num.try_into().unwrap())?;
+ let a = StructArray::from(b).into_data();
+ let c_array = FFI_ArrowArray::new(&a);
+ // Move exported array into output struct
+ unsafe { ptr::write(out, c_array) };
+ Ok(())
+}
+
+fn cdata_integration_import_schema_and_compare_to_json(
+ c_json_name: *const i8,
+ c_schema: *mut FFI_ArrowSchema,
+) -> Result<()> {
+ let json_name = unsafe { CStr::from_ptr(c_json_name) };
+ let json_schema = read_json_file_metadata(json_name.to_str()?)?.schema;
+
+ // The source ArrowSchema will be released when this is dropped
+ let imported_schema = unsafe { std::ptr::replace(c_schema, FFI_ArrowSchema::empty()) };
Review Comment:
Typically, the caller would expect the callee to take ownership of the struct contents by "moving" it as explained here:
https://arrow.apache.org/docs/format/CDataInterface.html#moving-an-array
In this particular case, the caller will always be Archery and it doesn't release the exported struct by itself: it expects the consumer to do that.
--
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 C Data Interface integration testing entrypoints [arrow-rs]
Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold commented on code in PR #5080:
URL: https://github.com/apache/arrow-rs/pull/5080#discussion_r1395521974
##########
arrow-integration-testing/src/lib.rs:
##########
@@ -100,3 +166,147 @@ pub fn read_gzip_json(version: &str, path: &str) -> ArrowJson {
let arrow_json: ArrowJson = serde_json::from_str(&s).unwrap();
arrow_json
}
+
+//
+// C Data Integration entrypoints
+//
+
+fn cdata_integration_export_schema_from_json(
+ c_json_name: *const i8,
+ out: *mut FFI_ArrowSchema,
+) -> Result<()> {
+ let json_name = unsafe { CStr::from_ptr(c_json_name) };
+ let f = read_json_file_metadata(json_name.to_str()?)?;
+ let c_schema = FFI_ArrowSchema::try_from(&f.schema)?;
+ // Move exported schema into output struct
+ unsafe { ptr::write(out, c_schema) };
+ Ok(())
+}
+
+fn cdata_integration_export_batch_from_json(
+ c_json_name: *const i8,
+ batch_num: c_int,
+ out: *mut FFI_ArrowArray,
+) -> Result<()> {
+ let json_name = unsafe { CStr::from_ptr(c_json_name) };
+ let b = read_single_batch_from_json_file(json_name.to_str()?, batch_num.try_into().unwrap())?;
+ let a = StructArray::from(b).into_data();
+ let c_array = FFI_ArrowArray::new(&a);
+ // Move exported array into output struct
+ unsafe { ptr::write(out, c_array) };
+ Ok(())
+}
+
+fn cdata_integration_import_schema_and_compare_to_json(
+ c_json_name: *const i8,
+ c_schema: *mut FFI_ArrowSchema,
+) -> Result<()> {
+ let json_name = unsafe { CStr::from_ptr(c_json_name) };
+ let json_schema = read_json_file_metadata(json_name.to_str()?)?.schema;
+
+ // The source ArrowSchema will be released when this is dropped
+ let imported_schema = unsafe { std::ptr::replace(c_schema, FFI_ArrowSchema::empty()) };
Review Comment:
Is that not a bug in archery, perhaps I've been marinating in Rust for too long, but passing ownership with a pointer seems to be relying on very weak implicit guarantees? I would have expected it to call release unless the data had been moved out?
--
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