-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Support "IS TRUE/FALSE" syntax #3189
Closed
Closed
Changes from 13 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
79dc1f4
scalar functionality
sarahyurick 4ced79f
IsTrue full functionality
sarahyurick 71c0001
IS FALSE functionality
sarahyurick 20cfe36
Update datafusion/proto/proto/datafusion.proto
sarahyurick 632700d
formatting
sarahyurick ecc3a7f
Merge branch 'master' of https://github.com/sarahyurick/arrow-datafusion
sarahyurick 15e42be
Update datafusion/common/src/scalar.rs
sarahyurick 450f776
Update datafusion/common/src/scalar.rs
sarahyurick 11b5559
err instead of panic
sarahyurick 70acf4b
format error
sarahyurick affa48d
tests
sarahyurick d408b2f
update is_true and is_false
sarahyurick e5c76a7
Merge branch 'apache:master' into master
sarahyurick 6b3ba49
update with andy's suggestions
sarahyurick 9b6d92b
Merge branch 'master' of https://github.com/sarahyurick/arrow-datafusion
sarahyurick 46bf619
rust format fix
sarahyurick 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 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 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 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 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 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 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 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 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 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 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 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,139 @@ | ||
// 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. | ||
|
||
//! IS FALSE expression | ||
|
||
use std::{any::Any, sync::Arc}; | ||
|
||
use crate::PhysicalExpr; | ||
use arrow::{ | ||
array::{Array, BooleanArray}, | ||
datatypes::{DataType, Schema}, | ||
record_batch::RecordBatch, | ||
}; | ||
use datafusion_common::DataFusionError; | ||
use datafusion_common::Result; | ||
use datafusion_common::ScalarValue; | ||
use datafusion_expr::ColumnarValue; | ||
|
||
/// IS FALSE expression | ||
#[derive(Debug)] | ||
pub struct IsFalseExpr { | ||
/// The input expression | ||
arg: Arc<dyn PhysicalExpr>, | ||
} | ||
|
||
impl IsFalseExpr { | ||
/// Create new not expression | ||
pub fn new(arg: Arc<dyn PhysicalExpr>) -> Self { | ||
Self { arg } | ||
} | ||
|
||
/// Get the input expression | ||
pub fn arg(&self) -> &Arc<dyn PhysicalExpr> { | ||
&self.arg | ||
} | ||
} | ||
|
||
impl std::fmt::Display for IsFalseExpr { | ||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { | ||
write!(f, "{} IS FALSE", self.arg) | ||
} | ||
} | ||
|
||
impl PhysicalExpr for IsFalseExpr { | ||
/// Return a reference to Any that can be used for downcasting | ||
fn as_any(&self) -> &dyn Any { | ||
self | ||
} | ||
|
||
fn data_type(&self, _input_schema: &Schema) -> Result<DataType> { | ||
Ok(DataType::Boolean) | ||
} | ||
|
||
fn nullable(&self, _input_schema: &Schema) -> Result<bool> { | ||
Ok(false) | ||
} | ||
|
||
fn evaluate(&self, batch: &RecordBatch) -> Result<ColumnarValue> { | ||
sarahyurick marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let arg = self.arg.evaluate(batch)?; | ||
match arg { | ||
ColumnarValue::Array(array) => { | ||
let array_len = array.len(); | ||
let my_array = ColumnarValue::Array(array).into_array(array_len); | ||
let true_array = BooleanArray::from(vec![Some(true)]); | ||
let false_array = BooleanArray::from(vec![Some(false)]); | ||
let null_array = BooleanArray::from(vec![None]); | ||
let mut result_vec = vec![]; | ||
for i in 0..array_len { | ||
let current = (*my_array).slice(i, 1); | ||
if (*current).eq(&false_array) { | ||
result_vec.push(Some(true)); | ||
} else if (*current).eq(&true_array) || (*current).eq(&null_array) { | ||
result_vec.push(Some(false)); | ||
} else { | ||
return Err(DataFusionError::Execution(format!("Cannot apply 'IS FALSE' to arguments of type '<{:?}> IS FALSE'. Supported form(s): '<BOOLEAN> IS FALSE'", current.data_type()))) | ||
} | ||
} | ||
|
||
let return_array = BooleanArray::from(result_vec); | ||
Ok(ColumnarValue::Array(Arc::new(return_array))) | ||
} | ||
ColumnarValue::Scalar(scalar) => Ok(ColumnarValue::Scalar( | ||
ScalarValue::Boolean(Some(scalar.is_false().unwrap())), | ||
)), | ||
} | ||
} | ||
} | ||
|
||
/// Create an IS FALSE expression | ||
pub fn is_false(arg: Arc<dyn PhysicalExpr>) -> Result<Arc<dyn PhysicalExpr>> { | ||
Ok(Arc::new(IsFalseExpr::new(arg))) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::expressions::col; | ||
use arrow::{ | ||
array::BooleanArray, | ||
datatypes::*, | ||
record_batch::RecordBatch, | ||
}; | ||
use std::sync::Arc; | ||
|
||
#[test] | ||
fn is_false_op() -> Result<()> { | ||
let schema = Schema::new(vec![Field::new("a", DataType::Boolean, true)]); | ||
let a = BooleanArray::from(vec![Some(true), Some(false), None]); | ||
let expr = is_false(col("a", &schema)?).unwrap(); | ||
let batch = RecordBatch::try_new(Arc::new(schema), vec![Arc::new(a)])?; | ||
|
||
// expression: "a is false" | ||
let result = expr.evaluate(&batch)?.into_array(batch.num_rows()); | ||
let result = result | ||
.as_any() | ||
.downcast_ref::<BooleanArray>() | ||
.expect("failed to downcast to BooleanArray"); | ||
|
||
let expected = &BooleanArray::from(vec![false, true, false]); | ||
|
||
assert_eq!(expected, result); | ||
|
||
Ok(()) | ||
} | ||
} |
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.
👍