You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2022/09/05 05:05:00 UTC
[GitHub] [arrow-rs] liukun4515 opened a new pull request, #2649: support `CastOption` for casting numeric
liukun4515 opened a new pull request, #2649:
URL: https://github.com/apache/arrow-rs/pull/2649
# 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 #2648
# 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
[GitHub] [arrow-rs] liukun4515 merged pull request #2649: support `CastOption` for casting numeric
Posted by GitBox <gi...@apache.org>.
liukun4515 merged PR #2649:
URL: https://github.com/apache/arrow-rs/pull/2649
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-rs] ursabot commented on pull request #2649: support `CastOption` for casting numeric
Posted by GitBox <gi...@apache.org>.
ursabot commented on PR #2649:
URL: https://github.com/apache/arrow-rs/pull/2649#issuecomment-1237601660
Benchmark runs are scheduled for baseline = 773f1b9d49d11a71129aaaebc9884559b7690cd3 and contender = 8ea6ca1cb8bba3e9293cc5c35f08f25c5b1e8336. 8ea6ca1cb8bba3e9293cc5c35f08f25c5b1e8336 is a master commit associated with this PR. Results will be available as each benchmark for each run completes.
Conbench compare runs links:
[Skipped :warning: Benchmarking of arrow-rs-commits is not supported on ec2-t3-xlarge-us-east-2] [ec2-t3-xlarge-us-east-2](https://conbench.ursa.dev/compare/runs/1316f4082b3847629cb052af3b0c4b25...ce09e077d6fa4c4ba7f8171793dd777b/)
[Skipped :warning: Benchmarking of arrow-rs-commits is not supported on test-mac-arm] [test-mac-arm](https://conbench.ursa.dev/compare/runs/279f65e6034845d4ab70e6e4d36084ec...5b7e37913209492bb2e1cee1bf791d04/)
[Skipped :warning: Benchmarking of arrow-rs-commits is not supported on ursa-i9-9960x] [ursa-i9-9960x](https://conbench.ursa.dev/compare/runs/b59ce1ef3506411a8cab73aed6ced659...5ec0aaa0efe94ad2bcdfa456e9b0ee52/)
[Skipped :warning: Benchmarking of arrow-rs-commits is not supported on ursa-thinkcentre-m75q] [ursa-thinkcentre-m75q](https://conbench.ursa.dev/compare/runs/7422b9ec44a64e2a8a741e08985505d3...cfaa0c4f81b443e788a8ffc7d96c7ad7/)
Buildkite builds:
Supported benchmarks:
ec2-t3-xlarge-us-east-2: Supported benchmark langs: Python, R. Runs only benchmarks with cloud = True
test-mac-arm: Supported benchmark langs: C++, Python, R
ursa-i9-9960x: Supported benchmark langs: Python, R, JavaScript
ursa-thinkcentre-m75q: Supported benchmark langs: C++, Java
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-rs] tustvold commented on a diff in pull request #2649: support `CastOption` for casting numeric
Posted by GitBox <gi...@apache.org>.
tustvold commented on code in PR #2649:
URL: https://github.com/apache/arrow-rs/pull/2649#discussion_r963429788
##########
arrow/src/compute/kernels/cast.rs:
##########
@@ -1456,21 +1621,62 @@ where
}
/// Convert Array into a PrimitiveArray of type, and apply numeric cast
-fn cast_numeric_arrays<FROM, TO>(from: &ArrayRef) -> Result<ArrayRef>
+fn cast_numeric_arrays<FROM, TO>(
+ from: &ArrayRef,
+ cast_options: &CastOptions,
+) -> Result<ArrayRef>
where
FROM: ArrowNumericType,
TO: ArrowNumericType,
FROM::Native: num::NumCast,
TO::Native: num::NumCast,
{
- Ok(Arc::new(numeric_cast::<FROM, TO>(
- from.as_any()
- .downcast_ref::<PrimitiveArray<FROM>>()
- .unwrap(),
- )))
+ if cast_options.safe {
+ // If the value can't be casted to the `TO::Native`, return null
+ Ok(Arc::new(numeric_cast::<FROM, TO>(
+ from.as_any()
+ .downcast_ref::<PrimitiveArray<FROM>>()
+ .unwrap(),
+ )))
+ } else {
+ // If the value can't be casted to the `TO::Native`, return error
+ Ok(Arc::new(numeric_cast_with_error::<FROM, TO>(
+ from.as_any()
+ .downcast_ref::<PrimitiveArray<FROM>>()
+ .unwrap(),
+ )?))
+ }
}
-/// Natural cast between numeric types
+// Natural cast between numeric types
+// If the value of T can't be casted to R, will throw error
+fn numeric_cast_with_error<T, R>(from: &PrimitiveArray<T>) -> Result<PrimitiveArray<R>>
+where
+ T: ArrowNumericType,
+ R: ArrowNumericType,
+ T::Native: num::NumCast,
+ R::Native: num::NumCast,
+{
+ let iter = from
+ .iter()
+ .map(|v| match v {
+ None => Ok(None),
+ Some(value) => match num::cast::cast::<T::Native, R::Native>(value) {
+ None => Err(ArrowError::CastError(format!(
+ "Can't cast value {:?} to type {}",
+ value,
+ R::DATA_TYPE
+ ))),
+ Some(v) => Ok(Some(v)),
+ },
+ })
+ .collect::<Result<Vec<Option<R::Native>>>>()?;
+
+ Ok(unsafe { PrimitiveArray::<R>::from_trusted_len_iter(iter) })
Review Comment:
I don't think the code above is correct, in particular you are taking an iterator of Option and collecting into a Buffer. I would not expect it to even compile...
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-rs] liukun4515 commented on a diff in pull request #2649: support `CastOption` for casting numeric
Posted by GitBox <gi...@apache.org>.
liukun4515 commented on code in PR #2649:
URL: https://github.com/apache/arrow-rs/pull/2649#discussion_r963223663
##########
arrow/src/compute/kernels/cast.rs:
##########
@@ -1456,21 +1621,62 @@ where
}
/// Convert Array into a PrimitiveArray of type, and apply numeric cast
-fn cast_numeric_arrays<FROM, TO>(from: &ArrayRef) -> Result<ArrayRef>
+fn cast_numeric_arrays<FROM, TO>(
+ from: &ArrayRef,
+ cast_options: &CastOptions,
+) -> Result<ArrayRef>
where
FROM: ArrowNumericType,
TO: ArrowNumericType,
FROM::Native: num::NumCast,
TO::Native: num::NumCast,
{
- Ok(Arc::new(numeric_cast::<FROM, TO>(
- from.as_any()
- .downcast_ref::<PrimitiveArray<FROM>>()
- .unwrap(),
- )))
+ if cast_options.safe {
+ // If the value can't be casted to the `TO::Native`, return null
+ Ok(Arc::new(numeric_cast::<FROM, TO>(
+ from.as_any()
+ .downcast_ref::<PrimitiveArray<FROM>>()
+ .unwrap(),
+ )))
+ } else {
+ // If the value can't be casted to the `TO::Native`, return error
+ Ok(Arc::new(numeric_cast_with_error::<FROM, TO>(
+ from.as_any()
+ .downcast_ref::<PrimitiveArray<FROM>>()
+ .unwrap(),
+ )?))
+ }
}
-/// Natural cast between numeric types
+// Natural cast between numeric types
+// If the value of T can't be casted to R, will throw error
+fn numeric_cast_with_error<T, R>(from: &PrimitiveArray<T>) -> Result<PrimitiveArray<R>>
+where
+ T: ArrowNumericType,
+ R: ArrowNumericType,
+ T::Native: num::NumCast,
+ R::Native: num::NumCast,
+{
+ let iter = from
+ .iter()
+ .map(|v| match v {
+ None => Ok(None),
+ Some(value) => match num::cast::cast::<T::Native, R::Native>(value) {
+ None => Err(ArrowError::CastError(format!(
+ "Can't cast value {:?} to type {}",
+ value,
+ R::DATA_TYPE
+ ))),
+ Some(v) => Ok(Some(v)),
+ },
+ })
+ .collect::<Result<Vec<Option<R::Native>>>>()?;
+
+ Ok(unsafe { PrimitiveArray::<R>::from_trusted_len_iter(iter) })
Review Comment:
I file a new pr for that: https://github.com/apache/arrow-rs/pull/2661
I can align this pr with `unary_checked` or `try_unary` with this pr https://github.com/apache/arrow-rs/pull/2650
@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
[GitHub] [arrow-rs] liukun4515 commented on a diff in pull request #2649: support `CastOption` for casting numeric
Posted by GitBox <gi...@apache.org>.
liukun4515 commented on code in PR #2649:
URL: https://github.com/apache/arrow-rs/pull/2649#discussion_r963202388
##########
arrow/src/compute/kernels/cast.rs:
##########
@@ -1456,21 +1621,62 @@ where
}
/// Convert Array into a PrimitiveArray of type, and apply numeric cast
-fn cast_numeric_arrays<FROM, TO>(from: &ArrayRef) -> Result<ArrayRef>
+fn cast_numeric_arrays<FROM, TO>(
+ from: &ArrayRef,
+ cast_options: &CastOptions,
+) -> Result<ArrayRef>
where
FROM: ArrowNumericType,
TO: ArrowNumericType,
FROM::Native: num::NumCast,
TO::Native: num::NumCast,
{
- Ok(Arc::new(numeric_cast::<FROM, TO>(
- from.as_any()
- .downcast_ref::<PrimitiveArray<FROM>>()
- .unwrap(),
- )))
+ if cast_options.safe {
+ // If the value can't be casted to the `TO::Native`, return null
+ Ok(Arc::new(numeric_cast::<FROM, TO>(
+ from.as_any()
+ .downcast_ref::<PrimitiveArray<FROM>>()
+ .unwrap(),
+ )))
+ } else {
+ // If the value can't be casted to the `TO::Native`, return error
+ Ok(Arc::new(numeric_cast_with_error::<FROM, TO>(
+ from.as_any()
+ .downcast_ref::<PrimitiveArray<FROM>>()
+ .unwrap(),
+ )?))
+ }
}
-/// Natural cast between numeric types
+// Natural cast between numeric types
+// If the value of T can't be casted to R, will throw error
+fn numeric_cast_with_error<T, R>(from: &PrimitiveArray<T>) -> Result<PrimitiveArray<R>>
+where
+ T: ArrowNumericType,
+ R: ArrowNumericType,
+ T::Native: num::NumCast,
+ R::Native: num::NumCast,
+{
+ let iter = from
+ .iter()
+ .map(|v| match v {
+ None => Ok(None),
+ Some(value) => match num::cast::cast::<T::Native, R::Native>(value) {
+ None => Err(ArrowError::CastError(format!(
+ "Can't cast value {:?} to type {}",
+ value,
+ R::DATA_TYPE
+ ))),
+ Some(v) => Ok(Some(v)),
+ },
+ })
+ .collect::<Result<Vec<Option<R::Native>>>>()?;
+
+ Ok(unsafe { PrimitiveArray::<R>::from_trusted_len_iter(iter) })
Review Comment:
Do you mean use the `try_from_trusted_len_iter` API for the `MutableBuffer` or `Buffer`?
Does it can fit this 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
[GitHub] [arrow-rs] liukun4515 commented on a diff in pull request #2649: support `CastOption` for casting numeric
Posted by GitBox <gi...@apache.org>.
liukun4515 commented on code in PR #2649:
URL: https://github.com/apache/arrow-rs/pull/2649#discussion_r963202658
##########
arrow/src/compute/kernels/cast.rs:
##########
@@ -1456,21 +1621,62 @@ where
}
/// Convert Array into a PrimitiveArray of type, and apply numeric cast
-fn cast_numeric_arrays<FROM, TO>(from: &ArrayRef) -> Result<ArrayRef>
+fn cast_numeric_arrays<FROM, TO>(
+ from: &ArrayRef,
+ cast_options: &CastOptions,
+) -> Result<ArrayRef>
where
FROM: ArrowNumericType,
TO: ArrowNumericType,
FROM::Native: num::NumCast,
TO::Native: num::NumCast,
{
- Ok(Arc::new(numeric_cast::<FROM, TO>(
- from.as_any()
- .downcast_ref::<PrimitiveArray<FROM>>()
- .unwrap(),
- )))
+ if cast_options.safe {
+ // If the value can't be casted to the `TO::Native`, return null
+ Ok(Arc::new(numeric_cast::<FROM, TO>(
+ from.as_any()
+ .downcast_ref::<PrimitiveArray<FROM>>()
+ .unwrap(),
+ )))
+ } else {
+ // If the value can't be casted to the `TO::Native`, return error
+ Ok(Arc::new(numeric_cast_with_error::<FROM, TO>(
+ from.as_any()
+ .downcast_ref::<PrimitiveArray<FROM>>()
+ .unwrap(),
+ )?))
+ }
}
-/// Natural cast between numeric types
+// Natural cast between numeric types
+// If the value of T can't be casted to R, will throw error
+fn numeric_cast_with_error<T, R>(from: &PrimitiveArray<T>) -> Result<PrimitiveArray<R>>
+where
+ T: ArrowNumericType,
+ R: ArrowNumericType,
+ T::Native: num::NumCast,
+ R::Native: num::NumCast,
+{
+ let iter = from
+ .iter()
+ .map(|v| match v {
+ None => Ok(None),
+ Some(value) => match num::cast::cast::<T::Native, R::Native>(value) {
+ None => Err(ArrowError::CastError(format!(
+ "Can't cast value {:?} to type {}",
+ value,
+ R::DATA_TYPE
+ ))),
+ Some(v) => Ok(Some(v)),
+ },
+ })
+ .collect::<Result<Vec<Option<R::Native>>>>()?;
+
+ Ok(unsafe { PrimitiveArray::<R>::from_trusted_len_iter(iter) })
Review Comment:
Do you mean add a new api `try_xxx` for `PrimitiveArray`?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-rs] tustvold commented on a diff in pull request #2649: support `CastOption` for casting numeric
Posted by GitBox <gi...@apache.org>.
tustvold commented on code in PR #2649:
URL: https://github.com/apache/arrow-rs/pull/2649#discussion_r962789336
##########
arrow/src/compute/kernels/cast.rs:
##########
@@ -1456,21 +1621,62 @@ where
}
/// Convert Array into a PrimitiveArray of type, and apply numeric cast
-fn cast_numeric_arrays<FROM, TO>(from: &ArrayRef) -> Result<ArrayRef>
+fn cast_numeric_arrays<FROM, TO>(
+ from: &ArrayRef,
+ cast_options: &CastOptions,
+) -> Result<ArrayRef>
where
FROM: ArrowNumericType,
TO: ArrowNumericType,
FROM::Native: num::NumCast,
TO::Native: num::NumCast,
{
- Ok(Arc::new(numeric_cast::<FROM, TO>(
- from.as_any()
- .downcast_ref::<PrimitiveArray<FROM>>()
- .unwrap(),
- )))
+ if cast_options.safe {
+ // If the value can't be casted to the `TO::Native`, return null
+ Ok(Arc::new(numeric_cast::<FROM, TO>(
+ from.as_any()
+ .downcast_ref::<PrimitiveArray<FROM>>()
+ .unwrap(),
+ )))
+ } else {
+ // If the value can't be casted to the `TO::Native`, return error
+ Ok(Arc::new(numeric_cast_with_error::<FROM, TO>(
+ from.as_any()
+ .downcast_ref::<PrimitiveArray<FROM>>()
+ .unwrap(),
+ )?))
+ }
}
-/// Natural cast between numeric types
+// Natural cast between numeric types
+// If the value of T can't be casted to R, will throw error
+fn numeric_cast_with_error<T, R>(from: &PrimitiveArray<T>) -> Result<PrimitiveArray<R>>
+where
+ T: ArrowNumericType,
+ R: ArrowNumericType,
+ T::Native: num::NumCast,
+ R::Native: num::NumCast,
+{
+ let iter = from
+ .iter()
+ .map(|v| match v {
+ None => Ok(None),
+ Some(value) => match num::cast::cast::<T::Native, R::Native>(value) {
+ None => Err(ArrowError::CastError(format!(
+ "Can't cast value {:?} to type {}",
+ value,
+ R::DATA_TYPE
+ ))),
+ Some(v) => Ok(Some(v)),
+ },
+ })
+ .collect::<Result<Vec<Option<R::Native>>>>()?;
+
+ Ok(unsafe { PrimitiveArray::<R>::from_trusted_len_iter(iter) })
Review Comment:
You could use try_from_trusted_len_iter to avoid needing to create the intermediate vec
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-rs] liukun4515 commented on a diff in pull request #2649: support `CastOption` for casting numeric
Posted by GitBox <gi...@apache.org>.
liukun4515 commented on code in PR #2649:
URL: https://github.com/apache/arrow-rs/pull/2649#discussion_r963206083
##########
arrow/src/compute/kernels/cast.rs:
##########
@@ -1456,21 +1621,62 @@ where
}
/// Convert Array into a PrimitiveArray of type, and apply numeric cast
-fn cast_numeric_arrays<FROM, TO>(from: &ArrayRef) -> Result<ArrayRef>
+fn cast_numeric_arrays<FROM, TO>(
+ from: &ArrayRef,
+ cast_options: &CastOptions,
+) -> Result<ArrayRef>
where
FROM: ArrowNumericType,
TO: ArrowNumericType,
FROM::Native: num::NumCast,
TO::Native: num::NumCast,
{
- Ok(Arc::new(numeric_cast::<FROM, TO>(
- from.as_any()
- .downcast_ref::<PrimitiveArray<FROM>>()
- .unwrap(),
- )))
+ if cast_options.safe {
+ // If the value can't be casted to the `TO::Native`, return null
+ Ok(Arc::new(numeric_cast::<FROM, TO>(
+ from.as_any()
+ .downcast_ref::<PrimitiveArray<FROM>>()
+ .unwrap(),
+ )))
+ } else {
+ // If the value can't be casted to the `TO::Native`, return error
+ Ok(Arc::new(numeric_cast_with_error::<FROM, TO>(
+ from.as_any()
+ .downcast_ref::<PrimitiveArray<FROM>>()
+ .unwrap(),
+ )?))
+ }
}
-/// Natural cast between numeric types
+// Natural cast between numeric types
+// If the value of T can't be casted to R, will throw error
+fn numeric_cast_with_error<T, R>(from: &PrimitiveArray<T>) -> Result<PrimitiveArray<R>>
+where
+ T: ArrowNumericType,
+ R: ArrowNumericType,
+ T::Native: num::NumCast,
+ R::Native: num::NumCast,
+{
+ let iter = from
+ .iter()
+ .map(|v| match v {
+ None => Ok(None),
+ Some(value) => match num::cast::cast::<T::Native, R::Native>(value) {
+ None => Err(ArrowError::CastError(format!(
+ "Can't cast value {:?} to type {}",
+ value,
+ R::DATA_TYPE
+ ))),
+ Some(v) => Ok(Some(v)),
+ },
+ })
+ .collect::<Result<Vec<Option<R::Native>>>>()?;
+
+ Ok(unsafe { PrimitiveArray::<R>::from_trusted_len_iter(iter) })
Review Comment:
We can use bellow code to implement
```
let iter = from
.iter()
.map(|v| match v {
None => Ok(None),
Some(value) => match num::cast::cast::<T::Native, R::Native>(value) {
None => Err(ArrowError::CastError(format!(
"Can't cast value {:?} to type {}",
value,
R::DATA_TYPE
))),
Some(v) => Ok(Some(v)),
},
});
Buffer::try_from_trusted_len_iter(iter)
let data = unsafe {
ArrayData::new_unchecked(
T::DATA_TYPE,
len,
None,
null_bit_buffer,
0,
vec![buffer],
vec![],
)
};
Ok(PrimitiveArray::<T>::from(data))
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-rs] tustvold commented on a diff in pull request #2649: support `CastOption` for casting numeric
Posted by GitBox <gi...@apache.org>.
tustvold commented on code in PR #2649:
URL: https://github.com/apache/arrow-rs/pull/2649#discussion_r963429788
##########
arrow/src/compute/kernels/cast.rs:
##########
@@ -1456,21 +1621,62 @@ where
}
/// Convert Array into a PrimitiveArray of type, and apply numeric cast
-fn cast_numeric_arrays<FROM, TO>(from: &ArrayRef) -> Result<ArrayRef>
+fn cast_numeric_arrays<FROM, TO>(
+ from: &ArrayRef,
+ cast_options: &CastOptions,
+) -> Result<ArrayRef>
where
FROM: ArrowNumericType,
TO: ArrowNumericType,
FROM::Native: num::NumCast,
TO::Native: num::NumCast,
{
- Ok(Arc::new(numeric_cast::<FROM, TO>(
- from.as_any()
- .downcast_ref::<PrimitiveArray<FROM>>()
- .unwrap(),
- )))
+ if cast_options.safe {
+ // If the value can't be casted to the `TO::Native`, return null
+ Ok(Arc::new(numeric_cast::<FROM, TO>(
+ from.as_any()
+ .downcast_ref::<PrimitiveArray<FROM>>()
+ .unwrap(),
+ )))
+ } else {
+ // If the value can't be casted to the `TO::Native`, return error
+ Ok(Arc::new(numeric_cast_with_error::<FROM, TO>(
+ from.as_any()
+ .downcast_ref::<PrimitiveArray<FROM>>()
+ .unwrap(),
+ )?))
+ }
}
-/// Natural cast between numeric types
+// Natural cast between numeric types
+// If the value of T can't be casted to R, will throw error
+fn numeric_cast_with_error<T, R>(from: &PrimitiveArray<T>) -> Result<PrimitiveArray<R>>
+where
+ T: ArrowNumericType,
+ R: ArrowNumericType,
+ T::Native: num::NumCast,
+ R::Native: num::NumCast,
+{
+ let iter = from
+ .iter()
+ .map(|v| match v {
+ None => Ok(None),
+ Some(value) => match num::cast::cast::<T::Native, R::Native>(value) {
+ None => Err(ArrowError::CastError(format!(
+ "Can't cast value {:?} to type {}",
+ value,
+ R::DATA_TYPE
+ ))),
+ Some(v) => Ok(Some(v)),
+ },
+ })
+ .collect::<Result<Vec<Option<R::Native>>>>()?;
+
+ Ok(unsafe { PrimitiveArray::<R>::from_trusted_len_iter(iter) })
Review Comment:
I don't think the code above is correct, in particular you are taking an iterator of Option and collecting into a Buffer. I would not expect it to even compile...
I had thought PrimitiveArray had a try_from_trysted_len_iter but it does not, so ignore me. The real performance will likely be from not using iterators at all, but one step at a time and I don't imagine the cast kernels are a major bottleneck atm
--
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