-
Notifications
You must be signed in to change notification settings - Fork 247
fix : implement_try_eval_mode_arithmetic #2073
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
0e041ca
init_commit
coderfender 3f7e0e8
try_arithmetic_impl
coderfender 4c8f5d6
try_arithmetic_impl
coderfender b7b94f0
try_arithmetic_impl
coderfender ce72d79
try_arithmetic_impl
coderfender 956d6e9
try_arithmetic_impl
coderfender cf3d30c
try_arithmetic_impl
coderfender f6908a1
try_arithmetic_impl
coderfender 8d6501d
try_arithmetic_impl
coderfender d80a18a
try_arithmetic_impl
coderfender c8a212f
try_arithmetic_impl
coderfender e0373d4
try_arithmetic_impl
coderfender dc01c9f
try_arithmetic_impl
coderfender bd002fa
try_arithmetic_impl
coderfender File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| use arrow::array::{Array, ArrowNativeTypeOp, PrimitiveArray, PrimitiveBuilder}; | ||
| use arrow::array::{ArrayRef, AsArray}; | ||
|
|
||
| use arrow::datatypes::{ArrowPrimitiveType, DataType, Int32Type, Int64Type}; | ||
| use datafusion::common::DataFusionError; | ||
| use datafusion::physical_plan::ColumnarValue; | ||
| use std::sync::Arc; | ||
|
|
||
| pub fn try_arithmetic_kernel<T>( | ||
| left: &PrimitiveArray<T>, | ||
| right: &PrimitiveArray<T>, | ||
| op: &str, | ||
| ) -> Result<ArrayRef, DataFusionError> | ||
| where | ||
| T: ArrowPrimitiveType, | ||
| { | ||
| let len = left.len(); | ||
| let mut builder = PrimitiveBuilder::<T>::with_capacity(len); | ||
| match op { | ||
| "checked_add" => { | ||
| for i in 0..len { | ||
| if left.is_null(i) || right.is_null(i) { | ||
| builder.append_null(); | ||
| } else { | ||
| builder.append_option(left.value(i).add_checked(right.value(i)).ok()); | ||
| } | ||
| } | ||
| } | ||
| "checked_sub" => { | ||
| for i in 0..len { | ||
| if left.is_null(i) || right.is_null(i) { | ||
| builder.append_null(); | ||
| } else { | ||
| builder.append_option(left.value(i).sub_checked(right.value(i)).ok()); | ||
| } | ||
| } | ||
| } | ||
| "checked_mul" => { | ||
| for i in 0..len { | ||
| if left.is_null(i) || right.is_null(i) { | ||
| builder.append_null(); | ||
| } else { | ||
| builder.append_option(left.value(i).mul_checked(right.value(i)).ok()); | ||
| } | ||
| } | ||
| } | ||
| "checked_div" => { | ||
| for i in 0..len { | ||
| if left.is_null(i) || right.is_null(i) { | ||
| builder.append_null(); | ||
| } else { | ||
| builder.append_option(left.value(i).div_checked(right.value(i)).ok()); | ||
| } | ||
| } | ||
| } | ||
| _ => { | ||
| return Err(DataFusionError::Internal(format!( | ||
| "Unsupported operation: {:?}", | ||
| op | ||
| ))) | ||
| } | ||
| } | ||
|
|
||
| Ok(Arc::new(builder.finish()) as ArrayRef) | ||
| } | ||
|
|
||
| pub fn checked_add( | ||
| args: &[ColumnarValue], | ||
| data_type: &DataType, | ||
| ) -> Result<ColumnarValue, DataFusionError> { | ||
| checked_arithmetic_internal(args, data_type, "checked_add") | ||
| } | ||
|
|
||
| pub fn checked_sub( | ||
| args: &[ColumnarValue], | ||
| data_type: &DataType, | ||
| ) -> Result<ColumnarValue, DataFusionError> { | ||
| checked_arithmetic_internal(args, data_type, "checked_sub") | ||
| } | ||
|
|
||
| pub fn checked_mul( | ||
| args: &[ColumnarValue], | ||
| data_type: &DataType, | ||
| ) -> Result<ColumnarValue, DataFusionError> { | ||
| checked_arithmetic_internal(args, data_type, "checked_mul") | ||
| } | ||
|
|
||
| pub fn checked_div( | ||
| args: &[ColumnarValue], | ||
| data_type: &DataType, | ||
| ) -> Result<ColumnarValue, DataFusionError> { | ||
| checked_arithmetic_internal(args, data_type, "checked_div") | ||
| } | ||
|
|
||
| fn checked_arithmetic_internal( | ||
| args: &[ColumnarValue], | ||
| data_type: &DataType, | ||
| op: &str, | ||
| ) -> Result<ColumnarValue, DataFusionError> { | ||
| let left = &args[0]; | ||
| let right = &args[1]; | ||
|
|
||
| let (left_arr, right_arr): (ArrayRef, ArrayRef) = match (left, right) { | ||
| (ColumnarValue::Array(l), ColumnarValue::Array(r)) => (Arc::clone(l), Arc::clone(r)), | ||
| (ColumnarValue::Scalar(l), ColumnarValue::Array(r)) => { | ||
| (l.to_array_of_size(r.len())?, Arc::clone(r)) | ||
| } | ||
| (ColumnarValue::Array(l), ColumnarValue::Scalar(r)) => { | ||
| (Arc::clone(l), r.to_array_of_size(l.len())?) | ||
| } | ||
| (ColumnarValue::Scalar(l), ColumnarValue::Scalar(r)) => (l.to_array()?, r.to_array()?), | ||
| }; | ||
|
|
||
| // Rust only supports checked_arithmetic on Int32 and Int64 | ||
| let result_array = match data_type { | ||
| DataType::Int32 => try_arithmetic_kernel::<Int32Type>( | ||
| left_arr.as_primitive::<Int32Type>(), | ||
| right_arr.as_primitive::<Int32Type>(), | ||
| op, | ||
| ), | ||
| DataType::Int64 => try_arithmetic_kernel::<Int64Type>( | ||
| left_arr.as_primitive::<Int64Type>(), | ||
| right_arr.as_primitive::<Int64Type>(), | ||
| op, | ||
| ), | ||
| _ => Err(DataFusionError::Internal(format!( | ||
| "Unsupported data type: {:?}", | ||
| data_type | ||
| ))), | ||
| }; | ||
|
|
||
| Ok(ColumnarValue::Array(result_array?)) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We may eventually want to have a specialized version of the kernel for the scalar case to avoid the overhead of creating an array from the scalar. This does not need to happen as part of this PR, though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure ! I will create a follow up enhancement to track changes for a scalar impl. Thank you for the feed back @andygrove .